Class: Shadcn::Icon::Component

Inherits:
ApplicationViewComponent show all
Defined in:
app/components/shadcn/icon/component.rb

Overview

Inline replacement for the lucide-react icons the components import.

<%= render Shadcn::Icon::Component.new(:"chevron-down", class: "size-4") %>

Emits the same SVG lucide does — 24×24 viewBox, currentColor stroke, lucide lucide-<name> classes — so the [&_svg:not([class*='size-'])]:size-4 rules on the surrounding components behave identically.

Direct Known Subclasses

Spinner::Component

Constant Summary collapse

SVG_ATTRIBUTES =

The drawings live in vendor/lucide/icons as the files lucide publishes, and rake icons:build turns them into ShadcnViewComponent::Icons::PATHS. They used to be typed into this file by hand, which went exactly as that always goes: the count in the README said eleven of twenty-two, and search had been drawn from an older lucide — a magnifier with a shorter handle than the one every other icon in the set was drawn with.

{
  "xmlns" => "http://www.w3.org/2000/svg",
  "width" => "24",
  "height" => "24",
  "viewBox" => "0 0 24 24",
  "fill" => "none",
  "stroke" => "currentColor",
  "stroke-width" => "2",
  "stroke-linecap" => "round",
  "stroke-linejoin" => "round"
}.freeze

Constants inherited from ApplicationViewComponent

ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS

Instance Attribute Summary collapse

Attributes inherited from ApplicationViewComponent

#attributes

Instance Method Summary collapse

Methods inherited from ApplicationViewComponent

default_tag, #merged_style, #render_element, #shadcn_t, slot_name, #style_variants, #tag_name

Constructor Details

#initialize(name, **attributes) ⇒ Component

Returns a new instance of Component.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/components/shadcn/icon/component.rb', line 34

def initialize(name, **attributes)
  # Resolved through the registry, which owns the alias table: a host
  # registering `"more-horizontal"` and this rendering `"ellipsis"` have
  # to meet somewhere, and the file that stores the drawings is the only
  # place both can.
  @name = ShadcnViewComponent::IconRegistry.canonical(name)

  # Loud where it can be fixed, silent where it cannot. An icon is
  # decorative, and a gem should not be able to take down a page in an
  # application it has never seen — the same trade Rails makes with a
  # missing translation.
  raise ArgumentError, "unknown lucide icon: #{name}" if path.nil? && Rails.env.local?

  super(**attributes)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



50
51
52
# File 'app/components/shadcn/icon/component.rb', line 50

def name
  @name
end

Instance Method Details

#callObject



61
62
63
64
65
# File 'app/components/shadcn/icon/component.rb', line 61

def call
  return "".html_safe if path.nil?

  render_element(body: path.html_safe)
end

#css_classes(extra = nil) ⇒ Object



71
72
73
# File 'app/components/shadcn/icon/component.rb', line 71

def css_classes(extra = nil)
  ShadcnViewComponent.cn("lucide", "lucide-#{name}", extra)
end

#element_attributes(**defaults) ⇒ Object



67
68
69
# File 'app/components/shadcn/icon/component.rb', line 67

def element_attributes(**defaults)
  super(**SVG_ATTRIBUTES.merge(defaults))
end

#pathObject

The registry is read first so a host can replace one of the bundled eleven and not only add a twelfth. The other order ignored register("check", …) in silence — nothing raised, nothing logged, the gem's own tick still rendering — which leaves a host staring at an icon that will not change with no way to find out why.



57
58
59
# File 'app/components/shadcn/icon/component.rb', line 57

def path
  @path ||= Shadcn::Icon.registered[name] || ShadcnViewComponent::Icons::PATHS[name]
end