Whenever I try to use Element.remove() or Element.replaceChildren() on an HTML node that belongs to the subtree of a liveview modal provided by mix.gen.live, the entire modal closes and liveview triggers navigation to the previous page.
Removing phx-remove={hide_modal()}
from the liveview modal doesn’t have any effect. Adding phx-update="ignore"
doesn’t have any effect either.
This is the default boilerplate code from the liveview modal, in case it is handy:
def modal(assigns) do
assigns = assign_new(assigns, :return_to, fn -> nil end)
~H"""
<div id="modal" class="phx-modal fade-in" phx-remove={hide_modal()}>
<div
id="modal-content"
class="phx-modal-content fade-in-scale
bg-base-100 "
phx-click-away={JS.dispatch("click", to: "#close")}
phx-window-keydown={JS.dispatch("click", to: "#close")}
phx-key="escape"
>
<%= if @return_to do %>
<%= live_patch "✖",
to: @return_to,
id: "close",
class: "phx-modal-close",
phx_click: hide_modal()
%>
<% else %>
<a id="close" href="#" class="phx-modal-close" phx-click={hide_modal()}>✖</a>
<% end %>
<%= render_slot(@inner_block) %>
</div>
</div>
"""
end
defp hide_modal(js \\ %JS{}) do
js
|> JS.hide(to: "#modal", transition: "fade-out")
|> JS.hide(to: "#modal-content", transition: "fade-out-scale")
end
Using the same functions inside a shadow root does not present this issue, suggesting that this is a liveview issue.
Additionally, using the functions from the browser console does not present the issue either, example:
document.querySelector("[data-id=label]").remove()