Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Turbo] Support custom TurboStreamResponse actions #2298

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from

Conversation

DRaichev
Copy link

@DRaichev DRaichev commented Oct 24, 2024

Q A
Bug fix? no
New feature? yes
Issues -
License MIT

The current implementation of TurboStream & TurboStreamResponse has no generic action (only the predefined turbo actions) and what's more the class is marked as final, which does not allow adding support for more actions. I think the wrap function should be public or have a public function "custom" that exposes the functionality (I've opted for the latter)

I really like this library: https://github.com/marcoroth/turbo_power
It adds additional simple actions. For example I use set/delete attributes to enable/disable UI elements

This change will allow people to use this or similar libraries, or even build their own custom actions if they want to.

There is no impact to the rest of the functionality or DX

There is a minor change to the wrap function, it now accepts an array of attributes instead of a string. It builds the string from the array of attributes, and adds a leading space. This aims to prevent errors, as the previous implementation needs the $attr argument to start with a blank space otherwise it would produce invalid html

@carsonbot carsonbot added Feature New Feature Turbo Status: Needs Review Needs to be reviewed labels Oct 24, 2024
@DRaichev DRaichev force-pushed the TurboResponse-generic-actions branch 3 times, most recently from 13122e2 to 8514346 Compare October 24, 2024 08:48
@setineos
Copy link

I think it should also work for <twig:Turbo:Stream:*> components.

src/Turbo/src/Helper/TurboStream.php Outdated Show resolved Hide resolved
src/Turbo/src/Helper/TurboStream.php Outdated Show resolved Hide resolved
@nicolas-grekas
Copy link
Member

I think it should also work for twig:Turbo:Stream:* components.

This can be done by adding a generic <twig:Turbo:Stream> component maybe.

@DRaichev
Copy link
Author

@nicolas-grekas
I just realised if you pass "action" or "targets" into the attributes array those will conflict with the existing attributes.
I think we should throw an exception if those are present

@DRaichev DRaichev force-pushed the TurboResponse-generic-actions branch 4 times, most recently from 3e22298 to 66f7e36 Compare October 27, 2024 22:44
@smnandre
Copy link
Member

With the incoming <twig:Turbo:Frame> and <twig:Turbo:Stream>, do you think we still need this features ?

Kocal added a commit that referenced this pull request Oct 30, 2024
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[Turbo] Add generic `<Turbo:Stream>` component

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| Issues        | #2298 (comment)
| License       | MIT

I added a generic Turbo Stream component. To use it, you can do this:

```twig
<twig:Turbo:Stream action="turbo_frame_reload" targets="#count-post" />
```

The rendering is:

```twig
<turbo-stream action="turbo_frame_reload" targets="#count-post"></turbo-stream>
```

Commits
-------

f0ab499 [Turbo] Add generic `<Turbo:Stream>` component
@DRaichev
Copy link
Author

With the incoming <twig:Turbo:Frame> and <twig:Turbo:Stream>, do you think we still need this features ?

I think they are complementary. I'd rather use the twig component syntax only in templates and stick to this helper on the php side, plus it's better to have consistent functionality in both, especially for people who dislike the twig component syntax.

@DRaichev DRaichev force-pushed the TurboResponse-generic-actions branch 2 times, most recently from 23a9740 to 03629f7 Compare October 30, 2024 22:38
@DRaichev DRaichev force-pushed the TurboResponse-generic-actions branch 2 times, most recently from 1683ace to bb8aeed Compare October 30, 2024 22:44
@smnandre
Copy link
Member

Looks good to me!

Could you check the type of values and throw if not expected (a non-stringable object) ?

@smnandre smnandre changed the title [Turbo] [TurboStreamResponse] Custom action support [Turbo] Support custom TurboStreamResponse actions Oct 30, 2024
@DRaichev
Copy link
Author

Looks good to me!

Could you check the type of values and throw if not expected (a non-stringable object) ?

I believe this is going to throw:

foreach ($attr as $key => $value) {
    $key = htmlspecialchars($key);
    if (null === $value) {
        $attrString .= \sprintf(' %s', $key);
    } elseif (\is_int($value) || \is_float($value)) {
        $attrString .= \sprintf(' %s="%s"', $key, $value);
    } else {
        $attrString .= \sprintf(' %s="%s"', $key, htmlspecialchars($value)); // htmlspecialchars expect string so will throw here
    }
}

Or do you mean somewhere else ?

@DRaichev
Copy link
Author

Looks good to me!
Could you check the type of values and throw if not expected (a non-stringable object) ?

I believe this is going to throw:

foreach ($attr as $key => $value) {
    $key = htmlspecialchars($key);
    if (null === $value) {
        $attrString .= \sprintf(' %s', $key);
    } elseif (\is_int($value) || \is_float($value)) {
        $attrString .= \sprintf(' %s="%s"', $key, $value);
    } else {
        $attrString .= \sprintf(' %s="%s"', $key, htmlspecialchars($value)); // htmlspecialchars expect string so will throw here
    }
}

Or do you mean somewhere else ?

So this is ready for merge then, right

@smnandre
Copy link
Member

After some thinking, why don't we rename the custom($action) method in action($name, .... ... ... .. ) ?

@DRaichev
Copy link
Author

After some thinking, why don't we rename the custom($action) method in action($name, .... ... ... .. ) ?

Yeah, I was thinking about this when I renamed the one in TurboStreamResponse
It should be either custom in both or action in both.

I don't really have a preference, both work, so I leave the choice to you

@smnandre
Copy link
Member

Let's use action in both then (sorry for the late comment)

@DRaichev DRaichev force-pushed the TurboResponse-generic-actions branch from bb8aeed to 488e061 Compare October 31, 2024 19:53
Copy link
Member

@smnandre smnandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last couple of minor suggestions..

Could you update the CHANGELOG in the src/Turbo repository ?

And let's merge this :)

@DRaichev DRaichev force-pushed the TurboResponse-generic-actions branch 4 times, most recently from 8c46bf3 to 1b1aa6e Compare October 31, 2024 21:01
@DRaichev
Copy link
Author

Ready to merge

Copy link
Member

@smnandre smnandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @DRaichev !

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Nov 1, 2024
src/Turbo/CHANGELOG.md Outdated Show resolved Hide resolved
@DRaichev DRaichev force-pushed the TurboResponse-generic-actions branch from 1b1aa6e to 4b62702 Compare November 1, 2024 07:47
@DRaichev DRaichev force-pushed the TurboResponse-generic-actions branch from 4b62702 to 6c3a3b7 Compare November 1, 2024 07:51
@DRaichev
Copy link
Author

DRaichev commented Nov 1, 2024

Updated the changelog, now ready to merge

Copy link
Member

@smnandre smnandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature Status: Reviewed Has been reviewed by a maintainer Turbo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants