Class: Primer::OcticonComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/octicon_component.rb

Overview

‘Octicon` renders an <%= link_to_octicons %> with <%= link_to_system_arguments_docs %>. `Octicon` can also be rendered with the `primer_octicon` helper, which accepts the same arguments.

Constant Summary collapse

SIZE_DEFAULT =
:small
SIZE_MEDIUM =
:medium
SIZE_LARGE =
:large
SIZE_MAPPINGS =
{
  SIZE_DEFAULT => 16,
  SIZE_MEDIUM => 32,
  SIZE_LARGE => 64
}.freeze
SIZE_OPTIONS =
SIZE_MAPPINGS.keys

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

Class Method Summary collapse

Instance Method Summary collapse

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(icon_name = nil, icon: nil, size: SIZE_DEFAULT, use_symbol: false, **system_arguments) ⇒ OcticonComponent

Returns a new instance of OcticonComponent.

Examples:

Default

<%= render(Primer::OcticonComponent.new("check")) %>
<%= render(Primer::OcticonComponent.new(icon: "check")) %>

Medium

<%= render(Primer::OcticonComponent.new("people", size: :medium)) %>

Large

<%= render(Primer::OcticonComponent.new("x", size: :large)) %>

Helper

<%= primer_octicon("check") %>

Parameters:

  • icon (String) (defaults to: nil)

    Name of <%= link_to_octicons %> to use.

  • size (Symbol) (defaults to: SIZE_DEFAULT)

    <%= one_of(Primer::OcticonComponent::SIZE_MAPPINGS) %>

  • use_symbol (Boolean) (defaults to: false)

    EXPERIMENTAL (May change or be removed) - Set to true when using with <%= link_to_component(Primer::OcticonSymbolsComponent) %>.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/components/primer/octicon_component.rb', line 39

def initialize(icon_name = nil, icon: nil, size: SIZE_DEFAULT, use_symbol: false, **system_arguments)
  icon_key = icon_name || icon
  cache_key = Primer::Octicon::Cache.get_key(symbol: icon_key, size: size, **system_arguments.slice(:height, :width))

  @system_arguments = system_arguments
  @system_arguments[:tag] = :svg
  @system_arguments[:aria] ||= {}
  @use_symbol = use_symbol

  if @system_arguments[:aria][:label] || @system_arguments[:"aria-label"]
    @system_arguments[:role] = "img"
  else
    @system_arguments[:aria][:hidden] = true
  end

  if (cache_icon = Primer::Octicon::Cache.read(cache_key))
    @icon = cache_icon
  else
    # Filter out classify options to prevent them from becoming invalid html attributes.
    # Note height and width are both classify options and valid html attributes.
    octicon_options = {
      height: SIZE_MAPPINGS[fetch_or_fallback(SIZE_OPTIONS, size, SIZE_DEFAULT)]
    }.merge(@system_arguments.slice(:height, :width))

    @icon = Octicons::Octicon.new(icon_key, octicon_options)
    Primer::Octicon::Cache.set(cache_key, @icon)
  end

  @system_arguments[:classes] = class_names(
    @icon.options[:class],
    @system_arguments[:classes]
  )
  @system_arguments.merge!(@icon.options.except(:class, :'aria-hidden'))
end

Class Method Details

._after_compileObject



74
75
76
# File 'app/components/primer/octicon_component.rb', line 74

def self._after_compile
  Primer::Octicon::Cache.preload!
end