Class: Loco::IconComponent
- Inherits:
-
LocoMotion::BaseComponent
- Object
- ViewComponent::Base
- LocoMotion::BaseComponent
- Loco::IconComponent
- Includes:
- LocoMotion::Concerns::TippableComponent
- Defined in:
- app/components/loco/icon_component.rb
Overview
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.
The icon color is inherited from the parent's text color, since the
bundled SVGs use currentColor.
Creates an icon component rendered as inline SVG from any installed icon library. LocoMotion bundles only a small, curated set of Heroicons — the icons its own components render, so they work with zero setup:
check,check-circlechevron-left,chevron-rightexclamation-circle,exclamation-triangleinformation-circleswatchtrashx-mark
Each is bundled in all four Heroicons variants (outline, solid, mini,
and micro). Every other icon — the rest of Heroicons, or any other library
like Lucide, Phosphor, Tabler, and brand sets — is synced into your own
application (see the loco_motion:icons:add / loco_motion:icons:sync
tasks).
Direct Known Subclasses
Daisy::DataInput::CallyComponent::NextIcon, Daisy::DataInput::CallyComponent::PreviousIcon
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
Instance Method Summary collapse
- #before_render ⇒ Object
-
#call ⇒ Object
Renders the icon component.
-
#initialize(*args, **kws, &block) ⇒ IconComponent
constructor
Create a new instance of the IconComponent.
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
Methods included from LocoMotion::Concerns::InspectableComponent
Constructor Details
#initialize(*args, **kws, &block) ⇒ IconComponent
Create a new instance of the IconComponent.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/components/loco/icon_component.rb', line 82 def initialize(*args, **kws, &block) if kws.key?(:library) || kws.key?(:variant) raise ArgumentError, "loco_icon no longer accepts `library:` or `variant:` — encode " \ "them in the icon token instead, e.g. " \ 'loco_icon("lucide:heart/duotone").' end super # Accept either the :icon keyword argument or the first positional argument @icon = config_option(:icon, args[0]) @css = config_option(:css, "") end |
Instance Method Details
#before_render ⇒ Object
97 98 99 100 101 |
# File 'app/components/loco/icon_component.rb', line 97 def before_render super add_css(:component, "where:size-5") unless @css.include?("size-") end |
#call ⇒ Object
Renders the icon component.
Because this is an inline component which might be utilized alongside
text, we utilize the call method instead of a template to ensure that no
additional whitespace gets added to the output.
110 111 112 113 114 115 116 117 |
# File 'app/components/loco/icon_component.rb', line 110 def call LocoMotion::Icons::Renderer.new( name: @icon, library: LocoMotion.configuration.default_icon_library, variant: LocoMotion.configuration.default_icon_variant, attributes: rendered_html(:component) ).to_svg.html_safe end |