Class: Primer::Beta::IconButton

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

Overview

Use ‘IconButton` to render Icon-only buttons without the default button styles.

‘IconButton` will always render with a tooltip unless the tag is `:summary`.

Constant Summary collapse

DEFAULT_SCHEME =
:default
SCHEME_MAPPINGS =
{
  DEFAULT_SCHEME => "Button--secondary",
  :danger => "Button--danger",
  :invisible => "Button--invisible"
}.freeze
SCHEME_OPTIONS =
SCHEME_MAPPINGS.keys

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

Constants included from AttributesHelper

AttributesHelper::PLURAL_ARIA_ATTRIBUTES, AttributesHelper::PLURAL_DATA_ATTRIBUTES

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

Methods included from AttributesHelper

#aria, #data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes

Constructor Details

#initialize(icon:, scheme: DEFAULT_SCHEME, show_tooltip: true, tooltip_direction: Primer::Alpha::Tooltip::DIRECTION_DEFAULT, size: Primer::Beta::Button::DEFAULT_SIZE, disabled: false, **system_arguments) ⇒ IconButton

Returns a new instance of IconButton.

Parameters:

  • icon (String)

    Name of <%= link_to_octicons %> to use.

  • tag (Symbol)

    <%= one_of(Primer::Beta::BaseButton::TAG_OPTIONS) %>

  • scheme (Symbol) (defaults to: DEFAULT_SCHEME)

    <%= one_of(Primer::Beta::IconButton::SCHEME_OPTIONS) %>

  • size (Symbol) (defaults to: Primer::Beta::Button::DEFAULT_SIZE)

    <%= one_of(Primer::Beta::Button::SIZE_OPTIONS) %>

  • disabled (Boolean) (defaults to: false)

    Whether or not the button is disabled. If true, this option forces ‘tag:` to `:button`.

  • type (Symbol)

    <%= one_of(Primer::Beta::BaseButton::TYPE_OPTIONS) %>

  • aria-label (String)

    String that can be read by assistive technology. A label should be short and concise. See the accessibility section for more information.

  • aria-description (String)

    String that can be read by assistive technology. A description can be longer as it is intended to provide more context and information. See the accessibility section for more information.

  • show_tooltip (Boolean) (defaults to: true)

    Whether or not to show a tooltip when this button is hovered. Tooltips should only be hidden if the aria label is redundant, i.e. if the icon has a widely understood definition.

  • tooltip_direction (Symbol) (defaults to: Primer::Alpha::Tooltip::DIRECTION_DEFAULT)

    (Primer::Alpha::Tooltip::DIRECTION_DEFAULT) <%= one_of(Primer::Alpha::Tooltip::DIRECTION_OPTIONS) %>

  • 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/components/primer/beta/icon_button.rb', line 39

def initialize(icon:, scheme: DEFAULT_SCHEME, show_tooltip: true, tooltip_direction: Primer::Alpha::Tooltip::DIRECTION_DEFAULT, size: Primer::Beta::Button::DEFAULT_SIZE, disabled: false, **system_arguments)
  @icon = icon

  @show_tooltip = show_tooltip
  @system_arguments = system_arguments
  @system_arguments[:id] ||= self.class.generate_id
  @system_arguments[:disabled] = disabled

  @system_arguments[:classes] = class_names(
    "Button",
    "Button--iconOnly",
    SCHEME_MAPPINGS[fetch_or_fallback(SCHEME_OPTIONS, scheme, DEFAULT_SCHEME)],
    Primer::Beta::Button::SIZE_MAPPINGS[fetch_or_fallback(Primer::Beta::Button::SIZE_OPTIONS, size, Primer::Beta::Button::DEFAULT_SIZE)],
    system_arguments[:classes]
  )

  validate_aria_label

  @aria_label = aria("label", @system_arguments)
  @aria_description = aria("description", @system_arguments)

  return unless render_tooltip?

  @tooltip_arguments = {
    for_id: @system_arguments[:id],
    direction: tooltip_direction
  }

  # If we have both an `aria-label` and a `aria-description`, we create a `Tooltip` with the description type and keep the `aria-label` in the button.
  # Otherwise, the `aria-label` is used as the tooltip text, which is the `aria-labelled-by` of the button, so we don't set it in the button.
  if @aria_label.present? && @aria_description.present?
    @system_arguments.delete(:"aria-description")
    @system_arguments[:aria].delete(:description) if @system_arguments.include?(:aria)
    @tooltip_arguments[:text] = @aria_description
    @tooltip_arguments[:type] = :description
  else
    @system_arguments.delete(:"aria-label")
    @system_arguments[:aria].delete(:label) if @system_arguments.include?(:aria)
    @tooltip_arguments[:text] = @aria_label
    @tooltip_arguments[:type] = :label
  end

  @tooltip = Primer::Alpha::Tooltip.new(**@tooltip_arguments)
  aria_key = @tooltip_arguments[:type] == :label ? :labelledby : :describedby

  @system_arguments[:aria] = merge_aria(
    @system_arguments, { aria: { aria_key => @tooltip.id } }
  )
end