ux icon indicating copy to clipboard operation
ux copied to clipboard

[LiveComponent] Fix morphing for Alpine.js v3

Open Kobo-one opened this issue 1 year ago • 2 comments

Q A
Bug fix? yes
New feature? no
Issues /
License MIT

This PR fixes live component compatibility for Alpine v3.

Currently, the live component controller already implements the Alpine morph plugin. This works fine for Alpine v2, but since v3, the __x property is no longer used by Alpine. Instead, we can check for _x_dataStack.

Also, the morph plugin no longer requires the __x property; the DOM element should be used instead.

I made sure the changes support both Alpine v2 and v3.

Original implementation and issue:

  • https://github.com/symfony/ux/issues/984
  • https://github.com/symfony/ux/pull/1126

Kobo-one avatar Oct 11 '24 12:10 Kobo-one

Thank you @Kobo-one

smnandre avatar Oct 11 '24 15:10 smnandre

Hi,

My current test setup uses a reversed logic; an alpine element inside of a LiveComponent. The morph is necessary to keep the alpine state when a LiveComponent re-renders with DOM changes.

But as your video shows there might be an issue with LiveComponents inside an Alpine element. Oddly enough I also tested this and did not see any weird behaviour. Both with an alpine component surrounding a LiveComponent as well as sharing the same DOM element.

Could you share more details about how your setup is structured in Twig?

That said, this shows the PR has a breaking change and it should not be merged (yet).

Kobo-one avatar Oct 15 '24 12:10 Kobo-one

I get the same bug. I test the patch from @Kobo-one and it's worked for me.

So, if we use Alpine.js in a live component, it could be buggy actually.

This patch permits to fix that bug creating a bug when using Alpine.js wrapping a live component.

Did you manage it a different way ?

RobinDev avatar Feb 11 '25 17:02 RobinDev

Maybe if you guys could update this PR with a simple test covering the 2 scenarios (alpine wrapping live and vice versa)

(and when i say test, a clean page we can run and test in browsers would do the trick i guess)

What do you think ?

smnandre avatar Feb 11 '25 22:02 smnandre

I get the same bug. I test the patch from @Kobo-one and it's worked for me.

So, if we use Alpine.js in a live component, it could be buggy actually.

This patch permits to fix that bug creating a bug when using Alpine.js wrapping a live component.

Did you manage it a different way ?

@RobinDev since you have the same issue, when I wasn't able to reproduce it in https://github.com/symfony/ux/pull/2259#pullrequestreview-2368996095, could you provide use a reproducer please? 🙏🏻

Kocal avatar Feb 15 '25 18:02 Kocal

I am closing due to inactivity, feel free to re-open the PR if necessary. Thanks for the contribution!

Kocal avatar Oct 09 '25 09:10 Kocal