Class: Blacklight::Icons::IconComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/blacklight/icons/icon_component.rb

Overview

This is the base class for icon components. You should extend this class for each icon.

You can override the default svg by setting:

Blacklight::Icons::MyIconComponent.svg = '<svg>your SVG here</svg>'

Instance Method Summary collapse

Constructor Details

#initialize(svg: nil, tag: :span, name: nil, label: nil, aria_hidden: nil, classes: nil, **options) ⇒ IconComponent

rubocop:disable Metrics/ParameterLists



11
12
13
14
15
16
17
# File 'app/components/blacklight/icons/icon_component.rb', line 11

def initialize(svg: nil, tag: :span, name: nil, label: nil, aria_hidden: nil, classes: nil, **options)
  self.svg = svg if svg
  @name = name
  @assigned_classes = Array(classes)
  @tag = tag
  @options = options.merge(aria: options.fetch(:aria, {}).reverse_merge(label: label, hidden: aria_hidden))
end

Instance Method Details

#callObject

rubocop:enable Metrics/ParameterLists



20
21
22
23
24
# File 'app/components/blacklight/icons/icon_component.rb', line 20

def call
  tag.public_send(@tag, svg&.html_safe, # rubocop:disable Rails/OutputSafety
                  class: classes,
                  **@options)
end

#classesObject



28
29
30
# File 'app/components/blacklight/icons/icon_component.rb', line 28

def classes
  @classes ||= @assigned_classes + ['blacklight-icons', "blacklight-icons-#{name}"]
end

#nameObject



32
33
34
# File 'app/components/blacklight/icons/icon_component.rb', line 32

def name
  @name ||= self.class.name.demodulize.underscore.sub('_component', '')
end