Sha256: fd385cf7868a3f4e506c42eee0f530b968f7505e8f2b72c65fc7d37df4e65a1b

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

class FoxTail::CollapsibleTriggerComponent < FoxTail::TriggerBaseComponent
  has_option :open, default: false, type: :boolean

  def trigger_type
    options[:trigger_type] ||= :click
  end

  def before_render
    super

    html_attributes[:class] = classnames theme.classname("root.base"),
                                         open? && theme.classname("root.expanded"),
                                         !open? && theme.classname("root.collapsed"),
                                         html_class
  end

  def stimulus_controller_options
    super.merge open: open?,
                collapsed_classes: theme.classname("root.collapsed"),
                expanded_classes: theme.classname("root.expanded")
  end

  class StimulusController < FoxTail::StimulusController
    TRIGGER_TYPES = {
      click: {
        toggle: :click
      }
    }.freeze

    def collapsible_identifier
      FoxTail::CollapsibleComponent.stimulus_controller_identifier
    end

    def attributes(options = {})
      trigger_type = TRIGGER_TYPES[options[:trigger_type]&.to_sym]
      attributes = super options
      attributes[:data][classes_key(:collapsed)] = options[:collapsed_classes]
      attributes[:data][classes_key(:expanded)] = options[:expanded_classes]
      attributes[:data][outlet_key(collapsible_identifier)] = options[:selector]
      attributes[:data][:action] = build_actions(trigger_type)
      attributes[:aria] = { expanded: options[:opened], controls: extract_controls(options[:collapsible_selector]) }
      attributes
    end

    private

    def extract_controls(selector)
      selector = selector&.to_s
      selector[1..] if selector.present? && selector.start_with?("#") && !selector.match?(/\s/)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fox_tail-0.1.1 app/components/fox_tail/collapsible_trigger_component.rb
fox_tail-0.1.0 app/components/fox_tail/collapsible_trigger_component.rb