Class: CCS::Components::GovUK::CookieBanner

Inherits:
Base
  • Object
show all
Defined in:
lib/ccs/components/govuk/cookie_banner.rb,
lib/ccs/components/govuk/cookie_banner/action.rb,
lib/ccs/components/govuk/cookie_banner/message.rb

Overview

GOV.UK Cookie Banner

This is used to generate the cookie banner component from the GDS - Components - Cookie banner

Defined Under Namespace

Classes: Action, Message

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the cookie banner

{ class: 'govuk-cookie-banner' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(messages:, **options) ⇒ CookieBanner

Returns a new instance of CookieBanner.

Parameters:

  • messages (Array<Hash>)

    An array of messages for the cookie banner. See Message#initialize for details of the items in the array.

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the cookie banner HTML

  • :attributes (Hash)

    any additional attributes that will added as part of the HTML



29
30
31
32
33
34
35
36
37
# File 'lib/ccs/components/govuk/cookie_banner.rb', line 29

def initialize(messages:, **options)
  super(**options)

  (@options[:attributes][:data] ||= {})[:nosnippet] = 'true'
  @options[:attributes][:role] = 'region'
  (@options[:attributes][:aria] ||= {})[:label] ||= 'Cookie banner'

  @messages = messages.map { |message| Message.new(context: @context, **message) }
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Cookie banner component

Returns:

  • (ActiveSupport::SafeBuffer)


43
44
45
46
47
# File 'lib/ccs/components/govuk/cookie_banner.rb', line 43

def render
  tag.div(**options[:attributes]) do
    messages.each { |message| concat(message.render) }
  end
end