Module: LocoMotion::Icons::Reference

Defined in:
lib/loco_motion/icons/reference.rb

Overview

Parses a qualified icon token into its library, name, and variant.

The grammar is [library:]name[/variant] — the same form used in Configuration#icon_safelist — so an icon reference is fully self-contained wherever it appears (a loco_icon call, a component's icon: option, or the safelist). That keeps rendering and treeshaking in lockstep: the Scanner extracts exactly the token the Renderer resolves.

"heart" => library: default, name: "heart", variant: default "lucide:heart" => library: "lucide", name: "heart", variant: default "phosphor:gear/bold" => library: "phosphor", name: "gear", variant: "bold" "bolt/solid" => library: default, name: "bolt", variant: "solid"

An explicit library: / variant: passed alongside the token is used only as the default — anything the token itself specifies wins.

Class Method Summary collapse

Class Method Details

.parse(token, default_library: nil, default_variant: nil) ⇒ Hash

Returns { library:, variant:, name: }. name is always a String. library is a String whenever the token names one explicitly (lib:name) or a default_library: is supplied; it stays nil when the token omits a library and no default was given. variant keeps the resolved type (or nil), since some backends distinguish :outline from "outline".

Parameters:

  • token (String, Symbol)

    The icon token, [library:]name[/variant].

  • default_library (String, Symbol, nil) (defaults to: nil)

    Library to use when the token does not name one.

  • default_variant (String, Symbol, nil) (defaults to: nil)

    Variant to use when the token does not name one.

Returns:

  • (Hash)

    { library:, variant:, name: }. name is always a String. library is a String whenever the token names one explicitly (lib:name) or a default_library: is supplied; it stays nil when the token omits a library and no default was given. variant keeps the resolved type (or nil), since some backends distinguish :outline from "outline".



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/loco_motion/icons/reference.rb', line 43

def parse(token, default_library: nil, default_variant: nil)
  before_variant, _slash, variant_part = token.to_s.partition("/")
  library_part, colon, name_part = before_variant.partition(":")

  if colon.empty?
    name = library_part
    library = default_library
  else
    name = name_part
    library = library_part
  end

  {
    library: library&.to_s,
    variant: variant_part.empty? ? default_variant : variant_part,
    name: name
  }
end