defmodule BitstylesPhoenix.Component.UseSVG do
use BitstylesPhoenix.Component
@moduledoc """
Components for rendering SVGs.
"""
@doc ~S"""
Renders an SVG tag with a `use` reference.
"""
story(
"A referenced SVG (inlined on the page)",
'''
iex> assigns = %{}
...> render ~H"""
...> <.ui_svg use="arrow"/>
...> """
"""
"""
''',
extra_html: """
"""
)
story(
"A referenced SVG (external file)",
'''
iex> assigns = %{}
...> render ~H"""
...> <.ui_svg use="logo" file="assets/logo.svg" viewbox="0 0 400 280"/>
...> """
"""
"""
''',
background: "white"
)
story(
"A referenced SVG (external file with symbols)",
'''
iex> assigns = %{}
...> render ~H"""
...> <.ui_svg file="assets/icons.svg" use="icon-bin"/>
...> """
"""
"""
''',
background: "white"
)
def ui_svg(assigns) do
extra =
assigns
|> assigns_to_attributes([:file, :use])
|> Keyword.put_new(:xmlns, "http://www.w3.org/2000/svg")
assigns = assign(assigns, href: href(assigns), extra: extra)
~H"""
"""
end
defp href(assigns), do: "#{assigns[:file]}##{assigns[:use]}"
end