Class: Hero::IconComponent

Inherits:
LocoMotion::BaseComponent show all
Includes:
LocoMotion::Concerns::TippableComponent
Defined in:
app/components/hero/icon_component.rb

Overview

Note:

By default, icons are displayed with the ‘size-5` Tailwind class. This can be overridden without using the `!` modifier because we utilize the `:where()` pseudo-class to ensure our default classes have the lowest CSS specificity.

Creates an icon component using Heroicons, a set of free, MIT-licensed high-quality SVG icons. For a complete list of available icons, visit heroicons.com.

Constant Summary

Constants inherited from LocoMotion::BaseComponent

LocoMotion::BaseComponent::EMPTY_PART_IGNORED_TAGS, LocoMotion::BaseComponent::SELF_CLOSING_TAGS

Instance Attribute Summary

Attributes inherited from LocoMotion::BaseComponent

#config, #loco_parent

Instance Method Summary collapse

Methods inherited from LocoMotion::BaseComponent

build, #component_ref, #config_option, #cssify, define_modifier, define_modifiers, define_part, define_parts, define_size, define_sizes, #empty_part_content, #inspect, #part, register_component_initializer, register_component_setup, #rendered_css, #rendered_data, #rendered_html, #rendered_stimulus_controllers, #rendered_tag_name, renders_many, renders_one, set_component_name, #set_loco_parent, #strip_spaces

Constructor Details

#initialize(*args, **kws, &block) ⇒ IconComponent

Create a new instance of the IconComponent.

Parameters:

  • args (Array)

    If provided, the first argument is considered the ‘icon` name.

  • kws (Hash)

    The keyword arguments for the component.

Options Hash (**kws):

  • icon (String)

    The name of the icon to display. See heroicons.com for available icons.

  • variant (Symbol)

    The variant of the icon to use (default: :outline).

  • css (String)

    Additional CSS classes for styling. Common options include:

    • Size: ‘size-4`, `size-10`, `size-14`

    • Color: ‘text-red-600`, `text-green-600`, `text-yellow-400`

    • Animation: ‘animate-pulse`, `animate-spin`

  • tip (String)

    The tooltip text to display when hovering over the icon.



50
51
52
53
54
55
56
57
58
# File 'app/components/hero/icon_component.rb', line 50

def initialize(*args, **kws, &block)
  super

  # Accept either the :icon keyword argument or the first positional argument
  @icon = config_option(:icon, args[0])
  @variant = config_option(:variant)

  @css = config_option(:css, "")
end

Instance Method Details

#before_renderObject



60
61
62
63
64
65
# File 'app/components/hero/icon_component.rb', line 60

def before_render
  super

  add_html(:component, { variant: @variant }) if @variant
  add_css(:component, "where:size-5") unless @css.include?("size-")
end

#callObject

Renders the icon component.

Because this is an inline component which might be utlized alongside text, we utilize the ‘call` method instead of a template to ensure that no additional whitespace gets added to the output.



74
75
76
# File 'app/components/hero/icon_component.rb', line 74

def call
  heroicon(@icon, **rendered_html(:component))
end