I’m using JS
commands to toggle a content. Like this minimal example
defmodule JsToggleBugWeb.Page do
use JsToggleBugWeb, :live_view
alias Phoenix.LiveView.JS
def mount(_, _, socket) do
{:ok, assign(socket, :visible, false)}
end
def handle_event("toggle", _, socket) do
{:noreply, assign(socket, :visible, !socket.assigns.visible)}
end
def handle_event("raise", _, _socket), do: raise("Raise")
def render(assigns) do
~H"""
<div>
<div><%= if @visible, do: "Visible", else: "Not visible" %></div>
<div id="container" class={@visible && "visible1"}>Toggled content</div>
<button phx-click={toggle(@visible)}>Toggle</button>
<button phx-click="raise">Raise</button>
</div>
"""
end
defp toggle(true = _is_visible?) do
JS.remove_class("visible", to: "#container")
|> JS.push("toggle")
end
defp toggle(false = _is_visible?) do
JS.add_class("visible", to: "#container")
|> JS.push("toggle")
end
end
The problem is if page crashes (using “Raise” button here) the visible
class persists after LV is re-mounted.
I don’t know if this is a bad usage from me, or it’s a bug. I can report this bug (I do have a POC repo for this) but first I wanted to make sure I’m not doing something wrong.
(I also do have a showcase video but it doesn’t allow video uploads here)