Skip to content

Commit

Permalink
Merge pull request #345 from petalframework/heroicons
Browse files Browse the repository at this point in the history
Generate list of heroicons if the dependency exists
  • Loading branch information
mitkins authored Sep 17, 2024
2 parents 6770295 + c66c036 commit c911a13
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
98 changes: 59 additions & 39 deletions lib/petal_components/icon.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,76 @@ defmodule PetalComponents.Icon do

require Logger

attr :rest, :global,
doc: "the arbitrary HTML attributes for the svg container",
include: ~w(role aria-hidden)

attr :name, :any, required: true
attr :class, :any, default: nil, doc: "svg class"

defp heroicon(%{name: "hero-" <> _} = assigns) do
~H"""
<span class={[@name, @class]} {@rest} />
"""
end

@icons [
{"", "/24/outline"},
{"-solid", "/24/solid"},
{"-mini", "/20/solid"},
{"-micro", "/16/solid"}
]

deps_paths = Mix.Project.deps_paths()
heroicons_path = deps_paths[:heroicons]

heroicon_names =
for {suffix, dir} <- @icons do
path =
Path.join(heroicons_path, dir)
|> Path.expand()
# Heroicons aren't available when pushing to hex
if !heroicons_path do
attr :rest, :global,
doc: "the arbitrary HTML attributes for the heroicon container",
include: ~w(role aria-hidden)

# Read folder
case File.ls(path) do
{:ok, file_names} ->
for file_name <- file_names, do: Path.rootname(file_name) <> suffix
attr :name, :any, required: true
attr :class, :any, default: nil, doc: "class applied to heroicon container"

{:error, _reason} ->
[]
end
def icon(%{name: "hero-" <> _} = assigns) do
~H"""
<span class={[@name, @class]} {@rest} />
"""
end
|> Enum.flat_map(fn x -> x end)
end

quote do
# If heroicons are included as a dependency (either in :dev or in the
# calling application), then generate a function per heroicon variant
if heroicons_path do
attr :rest, :global,
doc: "the arbitrary HTML attributes for the svg container",
doc: "the arbitrary HTML attributes for the heroicon container",
include: ~w(role aria-hidden)

attr :name, :any, required: true
attr :class, :any, default: nil, doc: "svg class"
end
attr :class, :any, default: nil, doc: "class applied to heroicon container"

for heroicon_name <- heroicon_names do
def icon(%{name: "hero-" <> unquote(heroicon_name)} = assigns), do: heroicon(assigns)
defp heroicon(%{name: "hero-" <> _} = assigns) do
~H"""
<span class={[@name, @class]} {@rest} />
"""
end

@icons [
{"", "/24/outline"},
{"-solid", "/24/solid"},
{"-mini", "/20/solid"},
{"-micro", "/16/solid"}
]

heroicon_names =
for {suffix, dir} <- @icons do
path =
Path.join(heroicons_path, dir)
|> Path.expand()

# Read folder
case File.ls(path) do
{:ok, file_names} ->
for file_name <- file_names, do: Path.rootname(file_name) <> suffix

{:error, _reason} ->
[]
end
end
|> Enum.flat_map(fn x -> x end)

quote do
attr :rest, :global,
doc: "the arbitrary HTML attributes for the svg container",
include: ~w(role aria-hidden)

attr :name, :any, required: true
attr :class, :any, default: nil, doc: "svg class"
end

for heroicon_name <- heroicon_names do
def icon(%{name: "hero-" <> unquote(heroicon_name)} = assigns), do: heroicon(assigns)
end
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ defmodule PetalComponents.MixProject do
tag: "v2.1.5",
app: false,
compile: false,
sparse: "optimized"}
sparse: "optimized",
only: [:dev, :test]}
]
end

Expand Down

0 comments on commit c911a13

Please sign in to comment.