You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR was squashed before being merged into the main branch.
Discussion
----------
feat(twig): implements stimulus_action() and stimulus_target() Twig functions, close#119
Hi 👋
This PR is a proposal for #119, which adds `stimulus_action()` and `stimulus_target()` Twig functions.
`stimulus_action()` can be a bit hard to use because it supports many syntaxes, since Stimulus allows to use multiple actions in a single `data-action` attribute and we should takes care of the default event:
- `stimulus_action('controller', 'method')`
- a custom event: `stimulus_action('controller', 'method', 'event)`
- multiple controllers/actions:
```twig
{{ stimulus_action({
'controller': 'method',
'controller-2': ['method', 'method2'],
'controller-3': {'click': 'onClick'},
'controller-4': ['method', {'click': 'onClick'}, {'window@resize': 'onWindowResize'}],
}) }}
```
For `stimulus_target`:
- `stimulus_target('controller', 'target')`
- `stimulus_target('controller', 'target1 target2')`, Stimulus allows multiple targets
- `stimulus_target({ 'controller1': 'target', 'controller2': 'target2 target3' })`, array syntax
Usage:
```twig
<div {{ stimulus_controller('foo') }}>
<div {{ stimulus_target('foo', 'myTarget') }}></div>
<!-- <div data-foo-target="myTarget"></div> -->
<div {{ stimulus_target('foo', 'myTarget mySecondTarget') }}></div>
<!-- <div data-foo-target="myTarget mySecondTarget"></div> -->
<div {{ stimulus_target({ 'foo': 'myTarget', 'bar': 'anotherTarget' }) }}></div>
<!-- <div data-foo-target="myTarget" data-bar-target="anotherTarget"></div> -->
{# Listen to default event and call "onClick" #}
<a {{ stimulus_action('foo', 'onClick') }}>A link</a>
<!-- <div data-action="foo#onClick">A link</a> -->
{# Listen to event "click" and call "onClick" #}
<a {{ stimulus_action('foo', 'onClick', 'click') }}>A Link</a>
<!-- <div data-action="click->foo#onClick">A link</a> -->
{# Listen to default event, call "onClick" from "foo" controller and "onClick" + "onSomethingElse" from "bar" controller #}
<a {{ stimulus_action({
'foo': 'onClick',
'bar': ['onClick', 'onSomethingElse']
}) }}>A link</a>
<!-- <div data-action="click->foo#onClick bar#onClick bar#onSomethingElse">A div</div> -->
{# It is possible to pass a map, here "bar#onWindowResize" will be called on "window@resize" #}
<div {{ stimulus_action({
'foo': 'onClick',
'bar': ['onClick', {'window@resize': 'onWindowResize'}]
}) }}>A div</a>
<!-- <div data-action="foo#onClick bar#onClick window@resize->bar#onWindowResize">A div</div> -->
</div>
```
WDYT? Thanks!
---
For #119 (comment), I think it should belongs to another PR.
Commits
-------
c022b08 feat(twig): implements stimulus_action() and stimulus_target() Twig functions, close#119
thrownew \InvalidArgumentException('You cannot pass a string to the second or third argument while passing an array to the first argument of stimulus_action(): check the documentation.');
thrownew \InvalidArgumentException('You cannot pass a string to the second argument while passing an array to the first argument of stimulus_target(): check the documentation.');
0 commit comments