Conn.request_path doesn't change in header

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.

  1. Have a live footer and header with LiveView
  2. Liveview Navbar, component in layout receives socket? - #4 by darkmarmot
  3. Both layouts for liveview and normal view renders when using live_render on root.html.leex - #2 by Matsa59
  4. and more

finally, i worked this out.

  1. put render PragmaticWeb.SharedView, "header.html", assigns in live.html.leex not root.html.leex

  2. 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" %>

  3. it’s better to use live component

i uploaded a demo to github