# frozen_string_literal: true module Primer module Beta # Use `IconButton` to render Icon-only buttons without the default button styles. # # `IconButton` will always render with a tooltip unless the tag is `:summary`. # # @accessibility # `IconButton` requires an `aria-label`, which will provide assistive technologies with an accessible label. # The `aria-label` should describe the action to be invoked rather than the icon itself. For instance, # if your `IconButton` renders a magnifying glass icon and invokes a search action, the `aria-label` should be # `"Search"` instead of `"Magnifying glass"`. # Either `aria-label` or `aria-description` will be used for the `Tooltip` text, depending on which one is present. # Either `aria-label` or `aria-description` will be used for the `Tooltip` text, depending on which one is present. # [Learn more about best functional image practices (WAI Images)](https://www.w3.org/WAI/tutorials/images/functional) class IconButton < Primer::Component status :beta DEFAULT_SCHEME = :default SCHEME_MAPPINGS = { DEFAULT_SCHEME => "Button--secondary", :danger => "Button--danger", :invisible => "Button--invisible" }.freeze SCHEME_OPTIONS = SCHEME_MAPPINGS.keys # @param icon [String] Name of <%= link_to_octicons %> to use. # @param tag [Symbol] <%= one_of(Primer::Beta::BaseButton::TAG_OPTIONS) %> # @param scheme [Symbol] <%= one_of(Primer::Beta::IconButton::SCHEME_OPTIONS) %> # @param size [Symbol] <%= one_of(Primer::Beta::Button::SIZE_OPTIONS) %> # @param disabled [Boolean] Whether or not the button is disabled. If true, this option forces `tag:` to `:button`. # @param type [Symbol] <%= one_of(Primer::Beta::BaseButton::TYPE_OPTIONS) %> # @param 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. # @param 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. # @param show_tooltip [Boolean] 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. # @param tooltip_direction [Symbol] (Primer::Alpha::Tooltip::DIRECTION_DEFAULT) <%= one_of(Primer::Alpha::Tooltip::DIRECTION_OPTIONS) %> # @param system_arguments [Hash] <%= link_to_system_arguments_docs %> 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 private def render_tooltip? @show_tooltip && @system_arguments[:tag] != :summary end end end end