Sha256: 139cfaf9b642a547c0e76922f3866dcfee0436344b7e7691df4778d345946d2f

Contents?: true

Size: 1.93 KB

Versions: 12

Compression:

Stored size: 1.93 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-visually-hidden'))
              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

12 entries across 12 versions & 1 rubygems

Version Path
ccs-frontend_helpers-2.1.0 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-2.0.0 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-1.2.0 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-1.1.2 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-1.1.1 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-1.1.0 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-1.1.0.beta0 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-1.0.0 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-0.5.0.beta1 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-0.5.0.beta0 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-0.3.0 lib/ccs/components/govuk/warning_text.rb
ccs-frontend_helpers-0.2.0 lib/ccs/components/govuk/warning_text.rb