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

[LiveComponent] Make live_action compatible with Twig components #2586

Open
gremo opened this issue Feb 21, 2025 · 5 comments
Open

[LiveComponent] Make live_action compatible with Twig components #2586

gremo opened this issue Feb 21, 2025 · 5 comments

Comments

@gremo
Copy link
Contributor

gremo commented Feb 21, 2025

Right now, you cannot directly use the live_action helper to render live attributes because it returns a string. This means the following will not work:

<twig:Button type="button" {{ live_action("foo", { bar: "baz" }) }}>
  Click me!
</twig:Button>

On the other hand, some helpers like stimulus_controller and stimulus_action work because they return an iterable that can be used with the Twig spread operator .....

I don't if why this returns a string instead of the StimulusAttributes instance? What I'm missing?

This would be a great DX addition 👍

I changed LiveComponentRuntime to return the StimulusAttributes instance and the following will work without any problem:

<twig:Button
    type="button"
    content="Click me!"
    {{ ...live_action("foo", { bar: "baz" }) }}
/>

<button type="button" {{ live_action("foo", { bar: "baz" }) }}>
    Click me!
</div>
@smnandre
Copy link
Member

This helper is a quick dedicated helper that has no objective to replace the stimulus attributes. Why don't you use stimulus_action here ?

@gremo
Copy link
Contributor Author

gremo commented Feb 21, 2025

Yes, it is clear to me that the helper is not meant to replace the one from Stimulus 😄 but forgive me for asking: it uses the StimulusAttributes class to create the set of attributes and works in practically the same way... so why return its version as a string, making it incompatible with the components? Wouldn't it have been simpler, first and foremost, to return the StimulusAttributes object instead?

There is something I'm missing: is live_action used elsewhere where a string type is actually required?

@smnandre
Copy link
Member

The first and end goal is to render in html

data-action="live#action" data-live-action-param="foo"

.. as any Twig function would do :)

The internal implementation is internal, as we want to keep the possibility to changes StimulusAttributes.

Again, what is the problem you have with the stimulus_action helper (that seems to do what you wan) ?

@gremo
Copy link
Contributor Author

gremo commented Feb 21, 2025

Nothing special prevents me from doing it 😄 Clearly, I can use stimulus_action, but since live_action is already available, my intention was to use that, even though they are obviously equivalent.

If we want to be precise, using live_action would immediately indicate that an element is calling a Live controller, whereas with stimulus_action, I would be inclined to think that it's a custom Stimulus controller of mine.

Mine was just an observation and a suggestion to make everything more usable and improve the "DX". If you think this change is unnecessary, thank you, and feel free to close this issue.

@smnandre
Copy link
Member

I understand the question and i'd be happy to discuss the future implementation / DX (I really do!).

Now we cannot change it either way, as it's released like this.

Or any code expecting a string would break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants