Module: ViewComponent::IconComponent

Included in:
UiHelper
Defined in:
app/helpers/view_component/icon_component.rb

Constant Summary collapse

ICONS_PATH =
NeoComponents::Engine.gem_root.join("app", "assets", "icons")

Instance Method Summary collapse

Instance Method Details

#icon(icon_name, css: nil, span_css: nil, stroke_width: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/view_component/icon_component.rb', line 7

def icon(icon_name, css: nil, span_css: nil, stroke_width: nil)
  file = Rails.cache.fetch("neo_components:icon:#{icon_name}") do
    ICONS_PATH.join("#{icon_name}.svg").read
  end

  svg = Nokogiri::HTML::DocumentFragment.parse(file).at_css('svg')
  svg['class'] = css
  if stroke_width.present?
    # Use the stroke_width passed from helper
    svg['stroke-width'] = stroke_width
  elsif svg['stroke-width'].blank?
    # Use default only if SVG does not have stroke-width
    svg['stroke-width'] = '1.5'
  end
  (:span, svg.to_html.html_safe, class: "flex justify-center items-center #{span_css}".strip) # rubocop:disable Rails/OutputSafety
end