phoenix_active_link v0.3.2 PhoenixActiveLink View Source
PhoenixActiveLink provides helpers to add active links in views.
Configuration
Default options can be customized in the configuration:
use Mix.Config
config :phoenix_active_link, :defaults,
wrap_tag: :li,
class_active: "enabled",
class_inactive: "disabled"
Integrate in Phoenix
The simplest way to add the helpers to Phoenix is to import PhoenixActiveLink
either in your web.ex
under views to have it available under every views,
or under for example App.LayoutView
to have it available in your layout.
Link to this section Summary
Functions
active_link/3
is a wrapper around Phoenix.HTML.Link.link/2
.
active_path?/2
is a helper to determine if the element should be in active state or not.
Link to this section Functions
active_link/3
is a wrapper around Phoenix.HTML.Link.link/2
.
It generates a link and adds an active
class depending on the
desired state. It can be customized using the following options.
Options
:active
- Seeactive_path?/2
documentation for more information:wrap_tag
- Wraps the link in another tag which will also have the same active class. This options is useful for usage withli
in bootstrap for example.:class_active
- The class to add when the link is active. Defaults to"active"
:class_inactive
- The class to add when the link is not active. Empty by default.:active_disable
- Uses aspan
element instead of an anchor when not active.
Examples
<%= active_link(@conn, "Link text", to: "/my/path") %>
<%= active_link(@conn, "Link text", to: "/my/path", wrap_tag: :li) %>
<%= active_link(@conn, "Link text", to: "/my/path", active: :exact) %>
active_path?/2
is a helper to determine if the element should be in active state or not.
The :opts
should contain the :to
option and the active detection can be customized
using by passing :active
one of the following values.
true
- Will always returntrue
false
- Will always returnfalse
:inclusive
- Will returntrue
if the current path starts with the link path.For example,
active_path?(conn, to: "/foo")
will returntrue
if the path is"/foo"
or"/foobar"
.:exclusive
- Will returntrue
if the current path and the link path are the same, but will ignore the trailing slashesFor example,
active_path?(conn, "/foo")
will returntrue
when the path is"/foo/"
:exact
- Will returntrue
if the current path and the link path are exactly the same, including trailing slashes.a
%Regex{}
- Will returntrue
if the current path matches the regex.Beware that
active?(conn, active: ~r/foo/)
will returntrue
if the path is"/bar/foo"
, so you must useactive?(conn, active: ~r/^foo/)
if you want to match the beginning of the path.a
{controller, action}
list - A list of tuples with a controller module and an action symbol.Both can be the
:any
symbol to match any controller or action.a
{live_view, action}
list - A list of tuples with a live view module and an action symbol.Both can be the
:any
symbol to match any live view module or action.:exact_with_params
- Will returntrue
if the current path and the link path are exactly the same, including trailing slashes and query string as is.:inclusive_with_params
- Will returntrue
if the current path is equal to the link path and the query params of the current path are included to the link path. For example,active_path?(conn, to: "/foo?bar=2")
will returntrue
if the path is"/foo?bar=2"
or"/foo?baz=2&bar=2"
. For example,active_path?(conn, to: "/foo?bar=2")
will returnfalse
if the path is"/foobaz?bar=2"
.
Examples
active_path?(conn, to: "/foo")
active_path?(conn, to: "/foo", active: false)
active_path?(conn, to: "/foo", active: :exclusive)
active_path?(conn, to: "/foo", active: ~r(^/foo/[0-9]+))
active_path?(conn, to: "/foo", active: [{MyController, :index}, {OtherController, :any}])
active_path?(conn, to: "/foo", active: [{MyLive, :index}, {OtherLive, :any}])
active_path?(conn, to: "/foo?baz=2", active: :inclusive_with_params)