Class: Bootstrap5Helper::Overlay::Menu

Inherits:
Component
  • Object
show all
Defined in:
lib/bootstrap5_helper/overlay/menu.rb

Overview

Builds a menu component for use in dropdowns.

Instance Method Summary collapse

Methods inherited from Component

#capture, #concat, #config, #content_tag, #parse_arguments, #parse_context_or_options, #parse_tag_or_options, #parse_text_or_options, #uuid

Constructor Details

#initialize(template, *tag_or_options, &block) ⇒ Menu

Class constructor

Parameters:

  • template (ActionView)
  • tag_or_options (Symbol|Hash)
  • opts (Hash)


16
17
18
19
20
21
22
23
24
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 16

def initialize(template, *tag_or_options, &block)
  super(template)

  @tag, opts = parse_tag_or_options(*tag_or_options, {})
  @id        = opts.fetch(:id,    uuid)
  @class     = opts.fetch(:class, '')
  @data      = opts.fetch(:data,  {})
  @content   = block || proc { '' }
end

Instance Method Details

#dividerString

Builds a divider element

Returns:

  • (String)


128
129
130
131
132
133
134
135
136
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 128

def divider
  nav_item_wrapper do
    (
      config({ overlay_menus: :divider }, :div),
      '',
      class: 'dropdown-divider'
    )
  end
end

#header(text, opts = {}, &block) ⇒ String

Builds a Header component

Parameters:

  • text (Symbol|String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:

  • (String)


112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 112

def header(text, opts = {}, &block)
  nav_item_wrapper do
    build_sub_component(
      config({ overlay_menus: :header }, :h6),
      text,
      'header',
      opts,
      &block
    )
  end
end

#item(target, opts = {}) ⇒ String

Use this method when you are using the item in the menu as trigger for something like tab content.

Parameters:

  • target (Symbol|String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)
  • :aria (Hash)

Returns:

  • (String)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 61

def item(target, opts = {})
  id    = opts.fetch(:id,    nil)
  klass = opts.fetch(:class, '')
  data  = opts.fetch(:data,  {}).merge('bs-toggle' => 'tab')
  aria  = opts.fetch(:aria,  {})

  nav_item_wrapper do
    (
      :a,
      id:    id,
      class: "dropdown-item #{klass}",
      href:  "##{target}",
      aria:  aria,
      data:  data
    ) do
      block_given? ? yield : target.to_s.titleize
    end
  end
end

Use this method when the menu ‘item` is nothing more than a hyperlink.

Parameters:

  • name (String) (defaults to: nil)
  • options (Hash) (defaults to: nil)
  • html_options (Hash) (defaults to: nil)

Returns:

  • (String)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 34

def link(name = nil, options = nil, html_options = nil, &block)
  if block_given?
    options ||= {}
    options[:class] = (options[:class] || '') << ' dropdown-item'
  else
    html_options ||= {}
    html_options[:class] = (html_options[:class] || '') << ' dropdown-item'
  end

  nav_item_wrapper do
    @template.link_to(name, options, html_options, &block)
  end
end

#text(text, opts = {}, &block) ⇒ String

Builds a Text component

Parameters:

  • text (Symbol|String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:

  • (String)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 91

def text(text, opts = {}, &block)
  nav_item_wrapper do
    build_sub_component(
      config({ overlay_menus: :text }, :span),
      text,
      'item-text',
      opts,
      &block
    )
  end
end

#to_sString

String representation of the object.

Returns:

  • (String)


142
143
144
145
146
147
148
149
150
151
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 142

def to_s
  (
    @tag || config({ overlay_menus: :base }, :div),
    id:    @id,
    class: "dropdown-menu #{@class}",
    data:  @data
  ) do
    @content.call(self)
  end
end