# frozen_string_literal: true module Primer module Navigation # This component is part of navigation components such as `Primer::TabNavComponent` # and `Primer::UnderlineNavComponent` and should not be used by itself. # # @accessibility # `TabComponent` renders the selected anchor tab with `aria-current="page"` by default. # When the selected tab does not correspond to the current page, such as in a nested inner tab, make sure to use aria-current="true" class TabComponent < Primer::Component DEFAULT_ARIA_CURRENT_FOR_ANCHOR = :page ARIA_CURRENT_OPTIONS_FOR_ANCHOR = [true, DEFAULT_ARIA_CURRENT_FOR_ANCHOR].freeze # Panel controlled by the Tab. This will not render anything in the tab itself. # It will provide a accessor for the Tab's parent to call and render the panel # content in the appropriate place. # Refer to `UnderlineNavComponent` and `TabNavComponent` implementations for examples. # # @param system_arguments [Hash] <%= link_to_system_arguments_docs %> renders_one :panel, lambda { |**system_arguments| return unless @with_panel system_arguments[:tag] = :div system_arguments[:role] ||= :tabpanel system_arguments[:tabindex] = 0 system_arguments[:hidden] = true unless @selected label_present = aria("label", system_arguments) || aria("labelledby", system_arguments) unless label_present if @id.present? system_arguments[:"aria-labelledby"] = @id elsif !Rails.env.production? raise ArgumentError, "Panels must be labelled. Either set a unique `id` on the tab, or set an `aria-label` directly on the panel" end end Primer::BaseComponent.new(**system_arguments) } # Icon to be rendered in the Tab left. # # @param kwargs [Hash] The same arguments as <%= link_to_component(Primer::OcticonComponent) %>. renders_one :icon, lambda { |icon = nil, **system_arguments| system_arguments[:classes] = class_names( @icon_classes, system_arguments[:classes] ) Primer::OcticonComponent.new(icon, **system_arguments) } # The Tab's text. # # @param kwargs [Hash] The same arguments as <%= link_to_component(Primer::Beta::Text) %>. renders_one :text, Primer::Beta::Text # Counter to be rendered in the Tab right. # # @param kwargs [Hash] The same arguments as <%= link_to_component(Primer::CounterComponent) %>. renders_one :counter, Primer::CounterComponent attr_reader :selected # @example Default # <%= render(Primer::Navigation::TabComponent.new(selected: true)) do |c| %> # <% c.text { "Selected" } %> # <% end %> # <%= render(Primer::Navigation::TabComponent.new) do |c| %> # <% c.text { "Not selected" } %> # <% end %> # # @example With icons and counters # <%= render(Primer::Navigation::TabComponent.new) do |c| %> # <% c.icon(:star) %> # <% c.text { "Tab" } %> # <% end %> # <%= render(Primer::Navigation::TabComponent.new) do |c| %> # <% c.icon(:star) %> # <% c.text { "Tab" } %> # <% c.counter(count: 10) %> # <% end %> # <%= render(Primer::Navigation::TabComponent.new) do |c| %> # <% c.text { "Tab" } %> # <% c.counter(count: 10) %> # <% end %> # # @example Inside a list # <%= render(Primer::Navigation::TabComponent.new(list: true)) do |c| %> # <% c.text { "Tab" } %> # <% end %> # # @example With custom HTML # <%= render(Primer::Navigation::TabComponent.new) do %> #