Class: Bootstrap5Helper::Nav
- Defined in:
- lib/bootstrap5_helper/nav.rb
Overview
Builds a Nav Component that can be used in other components.
Instance Method Summary collapse
-
#dropdown(name, opts = {}, &block) ⇒ String
Creates a dropdown menu for the nav.
-
#initialize(template, *tag_or_options, &block) ⇒ Nav
constructor
Class constructor.
-
#item(target, opts = {}) ⇒ String
Adds an nav-item to the nav component.
-
#link(name = nil, options = nil, html_options = nil, &block) ⇒ String
Use this when the nav item is nothing more than a hyperlink.
-
#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) ⇒ Nav
Class constructor
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bootstrap5_helper/nav.rb', line 17 def initialize(template, *, &block) super(template) @tag, args = (*, {}) @tag ||= config({ navs: :base }, :ul) @id = args.fetch(:id, uuid) @class = args.fetch(:class, '') @data = args.fetch(:data, {}) @child = args.fetch(:child, {}) @content = block || proc { '' } @dropdown = Dropdown.new(@template) end |
Instance Method Details
#dropdown(name, opts = {}, &block) ⇒ String
Creates a dropdown menu for the nav.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/bootstrap5_helper/nav.rb', line 93 def dropdown(name, opts = {}, &block) id = opts.fetch(:id, nil) klass = opts.fetch(:class, '') data = opts.fetch(:data, {}).merge('bs-toggle' => 'dropdown') aria = opts.fetch(:aria, {}).merge(haspopup: true, expanded: false) data.merge!('bs-display' => 'static') if @child[:data]&.key?('bs-display') nav_item_wrapper id: id, class: 'dropdown', data: data do content_tag( :a, name, class: "nav-link dropdown-toggle #{klass}", href: '#', data: data, role: 'button', aria: aria ) + @dropdown.(opts, &block).to_s.html_safe end end |
#item(target, opts = {}) ⇒ String
Adds an nav-item to the nav component. this method gets used when the nav-item links to content in a tab or something.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/bootstrap5_helper/nav.rb', line 44 def item(target, opts = {}) id = opts.fetch(:id, nil) klass = opts.fetch(:class, '') data = opts.fetch(:data, {}) aria = opts.fetch(:aria, {}) nav_item_wrapper id: id, data: data do content_tag( :a, class: "nav-link #{klass}", href: "##{target}", tabindex: -1, data: @child[:data], aria: aria ) do block_given? ? yield : target.to_s.titleize end end end |
#link(name = nil, options = nil, html_options = nil, &block) ⇒ String
Use this when the nav item is nothing more than a hyperlink.
72 73 74 75 76 77 78 79 |
# File 'lib/bootstrap5_helper/nav.rb', line 72 def link(name = nil, = nil, = nil, &block) ||= {} [:class] = ([:class] || '') << ' nav-link' nav_item_wrapper do @template.link_to(name, , , &block) end end |
#to_s ⇒ String
String representation of the object.
119 120 121 122 123 |
# File 'lib/bootstrap5_helper/nav.rb', line 119 def to_s content_tag(@tag, id: @id, class: "nav #{@class}", data: @data) do @content.call(self) end end |