Class: CCS::Components::GovUK::Header::Navigation

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers
Defined in:
lib/ccs/components/govuk/header/navigation.rb

Overview

GOV.UK Header navigation

The header navigation section

Instance Method Summary collapse

Constructor Details

#initialize(navigation:, context:, menu_button: nil) ⇒ Navigation

Returns a new instance of Navigation.

Parameters:

  • navigation (Hash)

    options for the navigation

  • context (ActionView::Base)

    the view context

  • menu_button (Hash) (defaults to: nil)

    options for the menu button

Options Hash (navigation:):

  • :items (Array)

    an array of links for the navigation section. See Link#initialize for details of the items in the array.

  • :classes (String)

    additional CSS classes for the navigation HTML

  • :label (String)

    text for the aria-label attribute of the navigation. Defaults to the menu button text

Options Hash (menu_button:):

  • :text (String)

    text for the button that opens the mobile navigation menu. By default, this is set to Menu.

  • :label (String)

    text for the aria-label attribute of the button that opens the mobile navigation. Defaults to Show or hide menu.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ccs/components/govuk/header/navigation.rb', line 44

def initialize(navigation:, context:, menu_button: nil)
  menu_button ||= {}
  menu_button[:text] ||= 'Menu'

  menu_button[:aria] = { controls: 'navigation' }
  menu_button[:aria][:label] = menu_button[:label] if menu_button[:label]

  @menu_button = menu_button
  @navigation_links = navigation[:items].map { |navigation_link| Link.new(context: context, **navigation_link) }
  @navigation_label = navigation[:label] || menu_button[:text]
  @navigation_classes = "govuk-header__navigation #{navigation[:classes]}".rstrip
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Navigation

Returns:

  • (ActiveSupport::SafeBuffer)


61
62
63
64
65
66
67
68
# File 'lib/ccs/components/govuk/header/navigation.rb', line 61

def render
  tag.nav(aria: { label: navigation_label }, class: navigation_classes) do
    concat(button_tag(menu_button[:text], type: :button, class: 'govuk-header__menu-button govuk-js-header-toggle', aria: menu_button[:aria], hidden: true))
    concat(tag.ul(id: 'navigation', class: 'govuk-header__navigation-list') do
      navigation_links.each { |navigation_link| concat(navigation_link.render) }
    end)
  end
end