I want to render an icon based on some kind of params:
defp render_status_icon(case, classes) do
icon_name = case case.status do
:open -> "hero-inbox-solid text-emerald-500"
:closed -> "hero-lock-closed text-gray-500"
:archived -> "hero-archive-box text-gray-500"
_ -> "hero-question-mark-circle text-blue-500"
end
content_tag(:span, class: "#{icon_name} #{classes}") do
end
end
I call it like this in my .heex file:
<%= render_status_icon(case, "h-4 w-4") %>
It works fine, but content_tag is deprecated.
What would be the correct pattern using core_components? It seems i cannot simply call those functions?
Or do I need to create a new Phoenix.Component just to render this?
Or should I use a sigil and simply render <span> myself?
As a note for anyone else, this actually will throw a warning of using a local variable (icon_name) in the template. Instead one should put this into the assigns as well:
attr :status, :atom, required: true
attr :class, :string, default: ""
def status_icon(%{status: status} = assigns) do
icon_name = case status do
:open -> "hero-inbox-solid text-emerald-500"
:closed -> "hero-lock-closed text-gray-500"
:archived -> "hero-archive-box text-gray-500"
_ -> "hero-question-mark-circle text-blue-500"
end
assigns = assign(assigns, :icon_name, icon_name)
~H"<span class={[@icon_name, @class]} />"
end