-
-
Notifications
You must be signed in to change notification settings - Fork 360
[Twig][Live] add component attribute system/helper #220
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
[Twig][Live] add component attribute system/helper #220
Conversation
629d834 demonstrates a bit of a hack to remove the requirement for BTW, I'm totally fine to just live with |
I love this feature @kbond |
To make this feature live, I was thinking to mark the property as a |
I like this - and the idea is proven by other libraries.
See https://twig.symfony.com/doc/3.x/api.html#escaper-extension - look for
I can't think of why this would be a problem. So 👍
2 ideas for this, both which are beyond the scope of this PR: A) Allow using "…args" for the “props” {{ component('my_component', attributes: { class: 'foo', style: 'color:red' }, otherProp: 'bar' ) }} It removes an extra
|
Also, it would be great if, for live components, we could also do: <div{{ attributes }}> Instead of: <div {{ init_live_component(this) }}> |
This is what I used. The hack I was referring to is the weird way of adding it in the extension. I don't think there's a great way for a third party bundle to add these. |
13a6727
to
947cac7
Compare
11b6989
to
3123f35
Compare
5f5fd76
to
ae725d4
Compare
a8fa0ab
to
dc76d03
Compare
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] add ability to have array props | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | n/a | License | MIT Discovered a bug when working on #220. You currently can't have `LiveProp`'s that are "arrays". This is because the [_expose_ system](https://symfony.com/bundles/ux-live-component/current/index.html#modifying-embedded-properties-with-the-exposed-option) uses arrays and we weren't strict enough in determining when this system was being used. Commits ------- f71e718 [LiveComponent] add ability to have array props
09a404e
to
af957de
Compare
af957de
to
f921ece
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like this version!
src/LiveComponent/src/EventListener/AddLiveAttributesSubscriber.php
Outdated
Show resolved
Hide resolved
src/LiveComponent/src/EventListener/AddLiveAttributesSubscriber.php
Outdated
Show resolved
Hide resolved
src/LiveComponent/src/EventListener/LiveComponentSubscriber.php
Outdated
Show resolved
Hide resolved
7ebc0f2
to
b192afc
Compare
a9edf88
to
3c800d8
Compare
- adds `HasAttributesTrait` for components which makes `attributes` variable available in component templates - adds `PostMount` component hook to intercept extra props - adds `PreRenderEvent` to intercept/manipulate twig template/variables before rendering - [BC BREAK] Remove `init_live_component()` twig function, use `{{ attributes }}` instead.
3c800d8
to
d638782
Compare
Thank you Kevin! |
Hi, I wanted to use a
{{ component('input', {type: 'text', name: 'query', value: form.address.vars.data }) }}
I saw that it was this error that is affected: https://github.com/symfony/ux-twig-component/blob/2.x/src/HasAttributesTrait.php#L49 |
Ah, good catch, so you'd expect the html to be: <input type="text" name="query" value="" /> |
@kbond Exactly |
Something else to improve (maybe): |
Another good catch, @weaverryan and I were discussing and we think null or empty string should mean render the attribute name only: {{ component('input', {type: 'text', name: 'query', value: null, autofocus: null }) }}
<input type="text" name="query" value autofocus /> WDYT? |
Yes, |
You're thinking? {{ component('input', {type: 'text', name: 'query', value: '', autofocus: null }) }}
<input type="text" name="query" value="" autofocus /> There are some limitations to doing this when working with live components. But we are discussing some solutions. |
Yeah |
This PR was merged into the 2.x branch. Discussion ---------- [Live] refactor AddLiveAttributesSubscriber | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Tickets | n/a | License | MIT Cleanup from #220, this subscriber should not depend on the twig runtime. Commits ------- 2d429b0 [Live] refactor AddLiveAttributesSubscriber
…autofocus) (kbond) This PR was merged into the 2.x branch. Discussion ---------- [Twig][Live] null attribute values render without value (ie autofocus) | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | n/a | License | MIT Ref: #220 (comment) A null attribute value renders without the _value_: ```twig {# templates/components/my_component.html.twig #} <input{{ attributes}}/> {# render component #} {{ component('my_component', { type: 'text', value: '', autofocus: null }) }} {# renders as: #} <input type="text" value="" autofocus/> ``` **TODO:** - [x] `null` vs `''` (#220 (comment)) - [x] attributes without a value like `autofocus` (#220 (comment)) - [x] LiveComponent dehydration/hydration issues (`null` values are lost during re-render) - solved in #264 Commits ------- c1e484b [Twig][Live] null attribute values render without value (ie autofocus)
…autofocus) (kbond) This PR was merged into the 2.x branch. Discussion ---------- [Twig][Live] null attribute values render without value (ie autofocus) | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | n/a | License | MIT Ref: symfony/ux#220 (comment) A null attribute value renders without the _value_: ```twig {# templates/components/my_component.html.twig #} <input{{ attributes}}/> {# render component #} {{ component('my_component', { type: 'text', value: '', autofocus: null }) }} {# renders as: #} <input type="text" value="" autofocus/> ``` **TODO:** - [x] `null` vs `''` (symfony/ux#220 (comment)) - [x] attributes without a value like `autofocus` (symfony/ux#220 (comment)) - [x] LiveComponent dehydration/hydration issues (`null` values are lost during re-render) - solved in #264 Commits ------- c1e484b [Twig][Live] null attribute values render without value (ie autofocus)
This PR was merged into the 2.x branch. Discussion ---------- [Twig][Live] Native attributes | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | n/a | License | MIT This removes the need for the `HasAttributesTrait` that was introduced in #220. Attributes are now "a thing all component have". `ComponentFactory::create()` returns a new `MountedComponent` object that holds the "mounted" component, attributes and component metadata. `LiveComponentHydrator::dehydrate()` now accepts an instance of `MountedComponent` and `LiveComponentHydrator::hydrate()` now returns an instance of `MountedComponent`. If the component is live, any attributes are dehydrated/hydrated as "readonly" data (they can't be modified by the frontend). **TODO** - [x] Update docs referring to `HasAttributesTrait` Commits ------- 37acff2 [Twig][Live] make attributes a "native" feature
…autofocus) (kbond) This PR was merged into the 2.x branch. Discussion ---------- [Twig][Live] null attribute values render without value (ie autofocus) | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | n/a | License | MIT Ref: symfony/ux#220 (comment) A null attribute value renders without the _value_: ```twig {# templates/components/my_component.html.twig #} <input{{ attributes}}/> {# render component #} {{ component('my_component', { type: 'text', value: '', autofocus: null }) }} {# renders as: #} <input type="text" value="" autofocus/> ``` **TODO:** - [x] `null` vs `''` (symfony/ux#220 (comment)) - [x] attributes without a value like `autofocus` (symfony/ux#220 (comment)) - [x] LiveComponent dehydration/hydration issues (`null` values are lost during re-render) - solved in #264 Commits ------- c1e484b [Twig][Live] null attribute values render without value (ie autofocus)
A common pattern with other component library's (ie blade components/vue) is to pass html attributes to your component that are set on the component's root node. This proposal adds a way to do this with twig components. Enable on your component by adding the
HasAttributesTrait
. Attributes are any data passed tocomponent()
that cannot be mounted on the component itself. This extra data is added to aComponentAttributes
object that lives as a public property on your component (available asattributes
in your component's template).This should all work out-of-the box with live components.
Todo:
init_live_component()
into attributes when availablePreRender
event.ComponentAttributes
api (defaults instead of merge)attributes
always available in normal twig component templates (not sure about this - it would effectively make attributes native but it a lame way)init_live_component()
withattributes
in docsUsage
To use, add the
HasAttributesTrait
to your component:Then, in your component's template:
When rendering the component, you can pass an array of html attributes to add:
Defaults & Merging
In your component template, you can set defaults that are merged with
passed attributes. The passed attributes override the default with
the exception of class. For class, the defaults are prepended:
Only
Without
Live Components
We removed the
init_live_component()
twig function (BC BREAK) and replaced with theattributes
variable which is always available (even if your component doesn't useHasAttributesTrait
).To upgrade, replace all calls to
init_live_component()
withattributes
: