Class: DaisyUI::Dropdown

Inherits:
Base
  • Object
show all
Defined in:
lib/daisy_ui/dropdown.rb

Constant Summary collapse

PLACEMENT_MODIFIERS =

Placement modifiers that, in :popover mode, must live on the popover element itself (DaisyUI’s ‘position-area` rule is on `.dropdown`).

%i[start center end top bottom left right].freeze
DEFAULT_STIMULUS_IDENTIFIER =

Default Stimulus identifier for the opt-in popover controller. Namespaced so it never collides with a consumer app’s own ‘dropdown` controller.

"daisy-dropdown"

Constants inherited from Base

Base::BOOLS, Base::COLOR_MODIFIERS

Instance Method Summary collapse

Methods inherited from Base

inherited, register_modifiers

Constructor Details

#initialize(*modifiers, as: :div, id: nil, popover_id: nil, stimulus: false) ⇒ Dropdown

Returns a new instance of Dropdown.



17
18
19
20
21
22
23
24
# File 'lib/daisy_ui/dropdown.rb', line 17

def initialize(*modifiers, as: :div, id: nil, popover_id: nil, stimulus: false, **)
  @popover = modifiers.include?(:popover)
  @popover_id = popover_id if @popover
  # stimulus: true enables the controller; a String/Symbol enables AND
  # overrides the identifier. Only meaningful in :popover mode.
  @stimulus = stimulus
  super(*modifiers, as:, id:, **)
end

Instance Method Details

#button(*modifiers, **options) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/daisy_ui/dropdown.rb', line 46

def button(*modifiers, **options, &)
  if popover?
    render Button.new(*modifiers, as: :button, **popover_button_options(options), &)
  elsif tap_to_close?
    render Button.new(*modifiers, as: :summary, **options, &)
  else
    render Button.new(*modifiers, as: :div, role: :button, tabindex: 0, **options, &)
  end
end

#content(*modifiers, as: :div, **options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/daisy_ui/dropdown.rb', line 56

def content(*modifiers, as: :div, **options, &)
  return render_as(*modifiers, as:, **popover_menu_options(options), &) if popover?

  content_classes = component_classes("dropdown-content", options:)

  if tap_to_close?
    render_as(*modifiers, as:, class: content_classes, **options, &)
  else
    render_as(*modifiers, as:, tabindex: 0, class: content_classes, **options, &)
  end
end


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/daisy_ui/dropdown.rb', line 68

def menu(*modifiers, **options, &)
  if popover?
    options[:role] ||= "menu"
    return render Menu.new(*modifiers, **popover_menu_options(options), &)
  end

  menu_classes = component_classes("dropdown-content", options:)

  if tap_to_close?
    render Menu.new(*modifiers, class: menu_classes, **options, &)
  else
    render Menu.new(*modifiers, tabindex: 0, class: menu_classes, **options, &)
  end
end

#popover?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/daisy_ui/dropdown.rb', line 83

def popover?
  modifiers.include?(:popover)
end

#tap_to_close?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/daisy_ui/dropdown.rb', line 87

def tap_to_close?
  modifiers.include?(:tap_to_close)
end

#view_templateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/daisy_ui/dropdown.rb', line 26

def view_template
  if popover?
    # DaisyUI's popover variant is a FLAT structure: the trigger button and
    # the popover menu are siblings, and `dropdown` + the placement class
    # ride the popover element (that is what carries `position-area`). The
    # wrapper here is a plain, non-positioning container.
    public_send(as, **popover_root_attributes) do
      yield self if block_given?
    end
  elsif tap_to_close?
    details(class: classes, **attributes) do
      yield self if block_given?
    end
  else
    public_send(as, class: classes, **attributes) do
      yield self if block_given?
    end
  end
end