Class: UI::DropdownMenuTrigger

Inherits:
Phlex::HTML
  • Object
show all
Includes:
DropdownMenuTriggerBehavior, SharedAsChildBehavior
Defined in:
app/components/ui/dropdown_menu_trigger.rb

Overview

Trigger - Phlex implementation

Wrapper that adds toggle action to child element. Uses DropdownMenuTriggerBehavior concern for shared styling logic.

Supports asChild pattern for composition without wrapper elements.

Examples:

Basic usage

render UI::Trigger.new { button { "Open Menu" } }

With asChild - pass attributes to custom button

render UI::Trigger.new(as_child: true) do |trigger_attrs|
  render UI::Button.new(**trigger_attrs) { "Menu" }
end

Instance Method Summary collapse

Methods included from SharedAsChildBehavior

#merge_attributes

Methods included from DropdownMenuTriggerBehavior

#dropdown_menu_trigger_base_classes, #dropdown_menu_trigger_classes, #dropdown_menu_trigger_data_attributes, #dropdown_menu_trigger_html_attributes

Constructor Details

#initialize(as_child: false, classes: "", **attributes) ⇒ DropdownMenuTrigger

Returns a new instance of DropdownMenuTrigger.

Parameters:

  • as_child (Boolean) (defaults to: false)

    When true, yields attributes to block instead of rendering wrapper

  • classes (String) (defaults to: "")

    Additional CSS classes to merge

  • attributes (Hash)

    Additional HTML attributes



24
25
26
27
28
# File 'app/components/ui/dropdown_menu_trigger.rb', line 24

def initialize(as_child: false, classes: "", **attributes)
  @as_child = as_child
  @classes = classes
  @attributes = attributes
end

Instance Method Details

#view_template(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/components/ui/dropdown_menu_trigger.rb', line 30

def view_template(&block)
  trigger_attrs = dropdown_menu_trigger_html_attributes.deep_merge(@attributes)

  if @as_child
    # When as_child is true, only pass functional attributes (data, aria, tabindex, role)
    # The child component handles its own styling (following shadcn pattern)
    yield(trigger_attrs.except(:class)) if block_given?
  else
    # Default: render wrapper div with trigger behavior
    div(**trigger_attrs) do
      yield if block_given?
    end
  end
end