Class: CCS::Components::GovUK::Panel

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

Overview

GOV.UK Panel

This is used to generate the panel component from the GDS - Components - Panel

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the panel

{ class: 'govuk-panel govuk-panel--confirmation' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(title_text:, panel_text: nil, **options) ⇒ Panel

Returns a new instance of Panel.

Parameters:

  • title_text (String)

    title text for the panel which will be contained in haeding tags

  • panel_text (String) (defaults to: nil)

    text to use within the panel component. 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 panel HTML

  • :heading_level (Integer, String) — default: default: 1

    heading level for the panel title text

  • :attributes (Hash) — default: {}

    any additional attributes that will added as part of the HTML



32
33
34
35
36
37
# File 'lib/ccs/components/govuk/panel.rb', line 32

def initialize(title_text:, panel_text: nil, **options)
  super(**options)

  @title_text = title_text
  @panel_text = panel_text
end

Instance Method Details

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

Generates the HTML for the GOV.UK Panel component

Yields:

  • HTML that will be contained within the panel body. Ignored if panel text is given

Returns:

  • (ActiveSupport::SafeBuffer)


45
46
47
48
49
50
51
52
53
54
# File 'lib/ccs/components/govuk/panel.rb', line 45

def render
  tag.div(**options[:attributes]) do
    concat(tag.send(:"h#{options[:heading_level] || 1}", title_text, class: 'govuk-panel__title'))
    if panel_text || block_given?
      concat(tag.div(class: 'govuk-panel__body') do
        panel_text || yield
      end)
    end
  end
end