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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/LiveComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ The following hooks are available (along with the arguments that are passed):
* ``loading.state:finished`` args ``(element: HTMLElement)``
* ``model:set`` args ``(model: string, value: any, component: Component)``

Adding a Stimulus Controller to your Component Root Element
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To add a custom Stimulus controller to your root component element:

.. code-block:: twig

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

Loading States
--------------

Expand Down
3 changes: 2 additions & 1 deletion src/TwigComponent/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"require-dev": {
"symfony/framework-bundle": "^5.4|^6.0",
"symfony/phpunit-bridge": "^6.0",
"symfony/twig-bundle": "^5.4|^6.0"
"symfony/twig-bundle": "^5.4|^6.0",
"symfony/webpack-encore-bundle": "^1.15"
},
"conflict": {
"symfony/config": "<5.4.0"
Expand Down
6 changes: 6 additions & 0 deletions src/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,12 @@ Set an attribute's value to ``null`` to exclude the value when rendering:
{# renders as: #}
<input type="text" value="" autofocus/>

To add a custom Stimulus controller to your root component element:

.. code-block:: twig

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

.. note::

You can adjust the attributes variable exposed in your template::
Expand Down
20 changes: 19 additions & 1 deletion src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

namespace Symfony\UX\TwigComponent;

use Symfony\WebpackEncoreBundle\Dto\AbstractStimulusDto;

/**
* @author Kevin Bond <[email protected]>

*
* @immutable
*/
Expand Down Expand Up @@ -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

{
$controllersAttributes = $stimulusDto->toArray();
$attributes = $this->attributes;

$attributes['data-controller'] = implode(' ', array_merge(
explode(' ', $attributes['data-controller']),
explode(' ', $controllersAttributes['data-controller'] ?? [])
));
unset($controllersAttributes['data-controller']);

$clone = new self($attributes);

// add the remaining attributes for values/classes
return $clone->defaults($controllersAttributes);
}
}
27 changes: 27 additions & 0 deletions src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\UX\TwigComponent\ComponentAttributes;
use Symfony\WebpackEncoreBundle\Dto\AbstractStimulusDto;

/**
* @author Kevin Bond <[email protected]>
Expand Down Expand Up @@ -60,4 +61,30 @@ public function testCanGetWithout(): void

$this->assertSame(['class' => 'foo'], $attributes->without('style')->all());
}

public function testCanAddStimulusController(): void
{
$attributes = new ComponentAttributes([
'class' => 'foo',
'data-controller' => 'live',
'data-live-data-value' => '{}',
]);

$controllerDto = $this->createMock(AbstractStimulusDto::class);
$controllerDto->expects(self::once())
->method('toArray')
->willReturn([
'data-controller' => 'foo bar',
'data-foo-name-value' => 'ryan',
]);

$attributes = $attributes->add($controllerDto);

$this->assertEquals([
'class' => 'foo',
'data-controller' => 'live foo bar',
'data-live-data-value' => '{}',
'data-foo-name-value' => 'ryan',
], $attributes->all());
}
}