class Bootstrap4Helper::Nav

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

Public Class Methods

new(template, opts = {}, &block) click to toggle source

Class constructor

@param [ActionView] template @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @option opts [Hash] :child

Calls superclass method Bootstrap4Helper::Component::new
# 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

Public Instance Methods

dropdown(name, opts = {}, &block) click to toggle source

Creates a dropdown menu for the nav.

@param [NilClass|Symbol|String] name @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @option opts [Hash] :aria @return [String]

item(target, opts = {}) { |: target.titleize| ... } click to toggle source

Adds an nav-item to the nav component. this method gets used when the nav-item links to content in a tab or something.

@param [Symbol|String] target @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @option opts [Hash] :aria @return [String]

# 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,  {})

  content_tag :li, id: id, class: 'nav-item', 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
to_s() click to toggle source

String representation of the object.

@return [String]

# File lib/bootstrap4_helper/nav.rb, line 107
def to_s
  content_tag :ul, id: @id, class: "nav #{@class}" do
    @content.call(self)
  end
end