Add overrides for fill, viewBox, stroke, etc
This commit is contained in:
@ -13,6 +13,8 @@ defmodule Heroicons do
|
|||||||
end
|
end
|
||||||
|
|
||||||
icon_dir = Module.get_attribute(module, :icon_dir)
|
icon_dir = Module.get_attribute(module, :icon_dir)
|
||||||
|
default_attrs = Module.get_attribute(module, :default_attrs)
|
||||||
|
|
||||||
|
|
||||||
icon_paths =
|
icon_paths =
|
||||||
Path.absname(icon_dir, :code.priv_dir(:heroicons))
|
Path.absname(icon_dir, :code.priv_dir(:heroicons))
|
||||||
@ -20,12 +22,12 @@ defmodule Heroicons do
|
|||||||
|> Path.wildcard()
|
|> Path.wildcard()
|
||||||
|
|
||||||
for path <- icon_paths do
|
for path <- icon_paths do
|
||||||
generate_function(path)
|
generate_function(path, default_attrs)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
def generate_function(path) do
|
def generate_function(path, default_attrs) do
|
||||||
name =
|
name =
|
||||||
Path.basename(path, ".svg")
|
Path.basename(path, ".svg")
|
||||||
|> String.replace("-", "_")
|
|> String.replace("-", "_")
|
||||||
@ -33,8 +35,7 @@ defmodule Heroicons do
|
|||||||
|
|
||||||
icon = File.read!(path)
|
icon = File.read!(path)
|
||||||
{i, _} = :binary.match(icon, ">")
|
{i, _} = :binary.match(icon, ">")
|
||||||
|
{_, body} = String.split_at(icon, i)
|
||||||
{head, tail} = String.split_at(icon, i)
|
|
||||||
|
|
||||||
doc = """
|
doc = """
|
||||||
)}) {: width=24px}
|
)}) {: width=24px}
|
||||||
@ -48,16 +49,17 @@ defmodule Heroicons do
|
|||||||
@doc unquote(doc)
|
@doc unquote(doc)
|
||||||
@spec unquote(name)(keyword(binary)) :: binary
|
@spec unquote(name)(keyword(binary)) :: binary
|
||||||
def unquote(name)(opts \\ []) do
|
def unquote(name)(opts \\ []) do
|
||||||
|
opts = Keyword.merge(unquote(default_attrs), opts)
|
||||||
attrs =
|
attrs =
|
||||||
for {k, v} <- opts do
|
for {k, v} <- opts do
|
||||||
safe_k =
|
safe_k =
|
||||||
k |> Atom.to_string() |> String.replace("_", "-") |> Phoenix.HTML.Safe.to_iodata()
|
k |> Atom.to_string() |> String.replace("_", "-") |> Phoenix.HTML.Safe.to_iodata()
|
||||||
safe_v = v |> Phoenix.HTML.Safe.to_iodata()
|
safe_v = v |> Phoenix.HTML.Safe.to_iodata()
|
||||||
|
|
||||||
{:safe, [safe_k, ?=, ?", safe_v, ?"]}
|
{:safe, [?\s, safe_k, ?=, ?", safe_v, ?"]}
|
||||||
end
|
end
|
||||||
|
|
||||||
{:safe, [unquote(head), Phoenix.HTML.Safe.to_iodata(attrs), unquote(tail)]}
|
{:safe, ["<svg", Phoenix.HTML.Safe.to_iodata(attrs), unquote(body)]}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,5 +5,6 @@ defmodule Heroicons.Outline do
|
|||||||
For primary navigation and marketing sections, designed to be rendered at 24x24.
|
For primary navigation and marketing sections, designed to be rendered at 24x24.
|
||||||
"""
|
"""
|
||||||
@icon_dir "outline/"
|
@icon_dir "outline/"
|
||||||
|
@default_attrs [xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor"]
|
||||||
@before_compile Heroicons
|
@before_compile Heroicons
|
||||||
end
|
end
|
||||||
|
@ -6,5 +6,6 @@ defmodule Heroicons.Solid do
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@icon_dir "solid/"
|
@icon_dir "solid/"
|
||||||
|
@default_attrs [xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 20 20", fill: "currentColor"]
|
||||||
@before_compile Heroicons
|
@before_compile Heroicons
|
||||||
end
|
end
|
||||||
|
@ -26,6 +26,14 @@ defmodule HeroiconsTest do
|
|||||||
assert Heroicons.Outline.academic_cap(multiword_key: "foo")
|
assert Heroicons.Outline.academic_cap(multiword_key: "foo")
|
||||||
|> Phoenix.HTML.safe_to_string() =~
|
|> Phoenix.HTML.safe_to_string() =~
|
||||||
~s(multiword-key="foo")
|
~s(multiword-key="foo")
|
||||||
|
|
||||||
|
assert Heroicons.Outline.academic_cap(viewBox: "0 0 12 12")
|
||||||
|
|> Phoenix.HTML.safe_to_string() =~
|
||||||
|
~s(viewBox=\"0 0 12 12\")
|
||||||
|
|
||||||
|
refute Heroicons.Outline.academic_cap(viewBox: "0 0 12 12")
|
||||||
|
|> Phoenix.HTML.safe_to_string() =~
|
||||||
|
~s(viewBox=\"0 0 24 24\")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "generated docs" do
|
test "generated docs" do
|
||||||
|
Reference in New Issue
Block a user