module BootstrapNavbar::Helpers::Bootstrap4
def navbar(options = {}, &block)
options = options.dup
container = options.key?(:container) ? options.delete(:container) : false
brand = if options[:brand]
prepare_html <<~HTML
#{options.delete(:brand)}
HTML
end
wrapper options do
if container
container container do
prepare_html <<~HTML
#{brand}
#{capture(&block) if block_given?}
HTML
end
else
prepare_html <<~HTML
#{brand}
#{capture(&block) if block_given?}
HTML
end
end
end
def navbar_collapse(options = {}, &block)
options = options.dup
options[:class] = [options[:class], 'collapse', 'navbar-collapse'].compact
options[:class] = options[:class].join(' ')
options[:id] ||= 'navbar-collapsable'
attributes = attributes_for_tag(options)
toggler_attributes = attributes_for_tag(
class: 'navbar-toggler',
type: 'button',
'data-toggle' => 'collapse',
'data-target' => "##{options[:id]}",
'aria-controls' => options[:id],
'aria-expanded' => false,
'aria-label' => 'Toggle navigation'
)
prepare_html <<~HTML
#{capture(&block) if block_given?}
HTML
end
def navbar_group(options = {}, &block)
options = options.dup
options[:class] = [options[:class], 'navbar-nav'].compact.join(' ')
attributes = attributes_for_tag(options)
prepare_html <<~HTML
#{capture(&block) if block_given?}
HTML
end
def navbar_item(text, url = nil, list_item_options = nil, link_options = nil, &block)
text, url, list_item_options, link_options = capture(&block), text, url, list_item_options if block_given?
url ||= '#'
list_item_options = list_item_options ? list_item_options.dup : {}
link_options = link_options ? link_options.dup : {}
list_item_options[:class] = [list_item_options[:class], 'nav-item'].compact.join(' ')
link_options[:class] = [link_options[:class], 'nav-link'].compact
link_options[:class] << 'active' if current_url_or_sub_url?(url)
link_options[:class] = link_options[:class].join(' ')
list_item_attributes = attributes_for_tag(list_item_options)
link_attributes = attributes_for_tag(link_options)
prepare_html <<~HTML
#{text}
HTML
end
def navbar_dropdown(text, id = '', list_item_options = {}, link_options = {}, &block)
list_item_options, link_options = list_item_options.dup, link_options.dup
list_item_options[:class] = [list_item_options[:class], 'nav-item', 'dropdown'].compact.join(' ')
wrapper_class = [*list_item_options.delete(:wrapper_class), 'dropdown-menu'].compact.join(' ')
list_item_attributes = attributes_for_tag(list_item_options)
link_options[:class] = [link_options[:class], 'nav-link', 'dropdown-toggle'].compact.join(' ')
id ||= "navbarDropdownMenuLink#{text}"
link_attributes = attributes_for_tag(link_options)
prepare_html <<~HTML
#{text}
#{capture(&block) if block_given?}
HTML
end
def navbar_dropdown_item(text, url = '#', link_options = {}, &block)
link_options = link_options.dup
link_options[:class] = [link_options[:class], 'dropdown-item'].compact
link_options[:class] << 'active' if current_url_or_sub_url?(url)
link_options[:class] = link_options[:class].join(' ')
link_attributes = attributes_for_tag(link_options)
prepare_html <<~HTML
#{text}
HTML
end
def navbar_dropdown_divider
''
end
private
def container(container, &block)
container_class = [
'container',
(container unless container == true)
].compact.join('-')
attributes = attributes_for_tag(class: container_class)
prepare_html <<~HTML
#{capture(&block) if block_given?}
HTML
end
def wrapper(options, &block)
options = options.dup
options[:class] = [options[:class], 'navbar'].compact
options[:class] << "navbar-#{options.key?(:color_scheme) ? options.delete(:color_scheme) : 'dark'}"
if bg = options.delete(:bg)
options[:class] << "bg-#{bg == true ? 'dark' : bg}"
end
if options.key?(:sticky) && options.delete(:sticky) === true
options[:class] << 'sticky-top'
elsif options.key?(:placement)
options[:class] << "fixed-#{options.delete(:placement)}"
end
options[:class] << "navbar-expand-#{options.delete(:expand_at) || 'sm'}"
options[:class] = options[:class].join(' ')
attributes = attributes_for_tag(options)
prepare_html <<~HTML
HTML
end
end