Class: Bootstrap5Helper::Overlay::Menu
- Defined in:
- lib/bootstrap5_helper/overlay/menu.rb
Overview
Builds a menu component for use in dropdowns.
Instance Method Summary collapse
-
#divider ⇒ String
Builds a divider element.
-
#header(text, opts = {}, &block) ⇒ String
Builds a Header component.
-
#initialize(template, *tag_or_options, &block) ⇒ Menu
constructor
Class constructor.
-
#item(target, opts = {}) ⇒ String
Use this method when you are using the item in the menu as trigger for something like tab content.
-
#link(name = nil, options = nil, html_options = nil, &block) ⇒ String
Use this method when the menu ‘item` is nothing more than a hyperlink.
-
#text(text, opts = {}, &block) ⇒ String
Builds a Text component.
-
#to_s ⇒ String
String representation of the object.
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
16 17 18 19 20 21 22 23 24 |
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 16 def initialize(template, *, &block) super(template) @tag, opts = (*, {}) @id = opts.fetch(:id, uuid) @class = opts.fetch(:class, '') @data = opts.fetch(:data, {}) @content = block || proc { '' } end |
Instance Method Details
#divider ⇒ String
Builds a divider element
128 129 130 131 132 133 134 135 136 |
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 128 def divider nav_item_wrapper do content_tag( config({ overlay_menus: :divider }, :div), '', class: 'dropdown-divider' ) end end |
#header(text, opts = {}, &block) ⇒ String
Builds a Header component
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.
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 content_tag( :a, id: id, class: "dropdown-item #{klass}", href: "##{target}", aria: aria, data: data ) do block_given? ? yield : target.to_s.titleize end end end |
#link(name = nil, options = nil, html_options = nil, &block) ⇒ String
Use this method when the menu ‘item` is nothing more than a hyperlink.
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, = nil, = nil, &block) if block_given? ||= {} [:class] = ([:class] || '') << ' dropdown-item' else ||= {} [:class] = ([:class] || '') << ' dropdown-item' end nav_item_wrapper do @template.link_to(name, , , &block) end end |
#text(text, opts = {}, &block) ⇒ String
Builds a Text component
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_s ⇒ String
String representation of the object.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 142 def to_s content_tag( @tag || config({ overlay_menus: :base }, :div), id: @id, class: "dropdown-menu #{@class}", data: @data ) do @content.call(self) end end |