Class: Yattho::Alpha::OcticonSymbols

Inherits:
Component
  • Object
show all
Defined in:
app/components/yattho/alpha/octicon_symbols.rb

Overview

OcticonSymbols renders a symbol dictionary using a list of <%= link_to_octicons %>.

Direct Known Subclasses

OcticonSymbolsComponent

Constant Summary

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Method Summary collapse

Methods inherited from Component

deprecated?, generate_id

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(icons: []) ⇒ OcticonSymbols

Returns a new instance of OcticonSymbols.

Examples:

Symbol dictionary

<%= render(Yattho::Beta::Octicon.new(icon: :check, use_symbol: true, color: :success)) %>
<%= render(Yattho::Beta::Octicon.new(icon: :check, use_symbol: true, color: :danger)) %>
<%= render(Yattho::Beta::Octicon.new(icon: :check, use_symbol: true, size: :medium)) %>
<%= render(Yattho::Alpha::OcticonSymbols.new(icons: [{ symbol: :check }, { symbol: :check, size: :medium }])) %>

Parameters:

  • icons (Array<Hash>) (defaults to: [])

    List of icons to render, in the format { symbol: :icon_name, size: :small }



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/components/yattho/alpha/octicon_symbols.rb', line 16

def initialize(icons: [])
  @icons = {}
  icons.each do |icon|
    symbol = icon[:symbol]
    size = Yattho::Beta::Octicon::SIZE_MAPPINGS[
      fetch_or_fallback(Yattho::Beta::Octicon::SIZE_OPTIONS, icon[:size] || Yattho::Beta::Octicon::SIZE_DEFAULT,
                        Yattho::Beta::Octicon::SIZE_DEFAULT)
    ]

    cache_key = Yattho::Octicon::Cache.get_key(symbol: symbol, size: size)

    if (cache_icon = Yattho::Octicon::Cache.read(cache_key))
      icon_instance = cache_icon
    else
      icon_instance = Octicons::Octicon.new(symbol, height: size)

      Yattho::Octicon::Cache.set(cache_key, icon_instance)
    end

    # Don't put the same icon twice
    @icons[[symbol, icon_instance.height]] = icon_instance if @icons[[symbol, icon_instance.height]].nil?
  end
end

Instance Method Details

#render?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/components/yattho/alpha/octicon_symbols.rb', line 40

def render?
  @icons.any?
end

#symbol_tagsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/components/yattho/alpha/octicon_symbols.rb', line 44

def symbol_tags
  safe_join(
    @icons.values.map do |icon|
      (
        :symbol,
        icon.path.html_safe, # rubocop:disable Rails/OutputSafety
        id: "octicon_#{icon.symbol}_#{icon.height}",
        viewBox: icon.options[:viewBox],
        width: icon.width,
        height: icon.height
      )
    end
  )
end