Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Icon): parse icon from file, closes #2 #3

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/mbta_metro/components/icon.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule MbtaMetro.Components.Icon do
@moduledoc false

require Logger

use Phoenix.Component

alias MbtaMetro.Utils
Expand Down Expand Up @@ -34,11 +36,15 @@ defmodule MbtaMetro.Components.Icon do

for {file, name, type} <- icons do
defp icon(unquote(type), unquote(name), opts) do
attrs = opts_to_attrs(opts)

"<svg" <> rest = unquote(file)

Phoenix.HTML.raw(["<svg", attrs, rest])
with {:ok, html} <- Floki.parse_fragment(unquote(file)),
[{"svg", svg_attrs, svg_content}] <- Floki.find(html, "svg") do
{"svg", [opts_to_attrs(opts) | svg_attrs], svg_content}
|> Floki.raw_html()
|> Phoenix.HTML.raw()
else
_ ->
Logger.warning("Could not parse icon #{unquote(name)} #{unquote(type)}")
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule MbtaMetro.MixProject do
{:esbuild, "0.8.1", only: :dev, optional: true, runtime: Mix.env() == :dev},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:faker, "0.18.0", only: :dev, runtime: false},
{:floki, "0.36.3", only: :test, runtime: false},
{:floki, "~> 0.36"},
{:jason, "1.4.4"},
{:heroicons, "0.5.6", optional: true},
{:phoenix, "1.7.14"},
Expand Down