Class: CCS::Components::GovUK::NotificationBanner

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

Overview

GOV.UK Notification Banner

This helper is used for generating the notification banner component from the GDS - Components - Notification banner

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the notifaction banner

{ class: 'govuk-notification-banner', data: { module: 'govuk-notification-banner' } }.freeze
DEFAULT_OPTIONS =

Default options used in normal versions of the notification banner

{
  title_text: 'Important',
  title_id: 'govuk-notification-banner-title',
  role: 'region'
}.freeze
SUCCESS_BANNER_OPTIONS =

Specific options for the success version of the notification banner

{
  classes: ' govuk-notification-banner--success',
  title_text: 'Success',
  role: 'alert'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(text: nil, success_banner: nil, **options) ⇒ NotificationBanner

Returns a new instance of NotificationBanner.

Parameters:

  • text (String) (defaults to: nil)

    the text that will be used for the heading in the content section of the banner. It is ignored if a block is given

  • success_banner (Boolean) (defaults to: nil)

    will use the success banner options if this is set to true

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the notification banner HTML

  • :title_text (String) — default: 'Important'

    the title text shown at the top of the banner

  • :title_id (String) — default: 'govuk-notification-banner-title'

    the ID for the title text

  • :role (String) — default: 'region'

    the role for the banner

  • :heading_level (String) — default: 2

    the heading level for the title text

  • :attributes (Hash) — default: {}

    any additional attributes that will added as part of the HTML



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

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

  @text = text
  @success_banner = success_banner || false

  set_additional_options_for_banner
end

Instance Method Details

#render { ... } ⇒ ActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Notification banner component

Yields:

  • HTML that will be contained within the ‘govuk-notification-banner__content’ div

Returns:

  • (ActiveSupport::SafeBuffer)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ccs/components/govuk/notification_banner.rb', line 50

def render
  tag.div(**options[:attributes]) do
    concat(tag.div(class: 'govuk-notification-banner__header') do
      tag.send(:"h#{options[:heading_level] || 2}", options[:title_text], class: 'govuk-notification-banner__title', id: options[:title_id])
    end)
    concat(tag.div(class: 'govuk-notification-banner__content') do
      if block_given?
        yield
      else
        tag.p(text, class: 'govuk-notification-banner__heading')
      end
    end)
  end
end