I have a navigation bar in my web app. I need to update the active route link with css classes to show that it’s the active route.
My current approach is as follows:
In my NavigationView
I have a helper function like this:
defmodule MyAppWeb.NavigationView do
use MyAppWeb, :view
def get_link_classes(conn, link) do
classes = ["common-link-class"]
classes =
if get_current_path(conn) == link do
["active-link-class" | classes]
else
classes
end
Enum.join(classes, " ")
end
defp get_current_path(conn) do
conn
|> Map.get(:request_path)
|> String.split(~r/\//)
|> Enum.at(1)
end
end
The in my markup I use the helper function as follows:
<ul>
<li>
<%= link "Home", to: Routes.page_path(@conn, :index), class: get_link_classes(@conn, "") %>
</li>
<li>
<%= link "Blog", to: Routes.blog_path(@conn, :index), class: get_link_classes(@conn, "blog")%>
</li>
<li>
<%= link "About Me", to: Routes.blog_path(@conn, :index), class: get_link_classes(@conn, "about") %>
</li>
</ul>
Is there a better approach to get the active link?