Class: Yattho::IconButton

Inherits:
Component
  • Object
show all
Defined in:
app/components/yattho/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 => "",
  :danger => "btn-octicon-danger"
}.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

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(icon:, scheme: DEFAULT_SCHEME, box: false, tooltip_direction: Yattho::Alpha::Tooltip::DIRECTION_DEFAULT, **system_arguments) ⇒ IconButton

Returns a new instance of IconButton.

Examples:

Default


<%= render(Yattho::IconButton.new(icon: :search, "aria-label": "Search", id: "search-button")) %>

Schemes


<%= render(Yattho::IconButton.new(icon: :search, "aria-label": "Search")) %>
<%= render(Yattho::IconButton.new(icon: :trash, "aria-label": "Delete", scheme: :danger)) %>

In a BorderBox


<%= render(Yattho::Beta::BorderBox.new) do |component| %>
  <% component.body do %>
    <%= render(Yattho::Beta::Text.new(pr: 2)) { "Body" } %>
    <%= render(Yattho::IconButton.new(icon: :pencil, box: true, "aria-label": "Edit")) %>
  <% end %>
<% end %>

With an ‘aria-description`

@description
  If you need to have a longer description for the icon button, use both the `aria-label` and `aria-description`
  attributes. A label should be short and concise, while the description can be longer as it is intended to provide
  more context and information. See the accessibility section for more information.
@code
  <%= render(Yattho::IconButton.new(icon: :bold, "aria-label": "Bold", "aria-description": "Add bold text, Cmd+b")) %>

Custom tooltip direction


<%= render(Yattho::IconButton.new(icon: :search, "aria-label": "Search", tooltip_direction: :e)) %>

Parameters:

  • scheme (Symbol) (defaults to: DEFAULT_SCHEME)

    <%= one_of(Yattho::IconButton::SCHEME_OPTIONS) %>

  • icon (String)

    Name of <%= link_to_octicons %> to use.

  • tag (Symbol)

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

  • type (Symbol)

    <%= one_of(Yattho::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.

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

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

  • box (Boolean) (defaults to: false)

    Whether the button is in a <%= link_to_component(Yattho::Beta::BorderBox) %>. If ‘true`, the button will have the `Box-btn-octicon` class.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/components/yattho/icon_button.rb', line 63

def initialize(icon:, scheme: DEFAULT_SCHEME, box: false,
               tooltip_direction: Yattho::Alpha::Tooltip::DIRECTION_DEFAULT, **system_arguments)
  @icon = icon

  @system_arguments = system_arguments

  @system_arguments[:id] ||= "icon-button-#{SecureRandom.hex(4)}"

  @system_arguments[:classes] = class_names(
    "btn-octicon",
    SCHEME_MAPPINGS[fetch_or_fallback(SCHEME_OPTIONS, scheme, DEFAULT_SCHEME)],
    system_arguments[:classes],
    "Box-btn-octicon" => box
  )

  validate_aria_label

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

  @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
end