Rails Icons

repository·main·Indexed 43 days ago

https://github.com/rails-designer/rails_icons

A Rails gem providing a unified interface for using icon libraries like Heroicons, Lucide, and Feather. It supports inline SVG rendering via the `icon` helper, high-performance SVG sprites via `sprite_icon`, and base64-encoded URIs via `encoded_icon`. The gem includes generators for installation, configuration, and syncing libraries from Git or local directories, as well as a built-in icon preview engine.

Tokens
4.5K
Snippets
20
Records
23
Agent score
55%

What's inside Rails Icons

  1. Use SVG Sprites for performance

    main

    For improved performance, use sprite_icon instead of icon. This method references shared <symbol> definitions via <use href="..."> rather than inlining the full SVG code.

    External Sprite (Default)

    By default, Rails Icons serves a sprite at /rails_icons/sprite.svg. This is reachable even if the preview engine is behind authentication.

    <%= sprite_icon "check" %>
    # Renders: <svg><use href="/rails_icons/sprite.svg#heroicons_outline_check"></use></svg>

    You can override the location per icon:

    <%= sprite_icon "check", sprite_location: "/assets/sprites.svg" %>

    Inline Sprite

    To embed the sprite directly in your layout (reducing HTTP requests but increasing HTML size), set config.default_sprite_location = nil in your initializer and use the icons_sprite helper in your layout.

    <body>
      <%= icons_sprite %>
      <%= sprite_icon "check" %>
    </body>

    You can also generate a sprite for a specific subset of icons:

    <%= icons_sprite ["check", "search"], library: "heroicons", variant: "outline" %>
    # External sprite usage
    <%= sprite_icon "check" %>
    
    # Inline sprite usage
    <%= icons_sprite %>
    <%= sprite_icon "check" %>
  2. Add custom icon libraries

    main

    You can use any icon library by placing SVG files in your application's asset directory. Rails Icons looks for files at <icons_path>/<library_name>/<name>.svg.

    Local Custom Library

    1. Create the directory: app/assets/svg/icons/<library_name>/.
    2. Drop your .svg files there.
    3. Use them: icon "name", library: :library_name.

    Alternatively, use the generator to scaffold the directory and configuration:

    rails generate rails_icons:install --library=my_custom_lib

    Git-sourced Custom Library

    To sync icons from a remote Git repository, define a custom_library in the initializer:

    RailsIcons.configure do |config|
      config.custom_library :my_icons, source: {
        url: "https://github.com/user/icons.git",
        variants: { default: "." }
      }
    end

    After configuring, sync the icons using:

    rails generate rails_icons:sync --library=my_icons
    # Usage for a custom library
    icon "apple", library: :simple_icons
  3. Install Rails Icons

    main

    To use Rails Icons in your Rails application, add the gem to your Gemfile and run the installation generator. You can specify one or more supported libraries and specific variants during installation.

    Note: The generator also mounts an icon preview at /rails_icons for browsing and searching. This route is open by default; ensure you restrict it in production environments.

    # Add the gem
    bundle add rails_icons
    
    # Install a single library
    rails generate rails_icons:install --library=heroicons
    
    # Install multiple libraries at once
    rails generate rails_icons:install --libraries=heroicons lucide
    
    # Install specific variants
    rails generate rails_icons:install --library=heroicons --variants solid outline
  4. Configure Rails Icons

    main

    Configure default settings and sprite behavior in config/initializers/rails_icons.rb using the RailsIcons.configure block.

    Key configuration options:

    • default_library: The library used when no library is specified.
    • default_variant: The variant used when no variant is specified.
    • default_sprite_location: The URL for the sprite. Set to nil for inline mode.
    • validate_sprite_icons: Boolean to validate that referenced icons exist on disk.
    • sprite: A hash defining which icons to include in the sprite, organized by library and variant.
    # config/initializers/rails_icons.rb
    RailsIcons.configure do |config|
      config.default_library = "heroicons"
      config.default_variant = "outline"
    
      # Set to nil to use inline mode
      config.default_sprite_location = "/rails_icons/sprite.svg"
    
      config.validate_sprite_icons = false
    
      config.sprite = {
        heroicons: {
          outline: %w[check chevron-down menu search x],
          mini: %w[check chevron-down]
        }
      }
    end
  5. Generate the Rails Icons initializer

    main

    Use the Rails generator to create the config/initializers/rails_icons.rb configuration file. This file is used to define your default icon library, variants, custom icon paths, and library registrations.

    Available CLI options:

    • --library: Choose a single library to include.
    • --libraries: Choose multiple libraries (array).
    • --destination: Specify a custom icons folder path (defaults to the current RailsIcons.configuration.icons_path).
    rails generate rails_icons:initializer --library heroicons --libraries lucide --destination app/assets/icons
  6. Enable Rails Icons autocompletion in Ruby LSP

    main

    The rails_icons project includes a Ruby LSP addon that provides autocompletion for the icon method. When using Ruby LSP, typing icon " will trigger a list of available icon names based on the SVG files found in your project's icon directories.

    By default, the addon indexes icons from:

    1. The path configured via RailsIcons.configuration.icons_path (defaults to app/assets/svg/icons).
    2. A sub-directory defined by RailsIcons.configuration.default_library within that path.

    To ensure autocompletion works correctly, make sure your icons are stored in the expected directory or explicitly configure the path in your Rails initializer.

    # Example of how the autocompletion is triggered in your code
    icon "home"
  7. Use the `icon` helper

    main

    The icon helper renders an inline SVG icon. It uses the default library and variant defined in config/initializers/rails_icons.rb unless otherwise specified.

    Supported options:

    • library: The name of the icon library (e.g., heroicons, lucide).
    • variant: The specific style of the icon (e.g., solid, outline, mini).
    • class: CSS classes for styling (supports strings or arrays for conditional classes).
    • data: HTML data attributes.
    • stroke_width: Sets the SVG stroke width.
    # Use defaults
    icon "check"
    
    # Specify variant
    icon "check", variant: "solid"
    
    # Specify library
    icon "check", library: "heroicons"
    
    # Add CSS classes
    icon "check", class: "text-green-500"
    
    # Add conditional CSS classes
    icon "check", class: ["size-4", "bg-red-500": !verified?, "bg-green-500": verified?]
    
    # Add data attributes
    icon "check", data: { controller: "swap" }
    
    # Set stroke width
    icon "check", stroke_width: 2
  8. Configure Rails Icons in the initializer

    main

    The config/initializers/rails_icons.rb file allows you to manage how icons are loaded and accessed. Key configuration options include:

    • config.default_library: Sets the fallback library if none is specified.
    • config.default_variant: Sets a default variant (e.g., 'outline' or 'solid') for all libraries.
    • config.icons_path: Defines the directory where icon files are stored.
    • config.custom_library :name: Registers a custom library folder.

    Example configuration structure:

    RailsIcons.configure do |config|
      config.default_library = "heroicons"
      # config.default_variant = ""
      # Default icons path
      config.icons_path = "app/assets/icons"
    
      # First-party library configurations are injected here
    
      config.custom_library :my_custom_icons
    end
  9. Configure Rails Icons via Icons.configure

    main

    You can configure the RailsIcons gem within a Rails initializer (e.g., config/initializers/rails_icons.rb). The configuration block allows you to set the base path for icon assets and manage sprite settings.

    Available configuration keys:

    • base_path: The root directory used to resolve icon paths (defaults to Rails.root).
    • sprite: A hash for sprite-specific configuration.
    • default_sprite_location: The URL path where the generated sprite SVG will be served (defaults to "/rails_icons/sprite.svg").
    • validate_sprite_icons: A boolean determining whether to validate icons during sprite generation (defaults to false).
    RailsIcons::Icons.configure do |config|
      config.base_path = Rails.root
      config.default_sprite_location = "/custom/path/sprite.svg"
      config.validate_sprite_icons = true
      config.sprite = {
        # sprite specific settings
      }
    end