Class: Daisy::Navigation::TabsComponent

Inherits:
LocoMotion::BaseComponent show all
Defined in:
app/components/daisy/navigation/tabs_component.rb

Overview

Note:

When using radio button tabs, the titles must be simple strings and cannot contain HTML elements or icons. Radio tabs also keep the browser's native radio-group keyboard behavior: arrow keys move the selection immediately (automatic activation), unlike JS-driven tabs below, where arrows only move focus and Enter/Space selects.

Note:

Link-mode tabs render as <a> elements when given an href, and as <button> elements otherwise, so JavaScript-driven tabs remain focusable and keyboard-activatable.

Note:

Tabs that have a content panel but no href are automatically wired to the loco-tabs Stimulus controller, which implements the ARIA tabs pattern with manual activation: a roving tabindex keeps each tablist a single Tab stop, Left / Right / Home / End move focus between tabs, and Enter / Space (or a click) activates the focused tab. Register it once from the npm package: application.register("loco-tabs", TabsController). The attributes are inert if the controller is not registered.

Creates a tabbed navigation component that can be used either as links or radio buttons with associated content.

Defined Under Namespace

Classes: TabComponent

Constant Summary

Constants inherited from LocoMotion::BaseComponent

LocoMotion::BaseComponent::EMPTY_PART_IGNORED_TAGS, LocoMotion::BaseComponent::SELF_CLOSING_TAGS

Instance Attribute Summary collapse

Attributes inherited from LocoMotion::BaseComponent

#config, #loco_parent

Instance Method Summary collapse

Methods inherited from LocoMotion::BaseComponent

build, #component_ref, #config_option, #cssify, define_modifier, define_modifiers, define_part, define_parts, define_size, define_sizes, #empty_part_content, #inspect, #part, register_component_initializer, register_component_setup, #rendered_css, #rendered_data, #rendered_html, #rendered_stimulus_controllers, #rendered_tag_name, renders_many, renders_one, set_component_name, #set_loco_parent, #strip_spaces

Methods included from LocoMotion::Concerns::InspectableComponent

#build_inspect_string

Constructor Details

#initialize(*args, **kws, &block) ⇒ TabsComponent

Create a new instance of the TabsComponent.

Parameters:

  • args (Array) —

    Not used.

  • kws (Hash) —

    The keyword arguments for the component.

Options Hash (**kws):

  • name (String) —

    The name attribute for radio button tabs (default: auto-generated UUID).

  • radio (Boolean) —

    Whether to use radio buttons instead of links (default: false).

  • css (String) —

    Additional CSS classes for styling. Common options include:

    • Style: tabs-border, tabs-lift
    • Size: tabs-xl, tabs-lg, tabs-md (default), tabs-sm, tabs-xs
    • Width: w-full, w-[500px]


277
278
279
280
281
282
# File 'app/components/daisy/navigation/tabs_component.rb', line 277

def initialize(*args, **kws, &block)
  super

  @name = config_option(:name, "tabs-#{SecureRandom.uuid}")
  @radio = config_option(:radio, false)
end

Instance Attribute Details

#name ⇒ Object (readonly)

Returns the value of attribute name.



256
257
258
# File 'app/components/daisy/navigation/tabs_component.rb', line 256

def name
  @name
end

#radio ⇒ Object (readonly)

Returns the value of attribute radio.



256
257
258
# File 'app/components/daisy/navigation/tabs_component.rb', line 256

def radio
  @radio
end

Instance Method Details

#before_render ⇒ Object



284
285
286
287
288
289
290
291
292
# File 'app/components/daisy/navigation/tabs_component.rb', line 284

def before_render
  add_css(:component, "tabs")
  add_html(:component, { role: "tablist" })

  # Radio tabs switch natively; link/button tablists get the loco-tabs
  # Stimulus controller for JS-driven switching. The attributes are
  # inert until the app registers the controller.
  add_stimulus_controller(:component, "loco-tabs") unless radio?
end

#radio? ⇒ Boolean

Returns:

  • (Boolean)


294
295
296
# File 'app/components/daisy/navigation/tabs_component.rb', line 294

def radio?
  radio
end