Class: UI::DropdownMenuComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
DropdownMenuBehavior, SharedAsChildBehavior
Defined in:
app/view_components/ui/dropdown_menu_component.rb

Overview

DropdownMenuComponent - ViewComponent implementation

Container for dropdown menus with Stimulus controller for interactivity. Uses DropdownMenuBehavior concern for shared styling logic.

Supports asChild pattern for composition without wrapper elements.

Examples:

Basic usage

render UI::DropdownMenuComponent.new do
  render UI::TriggerComponent.new { "Open" }
  render UI::ContentComponent.new { "Items" }
end

With asChild (no wrapper div)

render UI::DropdownMenuComponent.new(as_child: true) do |dropdown_attrs|
  # receive dropdown data attrs and pass them to trigger
end

Instance Method Summary collapse

Methods included from SharedAsChildBehavior

#merge_attributes

Methods included from DropdownMenuBehavior

#dropdown_menu_classes, #dropdown_menu_data_attributes, #dropdown_menu_html_attributes

Constructor Details

#initialize(as_child: false, placement: "bottom-start", offset: 4, flip: true, classes: "", **attributes) ⇒ DropdownMenuComponent

Returns a new instance of DropdownMenuComponent.



24
25
26
27
28
29
30
31
# File 'app/view_components/ui/dropdown_menu_component.rb', line 24

def initialize(as_child: false, placement: "bottom-start", offset: 4, flip: true, classes: "", **attributes)
  @as_child = as_child
  @placement = placement
  @offset = offset
  @flip = flip
  @classes = classes
  @attributes = attributes
end

Instance Method Details

#callObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/view_components/ui/dropdown_menu_component.rb', line 33

def call
  dropdown_attrs = dropdown_menu_html_attributes.deep_merge(@attributes)

  if @as_child
    # Yield attributes to block - child receives controller setup
    content_for(:dropdown_attrs, dropdown_attrs)
    content
  else
    # Default: render wrapper div with controller
     :div, **dropdown_attrs do
      content
    end
  end
end

Provide dropdown attributes to the block



49
50
51
# File 'app/view_components/ui/dropdown_menu_component.rb', line 49

def dropdown_attrs
  dropdown_menu_html_attributes.deep_merge(@attributes)
end