Skip to content

[Twig] Add a strategy for adding a Stimulus controller to a Twig component #589

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

Merged
merged 1 commit into from
Dec 5, 2022

Conversation

weaverryan
Copy link
Member

…onent root element

Q A
Bug fix? yes-ish
New feature? yes
Tickets Fix #527
License MIT

Hi!

This allows adding a custom Stimulus controller to the root element of a component. The syntax is:

<div {{ attributes.add(stimulus_controller('my-controller', { someValue: 'foo' })) }}>

This is a compromise of having a nice syntax vs not duplicating code. The attributes.add() method is designed to work specifically with the stimulus_* functions, so it's name is kinda funny. But it feels way better than something like attributes->stimulusController(stimulus_controller()). Just doing attributes.stimulusController('my-controller') isn't possible at the moment, because it would require us to create a StimulusControllersDto and that, unfortunately, requires a Twig Environment for escaping purposes.

Cheers!

@weaverryan weaverryan force-pushed the custom-stimulus-controller branch from 9764aa6 to 2d2a137 Compare December 5, 2022 15:48
@weaverryan weaverryan merged commit 80bbf4f into symfony:2.x Dec 5, 2022
@althaus
Copy link
Contributor

althaus commented Dec 15, 2022

Hey.... I've just stumbled upon this issue as the feature is already in the docs but not released yet.

Could you pack a new version with this change?

Cheers
Matthias

@weaverryan
Copy link
Member Author

Oooh, we need to add the versionadded annotation above the docs for this to make it clear! We’ll have as new release sometime soon… but not immediately.

@weaverryan weaverryan deleted the custom-stimulus-controller branch December 15, 2022 15:54
weaverryan added a commit that referenced this pull request Dec 15, 2022
@weaverryan
Copy link
Member Author

Added in sha: 8e5cab3

@@ -94,4 +95,21 @@ public function without(string ...$keys): self

return $clone;
}

public function add(AbstractStimulusDto $stimulusDto): self
Copy link
Member

Choose a reason for hiding this comment

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

Isn't a bit weird to have a method add that takes a StimulusDto?
Shouldn't this method be named withStimulus or something like that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably :). But it looked a bit terrible when used:

{{ attributes.withStimulus(stimulus_controller(...)) }}

The 2x Stimulus. But, we could potentially "open" this add() method later to accept other types of arguments, if we think of something.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, an array of key/values for example

@althaus
Copy link
Contributor

althaus commented Dec 16, 2022

Oooh, we need to add the versionadded annotation above the docs for this to make it clear! We’ll have as new release sometime soon… but not immediately.

Thanks for fixing the doc. I'm totally fine with using a non-release for trying things out. I just felt that integrating custom/legacy JS is quite common use case.

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

Successfully merging this pull request may close these issues.

[Live] Adding a custom Stimulus controller to a live component
3 participants