Class: ElementComponent::Components::Dropdown

Inherits:
Element
  • Object
show all
Defined in:
lib/element_component/components/dropdown.rb

Constant Summary collapse

VALID_DIRECTIONS =
%i[dropup dropend dropstart].freeze

Instance Attribute Summary

Attributes inherited from Element

#attributes, #contents, #element, #html

Instance Method Summary collapse

Methods inherited from Element

#add_attribute, #add_attribute!, #add_content, #add_content!, #new_element, #remove_attribute, #remove_attribute_value, #render, #reset_attributes!, #reset_contents!

Constructor Details

#initialize(direction: nil, **attributes, &block) ⇒ Dropdown

Returns a new instance of Dropdown.



13
14
15
16
17
18
19
# File 'lib/element_component/components/dropdown.rb', line 13

def initialize(direction: nil, **attributes, &block)
  super("div", &block)

  add_attribute(class: "dropdown")
  add_attribute(class: direction.to_s) if direction
  add_attribute(attributes) unless attributes.empty?
end

Instance Method Details

#toggle_button(label: "Dropdown", variant: :secondary, split: false, **btn_attributes, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/element_component/components/dropdown.rb', line 21

def toggle_button(label: "Dropdown", variant: :secondary, split: false, **btn_attributes, &block)
  if split
    split_btn = Button.new(variant: variant, class: "dropdown-toggle dropdown-toggle-split",
                           **btn_attributes)
    split_btn.add_content(
      Element.new("span", class: "visually-hidden") { add_content("Toggle dropdown") }
    )
    add_content(ButtonGroup.new do
      add_content(Button.new(variant: variant, **btn_attributes) { add_content(label) })
      add_content(split_btn)
    end)
  else
    btn = Element.new("button", "aria-expanded": "false", "data-bs-toggle": "dropdown",
                                class: "btn btn-#{variant} dropdown-toggle", type: "button", **btn_attributes)
    btn.add_content(label) if label
    instance_eval(&block) if block
    add_content(btn)
  end
  self
end