Class: Yattho::Alpha::Tooltip

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

Overview

‘Tooltip` only appears on mouse hover or keyboard focus and contain a label or description text. Use tooltips sparingly and as a last resort. Use tooltips as a last resort. Please consider [Tooltips alternatives](yattho.com/design/accessibility/tooltip-alternatives).

When using a tooltip, follow the provided guidelines to avoid accessibility issues:

  • Tooltips should contain only **non-essential text**. Tooltips can easily be missed and are not accessible on touch devices so never use tooltips to convey critical information.

  • ‘Tooltip` should be rendered through the API of <%= link_to_component(Yattho::ButtonComponent)%>, <%= link_to_component(Yattho::Beta::Link)%>, or <%= link_to_component(Yattho::IconButton)%>. Avoid using `Tooltip` a standalone component unless absolutely necessary (and only on interactive elements).

Constant Summary collapse

DIRECTION_DEFAULT =
:s
DIRECTION_OPTIONS =
[DIRECTION_DEFAULT, :n, :e, :w, :ne, :nw, :se, :sw].freeze
TYPE_FALLBACK =
:description
TYPE_OPTIONS =
[:label, :description].freeze

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(type:, for_id:, text:, direction: DIRECTION_DEFAULT, **system_arguments) ⇒ Tooltip

Returns a new instance of Tooltip.

Examples:

As a supplementary description for a button

@description
  In this example, the button has a visible label text, "Save". `type: :description` is set because the tooltip content is supplementary.
  A screen reader user who encounters this button will hear the accessible name, "Save" followed by the accessible description, "This will immediately impact all organization members".
@code
  <%= render(Yattho::ButtonComponent.new(id: "save-button")) do |component| %>
    <% component.with_tooltip(text: "This will immediately impact all organization members", type: :description, direction: :ne) %>
    Save
  <% end %>

As a label for an ‘IconButton`

@description
  An `IconButton` of `tag: :button` and `tag: :a` will render a tooltip using the `aria-label` content by default. While tooltips should generally be avoided, a tooltip on an `IconButton`
  has usability benefits because it provides a textual label for sighted users.
  A screen reader user who encounters the following button will hear the accessible name, "Bold".
@code
  <%= render(Yattho::IconButton.new(id: "bold-button-0", icon: :bold, "aria-label": "Bold")) %>

As a supplementary description for an ‘IconButton`

@description
  If you want to provide a description for the `IconButton`, set both `aria-label` and `aria-description` text. The tooltip will use the `aria-description` text.
  A screen reader user who encounters the following button will hear the accessible name "Search", followed by the accessible description "Use keywords like 'repo:' and 'org:' in your query".
@code
  <%= render(Yattho::IconButton.new(id: "search-button", icon: :search, "aria-label": "Search", "aria-description": "Use keywords like 'repo:' and 'org:' in your query")) %>

With direction

@description
  Set direction of tooltip with `direction`. The tooltip is responsive and will automatically adjust direction to avoid cutting off.
@code
  <%= render(Yattho::ButtonComponent.new(id: "North", m: 2)) do |component| %>
    <% component.with_tooltip(text: "This is a North-facing tooltip, and is responsive.", type: :description, direction: :n) %>
    North
  <% end %>
  <%= render(Yattho::ButtonComponent.new(id: "South", m: 2)) do |component| %>
    <% component.with_tooltip(text: "This is a South-facing tooltip, and is responsive.", type: :description, direction: :s) %>
    South
  <% end %>
  <%= render(Yattho::ButtonComponent.new(id: "East", m: 2)) do |component| %>
    <% component.with_tooltip(text: "This is a East-facing tooltip, and is responsive.", type: :description, direction: :e) %>
    East
  <% end %>
  <%= render(Yattho::ButtonComponent.new(id: "West", m: 2)) do |component| %>
    <% component.with_tooltip(text: "This is a West-facing tooltip, and is responsive.", type: :description, direction: :w) %>
    West
  <% end %>
  <%= render(Yattho::ButtonComponent.new(id: "Northwest", m: 2)) do |component| %>
    <% component.with_tooltip(text: "This is a Northwest-facing tooltip, and is responsive.", type: :description, direction: :nw) %>
    Northwest
  <% end %>
  <%= render(Yattho::ButtonComponent.new(id: "Southwest", m: 2)) do |component| %>
    <% component.with_tooltip(text: "This is a Southwest-facing tooltip, and is responsive.", type: :description, direction: :sw) %>
    Southwest
  <% end %>
  <%= render(Yattho::ButtonComponent.new(id: "Northeast", m: 2)) do |component| %>
    <% component.with_tooltip(text: "This is a Northeast-facing tooltip, and is responsive.", type: :description, direction: :ne) %>
    Northeast
  <% end %>
  <%= render(Yattho::ButtonComponent.new(id: "Southeast", m: 2)) do |component| %>
    <% component.with_tooltip(text: "This is a Southeast-facing tooltip, and is responsive.", type: :description, direction: :se) %>
    Southeast
  <% end %>

Directly using ‘Tooltip`

@description
  When you have a valid tooltip usecase for an interactive element that is not one of the supported components, you may need to use the `Tooltip` component directly.
  The tooltip should be placed directly adjacent after the element you are associating it with.
  The tooltip is absolutely positioned so ensure there is a wrapper with `position: relative` to avoid positioning issues.
@code
  <div style="position: relative;">
    <button type="button" id="test-button">Test</button>
    <%= render(Yattho::Alpha::Tooltip.new(for_id: "test-button", type: :description, text: "This tooltip should take up the full width", direction: :ne)) %>
  </div>

Parameters:

  • for_id (String)

    The ID of the element that the tooltip should be attached to.

  • type (Symbol)

    <%= one_of(Yattho::Alpha::Tooltip::TYPE_OPTIONS) %>

  • direction (Symbol) (defaults to: DIRECTION_DEFAULT)

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

  • text (String)

    The text content of the tooltip. This should be brief and no longer than a sentence.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>

Raises:

  • (TypeError)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/components/yattho/alpha/tooltip.rb', line 105

def initialize(type:, for_id:, text:, direction: DIRECTION_DEFAULT, **system_arguments)
  raise TypeError, "tooltip text must be a string" unless text.is_a?(String)

  @text = text
  @system_arguments = system_arguments
  @system_arguments[:id] ||= self.class.generate_id
  @system_arguments[:tag] = :'tool-tip'
  @system_arguments[:for] = for_id
  @system_arguments[:classes] = class_names(
    @system_arguments[:classes],
    "sr-only"
  )
  @system_arguments[:position] = :absolute
  @system_arguments[:'data-direction'] = fetch_or_fallback(DIRECTION_OPTIONS, direction, DIRECTION_DEFAULT).to_s
  @system_arguments[:'data-type'] = fetch_or_fallback(TYPE_OPTIONS, type, TYPE_FALLBACK).to_s
end

Instance Method Details

#callObject



122
123
124
# File 'app/components/yattho/alpha/tooltip.rb', line 122

def call
  render(Yattho::BaseComponent.new(**@system_arguments)) { @text }
end