Sha256: 6f7aa93879a34a8903b461d7e03b29b3a17e275060601f7e172a53bf73d07746

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require_relative '../base'

module CCS::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'))
          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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ccs-frontend_helpers-0.1.0.rc.2 lib/ccs/components/govuk/warning_text.rb