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

Inherits:
Base
  • Object
show all
Includes:
ActionView::Helpers::UrlHelper
Defined in:
lib/ccs/components/govuk/service_navigation/link.rb

Overview

GOV.UK Service navigation link

The individual service navigation link item

Constant Summary

Constants inherited from Base

Base::DEFAULT_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

#initialize(text:, href: nil, method: nil, **options) ⇒ Link

Returns a new instance of Link.

Parameters:

  • text (String)

    the text for the service navigation link

  • href (String) (defaults to: nil)

    the href for the service navigation link

  • method (String) (defaults to: nil)

    the method for the header request which will make it a button

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :active (Boolean)

    flag to mark the navigation item as active or not

  • :attributes (Hash)

    any additional attributes that will added as part of the HTML



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

def initialize(text:, href: nil, method: nil, **options)
  super(**options)

  @options[:attributes][:aria] = { current: options[:current] ? 'page' : 'true' } if options[:active] || options[:current]

  @text = text
  @href = href
  @method = method
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Service navigation link

Returns:

  • (ActiveSupport::SafeBuffer)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ccs/components/govuk/service_navigation/link.rb', line 51

def render
  tag.li(class: "govuk-service-navigation__item #{'govuk-service-navigation__item--active' if options[:active] || options[:current]}".rstrip) do
    if href
      if method
        options[:attributes][:class] = 'govuk-service-navigation__button_as_link'

        button_to(href, method: method, **options[:attributes]) do
          inner_content
        end
      else
        options[:attributes][:class] = 'govuk-service-navigation__link'

        link_to(inner_content, href, **options[:attributes])
      end
    else
      options[:attributes][:class] = 'govuk-service-navigation__text'

      tag.span(inner_content, **options[:attributes])
    end
  end
end