-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathicon.ex
190 lines (151 loc) · 5.14 KB
/
icon.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
if Code.ensure_loaded?(Surface) do
defmodule Iconify.Icon do
@moduledoc """
A `Surface` component for rendering icons using various methods.
## Specifying what icon to use
- `iconify` or `icon`: Any icon from Iconify (https://icones.js.org)
- `solid`: Shorthand for Heroicons solid icons
- `outline`: Shorthand for Heroicons outline icons
## Extra Properties
- `svg`: Optionally pass SVG markup directly
- `mode`: Sets what rendering mode to use (see `Iconify` docs)
- `class`: Any CSS classes to apply to the icon
## Examples
iex> alias Iconify.Icon
iex> ~F"<#Icon iconify=\"heroicons-solid:user\" class=\"w-6 h-6\" />"
# Returns rendered icon HTML
iex> ~F"<#Icon solid=\"user\" class=\"w-6 h-6\" />"
iex> ~F"<#Icon svg=\"<svg>...</svg>\" class=\"w-6 h-6\" />"
"""
use Surface.MacroComponent
# any icon from iconify: https://icones.js.org
prop(iconify, :string, required: false, static: true)
prop(icon, :string, required: false, static: true)
# shorthand for heroicons solid icons
prop(solid, :string, required: false, static: true)
# shorthand for heroicons outline icons
prop(outline, :string, required: false, static: true)
# pass SVG markup directly
prop(svg, :string, default: nil, required: false, static: true)
prop(mode, :atom, required: false, static: true)
prop(class, :css_class, default: nil)
@doc """
"""
def expand(attributes, _content, meta) do
static_props =
Surface.MacroComponent.eval_static_props!(__MODULE__, attributes, meta.caller)
svg = svg_icon(static_props)
icon = prepare_icon_name(static_props)
class =
Surface.AST.find_attribute_value(attributes, :class) ||
Application.get_env(:iconify_ex, :default_class, "w-4 h-4")
# TODO? simply include the phoenix component like so instead of duplicating logic, see https://github.com/surface-ui/surface/pull/685#issuecomment-1505978390
# quote_surface caller: meta.caller do
# ~F"""
# <Iconify.iconify class={^class} icon={^icon} />
# """
# end
if is_nil(svg) do
case Iconify.prepare(icon, static_props[:mode]) do
{:css, _fun, %{icon_name: icon_name}} ->
# icon_class = "#{Iconify.css_class()} #{icon_css_name} #{class_to_string(class)}"
quote_surface do
~F"""
<div iconify={^icon_name} class={^class} aria-hidden="true" />
"""
end
{:img, _fun, %{src: src}} ->
quote_surface do
~F"""
<img
src={^src}
class={^class}
onload={if Iconify.using_svg_inject?(), do: "SVGInject(this)"}
aria-hidden="true"
/>
"""
end
{:inline, fun, _assigns} ->
quote_surface do
~F"""
<{^fun} class={^class} />
"""
end
{:set, _fun, %{href: href}} ->
quote_surface do
~F"""
<svg class={^class} aria-hidden="true"><use href={^href} class={^class} /></svg>
"""
end
end
else
quote_surface do
~F"""
<div class={^class} aria-hidden="true">{^svg}</div>
"""
end
end
end
defmacro icon_name(icon) do
name = prepare_icon_name(icon)
Iconify.prepare(name, :css)
|> IO.inspect(label: "prepared icon")
name
end
defp svg_icon(%{svg: svg}) when is_binary(svg) do
svg
end
defp svg_icon(%{iconify: "<svg" <> _ = svg}) do
svg
end
defp svg_icon(_) do
nil
end
defp prepare_icon_name(%{iconify: icon})
when is_binary(icon) or (is_atom(icon) and not is_nil(icon)) do
icon
end
defp prepare_icon_name(%{icon: icon})
when is_binary(icon) or (is_atom(icon) and not is_nil(icon)) do
icon
end
defp prepare_icon_name(%{solid: icon})
when is_binary(icon) or (is_atom(icon) and not is_nil(icon)) do
"heroicons-solid:#{icon}"
end
defp prepare_icon_name(%{outline: icon})
when is_binary(icon) or (is_atom(icon) and not is_nil(icon)) do
"heroicons-outline:#{icon}"
end
defp prepare_icon_name(icon)
when is_binary(icon) or (is_atom(icon) and not is_nil(icon)) do
icon
end
defp prepare_icon_name(assigns) do
e = "iconify: icon name not found in assigns"
IO.inspect(assigns, label: e)
# raise e
""
end
def class_to_string(class) when is_binary(class) do
class
end
def class_to_string(%{original: class}) do
class
# |> class_to_string()
end
def class_to_string(class) when is_list(class) do
if Keyword.keyword?(class) do
IO.inspect(class, label: "kccccc")
Surface.css_class(class)
else
IO.inspect(class, label: "lccccc")
Enum.join(class, " ")
end
end
def class_to_string(class) do
IO.inspect(class, label: "occccc")
Surface.css_class(class)
end
end
end