Hey there,
In my application, I have links that need to perform as regular links (live_patch
) but that also need to trigger a JS command (to close a sidebar).
Before the JS event, my link looked like that and worked perfectly:
<%= live_patch name, to: entry_path %>
Then to perform both the patch and JS command, it turned like this
<a href="#" phx-click={ patch_to(entry_path) |> close_sidebar()}>
<%= name %>
</a>
defp close_sidebar(js \\ %JS{}) do
js |> JS.add_class("lsb-hidden", to: "#sidebar")
end
defp patch_to(js \\ %JS{}, path) do
js |> JS.push("patch-to", value: %{path: path}, target: "#sidebar")
end
def handle_event("patch-to", %{"path" => path}, socket) do
{:noreply, push_patch(socket, to: path)}
end
Still working but now my render_click
in tests are no longer working
I kinda made the tests to pass with the following hack.
<a id={absolute_path} href="#" phx-click={ patch_to(entry_path) |> close_sidebar()} data-path={entry_path} phx-hook="FakeHook" phx-target={@myself}>
<%= name %>
</a>
defp render_js_click(element, view) do
[path] = element |> render() |> parse_fragment!() |> attribute("data-path")
render_hook(element, "patch-to", %{path: path})
render(view)
end
Do you have a less hack-ish solution?