Class: CCS::Components::GovUK::ServiceNavigation::Navigation

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

Overview

GOV.UK Service navigation navigation

The service navigation navigation section

Instance Attribute Summary collapse

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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ccs/components/govuk/service_navigation/navigation.rb', line 48

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

  @navigation = { id: navigation[:id] || 'navigation' }

  menu_button[:aria] = { controls: @navigation[:id] }
  menu_button[:aria][:label] = menu_button[:label] if menu_button[:label] && menu_button[:label] != menu_button[:text]

  @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[:class] = "govuk-service-navigation__wrapper #{navigation[:classes]}".rstrip
end

Instance Attribute Details

Returns The classes for the navigation.

Returns:

  • (String)

    The classes for the navigation



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ccs/components/govuk/service_navigation/navigation.rb', line 22

class Navigation
  include ActionView::Context
  include ActionView::Helpers

  private

  attr_reader :navigation, :menu_button

  public

  # rubocop:disable Metrics/CyclomaticComplexity

  # @param navigation [Hash] options for the navigation
  # @param context [ActionView::Base] the view context
  # @param menu_button [Hash] options for the menu button
  #
  # @option navigation [Array] :items an array of links for the navigation section.
  #                            See {Components::GovUK::ServiceNavigation::Link#initialize Link#initialize} for details of the items in the array.
  # @option navigation [String] :classes additional CSS classes for the navigation HTML
  # @option navigation [String] :label text for the aria-label attribute of the navigation. Defaults to the menu button text
  #
  # @option menu_button [String] :text text for the button that opens the mobile navigation menu.
  #                                    By default, this is set to +Menu+.
  # @option menu_button [String] :label text for the aria-label attribute of the button that opens the mobile navigation.
  #                                     Defaults to +Show or hide menu+.

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

    @navigation = { id: navigation[:id] || 'navigation' }

    menu_button[:aria] = { controls: @navigation[:id] }
    menu_button[:aria][:label] = menu_button[:label] if menu_button[:label] && menu_button[:label] != menu_button[:text]

    @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[:class] = "govuk-service-navigation__wrapper #{navigation[:classes]}".rstrip
  end

  # rubocop:enable Metrics/CyclomaticComplexity

  # Generates the HTML for the GOV.UK Service Navigation navigation
  #
  # @return [ActiveSupport::SafeBuffer]

  def render
    tag.nav(aria: { label: navigation[:label] }, class: navigation[:class]) do
      concat(button_tag(menu_button[:text], type: :button, class: 'govuk-service-navigation__toggle govuk-js-service-navigation-toggle', aria: menu_button[:aria], hidden: true))
      concat(tag.ul(id: navigation[:id], class: 'govuk-service-navigation__list') do
        navigation[:links].each { |navigation_link| concat(navigation_link.render) }
      end)
    end
  end
end

Returns The id for the navigation.

Returns:

  • (String)

    The id for the navigation



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ccs/components/govuk/service_navigation/navigation.rb', line 22

class Navigation
  include ActionView::Context
  include ActionView::Helpers

  private

  attr_reader :navigation, :menu_button

  public

  # rubocop:disable Metrics/CyclomaticComplexity

  # @param navigation [Hash] options for the navigation
  # @param context [ActionView::Base] the view context
  # @param menu_button [Hash] options for the menu button
  #
  # @option navigation [Array] :items an array of links for the navigation section.
  #                            See {Components::GovUK::ServiceNavigation::Link#initialize Link#initialize} for details of the items in the array.
  # @option navigation [String] :classes additional CSS classes for the navigation HTML
  # @option navigation [String] :label text for the aria-label attribute of the navigation. Defaults to the menu button text
  #
  # @option menu_button [String] :text text for the button that opens the mobile navigation menu.
  #                                    By default, this is set to +Menu+.
  # @option menu_button [String] :label text for the aria-label attribute of the button that opens the mobile navigation.
  #                                     Defaults to +Show or hide menu+.

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

    @navigation = { id: navigation[:id] || 'navigation' }

    menu_button[:aria] = { controls: @navigation[:id] }
    menu_button[:aria][:label] = menu_button[:label] if menu_button[:label] && menu_button[:label] != menu_button[:text]

    @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[:class] = "govuk-service-navigation__wrapper #{navigation[:classes]}".rstrip
  end

  # rubocop:enable Metrics/CyclomaticComplexity

  # Generates the HTML for the GOV.UK Service Navigation navigation
  #
  # @return [ActiveSupport::SafeBuffer]

  def render
    tag.nav(aria: { label: navigation[:label] }, class: navigation[:class]) do
      concat(button_tag(menu_button[:text], type: :button, class: 'govuk-service-navigation__toggle govuk-js-service-navigation-toggle', aria: menu_button[:aria], hidden: true))
      concat(tag.ul(id: navigation[:id], class: 'govuk-service-navigation__list') do
        navigation[:links].each { |navigation_link| concat(navigation_link.render) }
      end)
    end
  end
end

Returns The aria label for the navigation.

Returns:

  • (String)

    The aria label for the navigation



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ccs/components/govuk/service_navigation/navigation.rb', line 22

class Navigation
  include ActionView::Context
  include ActionView::Helpers

  private

  attr_reader :navigation, :menu_button

  public

  # rubocop:disable Metrics/CyclomaticComplexity

  # @param navigation [Hash] options for the navigation
  # @param context [ActionView::Base] the view context
  # @param menu_button [Hash] options for the menu button
  #
  # @option navigation [Array] :items an array of links for the navigation section.
  #                            See {Components::GovUK::ServiceNavigation::Link#initialize Link#initialize} for details of the items in the array.
  # @option navigation [String] :classes additional CSS classes for the navigation HTML
  # @option navigation [String] :label text for the aria-label attribute of the navigation. Defaults to the menu button text
  #
  # @option menu_button [String] :text text for the button that opens the mobile navigation menu.
  #                                    By default, this is set to +Menu+.
  # @option menu_button [String] :label text for the aria-label attribute of the button that opens the mobile navigation.
  #                                     Defaults to +Show or hide menu+.

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

    @navigation = { id: navigation[:id] || 'navigation' }

    menu_button[:aria] = { controls: @navigation[:id] }
    menu_button[:aria][:label] = menu_button[:label] if menu_button[:label] && menu_button[:label] != menu_button[:text]

    @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[:class] = "govuk-service-navigation__wrapper #{navigation[:classes]}".rstrip
  end

  # rubocop:enable Metrics/CyclomaticComplexity

  # Generates the HTML for the GOV.UK Service Navigation navigation
  #
  # @return [ActiveSupport::SafeBuffer]

  def render
    tag.nav(aria: { label: navigation[:label] }, class: navigation[:class]) do
      concat(button_tag(menu_button[:text], type: :button, class: 'govuk-service-navigation__toggle govuk-js-service-navigation-toggle', aria: menu_button[:aria], hidden: true))
      concat(tag.ul(id: navigation[:id], class: 'govuk-service-navigation__list') do
        navigation[:links].each { |navigation_link| concat(navigation_link.render) }
      end)
    end
  end
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Service Navigation navigation

Returns:

  • (ActiveSupport::SafeBuffer)


69
70
71
72
73
74
75
76
# File 'lib/ccs/components/govuk/service_navigation/navigation.rb', line 69

def render
  tag.nav(aria: { label: navigation[:label] }, class: navigation[:class]) do
    concat(button_tag(menu_button[:text], type: :button, class: 'govuk-service-navigation__toggle govuk-js-service-navigation-toggle', aria: menu_button[:aria], hidden: true))
    concat(tag.ul(id: navigation[:id], class: 'govuk-service-navigation__list') do
      navigation[:links].each { |navigation_link| concat(navigation_link.render) }
    end)
  end
end