Sections toggle
One button that opens every folded section of a long page, or closes them all. For the reader who wants everything, and the one who wants only the headings.
The crew
Captain and chef, eleven seasons in these islands.
Rates
From £14,500 a week for up to eight guests, all meals included.
Availability
Free from 6 December, and most of February.
The crew
Captain and chef, eleven seasons in these islands.
Rates
From £14,500 a week for up to eight guests, all meals included.
Availability
Free from 6 December, and most of February.
Show code
---
import AccordionItem from '@somnia/core/components/AccordionItem.astro';
import SectionsToggle from '@somnia/core/components/SectionsToggle.astro';
---
<SectionsToggle />
<AccordionItem heading="h2" headingId="crew" title="The crew" open>…</AccordionItem>
<AccordionItem heading="h2" headingId="rates" title="Rates">…</AccordionItem>
<AccordionItem heading="h2" headingId="availability" title="Availability">…</AccordionItem> What it works on
- Whole sections of a page folded with
AccordionItemand aheading: each section's heading is its summary, at the heading's own size, with no card round it. See Accordion. - Every such section in the page's
main. To work on one part of a page only, put the toggle and its sections inside an element markeddata-sections-scope. - It leaves smaller things alone: a Read more or a See more inside a section stays as the reader left it.
Behaviour
- It says Open all sections until every section is open, then Close all sections.
- It follows the page: open the last closed section by hand and its words change by themselves.
- Sections opened this way are remembered for the tab like any other, where the page asks for that (
remember). - It isn't shown without JavaScript, or on a page with fewer than two sections, where it would have nothing to do.
- It is not printed. Paper opens every section unless the reader asked for only what's showing.
Where it goes
At the right, above the first section, where the eye lands after the page's opening. One on a page.
Do and don't
Do
- Use it on a page with four or more folded sections
- Start the sections most people need open
Don't
- Fold a short page to make room for it
- Add one to a list of questions: FAQs open one at a time on purpose
Accessibility
What it does for you
- A real button whose words say what it will do next.
- The sections are the browser’s own
details: each still opens by itself, and find-in-page opens the one holding a match. - Opening or closing everything doesn’t move the focus or scroll the page.
- Not shown where it would do nothing: without JavaScript, or with fewer than two sections.
What you need to do
- Give each section a real heading (
heading="h2"), so the page still has an outline when everything is closed. - Start the sections most people need open.
Keyboard
| Key | What it does |
|---|---|
| Tab | Moves to the button, then to each section’s heading. |
| Enter or Space | On the button: opens every section, or closes them all. On a heading: opens or closes that section. |
What a screen reader is told
- A button named "Open all sections", collapsed; once everything is open, "Close all sections", expanded.
- Each section: its heading and whether it is open, as the browser reports any
details.
This is what the browser reports to assistive technology. What each screen reader then says varies, and has not been listened to yet.
WCAG 2.2
| Criterion | Level | How this meets it |
|---|---|---|
| 1.3.1 Info and Relationships | A | Sections keep their headings whether open or closed. |
| 2.1.1 Keyboard | A | A native button and native details. |
| 4.1.2 Name, Role, Value | A | The button says whether everything is open. |
The criteria this component has a part in. A page that uses it still has to meet the rest.
Checked
| Check | How | Result |
|---|---|---|
| Automated (axe-core) | WCAG 2.0, 2.1 and 2.2 rules at A and AA, the page in light and in dark | Passed Nothing found, light or dark |
| Narrow screen | No sideways scrolling at 320px wide (WCAG 1.4.10) | Passed No sideways scrolling |
| Keyboard and what the browser reports | Real key presses on the live example, and the role, name and state given to assistive technology | Passed 5 of 5 checks pass |
| On paper | Somnia’s print check: no overlaps, stranded headings or controls on the page | Passed No faults on 4 sheets |
| Screen reader | Listened to by a person, in VoiceOver and NVDA | Not yet Not yet checked by a person |
The 5 checks made with the keyboard, on the example at the top of this page:
- Passed Tab reaches the button, which says what it will do
- Passed Enter opens every section in its part of the page, and its words change
- Passed It leaves sections outside its own part of the page alone
- Passed The focus stays on the button, and Enter closes them all
- Passed Opening the sections one by one with the keyboard changes its words by itself
Run by script on against Somnia DS 1.89.0, in Chromium with axe-core 4.14.0. How these checks work, and every component's results.
Not yet checked
- How it sounds in a screen reader (VoiceOver, NVDA, TalkBack) has not been checked by a person. The roles, names and states the browser reports are checked by script; what is actually spoken is not.
- How screen readers announce a heading inside a
summaryvaries between them. This has not been checked across them.
Evidence
Where it's used
| Where | For |
|---|---|
| Headland Yachting, yacht pages | Every section of a yacht’s page is folded; crew, reviews, rates and availability start open. On its staging site until launch. |
What we saw, and what changed
| When | What we saw | What changed |
|---|---|---|
| Yacht pages had just begun folding every section under its heading. Reading a whole page then took a press for each. | One button opens or closes them all (1.38.0). | |
| On the docs page, one press opened the sections and shut them again: the page showed the example twice, and the script had bound to each button twice. | It binds once, however many times the script is on the page. The same guard went into Read more and See more. |
From building and using it on real sites, each entry taken from the changelog or the build's own notes. This is observation by the studio and its clients, not research sessions with users.
What it rests on
- ARIA Authoring Practices: Disclosure: Each section is a disclosure; the browser’s
detailsgives the behaviour. - Nielsen Norman Group: Accordions for complex content: Folding suits pages where people want a few sections, not all of them.
What we don't know yet
- Whether people find the button has not been watched.