Class: MaquinaComponents::DropdownMenuHelper::DropdownMenuBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/maquina_components/dropdown_menu_helper.rb

Overview

Builder class for constructing dropdown menus

Instance Method Summary collapse

Constructor Details

#initialize(view_context) ⇒ DropdownMenuBuilder

Returns a new instance of DropdownMenuBuilder.



89
90
91
92
93
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 89

def initialize(view_context)
  @view = view_context
  @trigger_content = nil
  @content_block = nil
end

Instance Method Details

#content(align: :start, side: :bottom, width: :default, **options) { ... } ⇒ Object

Defines the menu content

Parameters:

  • align (Symbol) (defaults to: :start)

    Horizontal alignment

  • side (Symbol) (defaults to: :bottom)

    Which side to open

  • width (Symbol) (defaults to: :default)

    Width preset

  • options (Hash)

    Additional options

Yields:

  • Block containing menu items



114
115
116
117
118
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 114

def content(align: :start, side: :bottom, width: :default, **options, &block)
  @content_options = {align: align, side: side, width: width, **options}
  @content_builder = DropdownMenuContentBuilder.new(@view)
  @view.capture(@content_builder, &block)
end

#to_htmlObject

Generates the final HTML



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 121

def to_html
  parts = []

  if @trigger_content
    parts << @view.render(
      "components/dropdown_menu/trigger",
      **@trigger_options
    ) { @trigger_content }
  end

  if @content_builder
    parts << @view.render(
      "components/dropdown_menu/content",
      **@content_options
    ) { @content_builder.to_html }
  end

  @view.safe_join(parts)
end

#trigger(variant: :outline, size: :default, as_child: false, **options) { ... } ⇒ Object

Defines the trigger button

Parameters:

  • variant (Symbol) (defaults to: :outline)

    Button variant

  • size (Symbol) (defaults to: :default)

    Button size

  • as_child (Boolean) (defaults to: false)

    Whether to use custom trigger markup

  • options (Hash)

    Additional options

Yields:

  • Block for trigger content



102
103
104
105
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 102

def trigger(variant: :outline, size: :default, as_child: false, **options, &block)
  @trigger_options = {variant: variant, size: size, as_child: as_child, **options}
  @trigger_content = @view.capture(&block)
end