Class: NitroKit::Icon

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/icon.rb

Constant Summary collapse

SIZES =
%i[xs sm md lg xl].freeze
STROKE_WIDTHS =
(0.5..4).freeze

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, size: :md, label: nil, stroke_width: 1.5, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Icon

Returns a new instance of Icon.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/components/nitro_kit/icon.rb', line 10

def initialize(
  name,
  size: :md,
  label: nil,
  stroke_width: 1.5,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @name = name.to_s.tr("_", "-")
  @size = validate_choice!(:size, size, SIZES)
  @label = label
  if !label.nil? && (!label.is_a?(String) || label.strip.empty?)
    raise ArgumentError, "Icon label: must be a non-blank String"
  end
  @stroke_width = validate_stroke_width!(stroke_width)
  @icon = LucideRails::IconProvider.icon(@name)

  raise ArgumentError, "Unknown icon #{@name.inspect}" unless @icon

  icon_aria = label ? { label:, hidden: false } : { hidden: true }

  super(
    component: :icon,
    attributes: {
      id:,
      width: 24,
      height: 24,
      viewBox: "0 0 24 24",
      fill: "none",
      stroke: "currentColor",
      stroke_width: @stroke_width,
      stroke_linecap: "round",
      stroke_linejoin: "round",
      focusable: false,
      role: label && "img",
      aria: icon_aria
    }.compact,
    html:,
    aria:,
    data:,
    size:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



58
59
60
# File 'app/components/nitro_kit/icon.rb', line 58

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



58
59
60
# File 'app/components/nitro_kit/icon.rb', line 58

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



58
59
60
# File 'app/components/nitro_kit/icon.rb', line 58

def size
  @size
end

#stroke_widthObject (readonly)

Returns the value of attribute stroke_width.



58
59
60
# File 'app/components/nitro_kit/icon.rb', line 58

def stroke_width
  @stroke_width
end

Instance Method Details

#view_templateObject



60
61
62
# File 'app/components/nitro_kit/icon.rb', line 60

def view_template
  svg(**root_attributes) { raw safe(@icon) }
end