Class: Bootstrap5Helper::Nav

Inherits:
Component show all
Defined in:
lib/bootstrap5_helper/nav.rb

Overview

Builds a Nav Component that can be used in other components.

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) ⇒ Nav

Class constructor

Parameters:

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


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bootstrap5_helper/nav.rb', line 16

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

  @tag, args = parse_tag_or_options(*tag_or_options, {})
  @tag ||= config({ navs: :base }, :ul)

  @attrs    = args
  @id       = @attrs.delete(:id)       { uuid }
  @class    = @attrs.delete(:class)    { '' }
  @data     = @attrs.delete(:data)     { {} }
  @bs_attrs = @attrs.delete(:bs_attrs) { {} }
  @dropdown = Dropdown.new(@template)
  @content  = block || proc { '' }
end

Instance Method Details

Creates a dropdown menu for the nav.

Parameters:

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

Options Hash (opts):

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

Returns:

  • (String)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bootstrap5_helper/nav.rb', line 81

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)

  nav_item_wrapper(
    id:    id,
    class: 'dropdown',
    data:  (@bs_attrs[:data] || {}).merge(
      'bs-toggle'  => 'dropdown',
      'bs-display' => 'static'
    )
  ) do
    (
      :a,
      name,
      class: "nav-link dropdown-toggle #{klass}",
      href:  '#',
      data:  data,
      role:  'button',
      aria:  aria
    ) + @dropdown.menu(opts, &block).to_s.html_safe
  end
end

#item(target, opts = {}, &block) ⇒ 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.

Parameters:

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

Options Hash (opts):

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

Returns:

  • (String)


43
44
45
46
47
48
49
50
# File 'lib/bootstrap5_helper/nav.rb', line 43

def item(target, opts = {}, &block)
  case @tag
  when :nav
    nav_item_without_wrapper(target, opts, &block)
  when :ul
    nav_item_with_wrapper(target, opts, &block)
  end
end

Use this when the nav item is nothing more than a hyperlink.

Parameters:

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

Returns:

  • (String)


59
60
61
62
63
64
65
66
# File 'lib/bootstrap5_helper/nav.rb', line 59

def link(name = nil, options = nil, html_options = nil, &block)
  html_options ||= {}
  html_options[:class] = (html_options[:class] || '') << ' nav-link'

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

#to_sString

String representation of the object.

Returns:

  • (String)


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

def to_s
  (
    @tag,
    {
      id:    @id,
      class: "nav #{@class}",
      data:  @data
    }.merge(@attrs)
  ) do
    @content.call(self)
  end
end