Class: Bootstrap5Helper::Overlay::Menu
- 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
-
#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
itemis 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
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 17 def initialize(template, *, &block) super(template) @tag, opts = (*, {}) @attrs = opts @id = @attrs.delete(:id) { uuid } @class = @attrs.delete(:class) { '' } @data = @attrs.delete(:data) { {} } @content = block || proc { '' } end |
Instance Method Details
#divider ⇒ String
Builds a divider element
126 127 128 129 130 131 132 133 134 |
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 126 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
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.
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 content_tag( :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 |
#link(name = nil, options = nil, html_options = nil, &block) ⇒ String
Use this method when the menu item is nothing more than a
hyperlink.
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, = 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
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_s ⇒ String
String representation of the object.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/bootstrap5_helper/overlay/menu.rb', line 140 def to_s content_tag( @tag || config({ overlay_menus: :base }, :div), { id: @id, class: "dropdown-menu #{@class}", data: @data }.merge(@attrs) ) do @content.call(self) end end |