将任何图标库添加到 Rails 应用中。支持 Lucide、Phosphor、Heroicons 等
Add any icon library to a Rails app. Rails Icons has first-party support for a handful of libraries. It is library agnostic so it can be used with any icon library using the same interface. Rails Icons is a Rails gem for the Icons Ruby gem.
# Using the default icon library
icon "check", class: "text-gray-500"
# Using any custom library
icon "apple", library: "simple_icons", class: "text-black"The icons are sourced directly from their respective GitHub repositories via the Icons gem, ensuring Rails Icons remain lightweight.
Sponsored By Rails Designer
Add the gem:
bundle add rails_iconsInstall, choosing one of the supported libraries:
rails generate rails_icons:install --library=LIBRARY_NAMEExample
rails generate rails_icons:install --library=heroicons
# Or multiple at once
rails generate rails_icons:install --libraries=heroicons lucide
# Or only specific variants
rails generate rails_icons:install --library=heroicons --variants solid outlineThe generator also mounts an icon preview at /rails_icons where you can browse and search all your available icons. This route is open by default, so restrict it in production if needed.
…Rails Icons supports SVG sprites for improved performance. Instead of inlining each icon's full SVG, sprite icons reference a shared set of definitions via.
…Rails Icons serves the sprite at /rails_icons/sprite.svg out of the box — no controller, route or MIME type setup needed. The endpoint sits at the host app level, so it stays reachable even when the preview engine is mounted behind authentication.
%>Point at a precompiled file or a CDN by changing the location:
config.default_sprite_location = "https://cdn.example.com/sprite_icons.svg"Override per icon:
Set the location to nil and embed the sprite directly in your layout:
config.default_sprite_location = nil
You can also generate a sprite for a specific set of icons:
sprite_icon accepts the same options as icon:
sprite_icon "check"
sprite_icon "check", library: "heroicons", variant: "mini"
sprite_icon "check", class: "size-6", data: { controller: "swap" }, stroke_width: 2
sprite_icon "check", sprite_location: "/sprite.svg"icons_sprite generates the inline containing definitions:
icons_sprite # all configured icons
icons_sprite ["check", "search"] # specific icons
icons_sprite ["check", "search"], library: "heroicons", variant: "outline" # with library/variantRails Icons also includes a few animated icons. Great for loading states and so on. These are currently included:
faded-spinnertrailing-spinnerfading-dotsbouncing-dotsUse like this: icon "faded-spinner", library: "animated". The same attributes as the other libraries are available.
To sync all libraries, run:
rails generate rails_icons:syncTo sync only a specific library, run:
rails generate rails_icons:sync --library=heroicons
# Or multiple at once:
rails generate rails_icons:sync --libraries=heroicons lucideTo sync only specific variants for a library:
rails generate rails_icons:sync --library=heroicons --variants solid outlineRails Icons pulls SVGs straight from the path //.svg. No generator is required for custom icons. Configuration is optional and only used to define defaults.
To add a custom library, create its directory and drop in your SVGs:
app/assets/svg/icons/simple_icons/apple.svg(Alternatively, scaffold it with rails generate rails_icons:install --library=simple_icons, which creates the directory and adds config.custom_library :simple_icons in the initializer.)
Use them like any other library:
icon "apple", library: :simple_iconsTo add a git source for syncing, edit the initializer:
RailsIcons.configure do |config|
config.custom_library :my_icons, source: {
url: "https://github.com/user/icons.git",
variants: { default: "." }
}
endThen sync:
rails generate rails_icons:sync --library=my_iconsThis project uses Standard for formatting Ruby code. Please make sure to run be standardrb before submitting pull requests. Run tests via rails test.
Rails Icons is released under the MIT License.
暂无开放 Issues,或尚未同步最近议题。