name: Accordion (experimental) description: The accordion component lets users show and hide sections of related content on a page. govuk_frontend_components: - accordion body: | This component is based on the [GOV.UK Design System accordion component](https://design-system.service.gov.uk/components/accordion/) and is currently experimental because more research is needed to validate it. If using this component, [please feed back any research findings to the Design System team](https://design-system.service.gov.uk/get-in-touch/). accessibility_criteria: | The accordion must: * accept focus * be usable with a keyboard * allow the controls to change in appearance when keyboard focus moves to it * allow the controls to indicate when users hover their cursor over it * be usable with touch * be usable with voice commands * have visible text * indicate to users that each section can be expanded and collapsed * tell the user when a step has been expanded or collapsed * be readable when only the [text of the page is zoomed in](https://support.mozilla.org/en-US/kb/font-size-and-zoom-increase-size-of-web-pages#w_how-to-only-change-the-size-of-the-text) * zoom in up to 300% [without the text spilling off the screen](https://www.w3.org/WAI/WCAG21/Understanding/reflow.html) * pass [colour contrast](https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html) * be readable should a [user change colours](https://support.mozilla.org/en-US/kb/change-fonts-and-colors-websites-use) * associate panel content with the control that opens it using the `aria-controls` attribute * have `aria-label` / `aria-labelledby` on section content with a value that refers to the button that controls display of the content Section headings must use a button element so that users: * can toggle sections with the space and enter keys * cannot open sections in a new tab or window When CSS and / or JavaScript is unavailable, the component must: * be fully expanded * not be marked as expandable shared_accessibility_criteria: - link examples: default: data: items: - heading: text: Writing well for the web content: html:

This is the content for Writing well for the web.

- heading: text: Writing well for specialists content: html:

This is the content for Writing well for specialists.

- heading: text: Know your audience content: html:

This is the content for Know your audience.

- heading: text: How people read content: html:

This is the content for How people read.

with_supplied_identification: description: | An `id` for an individual accordion is optional as it's automatically generated, but it can be supplied if a specific `id` is required. The `id` must be unique across the domain of your service. This is because the open or closed state of individual instances of the accordion uses `localStorage` to persist across page loads. Used as an `id` in the HTML for the accordion as a whole, and also as a prefix for the `id`s of the section contents and the buttons that open them, so that those `id`s can be the target of `aria-labelledby` and `aria-control` attributes. data: id: with-supplied-id-thats-unique-across-the-domain items: - heading: text: Writing well for the web content: html:

This is the content for Writing well for the web.

- heading: text: Writing well for specialists content: html:

This is the content for Writing well for specialists.

- heading: text: Know your audience content: html:

This is the content for Know your audience.

- heading: text: How people read content: html:

This is the content for How people read.

with_summary: description: Adds a subheading below each section heading. data: items: - heading: text: Understanding agile project management summary: text: Introductions, methods, core features. content: html: '' - heading: text: Working with agile methods summary: text: Workspaces, tools and techniques, user stories, planning. content: html: '' - heading: text: Governing agile services summary: text: Principles, measuring progress, spending money. content: html: '' - heading: text: Phases of an agile project summary: text: Discovery, alpha, beta, live and retirement. content: html: '' with_data_attributes: description: | Adds custom data attributes to each section of the accordion. Accepts a hash, so multiple attributes can be added. The `data_attributes` option applies attributes to the outermost element in the accordion. Each item can also have a `data_attributes` hash, which are placed on the `button` that triggers the opening and closing - useful for differentiating between each section of the accordion. Data attributes can be added to the 'Show/hide all' link using the `data_attributes_show_all` option, primarily where custom tracking is required. These attributes are read from the accordion markup and then added to the link by JavaScript (which is how the link is created). More details on how this can be used with the GA4 event tracking can be found in the 'Advanced' section of the [event tracking documentation](https://github.com/alphagov/govuk_publishing_components/blob/main/docs/analytics-gtm/ga4-event-tracker.md). If `track_options` within `data_attributes_show_all` is set, then it is possible to pass a custom dimension when 'Show/Hide all' is clicked. data: data_attributes: custom_data_attr: custom-data-attr-accordion data_attributes_show_all: custom_data_attr-event-name: example custom_data_attr-attributes: "{ 'ui': { 'type': 'type value', 'section': 'section value' } }" tracking-options: "{ 'dimension114': 1 }" items: - heading: text: Writing well for the web content: html:

This is the content for Writing well for the web.

data_attributes: custom_data_attr: custom-data-attr-accordion-item-1 - heading: text: Writing well for specialists content: html:

This is the content for Writing well for specialists.

data_attributes: custom_data_attr: custom-data-attr-accordion-item-2 - heading: text: Know your audience content: html:

This is the content for Know your audience.

data_attributes: custom_data_attr: custom-data-attr-accordion-item-3 - heading: text: How people read content: html:

This is the content for How people read.

data_attributes: custom_data_attr: custom-data-attr-accordion-item-4 with_custom_data_module: description: The component includes its own `data-module` but others can be passed in addition if required, for example to apply tracking to an element. This will be included along with the components own `data-module`. data: data_attributes: module: gem-track-click items: - heading: text: Writing well for the web content: html:

This is the content for Writing well for the web.

- heading: text: Writing well for specialists content: html:

This is the content for Writing well for specialists.

different_heading_level: description: This will alter the level of the heading, not the appearance of the heading. data: heading_level: 3 items: - heading: text: Writing well for the web content: html:

This is the content for Writing well for the web.

- heading: text: Writing well for specialists content: html:

This is the content for Writing well for specialists.

- heading: text: Know your audience content: html:

This is the content for Know your audience.

- heading: text: How people read content: html:

This is the content for How people read.

with_margin_bottom: description: | The component accepts a number for margin bottom from `0` to `9` (`0px` to `60px`) using the [GOV.UK Frontend spacing scale](https://design-system.service.gov.uk/styles/spacing/#the-responsive-spacing-scale). It defaults to having a margin bottom of `30px`. data: margin_bottom: 0 items: - heading: text: Writing well for the web content: html:

This is the content for Writing well for the web.

- heading: text: Writing well for specialists content: html:

This is the content for Writing well for specialists.

with_section_open: description: | Adding `expanded: true` to the item will mean the section defaults to being open, rather than closed. Once a user opens or closes a section, the state of each section is remembered. data: items: - heading: text: Writing well for the web content: html:

This is the content for Writing well for the web.

expanded: true - heading: text: Writing well for specialists content: html:

This is the content for Writing well for specialists.

- heading: text: Know your audience content: html:

This is the content for Know your audience.

- heading: text: How people read content: html:

This is the content for How people read.

with_the_anchor_link_navigation: description: | Some apps require custom `id`s per accordion section heading. Custom `id`s allow you to link section headings, sometimes across multiple pages. For example on [guidance pages for Content Designers, referred to as "manuals",](https://www.gov.uk/guidance/how-to-publish-on-gov-uk/creating-and-updating-pages#associations) each manual includes multiple sets of accordions and will reference between specific sections to easily access content. Using the same rules, custom `id`s automatically open accordions when users click within another accordion that links to either * the `id` of an accordion section heading * an `id` within the content of an accordion (this will also automatically navigate to and open accordions on page load) This feature will only be used if the `anchor_navigation` flag is passed as `true`. This mitigates performance risk from event listeners on a large number of links. Unlike with the accordion-wide custom `id` attribute, any `id`s passed to accordion headings as part of this are not stored in `localStorage`. `id`s do not need to be unique across your domain, but should still be unique in the context of the page. data: anchor_navigation: true items: - heading: text: Writing well for the web id: writing-well-for-the-web-1 content: html:

This is content for accordion 1 of 2

This content contains a link

- heading: text: Writing well for specialists content: html:

This is content for accordion 2 of 2

- heading: text: Know your audience content: html:

This is the content for Know your audience.

- heading: text: How people read content: html:

This is the content for How people read.

with_track_show_all_clicks: description: | To switch on Google Analytics for the "Show all sections" button on click, pass `track_show_all_clicks: true` the values passed on open will be `Event Action: accordionOpened` `Event Category: pageElementInteraction` `Event Label: Show all sections` data: track_show_all_clicks: true items: - heading: text: Writing well for the web id: writing-well-for-the-web-2 content: html:

This is content for accordion 1 of 2

- heading: text: Writing well for specialists content: html:

This is content for accordion 2 of 2

with_track_sections: description: | To switch on Google Analytics for each section, on click, pass `track_sections: true` the values passed on open will be `Event Action: accordionOpened` / `accordionClosed` `Event Category: pageElementInteraction` and the `Event Label` being the heading text. If `track_options` in an item is set, then it is possible to pass a custom dimension when the section is clicked. (`track_show_all_clicks: true` can be added to track the "Show all sections" button as well, if required) data: track_sections: true items: - heading: text: Writing well for the web id: writing-well-for-the-web-3 content: html:

This is content for accordion 1 of 2

This content contains a link

data_attributes: track_options: dimension114: 1 - heading: text: Writing well for specialists content: html:

This is content for accordion 2 of 2