Class: LocoMotion::Icons::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/loco_motion/icons/renderer.rb

Overview

Renders an icon as inline SVG from a vendored or app-synced icon library.

Resolution checks, in order: the consuming application's own <Rails.root>/app/assets/svg/icons (so any library a consumer syncs in — or an icon they override — wins); in development only, the local icon cache (config.icon_cache_path) so a newly-used icon renders without re-running loco_motion:icons:sync; then LocoMotion's bundled icons shipped inside the engine. This lets the default Heroicons work with zero setup while letting consumers add or override icons without touching the gem. Test and production skip the cache, so the committed set stays the source of truth.

SVG files are parsed in XML mode so case-sensitive attributes like viewBox (and clipPath, linearGradient, ...) keep their casing — unlike HTML-mode parsing, which would lowercase them into invalid SVG.

Constant Summary collapse

DEFAULT_LIBRARY =
:heroicons
DEFAULT_VARIANT =
:outline

Instance Method Summary collapse

Constructor Details

#initialize(name:, library: DEFAULT_LIBRARY, variant: DEFAULT_VARIANT, attributes: {}) ⇒ Renderer

Returns a new instance of Renderer.

Parameters:

  • name (String, Symbol)

    The icon name. May be a qualified token, [library:]name[/variant] (e.g. "academic-cap", "lucide:heart", "phosphor:gear/bold"); anything the token specifies overrides the library: / variant: arguments. See LocoMotion::Icons::Reference.

  • library (String, Symbol) (defaults to: DEFAULT_LIBRARY)

    The icon library (default :heroicons).

  • variant (String, Symbol, nil) (defaults to: DEFAULT_VARIANT)

    The library variant / weight (e.g. :outline, :solid). nil means the library is flat (no variant subdirectory).

  • attributes (Hash) (defaults to: {})

    HTML attributes to apply to the <svg> — the shape returned by rendered_html (:class string, :data / :aria hashes, plus any other attributes).



50
51
52
53
54
55
56
# File 'lib/loco_motion/icons/renderer.rb', line 50

def initialize(name:, library: DEFAULT_LIBRARY, variant: DEFAULT_VARIANT, attributes: {})
  ref = Reference.parse(name, default_library: library || DEFAULT_LIBRARY, default_variant: variant)
  @name = ref[:name]
  @library = ref[:library].to_s
  @variant = ref[:variant]&.to_s
  @attributes = attributes || {}
end

Instance Method Details

#to_svgString

Returns The inline SVG markup with our attributes applied.

Returns:

  • (String)

    The inline SVG markup with our attributes applied.



61
62
63
64
65
66
# File 'lib/loco_motion/icons/renderer.rb', line 61

def to_svg
  document = Nokogiri::XML(::File.read(file_path))
  svg = document.root
  apply_attributes(svg)
  svg.to_xml
end