Class: FluentIcons::Component

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
lib/fluent-icons/component.rb

Overview

ViewComponent for rendering Fluent Icons

Usage:

<%= render FluentIcons::Component.new(name: "add") %>
<%= render FluentIcons::Component.new(name: "delete", style: "filled", weight: 24) %>
<%= render FluentIcons::Component.new(name: "search", class: "w-4 h-4 text-blue-500") %>

With Hotwire/Turbo:

<%= render FluentIcons::Component.new(name: "refresh", data: { action: "click->controller#refresh" }) %>

Instance Method Summary collapse

Constructor Details

#initialize(name:, style: 'regular', weight: 20, **options) ⇒ Component

Returns a new instance of Component.

Parameters:

  • name (String, Symbol)

    The icon name (e.g., “add”, “delete”, “search”)

  • style (String) (defaults to: 'regular')

    The icon style (default: “regular”)

  • weight (Integer, String) (defaults to: 20)

    The icon size/weight (default: 20)

  • options (Hash)

    Additional HTML attributes (class, id, data, aria, etc.)



18
19
20
21
22
23
# File 'lib/fluent-icons/component.rb', line 18

def initialize(name:, style: 'regular', weight: 20, **options)
  @name = name.to_s
  @style = style.to_s
  @weight = weight.to_i
  @options = options
end

Instance Method Details

#callObject



25
26
27
28
29
30
31
32
33
# File 'lib/fluent-icons/component.rb', line 25

def call
  # Use the existing caching system from the helper
  svg_html = Cache.fetch(@name, cache_options) do
    icon = FluentIcons::Fluent.new(@name, style: @style, weight: @weight, **@options)
    icon.to_svg
  end

  svg_html.html_safe
end