Created
May 7, 2024 21:08
-
-
Save Valian/e1533f83c6e22334e7cd70b3e8a948be to your computer and use it in GitHub Desktop.
Set data-active attributes on LiveView navigation in Phoenix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()} | |
); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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