Class: Primer::UnderlineNavComponent

Inherits:
Component
  • Object
show all
Includes:
TabbedComponentHelper
Defined in:
app/components/primer/underline_nav_component.rb

Overview

Use the UnderlineNav component to style navigation with a minimal underlined selected state, typically used for navigation placed at the top of the page.

Constant Summary collapse

ALIGN_DEFAULT =
:left
ALIGN_OPTIONS =
[ALIGN_DEFAULT, :right].freeze
BODY_TAG_DEFAULT =
:div
BODY_TAG_OPTIONS =
[BODY_TAG_DEFAULT, :ul].freeze

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 TabbedComponentHelper

#before_render

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:, with_panel: false, align: ALIGN_DEFAULT, body_arguments: { tag: BODY_TAG_DEFAULT }, **system_arguments) ⇒ UnderlineNavComponent

Returns a new instance of UnderlineNavComponent.

Examples:

Default

<%= render(Primer::UnderlineNavComponent.new(label: "Default")) do |component| %>
  <% component.tab(href: "#", selected: true) { "Item 1" } %>
  <% component.tab(href: "#") { "Item 2" } %>
  <% component.actions do %>
    <%= render(Primer::ButtonComponent.new) { "Button!" } %>
  <% end %>
<% end %>

With icons and counters

<%= render(Primer::UnderlineNavComponent.new(label: "With icons and counters")) do |component| %>
  <% component.tab(href: "#", selected: true) do |t| %>
    <% t.icon(icon: :star) %>
    <% t.text { "Item 1" } %>
  <% end %>
  <% component.tab(href: "#") do |t| %>
    <% t.icon(icon: :star) %>
    <% t.text { "Item 2" } %>
    <% t.counter(count: 10) %>
  <% end %>
  <% component.tab(href: "#") do |t| %>
    <% t.text { "Item 3" } %>
    <% t.counter(count: 10) %>
  <% end %>
  <% component.actions do %>
    <%= render(Primer::ButtonComponent.new) { "Button!" } %>
  <% end %>
<% end %>

Align right

<%= render(Primer::UnderlineNavComponent.new(label: "Align right", align: :right)) do |component| %>
  <% component.tab(href: "#", selected: true) do |t| %>
    <% t.text { "Item 1" } %>
  <% end %>
  <% component.tab(href: "#") do |t| %>
    <% t.text { "Item 2" } %>
  <% end %>
  <% component.actions do %>
    <%= render(Primer::ButtonComponent.new) { "Button!" } %>
  <% end %>
<% end %>

As a list

<%= render(Primer::UnderlineNavComponent.new(label: "As a list", body_arguments: { tag: :ul })) do |component| %>
  <% component.tab(href: "#", selected: true) do |t| %>
    <% t.text { "Item 1" } %>
  <% end %>
  <% component.tab(href: "#") do |t| %>
    <% t.text { "Item 2" } %>
  <% end %>
  <% component.actions do %>
    <%= render(Primer::ButtonComponent.new) { "Button!" } %>
  <% end %>
<% end %>

With panels

<%= render(Primer::UnderlineNavComponent.new(label: "With panels", with_panel: true)) do |component| %>
  <% component.tab(selected: true) do |t| %>
    <% t.text { "Item 1" } %>
    <% t.panel do %>
      Panel 1
    <% end %>
  <% end %>
  <% component.tab do |t| %>
    <% t.text { "Item 2" } %>
    <% t.panel do %>
      Panel 2
    <% end %>
  <% end %>
  <% component.actions do %>
    <%= render(Primer::ButtonComponent.new) { "Button!" } %>
  <% end %>
<% end %>

Parameters:

  • label (String)

    The ‘aria-label` on top level `<nav>` element.

  • with_panel (Boolean) (defaults to: false)

    Whether the TabNav should navigate through pages or panels.

  • align (Symbol) (defaults to: ALIGN_DEFAULT)

    <%= one_of(Primer::UnderlineNavComponent::ALIGN_OPTIONS) %> - Defaults to <%= Primer::UnderlineNavComponent::ALIGN_DEFAULT %>

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

    <%= link_to_system_arguments_docs %> for the body wrapper.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/components/primer/underline_nav_component.rb', line 124

def initialize(label:, with_panel: false, align: ALIGN_DEFAULT, body_arguments: { tag: BODY_TAG_DEFAULT }, **system_arguments)
  @with_panel = with_panel
  @align = fetch_or_fallback(ALIGN_OPTIONS, align, ALIGN_DEFAULT)

  @system_arguments = system_arguments
  @system_arguments[:tag] = navigation_tag(with_panel)
  @system_arguments[:classes] = class_names(
    @system_arguments[:classes],
    "UnderlineNav",
    "UnderlineNav--right" => @align == :right
  )

  @body_arguments = body_arguments
  @body_tag = fetch_or_fallback(BODY_TAG_OPTIONS, body_arguments[:tag]&.to_sym, BODY_TAG_DEFAULT)

  @body_arguments[:tag] = @body_tag
  @body_arguments[:classes] = class_names(
    "UnderlineNav-body",
    @body_arguments[:classes],
    "list-style-none" => list?
  )

  if with_panel
    @body_arguments[:role] = :tablist
    @body_arguments[:"aria-label"] = label
  else
    @system_arguments[:"aria-label"] = label
  end
end