Class: Primer::OcticonComponent

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

Overview

Renders an [Octicon](primer.style/octicons/) with <%= link_to_system_arguments_docs %>.

Constant Summary collapse

SIZE_DEFAULT =
:small
SIZE_MAPPINGS =
{
  SIZE_DEFAULT => 16,
  :medium => 32,
  :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

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, **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)) %>

Parameters:

  • icon (String) (defaults to: nil)

    Name of [Octicon](primer.style/octicons/) to use.

  • size (Symbol) (defaults to: SIZE_DEFAULT)

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

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/components/primer/octicon_component.rb', line 29

def initialize(icon_name = nil, icon: nil, size: SIZE_DEFAULT, **system_arguments)
  @icon = icon_name || icon
  @system_arguments = system_arguments

  @system_arguments[:class] = Primer::Classify.call(**@system_arguments)[:class]
  @system_arguments[:height] ||= SIZE_MAPPINGS[size]

  # 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_helper_options = @system_arguments.slice(:height, :width)
  @system_arguments = add_test_selector(@system_arguments).except(*Primer::Classify::VALID_KEYS, :classes).merge(octicon_helper_options)
end

Instance Method Details

#callObject



42
43
44
# File 'app/components/primer/octicon_component.rb', line 42

def call
  octicon(@icon, { **@system_arguments })
end