-
-
Notifications
You must be signed in to change notification settings - Fork 342
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
[LiveComponent] - Make DeterministicTwigIdCalculator Extendable/Overridable #2573
Comments
I'd be curious to know which one (the older and the new one)
"one" nested ? Because ..
Looks you have a lot of ids here, right ?
Very logical indeed After, you may have a problem somewhere because if you pass an "id" to a child component, it will be used as id. The determenistic value is not used in your component HTML then. So with no impact on the Morph. |
I will try to provide some more context. I have implemented infinite scrolling like described here https://ux.symfony.com/demos/live-component/infinite-scroll LiveListComponent {% for item in items %}
<twig:LiveComponentA
id="live_component_a_{{ item.id }}"
data-live-ignore
/>
{% endfor %} LiveComponentA - Twig part <div {{ attributes }}>
{# a lot of html #}
{# actually in my case this component was deeply nested in other Twig components #}
<twig:LiveComponentB />
{# a lot of html #}
</div> Let's assume that first page of items have ids So for first page of List component I have these element ids on page
So for second page of List component I have these element ids on page
As you can see, the deterministic calculator is providing same ids for first and second page. It cannot be solved with
readonly class LiveComponentIdEventListener
{
#[AsEventListener(priority: 100)]
public function onPostMountEvent(PostMountEvent $event): void
{
$metadata = $event->getMetadata();
if ($metadata === null || $metadata->get('live', false) !== true) {
return;
}
if (Strings::isNullOrWhiteSpace($data['id'] ?? null)) {
throw new RuntimeException(
sprintf(
'The Live Component "%s" (%s) must have and "id" attribute',
$metadata->getName(),
get_class($event->getComponent()),
),
);
}
}
}
{% for item in items %}
{% apply enforce_live_component_ids %}
<twig:LiveComponentA
id="live_component_a_{{ item.id }}"
data-live-ignore
/>
{% endapply %}
{% endfor %}
{% for item in items %}
<twig:LiveComponentA
key="{{ item.id }}"
data-live-ignore
/>
{% endfor %}
LiveComponentA - Twig part <div {{ attributes }}>
{# a lot of html #}
<twig:LiveComponentB />
{# a lot of html #}
</div> But this solution cannot be done by hooks and it would need to be part of Live Component library (if it's possible to implement). To answer your questions:
I don't know. Maybe it was not caused by update but I've changed something in my Twig templates which caused Morphdom to act differently then before. So my questions are:
|
The DeterministicId is not made for this case, but to differenciate multiple component rendered at the same time, and clearly cannot be made external, as yourself would not have the content of the component when it rerenders Half of the https://ux.symfony.com/demos/live-component/infinite-scroll-2 is about Morphing trick and the importance of choosing your ID manually. If you do use manual ID, they are used instead of the generated live-id. So any automation you would need/want to have, you can, using functions, filters, tags, or any implementation. I don't get: is there something preventing you to use the page as a prefix ? |
As I said before. Nothing is preventing me from using page / id / whatever from as element id prefix. But when you are using morphing trick to paginate multiple-nested live components, YOU HAVE TO pass this prefix (or set your own id) to EVERY Live Component which will occur in paginated item HTML. And that is just not an option for me to do this, because you have to remember that this, this and this Live Component is used in this morphing trick and when you update them (and maybe adding new Live Component to one them) you can't forget to set its own id because than morphing trick can stop working. It's just so much easily error-prone. So I am trying to come up with a solution which will alert me, if I add new Live Component to morphing pagination trick and forgot to set its unique id. |
Hi,
Could you please make DeterministicTwigIdCalculator extendable or overridable from the application?
I recently encountered an issue that took me a while to diagnose. I implemented a scrolling feature based on this tutorial.
Everything worked fine in an older version, but after an update, it stopped working. Instead of appending new items to the component, each new "page" replaced the previous content.
After investigating, I discovered that the issue was caused by Morphdom’s implementation and the deterministic ID calculator. It seems that Morphdom builds an element ID map based on all IDs in the DOM tree. Since I didn't provide a custom ID for one of my deeply nested Live Components, the deterministic calculator assigned it the same ID every time (every "page" has contained same set of ids). This caused Morphdom to behave incorrectly. After explicitly providing an ID for that Live Component, everything started working again.
This is roughly how my pagination code looked like.
My proposal is to make the service responsible for providing default IDs to components overridable (e.g., by implementing an interface). This would allow me to do something like this:
This approach would enforce the manual definition of element IDs instead of relying on the deterministic calculator, which can cause issues with the "infinite scroll hack" solution.
The text was updated successfully, but these errors were encountered: