Class: Yattho::Alpha::TabNav

Inherits:
Component
  • Object
show all
Includes:
TabNavHelper, TabbedComponentHelper
Defined in:
app/components/yattho/alpha/tab_nav.rb

Overview

Use ‘TabNav` to style navigation with a tab-based selected state, typically used for navigation placed at the top of the page. For panel navigation, use <%= link_to_component(Yattho::Alpha::TabPanels) %> instead.

Constant Summary collapse

BODY_TAG_DEFAULT =
:ul
TAG_DEFAULT =
:nav
TAG_OPTIONS =
[TAG_DEFAULT, :div].freeze

Constants included from TabNavHelper

TabNavHelper::EXTRA_ALIGN_DEFAULT, TabNavHelper::EXTRA_ALIGN_OPTIONS

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 included from TabNavHelper

#tab_nav_body_classes, #tab_nav_classes, #tab_nav_tab_classes

Methods included from TabbedComponentHelper

#before_render

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(label:, tag: TAG_DEFAULT, body_arguments: {}, **system_arguments) ⇒ TabNav

Returns a new instance of TabNav.

Examples:

Default with ‘<nav>`

@description
  `<nav>` is a landmark and should be reserved for main navigation links. See <%= link_to_accessibility %>.
@code
  <%= render(Yattho::Alpha::TabNav.new(label: "Default")) do |component| %>
    <% component.with_tab(selected: true, href: "#") { "Tab 1" } %>
    <% component.with_tab(href: "#") { "Tab 2" } %>
    <% component.with_tab(href: "#") { "Tab 3" } %>
  <% end %>

Default with ‘<div>`

<%= render(Yattho::Alpha::TabNav.new(label: "Default")) do |component| %>
  <% component.with_tab(selected: true, href: "#") { "Tab 1" } %>
  <% component.with_tab(href: "#") { "Tab 2" } %>
  <% component.with_tab(href: "#") { "Tab 3" } %>
<% end %>

With icons and counters

<%= render(Yattho::Alpha::TabNav.new(label: "With icons and counters")) do |component| %>
  <% component.with_tab(href: "#", selected: true) do |tab| %>
    <% tab.with_icon(icon: :star) %>
    <% tab.with_text { "Item 1" } %>
  <% end %>
  <% component.with_tab(href: "#") do |tab| %>
    <% tab.with_icon(icon: :star) %>
    <% tab.with_text { "Item 2" } %>
    <% tab.with_counter(count: 10) %>
  <% end %>
  <% component.with_tab(href: "#") do |tab| %>
    <% tab.with_text { "Item 3" } %>
    <% tab.with_counter(count: 10) %>
  <% end %>
<% end %>

With extra content

<%= render(Yattho::Alpha::TabNav.new(label: "With extra content")) do |component| %>
  <% component.with_tab(selected: true, href: "#") { "Tab 1" }%>
  <% component.with_tab(href: "#") { "Tab 2" } %>
  <% component.with_tab(href: "#") { "Tab 3" } %>
  <% component.with_extra do %>
    <%= render(Yattho::ButtonComponent.new(float: :right)) { "Button" } %>
  <% end %>
<% end %>

Adding extra content after the tabs

<%= render(Yattho::Alpha::TabNav.new(label: "Adding extra content after the tabs", display: :flex, body_arguments: { flex: 1 })) do |component| %>
  <% component.with_tab(selected: true, href: "#") { "Tab 1" }%>
  <% component.with_tab(href: "#") { "Tab 2" } %>
  <% component.with_tab(href: "#") { "Tab 3" } %>
  <% component.with_extra(align: :right) do %>
    <div>
      <%= render(Yattho::ButtonComponent.new) { "Button" } %>
    </div>
  <% end %>
<% end %>

Customizing the body

<%= render(Yattho::Alpha::TabNav.new(label: "Default", body_arguments: { classes: "custom-class", border: true, border_color: :accent_emphasis })) do |component| %>
  <% component.with_tab(selected: true, href: "#") { "Tab 1" }%>
  <% component.with_tab(href: "#") { "Tab 2" } %>
  <% component.with_tab(href: "#") { "Tab 3" } %>
<% end %>

Parameters:

  • tag (Symbol) (defaults to: TAG_DEFAULT)

    <%= one_of(Yattho::Alpha::TabNav::TAG_OPTIONS) %>

  • label (String)

    Sets an ‘aria-label` that helps assistive technology users understand the purpose of the links, and distinguish it from similar elements.

  • body_arguments (Hash) (defaults to: {})

    <%= link_to_system_arguments_docs %> for the body wrapper.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/components/yattho/alpha/tab_nav.rb', line 114

def initialize(label:, tag: TAG_DEFAULT, body_arguments: {}, **system_arguments)
  @align = EXTRA_ALIGN_DEFAULT
  @system_arguments = system_arguments
  @body_arguments = body_arguments

  @system_arguments[:tag] = fetch_or_fallback(TAG_OPTIONS, tag, TAG_DEFAULT)
  @system_arguments[:classes] = tab_nav_classes(system_arguments[:classes])

  @body_arguments[:tag] = BODY_TAG_DEFAULT
  @body_arguments[:classes] = tab_nav_body_classes(body_arguments[:classes])

  aria_label_for_page_nav(label)
end