Class: Rubicons::BaseIconSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rubicons/base_icon_set.rb

Overview

Base module for icon sets with common functionality

Constant Summary collapse

SIZE_MAP =

Default size mapping that can be overridden by specific icon sets

{
  xs:    '12',
  sm:    '16',
  md:    '20',
  lg:    '24',
  xl:    '32',
  "2xl": '40'
}.freeze

Class Method Summary collapse

Class Method Details

.apply_options_to_svg(svg_element, options) ⇒ Nokogiri::XML::Element

Helper method to apply options to an SVG element

Parameters:

  • svg_element (Nokogiri::XML::Element)

    SVG element to modify

  • options (Hash)

    options to apply

Returns:

  • (Nokogiri::XML::Element)

    modified SVG element



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubicons/base_icon_set.rb', line 60

def apply_options_to_svg(svg_element, options)
  svg_element['class'] = options.delete(:class) if options[:class]
  svg_element['stroke'] = options.delete(:stroke) if options[:stroke]
  svg_element['fill'] = options.delete(:fill) if options[:fill]
  svg_element['stroke-width'] = options.delete(:stroke_width).to_s if options[:stroke_width]

  # Add any remaining attributes from options
  options.each do |key, value|
    svg_element[key.to_s.tr('_', '-')] = value.to_s
  end

  svg_element
end

.available_icons(icons_path) ⇒ Array<Symbol>

Helper method to load all available icon names

Parameters:

  • icons_path (String)

    path to icons directory

Returns:

  • (Array<Symbol>)

    list of available icon names



77
78
79
# File 'lib/rubicons/base_icon_set.rb', line 77

def available_icons(icons_path)
  Dir.glob("#{icons_path}/*.svg").map { |path| File.basename(path, '.svg').to_sym }
end

.render_icon(name, icons_path, **options) ⇒ String

Helper method to render an SVG icon with customizations

Parameters:

  • name (String, Symbol)

    icon name

  • icons_path (String)

    path to icons directory

  • options (Hash)

    options for customizing the icon

Options Hash (**options):

  • :size (Symbol, String) — default: :md

    size of the icon

  • :class (String)

    CSS classes to add

  • :size_map (Hash)

    custom size mapping

Returns:

  • (String)

    rendered SVG



27
28
29
30
31
# File 'lib/rubicons/base_icon_set.rb', line 27

def render_icon(name, icons_path, **options)
  render_icon!(name, icons_path, **options)
rescue
  ''
end

.render_icon!(name, icons_path, **options) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubicons/base_icon_set.rb', line 33

def render_icon!(name, icons_path, **options)
  icon_path = File.join(icons_path, "#{name}.svg")
  raise ArgumentError, "Icon '#{name}' not found" unless File.exist?(icon_path)

  svg = File.read(icon_path)
  doc = Nokogiri::HTML::DocumentFragment.parse(svg)
  svg_element = doc.at_css 'svg'

  size_map = options.delete(:size_map) || self::SIZE_MAP
  size_key = (options.delete(:size) || :md).to_sym
  svg_size = size_map[size_key] || size_map[:md]

  svg_element['width']  = svg_size
  svg_element['height'] = svg_size
  svg_element['stroke'] = 'currentColor'
  svg_element['fill'] = 'none'
  svg_element['viewBox'] = "0 0 #{svg_size} #{svg_size}"

  apply_options_to_svg(svg_element, options)

  svg_element.to_s
end