The project is in a healthy, maintained state
A package that distributes Heroicons as a gem, for easy inclusion in Ruby projects.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

HeroiconsHelper

This gem helps you use Heroicons in your Ruby projects. It's inspired by heroicons-ruby and octicons_gem.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add heroicons_helper

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install heroicons_helper

Usage

This gem is super simple! Require the gem, and include the module:

require "heroicons_helper"

include HeroiconsHelper

You'll have a brand new method called heroicon whose signature looks like this:

heroicon(icon, variant, attributes: {})

where

  • icon is the Heroicon name (eg. :bell or `"bell")
  • variant is the type of Heroicons (eg., outline, solid, or mini)
  • attributes are any additional HTML attributes to add on to the resulting svg element

This one method call returns an object that represents the Heroicon, and you should call to_svg to get the resulting SVG string:

outline_icon = heroicon("x-mark", variant: HeroiconsHelper::Icon::VARIANT_OUTLINE)
puts outline_icon.to_svg
=> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"></path></svg>

Cache

This gem also comes with a simple caching system, which can be useful to preload icons. It works like this:

icons_to_preload = [{
    name: "thumb-down",
    variant: "outline",
}, {
    name: "refresh",
    variant: "solid",
},]

HeroiconsHelper::Cache.preload!(icons_to_preload) do |found, icon|
    # An instance of `FakeClass` will be stored in the cache
    FakeClass.new(icon) unless found
end

HeroiconsHelper::Cache.preload! does one of two things:

  • If, given the icons_to_preload array, an item is located in the cache, found is true and icon is the cached item
  • Otherwise, found is false, and icon is the element currently being iterated. Also, the last line of the block sets the cache

The Hash elements within icons_to_preload can also take height and width keys.

Development

To update the Heroicons set:

  1. Run npm install
  2. Run script/update_heroicons