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

Inherits:
Base
  • Object
show all
Defined in:
lib/ccs/components/govuk/service_navigation.rb,
lib/ccs/components/govuk/service_navigation/link.rb,
lib/ccs/components/govuk/service_navigation/navigation.rb

Overview

GOV.UK Service Navigation

This is used to generate the service navigation component from the GDS - Components - Service navigation

Defined Under Namespace

Classes: Link, Navigation

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the service navigation

{ class: 'govuk-service-navigation', data: { module: 'govuk-service-navigation' } }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(navigation: nil, menu_button: nil, service: nil, **options) ⇒ ServiceNavigation

Returns a new instance of ServiceNavigation.

Parameters:

  • navigation (Hash) (defaults to: nil)

    options for the navigation section of the service navigation. See Navigation#initialize for details of the options.

  • menu_button (Hash) (defaults to: nil)

    options for the menu button in the service navigation. See Navigation#initialize for details of the options.

  • service (Hash) (defaults to: nil)

    options for the service name

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (service:):

  • :name (String)

    the name of the service, included in the service navigation

  • :href (String)

    URL for the service name anchor

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the service navigation HTML

  • :attributes (Hash)

    additional attributes that will added as part of the service navigation HTML



37
38
39
40
41
42
43
44
# File 'lib/ccs/components/govuk/service_navigation.rb', line 37

def initialize(navigation: nil, menu_button: nil, service: nil, **options)
  super(**options)

  (@options[:attributes][:aria] ||= {})[:label] ||= 'Service information' if service

  @navigation = Navigation.new(navigation: navigation, menu_button: menu_button, context: @context) if navigation && navigation[:items].present?
  @service = service
end

Instance Attribute Details

#hrefString (readonly)

Returns The href for the service navigation.

Returns:

  • (String)

    The href for the service navigation



17
18
19
20
21
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
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ccs/components/govuk/service_navigation.rb', line 17

class ServiceNavigation < Base
  private

  attr_reader :navigation, :service

  public

  # @param navigation [Hash] options for the navigation section of the service navigation.
  #                          See {Components::GovUK::ServiceNavigation::Navigation#initialize Navigation#initialize} for details of the options.
  # @param menu_button [Hash] options for the menu button in the service navigation.
  #                           See {Components::GovUK::ServiceNavigation::Navigation#initialize Navigation#initialize} for details of the options.
  # @param service [Hash] options for the service name
  # @param options [Hash] options that will be used in customising the HTML
  #
  # @option service [String] :name the name of the service, included in the service navigation
  # @option service [String] :href URL for the service name anchor
  #
  # @option options [String] :classes additional CSS classes for the service navigation HTML
  # @option options [Hash] :attributes additional attributes that will added as part of the service navigation HTML

  def initialize(navigation: nil, menu_button: nil, service: nil, **options)
    super(**options)

    (@options[:attributes][:aria] ||= {})[:label] ||= 'Service information' if service

    @navigation = Navigation.new(navigation: navigation, menu_button: menu_button, context: @context) if navigation && navigation[:items].present?
    @service = service
  end

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

  def render
    if service
      tag.section(inner_content, **options[:attributes])
    else
      tag.div(inner_content, **options[:attributes])
    end
  end

  # The default attributes for the service navigation

  DEFAULT_ATTRIBUTES = { class: 'govuk-service-navigation', data: { module: 'govuk-service-navigation' } }.freeze

  private

  # Generates the HTML for the inner content of the GOV.UK Service navigation component
  #
  # @return [ActiveSupport::SafeBuffer]

  def inner_content
    tag.div(class: 'govuk-width-container') do
      tag.div(class: 'govuk-service-navigation__container') do
        concat(tag.span(service_navigation_service_name, class: 'govuk-service-navigation__service-name')) if service
        concat(navigation.render) if navigation
      end
    end
  end

  # Generates the service name section
  #
  # @return [ActiveSupport::SafeBuffer]

  def service_navigation_service_name
    if service[:href]
      link_to(service[:name], service[:href], class: 'govuk-service-navigation__link')
    else
      tag.span(service[:name], class: 'govuk-service-navigation__text')
    end
  end
end

#textString (readonly)

Returns Text for the service navigation.

Returns:

  • (String)

    Text for the service navigation



17
18
19
20
21
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
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ccs/components/govuk/service_navigation.rb', line 17

class ServiceNavigation < Base
  private

  attr_reader :navigation, :service

  public

  # @param navigation [Hash] options for the navigation section of the service navigation.
  #                          See {Components::GovUK::ServiceNavigation::Navigation#initialize Navigation#initialize} for details of the options.
  # @param menu_button [Hash] options for the menu button in the service navigation.
  #                           See {Components::GovUK::ServiceNavigation::Navigation#initialize Navigation#initialize} for details of the options.
  # @param service [Hash] options for the service name
  # @param options [Hash] options that will be used in customising the HTML
  #
  # @option service [String] :name the name of the service, included in the service navigation
  # @option service [String] :href URL for the service name anchor
  #
  # @option options [String] :classes additional CSS classes for the service navigation HTML
  # @option options [Hash] :attributes additional attributes that will added as part of the service navigation HTML

  def initialize(navigation: nil, menu_button: nil, service: nil, **options)
    super(**options)

    (@options[:attributes][:aria] ||= {})[:label] ||= 'Service information' if service

    @navigation = Navigation.new(navigation: navigation, menu_button: menu_button, context: @context) if navigation && navigation[:items].present?
    @service = service
  end

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

  def render
    if service
      tag.section(inner_content, **options[:attributes])
    else
      tag.div(inner_content, **options[:attributes])
    end
  end

  # The default attributes for the service navigation

  DEFAULT_ATTRIBUTES = { class: 'govuk-service-navigation', data: { module: 'govuk-service-navigation' } }.freeze

  private

  # Generates the HTML for the inner content of the GOV.UK Service navigation component
  #
  # @return [ActiveSupport::SafeBuffer]

  def inner_content
    tag.div(class: 'govuk-width-container') do
      tag.div(class: 'govuk-service-navigation__container') do
        concat(tag.span(service_navigation_service_name, class: 'govuk-service-navigation__service-name')) if service
        concat(navigation.render) if navigation
      end
    end
  end

  # Generates the service name section
  #
  # @return [ActiveSupport::SafeBuffer]

  def service_navigation_service_name
    if service[:href]
      link_to(service[:name], service[:href], class: 'govuk-service-navigation__link')
    else
      tag.span(service[:name], class: 'govuk-service-navigation__text')
    end
  end
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Service navigation component

Returns:

  • (ActiveSupport::SafeBuffer)


50
51
52
53
54
55
56
# File 'lib/ccs/components/govuk/service_navigation.rb', line 50

def render
  if service
    tag.section(inner_content, **options[:attributes])
  else
    tag.div(inner_content, **options[:attributes])
  end
end