Class: PhlexKit::AccordionTrigger

Inherits:
BaseComponent show all
Defined in:
app/components/phlex_kit/accordion/accordion_trigger.rb

Instance Method Summary collapse

Methods inherited from BaseComponent

#on

Constructor Details

#initialize(open: false, disabled: false, heading_level: 3, **attrs) ⇒ AccordionTrigger

Returns a new instance of AccordionTrigger.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
# File 'app/components/phlex_kit/accordion/accordion_trigger.rb', line 3

def initialize(open: false, disabled: false, heading_level: 3, **attrs)
  raise ArgumentError, "heading_level must be 1..6" unless (1..6).cover?(heading_level)
  @open = open
  @disabled = disabled
  @heading_level = heading_level
  @attrs = attrs
end

Instance Method Details

#view_templateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/components/phlex_kit/accordion/accordion_trigger.rb', line 10

def view_template(&)
  # APG accordion: each trigger is a button wrapped in a heading.
  # aria-expanded reflects the render-time open state; the controller keeps
  # it in sync and wires aria-controls to the content's id in connect().
  base = {
    type: "button", class: "pk-accordion-trigger",
    aria_expanded: @open ? "true" : "false", disabled: @disabled,
    data: { phlex_kit__accordion_target: "trigger", action: "click->phlex-kit--accordion#toggle" }
  }
  send(:"h#{@heading_level}", class: "pk-accordion-heading") do
    button(**mix(base, @attrs), &)
  end
end