Skip to content

[TwigComponent] Add support for AttributeValueInterface from twig/html-extra:^3.24.0 in ComponentAttributes#3408

Merged
Kocal merged 1 commit intosymfony:2.xfrom
Kocal:twig-component-add-twig3.24-html-attr-merge-support
Mar 23, 2026
Merged

[TwigComponent] Add support for AttributeValueInterface from twig/html-extra:^3.24.0 in ComponentAttributes#3408
Kocal merged 1 commit intosymfony:2.xfrom
Kocal:twig-component-add-twig3.24-html-attr-merge-support

Conversation

@Kocal
Copy link
Copy Markdown
Member

@Kocal Kocal commented Mar 23, 2026

Q A
Bug fix? no
New feature? yes
Deprecations? no
Documentation? no
Issues Fix #...
License MIT

Close twigphp/Twig#4790, replace twigphp/Twig#4791.

This PR update ComponentAttributes to support AttributeValueInterface from Twig 3.24 with html_attr_type and HTML attributes merging strategy.

This helps resolve situations where merging HTML attributes needs to be more sophisticated than a simple array_merge.

For example in UX Toolkit, we have an issue where it's not possible to use a single <twig:Button> with Dialog and Tooltip triggers, both triggers define a trigger_attrs with some attributes that may conflict. Here a simplified version:

{%- set dialog_trigger_attrs = {
    'data-action': 'click->dialog#open',
} -%}
{%- set tooltip_trigger_attrs = {
    'data-action': 'mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide',
} -%}

<twig:Button
    {{ ...dialog_trigger_attrs }}
    {{ ...tooltip_trigger_attrs }}
/>

Here, only data-action="mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide" will be rendered, the value from dialog_trigger_attrs is purely ignored.

By supporting the HTML attributes merging strategy introduced in Twig HTML Extra 3.24, we can use the new Twig filter html_attr_type:

{%- set dialog_trigger_attrs = {
    'data-action': 'click->dialog#open'|html_attr_type('sst'),
} -%}
{%- set tooltip_trigger_attrs = {
    'data-action': 'mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide'|html_attr_type('sst'),
} -%}

Combined to html_attr_merge filter (that return an array where some values are an instance of Twig\Extra\Html\HtmlAttr\AttributeValueInterface), the following example will correctly render data-action="click->dialog#open mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide":

<twig:Button
    {{ ...{}|html_attr_merge(dialog_trigger_attrs, tooltip_trigger_attrs) }}
/>

@Kocal Kocal self-assigned this Mar 23, 2026
@carsonbot carsonbot added Feature New Feature TwigComponent Status: Needs Review Needs to be reviewed labels Mar 23, 2026
…html-extra:^3.24.0` in `ComponentAttributes`

Close twigphp/Twig#4790, replace twigphp/Twig#4791.

This PR update `ComponentAttributes` to support `AttributeValueInterface` from Twig 3.24 with `html_attr_type` and HTML attributes merging strategy.

This helps resolve situations where merging HTML attributes needs to be more sophisticated than a simple `array_merge`.

For example in UX Toolkit, we have an issue where it's not possible to use a single `<twig:Button>` with `Dialog` and `Tooltip` triggers, both triggers define a `trigger_attrs` with some attributes that may conflict. Here a simplified version:
```
{%- set dialog_trigger_attrs = {
    'data-action': 'click->dialog#open',
} -%}
{%- set tooltip_trigger_attrs = {
    'data-action': 'mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide',
} -%}

<twig:Button
    {{ ...dialog_trigger_attrs }}
    {{ ...tooltip_trigger_attrs }}
/>
```

Here, only `data-action="mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide"` will be rendered, the value from `dialog_trigger_attrs` is purely ignored.

By supporting the HTML attributes merging strategy introduced in Twig HTML Extra 3.24, we can use the new Twig filter `html_attr_type`:
```twig
{%- set dialog_trigger_attrs = {
    'data-action': 'click->dialog#open'|html_attr_type('sst'),
} -%}
{%- set tooltip_trigger_attrs = {
    'data-action': 'mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide'|html_attr_type('sst'),
} -%}
```

Combined to `html_attr_merge` (that return an array where some values are an instance of `Twig\Extra\Html\HtmlAttr\AttributeValueInterface`), the following example will correctly render `data-action="click->dialog#open mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide"`:
```twig
<twig:Button
    {{ ...{}|html_attr_merge(dialog_trigger_attrs, tooltip_trigger_attrs) }}
/>
```
Copy link
Copy Markdown
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

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

❤️

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Mar 23, 2026
@Kocal Kocal merged commit ddb8ecb into symfony:2.x Mar 23, 2026
29 checks passed
@Kocal Kocal deleted the twig-component-add-twig3.24-html-attr-merge-support branch March 23, 2026 20:07
Kocal added a commit to Kocal/symfony-ux that referenced this pull request Mar 23, 2026
Kocal added a commit to Kocal/symfony-ux that referenced this pull request Mar 23, 2026
Kocal added a commit that referenced this pull request Mar 23, 2026
…a:^3.24` to correctly merge trigger's attributes (Kocal)

This PR was merged into the 2.x branch.

Discussion
----------

[Toolkit] Embrace `html_attr_type` from `twig/html-extra:^3.24` to correctly merge trigger's attributes

| Q              | A
| -------------- | ---
| Bug fix?       | no
| New feature?   | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations?  | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md -->
| Documentation? | no <!-- required for new features, or documentation updates -->
| Issues         | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License        | MIT

Related to #3408

Commits
-------

725c115 [Toolkit] Embrace `html_attr_type` from `twig/html-extra:^3.24` to correctly merge trigger's attributes
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 TwigComponent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow html_attr() to return a spreadable object for use with Twig Component attributes

3 participants