# frozen_string_literal: true module Yattho module Navigation # This component is part of navigation components such as `Yattho::Alpha::TabNav` # and `Yattho::Alpha::UnderlineNav` 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 < Yattho::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 `UnderlineNav` and `TabNav` implementations for examples. # # @param system_arguments [Hash] <%= link_to_system_arguments_docs %> renders_one :panel, lambda { |**system_arguments| return unless @with_panel deny_tag_argument(**system_arguments) system_arguments[:id] = @panel_id 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 Yattho::BaseComponent.new(**system_arguments) } # Icon to be rendered in the Tab left. # # @param kwargs [Hash] The same arguments as <%= link_to_component(Yattho::Beta::Octicon) %>. renders_one :icon, lambda { |icon = nil, **system_arguments| system_arguments[:classes] = class_names( @icon_classes, system_arguments[:classes] ) Yattho::Beta::Octicon.new(icon, **system_arguments) } # The Tab's text. # # @param kwargs [Hash] The same arguments as <%= link_to_component(Yattho::Beta::Text) %>. renders_one :text, Yattho::Beta::Text # Counter to be rendered in the Tab right. # # @param kwargs [Hash] The same arguments as <%= link_to_component(Yattho::Beta::Counter) %>. renders_one :counter, Yattho::Beta::Counter attr_reader :selected # @example Default # <%= render(Yattho::Navigation::TabComponent.new(selected: true)) do |component| %> # <% component.with_text { "Selected" } %> # <% end %> # <%= render(Yattho::Navigation::TabComponent.new) do |component| %> # <% component.with_text { "Not selected" } %> # <% end %> # # @example With icons and counters # <%= render(Yattho::Navigation::TabComponent.new) do |component| %> # <% component.with_icon(:star) %> # <% component.with_text { "Tab" } %> # <% end %> # <%= render(Yattho::Navigation::TabComponent.new) do |component| %> # <% component.with_icon(:star) %> # <% component.with_text { "Tab" } %> # <% component.with_counter(count: 10) %> # <% end %> # <%= render(Yattho::Navigation::TabComponent.new) do |component| %> # <% component.with_text { "Tab" } %> # <% component.with_counter(count: 10) %> # <% end %> # # @example Inside a list # <%= render(Yattho::Navigation::TabComponent.new(list: true)) do |component| %> # <% component.with_text { "Tab" } %> # <% end %> # # @example With custom HTML # <%= render(Yattho::Navigation::TabComponent.new) do %> #