Class: UI::DropdownMenu

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

Overview

DropdownMenu - Phlex 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::DropdownMenu.new do
  render UI::Trigger.new { button { "Open" } }
  render UI::Content.new { "Items here" }
end

With asChild (no wrapper div)

render UI::DropdownMenu.new(as_child: true) do |dropdown_attrs|
  # DropdownMenu yields data attributes to first child
  render UI::Trigger.new(**dropdown_attrs, as_child: true) do |trigger_attrs|
    render UI::Button.new(**trigger_attrs) { "Menu" }
  end
  render UI::Content.new { "Items" }
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) ⇒ DropdownMenu

Returns a new instance of DropdownMenu.

Parameters:

  • as_child (Boolean) (defaults to: false)

    When true, yields attributes to block instead of rendering wrapper

  • placement (String) (defaults to: "bottom-start")

    Floating UI placement (bottom-start, bottom-end, right-start, etc.)

  • offset (Integer) (defaults to: 4)

    Offset from trigger in pixels

  • flip (Boolean) (defaults to: true)

    Enable/disable flip middleware

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

    Additional CSS classes to merge

  • attributes (Hash)

    Additional HTML attributes



34
35
36
37
38
39
40
41
# File 'app/components/ui/dropdown_menu.rb', line 34

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

#view_template(&block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/ui/dropdown_menu.rb', line 43

def view_template(&block)
  dropdown_attrs = dropdown_menu_html_attributes.deep_merge(@attributes)

  if @as_child
    # Yield data attributes to block - child receives controller setup
    yield(dropdown_attrs) if block_given?
  else
    # Default: render wrapper div with controller
    div(**dropdown_attrs) do
      yield if block_given?
    end
  end
end