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

[TwigComponent] Support ...spread operator with html syntax #1023

Merged
merged 1 commit into from
Aug 7, 2023

Conversation

norkunas
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
Tickets Fix #845
License MIT

I think that's a new feature like in twig.

@norkunas norkunas force-pushed the spread branch 2 times, most recently from 73e24aa to 5a781d4 Compare July 27, 2023 07:43
@@ -216,5 +216,31 @@ public function getLexTests(): iterable
'{% verbatim %}<twig:Alert/>{% endverbatim %}',
'{% verbatim %}<twig:Alert/>{% endverbatim %}',
];

yield 'component_attr_spreading_self_closing' => [
'<twig:foobar bar="baz"{{...attr}}/>',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of having ... between {{ why not just do:

<twig:foobar bar="baz" ...attr />

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For me eyes it's easier to find that it's a variable like in twig

Copy link
Member

@kbond kbond Jul 31, 2023

Choose a reason for hiding this comment

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

A little funky looking but what about <twig:foobar bar="baz" :...attr />? (in addition to {{). This sort of lines up with dynamic attribute values: https://symfony.com/bundles/ux-twig-component/current/index.html#passing-props-as-html-attributes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My proposed solution is already in our prod and working, so I'd prefer as is 😁

Copy link
Member

Choose a reason for hiding this comment

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

Looking at React:

<button className={className} {...other} />

And Vue:

<button :className="className" v-bind="other">

Given these, the logical options are:

<twig:Button className={{ className }} {{...other}} />

or

<twig:Button live-bind="other">

And that live-bind thing looks silly :). So I agree with the proposed syntax.

Copy link
Member

Choose a reason for hiding this comment

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

Note that JSX for Vue.js also use {... } syntax.

{{... }} is nice because the two brackets reminds us about Twig

@@ -201,6 +203,15 @@ private function consumeAttributes(string $componentName): string
break;
}

if ($this->check('{{...') || $this->check('{{ ...')) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you should consume the whitespace before doing this check, tab, or multiples tab can be valid syntax too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is consumed 4 lines before, isn't it?

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

Wow! Fast turn-around on this :). We also need a mention in the docs - probably one example at the bottom of https://github.com/symfony/ux/blob/2.x/src/TwigComponent/doc/index.rst#passing-props-as-html-attributes would do it

@norkunas
Copy link
Contributor Author

norkunas commented Aug 1, 2023

Wow! Fast turn-around on this :). We also need a mention in the docs - probably one example at the bottom of https://github.com/symfony/ux/blob/2.x/src/TwigComponent/doc/index.rst#passing-props-as-html-attributes would do it

Added

@weaverryan
Copy link
Member

Thank you Tomas!

@weaverryan weaverryan merged commit 26a72bb into symfony:2.x Aug 7, 2023
37 of 38 checks passed
@norkunas norkunas deleted the spread branch August 8, 2023 03:30
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.

[TwigComponent] Let HTML components work together with stimulus helpers
5 participants