thre are many related issues if you search navbar. it seems adding a active class is not so easy in phoenix liveview like in rails.
eg.
- Have a live footer and header with LiveView
- Liveview Navbar, component in layout receives socket? - #4 by darkmarmot
- Both layouts for liveview and normal view renders when using live_render on root.html.leex - #2 by Matsa59
- and more
finally, i worked this out.
-
put
render PragmaticWeb.SharedView, "header.html", assigns
inlive.html.leex
notroot.html.leex
-
use @socket.view instead of @conn.request_path to add
active
css class
<%= live_redirect "Home", to: "/", class: if Kernel.inspect(@socket.view) == "DemoWeb.HomeLive", do: "active", else: "1" %>
-
it’s better to use live component
i uploaded a demo to github