Class: CCS::Components::GovUK::WarningText

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

Overview

GOV.UK Warning text

This helper is used for generating the warning text component from the GDS - Components - Warning text

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the warning text

{ class: 'govuk-warning-text' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(text: nil, **options) ⇒ WarningText

Returns a new instance of WarningText.

Parameters:

  • text (String) (defaults to: nil)

    (nil) the text for the warning If nil, then a block will be rendered

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the tag HTML

  • :icon_fallback_text (String)

    the fallback text for the icon (default: ‘Warning’)

  • :attributes (Hash) — default: {}

    any additional attributes that will added as part of the HTML



29
30
31
32
33
# File 'lib/ccs/components/govuk/warning_text.rb', line 29

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

  @text = text
end

Instance Method Details

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

Generates the HTML for the GOV.UK Warning text component

Yields:

  • HTML that will be used in the warning text. Ignored if text is passed.

Returns:

  • (ActiveSupport::SafeBuffer)


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

def render
  tag.div(**options[:attributes]) do
    concat(tag.span('!', class: 'govuk-warning-text__icon', aria: { hidden: true }))
    concat(tag.strong(class: 'govuk-warning-text__text') do
      concat(tag.span(options[:icon_fallback_text] || 'Warning', class: 'govuk-visually-hidden'))
      if text
        concat(text)
      else
        yield
      end
    end)
  end
end