-
I just started looking at using Alpine.js with Phoenix Live View today, and immediately hit some issues. I was following the guides out there, including https://dockyard.com/blog/2020/12/21/optimizing-user-experience-with-liveview written by the author of Live View and who added support for Alpine.js to LV in phoenixframework/phoenix_live_view#809. After digging into it I identified that 2.8.2 works fine, but 3.0.6 and later seems to break. I have duplicated the issue in a brand new bare-bones phoenix project. I configured LV to call import Alpine from 'alpinejs'
Alpine.start()
let liveSocket = new LiveSocket("/live", Socket, {
params: { _csrf_token: csrfToken },
dom: {
onBeforeElUpdated(from, to){
if (from.__x) { Alpine.clone(from.__x, to) }
}
}
}) And wrote a simple test as: <div class="row">
<div class="column">
<div class="phx-hero">
<h2>Live view counter</h2>
<h3><%= @count %></h4>
<button phx-click="increment">Increment</button>
</div>
</div>
<div class="column">
<div class="phx-hero" x-data="{ counter: 0}">
<h2>Alpine.js counter</h2>
<h3 x-text="counter"></h3>
<button @click="counter++">Increment</button>
</div>
</div>
</div> However, it appears that whenever LV updates the DOM, it is clobbering Alpine and not cloning the before state of the dom. I downgraded to version 2.8.2, and it works as expected: lv_a_expected.mp4This is what happens if I upgrade it to the next release, 3.0.6: lv_a_3bug.mp4And that behavior stays the same with the latest version, 3.2.0. So it seems that for some reason the dom is not being cloned properly when LV updates? This test is available at https://github.com/nshafer/a3test. The JS is in This might be related to #1647. Phoenix: 1.5.9 (latest) Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello,
Cheers, |
Beta Was this translation helpful? Give feedback.
Hello,
according to this thread
https://elixirforum.com/t/alpine-js-v3-support-for-liveview/40465
you should update the dom setting with
Cheers,
Sébastien.