Class: DaisyUI::Dropdown

Inherits:
BaseComponent show all
Defined in:
app/components/daisy_ui/actions/dropdown.rb

Overview

Dropdown component implementing DaisyUI’s dropdown styles

Examples:

Basic usage with text trigger

<%= render(DropdownComponent.new) do |d| %>
  <% d.with_trigger(text: "Click me") %>
  <% d.with_item(href: "#") { "Item 1" } %>
  <% d.with_item(href: "#") { "Item 2" } %>
<% end %>

With custom trigger content

<%= render(DropdownComponent.new) do |d| %>
  <% d.with_trigger do %>
    <%= helpers.cog_icon("h-5 w-5") %> Settings
  <% end %>
  <% d.with_item(href: "#") { "Item 1" } %>
  <% d.with_item(href: "#") { "Item 2" } %>
  <% d.with_divider %>
  <% d.with_item(href: "#", class: "text-error") { "Delete" } %>
<% end %>

Constant Summary collapse

POSITIONS =

Available dropdown positions from DaisyUI

{
  top: 'dropdown-top',
  top_end: 'dropdown-top-end',
  top_center: 'dropdown-top-center',
  bottom: 'dropdown-bottom',
  bottom_end: 'dropdown-bottom-end',
  bottom_center: 'dropdown-bottom-center',
  left: 'dropdown-left',
  left_end: 'dropdown-left-end',
  left_center: 'dropdown-left-center',
  right: 'dropdown-right',
  right_end: 'dropdown-right-end',
  right_center: 'dropdown-right-center'
}.freeze
ALIGNMENTS =
{
  start: 'dropdown-start',
  end: 'dropdown-end',
  center: 'dropdown-center'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(position: nil, hover: false, open: false, align: nil, menu_class: nil, menu_tabindex: 0, **system_arguments) ⇒ Dropdown

Returns a new instance of Dropdown.

Parameters:

  • position (Symbol) (defaults to: nil)

    Position of the dropdown content relative to the trigger

  • hover (Boolean, String) (defaults to: false)

    When true or ‘content’, opens the dropdown on hover instead of click

  • open (Boolean) (defaults to: false)

    When true, forces the dropdown to stay open

  • align (Symbol) (defaults to: nil)

    When :start, :end, or :center, aligns the dropdown content

  • menu_class (String) (defaults to: nil)

    Additional classes for the menu

  • menu_tabindex (Integer, nil) (defaults to: 0)

    Tabindex for the menu (defaults to 0)

  • system_arguments (Hash)

    Additional HTML attributes



69
70
71
72
73
74
75
76
77
78
# File 'app/components/daisy_ui/actions/dropdown.rb', line 69

def initialize(position: nil, hover: false, open: false, align: nil,
               menu_class: nil, menu_tabindex: 0, **system_arguments)
  @position = build_argument(position, POSITIONS, 'position')
  @hover = hover
  @open = open
  @align = build_argument(align, ALIGNMENTS, 'align')
  @menu_class = menu_class
  @menu_tabindex = menu_tabindex
  super(**system_arguments)
end

Instance Method Details

#callObject



80
81
82
83
84
85
86
87
# File 'app/components/daisy_ui/actions/dropdown.rb', line 80

def call
  tag.div(**dropdown_arguments) do
    safe_join([
      trigger,
      render_menu
    ].compact)
  end
end