Class: CCS::Components::GovUK::Accordion

Inherits:
Base
  • Object
show all
Defined in:
lib/ccs/components/govuk/accordion.rb,
lib/ccs/components/govuk/accordion/section.rb,
lib/ccs/components/govuk/accordion/section/header.rb,
lib/ccs/components/govuk/accordion/section/content.rb

Overview

GOV.UK Accordion

This is used for generating the accordion component from the GDS - Components - Accordion

Defined Under Namespace

Classes: Section

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the accordion

{ class: 'govuk-accordion', data: { module: 'govuk-accordion' } }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(accordion_id:, accordion_sections:, **options) ⇒ Accordion

Returns a new instance of Accordion.

Parameters:

  • accordion_id (String)

    used as an id in the HTML for the accordion as a whole, and also as a prefix for the ids of the section contents and the buttons that open them

  • accordion_sections (Array<Hash>)

    an array of accordion section attributes. See Section#initialize for details of the items in the array.

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the accordion HTML

  • :heading_level (Integer) — default: 2

    heading level, from 1 to 6

  • :hide_all_sections_text (String)

    The text content of the ‘Hide all sections’ button at the top of the accordion when all sections are expanded

  • :hide_section_text (String)

    The text content of the ‘Hide’ button within each section of the accordion, which is visible when the section is expanded

  • :hide_section_aria_label_text (String)

    Text made available to assistive technologies, like screen-readers, as the final part of the toggle’s accessible name when the section is expanded. Defaults to “Hide this section”.

  • :show_all_sections_text (String)

    The text content of the ‘Show all sections’ button at the top of the accordion when at least one section is collapsed

  • :show_section_text (String)

    The text content of the ‘Show’ button within each section of the accordion, which is visible when the section is collapsed

  • :show_section_aria_label_text (String)

    Text made available to assistive technologies, like screen-readers, as the final part of the toggle’s accessible name when the section is collapsed. Defaults to “Show this section”.

  • :attributes (Hash)

    any additional attributes that will added as part of the HTML



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ccs/components/govuk/accordion.rb', line 39

def initialize(accordion_id:, accordion_sections:, **options)
  super(**options)

  @options[:attributes][:id] = accordion_id
  @options[:heading_level] ||= 2

  %i[hide_all_sections hide_section hide_section_aria_label show_all_sections show_section show_section_aria_label].each do |data_attribute|
    data_attribute_name = :"#{data_attribute}_text"
    @options[:attributes][:data][:"i18n.#{data_attribute.to_s.gsub('_', '-')}"] = options[data_attribute_name] if options[data_attribute_name]
  end

  @accordion_sections = accordion_sections.map.with_index(1) { |accordion_section, index| Section.new(section: accordion_section, accordion_id: accordion_id, index: index, heading_level: @options[:heading_level]) }
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Accordion component

Returns:

  • (ActiveSupport::SafeBuffer)


57
58
59
60
61
# File 'lib/ccs/components/govuk/accordion.rb', line 57

def render
  tag.div(**options[:attributes]) do
    accordion_sections.each { |accordion_section| concat(accordion_section.render) }
  end
end