Sha256: 62eaf6cc0c7bf07bf742dc504b6fa0015658810b77fb0471bbd58f8a1664de11

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents

require_relative '../base'

module CCS
  module Components
    module GovUK
      # = GOV.UK Warning text
      #
      # This helper is used for generating the warning text component from the
      # {https://design-system.service.gov.uk/components/warning-text GDS - Components - Warning text}
      #
      # @!attribute [r] text
      #   @return [String] Text for the warning text

      class WarningText < Base
        private

        attr_reader :text

        public

        # @param text [String] (nil) the text for the warning
        #                            If nil, then a block will be rendered
        # @param options [Hash] options that will be used in customising the HTML
        #
        # @option options [String] :classes additional CSS classes for the tag HTML
        # @option options [String] :icon_fallback_text the fallback text for the icon (default: +'Warning'+)
        # @option options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML

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

          @text = text
        end

        # Generates the HTML for the GOV.UK Warning text component
        #
        # @yield HTML that will be used in the warning text. Ignored if text is passed.
        #
        # @return [ActiveSupport::SafeBuffer]

        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-warning-text__assistive'))
              if text
                concat(text)
              else
                yield
              end
            end)
          end
        end

        # The default attributes for the warning text

        DEFAULT_ATTRIBUTES = { class: 'govuk-warning-text' }.freeze
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ccs-frontend_helpers-0.1.2 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-0.1.1 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-0.1.1.rc.1 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-0.1.0.rc.7 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-0.1.0.rc.6 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-0.1.0.rc.5 lib/ccs/components/govuk/warning_text.rb