Class: Bootstrap4Helper::Nav

Inherits:
Component show all
Defined in:
lib/bootstrap4_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, #uuid

Constructor Details

#initialize(template, opts = {}, &block) ⇒ Nav

Class constructor

Parameters:

  • template (ActionView)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

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


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bootstrap4_helper/nav.rb', line 15

def initialize(template, opts = {}, &block)
  super(template)

  @id       = opts.fetch(:id,    uuid)
  @class    = opts.fetch(:class, '')
  @data     = opts.fetch(:data,  {})
  @child    = opts.fetch(:child, {})
  @content  = block || proc { '' }

  @dropdown = Dropdown.new(@template)
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)

Returns:

  • (String)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bootstrap4_helper/nav.rb', line 84

def dropdown(name, opts = {}, &block)
  id    = opts.fetch(:id,    nil)
  klass = opts.fetch(:class, '')
  data  = opts.fetch(:data,  {})
  aria  = opts.fetch(:aria,  {}).merge(haspopup: true, expanded: false)

   :li, id: id, class: 'nav-item dropdown', data: data do
    (
      :a,
      name,
      class: "nav-link dropdown-toggle #{klass}",
      href:  '#',
      data:  { toggle: 'dropdown' },
      role:  'button',
      aria:  aria
    ) + @dropdown.menu(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.

Parameters:

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

Options Hash (opts):

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

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bootstrap4_helper/nav.rb', line 40

def item(target, opts = {})
  id    = opts.fetch(:id,    nil)
  klass = opts.fetch(:class, '')
  data  = opts.fetch(:data,  {})
  aria  = opts.fetch(:aria,  {})

   :li, id: id, class: 'nav-item', data: data do
    (
      :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

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)


68
69
70
71
72
# File 'lib/bootstrap4_helper/nav.rb', line 68

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

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

#to_sString

String representation of the object.

Returns:

  • (String)


107
108
109
110
111
# File 'lib/bootstrap4_helper/nav.rb', line 107

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