dogweather

dogweather

First, the Hero icon infrastructure in Phoenix is fantastic — thank you for that.

I really like, though, getting as much help from my IDE as possible. And I try to minimize flipping between different screens for docs and translating in my head the third party naming conventions and how my app’s JS / CSS config names things.

So here, I’m brainstorming about ways to check my <.icon> uses at compile-time. There are 1,176 Hero icons, with all four variations.

Currently, I’m leaning towards generating a function for each so that I’d have nice function-name completion while typing. So:

Built-in

<.icon name="hero-plus-mini" />

Idea 1

<.icon_plus_mini />

There’d be 1,176 functions like this.

Idea 2

<.icon_plus kind = :mini />

There’d need to be only 294 functions.
Each would have an optional parameter with a locked-down @type kind :: :mini | :micro | :outline | :solid.

(I haven’t tested this syntax. And I’m thinking dialyzer would be needed to validate the kind argument.)

Idea 3

<.icon name = :plus, kind = :mini />

Have just 1 function that’d accept atoms for args, which would be used in structs, and therefore checked at compile-time.

I think the downside, though, is that the LSP / IDE wouldn’t be able to enumerate all the valid atom inputs in the same way it can enumerate valid function names. So while this would check at compile time, it wouldn’t be as convenient.


Has anyone thought about this?

Here’s an implementation of my first idea:

Summary
defmodule IndexFlowWeb.CoreComponents.IconGenerator do
  @moduledoc """
  Generates individual icon functions for each hero icon at compile time.
  """

  defmacro generate_icon_functions do
    icon_names = load_hero_icon_names()

    functions = for icon_name <- icon_names do
      # Convert "hero-plus-solid" to "icon_plus_solid"
      function_name =
        icon_name
        |> String.replace_prefix("hero-", "icon_")
        |> String.replace("-", "_")
        |> String.to_atom()

            quote do
        @doc """
        Renders the #{unquote(icon_name)} icon.

        ## Examples

            <.#{unquote(function_name)} />
            <.#{unquote(function_name)} class="h-4 w-4" />
        """
        def unquote(function_name)(assigns) do
          assigns = Map.put(assigns, :name, unquote(icon_name))
          icon(assigns)
        end
      end
    end

    {:__block__, [], functions}
  end

    defp load_hero_icon_names do
    # Use the deps directory path directly instead of Application.app_dir
    icons_dir = "deps/heroicons/optimized"

    # Define the mapping of directories to suffixes
    variants = [
      {"24/outline", ""},
      {"24/solid", "-solid"},
      {"20/solid", "-mini"},
      {"16/solid", "-micro"}
    ]

    # Use for comprehension to collect all icons
    for {dir, suffix} <- variants,
        full_path = Path.join(icons_dir, dir),
        File.exists?(full_path),
        file <- File.ls!(full_path),
        String.ends_with?(file, ".svg") do
      base_name = Path.basename(file, ".svg")
      "hero-#{base_name}#{suffix}"
    end
    |> Enum.uniq()
    |> Enum.sort()
  end
end

Showing Posts 1 to 3

cmo

cmo

You limit yourself to just heroicons in a few of those methods. I would add a brand or whatever attribute for future expansion.

I used to generate remix icons functions for the ones I used but migrated to the built-in method. Now I can use either set with the same component and syntax.

ouven

ouven

Hi, we use a semantic mapping in our icon component, like:

%{
faq: {:hero, „help“}
}

for instance.

At compile time we loop over our semantic names and build two functions for each. An icon component and a name function:

def icon_faq(assigns), do: …
def iconname_faq(), do: …

We still have a generic icon component to use inside other components with an icon attribute.

Now we use either the specific icon component or the name function for attributes.

Advantages are:

  • we can have different backends for different icons (hero and feather)
  • we have compile time checking
  • we have IDE support
  • we deliver only the icons we do actually use
  • semantic names are easy to remember

I hope you like some of the ideas.

rhcarvalho

rhcarvalho

If all you want is auto-complete, the Tailwind IntelliSense VSCode extension happily auto-completes the hero- classes for me.

All I had to do was update the config to tell it to auto-complete in an attribute named name (the default is class and className).

If you’re using another editor/IDE, a similar config might be available.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews