Add overrides for fill, viewBox, stroke, etc

This commit is contained in:
Max Veytsman
2021-04-19 15:21:09 -04:00
parent d104591f00
commit 4dc98ef82e
4 changed files with 18 additions and 6 deletions

View File

@ -13,6 +13,8 @@ defmodule Heroicons do
end
icon_dir = Module.get_attribute(module, :icon_dir)
default_attrs = Module.get_attribute(module, :default_attrs)
icon_paths =
Path.absname(icon_dir, :code.priv_dir(:heroicons))
@ -20,12 +22,12 @@ defmodule Heroicons do
|> Path.wildcard()
for path <- icon_paths do
generate_function(path)
generate_function(path, default_attrs)
end
end
@doc false
def generate_function(path) do
def generate_function(path, default_attrs) do
name =
Path.basename(path, ".svg")
|> String.replace("-", "_")
@ -33,8 +35,7 @@ defmodule Heroicons do
icon = File.read!(path)
{i, _} = :binary.match(icon, ">")
{head, tail} = String.split_at(icon, i)
{_, body} = String.split_at(icon, i)
doc = """
![](assets/#{Path.relative_to(path, :code.priv_dir(:heroicons))}) {: width=24px}
@ -48,16 +49,17 @@ defmodule Heroicons do
@doc unquote(doc)
@spec unquote(name)(keyword(binary)) :: binary
def unquote(name)(opts \\ []) do
opts = Keyword.merge(unquote(default_attrs), opts)
attrs =
for {k, v} <- opts do
safe_k =
k |> Atom.to_string() |> String.replace("_", "-") |> 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
{:safe, [unquote(head), Phoenix.HTML.Safe.to_iodata(attrs), unquote(tail)]}
{:safe, ["<svg", Phoenix.HTML.Safe.to_iodata(attrs), unquote(body)]}
end
end
end

View File

@ -5,5 +5,6 @@ defmodule Heroicons.Outline do
For primary navigation and marketing sections, designed to be rendered at 24x24.
"""
@icon_dir "outline/"
@default_attrs [xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor"]
@before_compile Heroicons
end

View File

@ -6,5 +6,6 @@ defmodule Heroicons.Solid do
"""
@icon_dir "solid/"
@default_attrs [xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 20 20", fill: "currentColor"]
@before_compile Heroicons
end

View File

@ -26,6 +26,14 @@ defmodule HeroiconsTest do
assert Heroicons.Outline.academic_cap(multiword_key: "foo")
|> Phoenix.HTML.safe_to_string() =~
~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
test "generated docs" do