Module: MaquinaComponents::DropdownMenuHelper

Defined in:
app/helpers/maquina_components/dropdown_menu_helper.rb

Overview

DropdownMenu Helper

Provides a builder pattern for creating dropdown menus with a clean API.

Examples:

Basic usage

<%= dropdown_menu do |menu| %>
  <% menu.trigger { "Options" } %>
  <% menu.content do %>
    <% menu.item "Profile", href: profile_path %>
    <% menu.item "Settings", href: settings_path %>
  <% end %>
<% end %>

With icons and shortcuts

<%= dropdown_menu do |menu| %>
  <% menu.trigger variant: :ghost, size: :icon do %>
    <%= icon_for :more_horizontal %>
  <% end %>
  <% menu.content align: :end, width: :md do %>
    <% menu.label "Actions" %>
    <% menu.item "Edit", href: edit_path, icon: :pencil do |item| %>
      <% item.shortcut "⌘E" %>
    <% end %>
    <% menu.separator %>
    <% menu.item "Delete", href: delete_path, method: :delete, variant: :destructive, icon: :trash %>
  <% end %>
<% end %>

Simple data-driven menu

<%= dropdown_menu_simple "Actions", items: [
  { label: "Edit", href: edit_path, icon: :pencil },
  { label: "Delete", href: delete_path, method: :delete, destructive: true }
] %>

Defined Under Namespace

Classes: DropdownMenuBuilder, DropdownMenuContentBuilder, DropdownMenuItemBuilder

Instance Method Summary collapse

Instance Method Details

Renders a dropdown menu using the builder pattern

Parameters:

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

    Additional CSS classes for the root element

  • html_options (Hash)

    Additional HTML attributes

Yields:

Returns:

  • (String)

    Rendered HTML



45
46
47
48
49
50
51
52
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 45

def dropdown_menu(css_classes: "", **html_options, &block)
  builder = DropdownMenuBuilder.new(self)
  capture(builder, &block)

  render "components/dropdown_menu", css_classes: css_classes, **html_options do
    builder.to_html
  end
end

Renders a simple dropdown menu from data

Parameters:

  • trigger_text (String)

    Text for the trigger button

  • items (Array<Hash>)

    Array of item configurations

  • trigger_options (Hash) (defaults to: {})

    Options for the trigger button

  • content_options (Hash) (defaults to: {})

    Options for the content container

Returns:

  • (String)

    Rendered HTML



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 61

def dropdown_menu_simple(trigger_text, items:, trigger_options: {}, content_options: {})
  dropdown_menu do |menu|
    menu.trigger(**trigger_options) { trigger_text }

    menu.content(**content_options) do
      items.each do |item|
        if item[:separator]
          menu.separator
        elsif item[:label] && item[:href].nil? && item[:action].nil?
          menu.label item[:label]
        else
          variant = item[:destructive] ? :destructive : :default
          menu.item(
            item[:label],
            href: item[:href],
            method: item[:method],
            icon: item[:icon],
            variant: variant,
            disabled: item[:disabled]
          )
        end
      end
    end
  end
end