Class: CCS::Components::GovUK::Header::Link

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

Overview

GOV.UK Header link

The individual header link item

Constant Summary

Constants inherited from Base

Base::DEFAULT_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Link.

Parameters:

  • text (String)

    the text for the header link

  • href (String) (defaults to: nil)

    the href for the header link

  • 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



30
31
32
33
34
35
# File 'lib/ccs/components/govuk/header/link.rb', line 30

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

  @text = text
  @href = href
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Header link

Returns:

  • (ActiveSupport::SafeBuffer)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ccs/components/govuk/header/link.rb', line 41

def render
  tag.li(class: "govuk-header__navigation-item #{'govuk-header__navigation-item--active' if options[:active]}".rstrip) do
    if href
      options[:attributes][:class] = 'govuk-header__link'

      link_to(text, href, **options[:attributes])
    else
      text
    end
  end
end