Skip to content

Instantly share code, notes, and snippets.

@Valian
Created May 7, 2024 21:08
Show Gist options
  • Save Valian/e1533f83c6e22334e7cd70b3e8a948be to your computer and use it in GitHub Desktop.
Save Valian/e1533f83c6e22334e7cd70b3e8a948be to your computer and use it in GitHub Desktop.
Set data-active attributes on LiveView navigation in Phoenix
window.addEventListener("phx:page-loading-start", (_info) => {
Array.from(document.getElementsByTagName("a")).forEach(
(a) => (a.dataset.active = "false")
);
});
window.addEventListener("phx:page-loading-stop", (_info) => {
Array.from(document.getElementsByTagName("a")).forEach(
(a) => {a.dataset.active = (a.href === document.location.href).toString()}
);
});
attr :icon, :string, required: true
attr :text, :string, required: true
attr :link, :string, required: true
def menu_link(assigns) do
~H"""
<.link navigate={@link} class="data-[active=true]:bg-black/20">
<.icon name={@icon} class="mr-2 flex-none" /> <%= @text %>
</.link>
"""
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment