shift73k/lib/shift73k_web/views/icon_helpers.ex

39 lines
1 KiB
Elixir
Raw Normal View History

2021-03-05 19:23:32 -05:00
defmodule Shift73kWeb.IconHelpers do
2021-02-25 17:53:44 -05:00
@moduledoc """
Generate SVG sprite use tags for SVG icons
"""
use Phoenix.HTML
2021-03-05 19:23:32 -05:00
alias Shift73kWeb.Router.Helpers, as: Routes
2021-02-25 17:53:44 -05:00
def icon_div(conn, name, div_opts \\ [], svg_opts \\ []) do
content_tag(:div, tag_opts(name, div_opts)) do
icon_svg(conn, name, svg_opts)
end
end
def icon_svg(conn, name, opts \\ []) do
2021-03-03 08:15:14 -05:00
opts = aria_hidden?(opts)
2021-02-25 17:53:44 -05:00
content_tag(:svg, tag_opts(name, opts)) do
~E"""
<%= if title = Keyword.get(opts, :aria_label), do: content_tag(:title, title) %>
<%= tag(:use, "xlink:href": Routes.static_path(conn, "/images/icons.svg##{name}")) %>
"""
2021-02-25 17:53:44 -05:00
end
end
defp tag_opts(name, opts) do
Keyword.update(opts, :class, name, fn c -> "#{c} #{name}" end)
2021-02-25 17:53:44 -05:00
end
2021-03-03 08:15:14 -05:00
defp aria_hidden?(opts) do
case Keyword.get(opts, :aria_hidden) do
"false" -> Keyword.drop(opts, [:aria_hidden])
false -> Keyword.drop(opts, [:aria_hidden])
"true" -> opts
_ -> Keyword.put(opts, :aria_hidden, "true")
end
end
2021-02-25 17:53:44 -05:00
end