Class: Bootstrap5Helper::Overlay::Menu

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

Overview

rubocop:disable Metrics/ClassLength 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)


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

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

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

Instance Method Details

#dividerString

Builds a divider element

Returns:

  • (String)


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

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)


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

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
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 61

def item(target, opts = {})
  attrs = opts
  klass = attrs.delete(:class) { '' }
  data  = attrs.delete(:data)  { {} }

  nav_item_wrapper do
    (
      :a,
      {
        class: "dropdown-item #{klass}",
        href:  "##{target}",
        data:  data.merge('bs-toggle' => 'tab')
      }.merge(attrs)
    ) 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)


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

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)


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

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)


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

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