Module: UI::DropdownMenuTriggerBehavior

Included in:
DropdownMenuTrigger, DropdownMenuTriggerComponent
Defined in:
app/behaviors/ui/dropdown_menu_trigger_behavior.rb

Overview

DropdownMenuTriggerBehavior

Shared behavior for DropdownMenuTrigger component across ERB, ViewComponent, and Phlex implementations.

Instance Method Summary collapse

Instance Method Details

Base classes for trigger (focus styles)



34
35
36
# File 'app/behaviors/ui/dropdown_menu_trigger_behavior.rb', line 34

def dropdown_menu_trigger_base_classes
  "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 rounded-sm"
end

Returns combined CSS classes for the trigger



25
26
27
28
29
30
31
# File 'app/behaviors/ui/dropdown_menu_trigger_behavior.rb', line 25

def dropdown_menu_trigger_classes
  classes_value = respond_to?(:classes, true) ? classes : @classes
  TailwindMerge::Merger.new.merge([
    dropdown_menu_trigger_base_classes,
    classes_value
  ].compact.join(" "))
end

Returns data attributes for Stimulus



39
40
41
42
43
44
45
# File 'app/behaviors/ui/dropdown_menu_trigger_behavior.rb', line 39

def dropdown_menu_trigger_data_attributes
  attributes_value = respond_to?(:attributes, true) ? attributes : @attributes
  (attributes_value&.fetch(:data, {}) || {}).merge({
    "ui--dropdown-target": "trigger",
    action: "click->ui--dropdown#toggle keydown.enter->ui--dropdown#toggle keydown.space->ui--dropdown#toggle"
  })
end

Returns HTML attributes for the trigger When as_child is true, only data attributes are returned to avoid overriding child’s classes



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/behaviors/ui/dropdown_menu_trigger_behavior.rb', line 9

def dropdown_menu_trigger_html_attributes
  attrs = {
    data: dropdown_menu_trigger_data_attributes,
    tabindex: "0",
    role: "button",
    "aria-haspopup": "menu"
  }

  # Only include class if it's not empty (to avoid overriding child component's classes)
  trigger_classes = dropdown_menu_trigger_classes
  attrs[:class] = trigger_classes unless trigger_classes.blank?

  attrs
end