Class: PhlexKit::TabsTrigger

Inherits:
BaseComponent show all
Defined in:
app/components/phlex_kit/tabs/tabs_trigger.rb

Instance Method Summary collapse

Methods inherited from BaseComponent

#on

Constructor Details

#initialize(value:, as: :button, active: false, **attrs) ⇒ TabsTrigger

Returns a new instance of TabsTrigger.



3
4
5
6
7
8
# File 'app/components/phlex_kit/tabs/tabs_trigger.rb', line 3

def initialize(value:, as: :button, active: false, **attrs)
  @value = value
  @as = as.to_sym
  @active = active
  @attrs = attrs
end

Instance Method Details

#view_templateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/components/phlex_kit/tabs/tabs_trigger.rb', line 9

def view_template(&)
  # Deterministic ids pair each trigger with its panel (aria-controls /
  # aria-labelledby); the controller re-scopes them per instance in
  # connect() so multiple tab sets sharing a value can't collide.
  # aria-selected and the roving tabindex are kept in sync by the
  # controller's activeValueChanged.
  base = {
    class: "pk-tabs-trigger", role: "tab", id: "pk-tabs-trigger-#{@value}",
    aria_selected: @active ? "true" : "false",
    aria_controls: "pk-tabs-panel-#{@value}",
    tabindex: @active ? "0" : "-1",
    data: { phlex_kit__tabs_target: "trigger", action: "click->phlex-kit--tabs#show", value: @value }
  }
  if @as == :a
    a(**mix(base, @attrs), &)
  else
    button(**mix(base.merge(type: :button), @attrs), &)
  end
end