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

[LiveComponent] Fix morphing for Alpine.js v3 #2259

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/LiveComponent/assets/dist/live_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1375,14 +1375,14 @@ function executeMorphdom(rootFromElement, rootToElement, modifiedFieldElements,
return false;
}
if (fromEl instanceof HTMLElement && toEl instanceof HTMLElement) {
if (typeof fromEl.__x !== 'undefined') {
if (typeof fromEl.__x !== 'undefined' || typeof fromEl._x_dataStack !== 'undefined') {
if (!window.Alpine) {
throw new Error('Unable to access Alpine.js though the global window.Alpine variable. Please make sure Alpine.js is loaded before Symfony UX LiveComponent.');
}
if (typeof window.Alpine.morph !== 'function') {
throw new Error('Unable to access Alpine.js morph function. Please make sure the Alpine.js Morph plugin is installed and loaded, see https://alpinejs.dev/plugins/morph for more information.');
}
window.Alpine.morph(fromEl.__x, toEl);
window.Alpine.morph(fromEl.__x || fromEl, toEl);
smnandre marked this conversation as resolved.
Show resolved Hide resolved
}
if (externalMutationTracker.wasElementAdded(fromEl)) {
fromEl.insertAdjacentElement('afterend', toEl);
Expand Down
6 changes: 3 additions & 3 deletions src/LiveComponent/assets/src/morphdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ export function executeMorphdom(

// skip special checking if this is, for example, an SVG
if (fromEl instanceof HTMLElement && toEl instanceof HTMLElement) {
// We assume fromEl is an Alpine component if it has `__x` property.
// We assume fromEl is an Alpine component if it has `__x` (Alpine v2) or `_x_dataStack` (Alpine v3) property.
// If it's the case, then we should morph `fromEl` to `ToEl` (thanks to https://alpinejs.dev/plugins/morph)
// in order to keep the component state and UI in sync.
// @ts-ignore
if (typeof fromEl.__x !== 'undefined') {
if (typeof fromEl.__x !== 'undefined' || typeof fromEl._x_dataStack !== 'undefined') {
// @ts-ignore
if (!window.Alpine) {
throw new Error(
Expand All @@ -138,7 +138,7 @@ export function executeMorphdom(
}

// @ts-ignore
window.Alpine.morph(fromEl.__x, toEl);
window.Alpine.morph(fromEl.__x || fromEl, toEl);
}

if (externalMutationTracker.wasElementAdded(fromEl)) {
Expand Down