Class: Icon::Component

Inherits:
Lookbook::BaseComponent
  • Object
show all
Defined in:
app/components/lookbook/icon/component.rb

Constant Summary collapse

ICON_CACHE =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, size: 4, stroke: 2, **html_attrs) ⇒ Component

Returns a new instance of Component.



7
8
9
10
11
12
# File 'app/components/lookbook/icon/component.rb', line 7

def initialize(name:, size: 4, stroke: 2, **html_attrs)
  @icon_name = name.to_s.tr("_", "-")
  @size = size || 4
  @stroke = stroke
  super(**html_attrs)
end

Instance Attribute Details

#strokeObject (readonly)

Returns the value of attribute stroke.



5
6
7
# File 'app/components/lookbook/icon/component.rb', line 5

def stroke
  @stroke
end

Instance Method Details

#read_svgObject



22
23
24
25
26
27
28
# File 'app/components/lookbook/icon/component.rb', line 22

def read_svg
  File.read(svg_path).html_safe
rescue
  if Rails.env.development? || Rails.env.test?
    raise "`#{@icon_name}` is not a valid icon name"
  end
end

#size_remsObject



14
15
16
# File 'app/components/lookbook/icon/component.rb', line 14

def size_rems
  "#{@size * 0.25}rem"
end

#svgObject



18
19
20
# File 'app/components/lookbook/icon/component.rb', line 18

def svg
  ICON_CACHE[@icon_name] ||= read_svg
end

#svg_pathObject



30
31
32
# File 'app/components/lookbook/icon/component.rb', line 30

def svg_path
  Lookbook::Engine.root.join("assets/icons/#{@icon_name}.svg")
end