Class: Rubicons::BaseIconSet
- Inherits:
-
Object
- Object
- Rubicons::BaseIconSet
- 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
-
.apply_options_to_svg(svg_element, options) ⇒ Nokogiri::XML::Element
Helper method to apply options to an SVG element.
-
.available_icons(icons_path) ⇒ Array<Symbol>
Helper method to load all available icon names.
-
.render_icon(name, icons_path, **options) ⇒ String
Helper method to render an SVG icon with customizations.
- .render_icon!(name, icons_path, **options) ⇒ Object
Class Method Details
.apply_options_to_svg(svg_element, options) ⇒ Nokogiri::XML::Element
Helper method to apply options to an 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 (svg_element, ) svg_element['class'] = .delete(:class) if [:class] svg_element['stroke'] = .delete(:stroke) if [:stroke] svg_element['fill'] = .delete(:fill) if [:fill] svg_element['stroke-width'] = .delete(:stroke_width).to_s if [:stroke_width] # Add any remaining attributes from 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
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
27 28 29 30 31 |
# File 'lib/rubicons/base_icon_set.rb', line 27 def render_icon(name, icons_path, **) render_icon!(name, icons_path, **) rescue '' end |
.render_icon!(name, icons_path, **options) ⇒ Object
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, **) 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 = .delete(:size_map) || self::SIZE_MAP size_key = (.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}" (svg_element, ) svg_element.to_s end |