FAQ accordion following the WAI-ARIA pattern with keyboard nav
An outdoor gear rental shop has an FAQ page. Sighted mouse users can already click a question to read the answer, but screen-reader and keyboard users get lost: nothing tells them which question is open, and they have to Tab through every control to move between questions. Build the FAQ as an accessible accordion that follows the WAI-ARIA accordion pattern.
The starter code includes the sections and the one that starts open:
const SECTIONS = [
{ id: 'pickup', question: 'When can I pick up my gear?', answer: 'Pickup opens at 8 AM on your first rental day. ...' },
{ id: 'late-return', question: 'What if I return something late?', answer: 'Each late day costs one extra rental day, ...' },
{ id: 'damage', question: 'Who pays for wear and tear?', answer: 'Normal wear is on us. ...' },
{ id: 'cleaning', question: 'Do I need to clean the tent?', answer: 'Just shake out the sand ...' },
];
const INITIAL_OPEN_ID = 'late-return';
<button> whose text is the question, placed inside a heading element (for example <h3>). The heading contains only the button. A decorative +/− icon is fine if it has aria-hidden="true".id, aria-expanded="true" or "false", and aria-controls set to its panel’s id.role="region" and aria-labelledby set to its button’s id, and contains the answer.INITIAL_OPEN_ID starts open. Clicking a closed question opens it and closes the one that was open. Clicking the open question closes it, so all answers can be closed.hidden attribute or display: none), so aria-controls always points to a real element.When focus is on a question button:
Load: "What if I return something late?" open (aria-expanded="true"), the rest closed
Click "When can I pick up..." pickup opens, late return closes
Click it again every section is closed
Focus "Do I need to clean..."
press ArrowDown focus wraps to "When can I pick up my gear?"
press End focus moves to "Do I need to clean the tent?"
id works, because those are unique.display on the panel overrides the hidden attribute. Don’t give panels a display rule, or hide them another way.preventDefault() on the navigation keys, so Home/End and the arrows don’t also scroll the page.The tests click your buttons, dispatch keydown events (key: 'ArrowDown', 'ArrowUp', 'Home', 'End') on the focused button, and read the page like a screen reader would. You don’t need test ids or class names. The test frame is hidden, so the tests track focus through element.focus() calls and document.activeElement: move focus with .focus(). They check that:
h1–h6 (or role="heading") that doesn’t contain the answer.aria-expanded is correct after every click, and only one is ever "true".aria-controls points to an element on the page with role="region", whose aria-labelledby is the button’s id and which contains the answer.hidden, display: none, visibility: hidden, or aria-hidden="true").document.activeElement is the expected question after each navigation key.Capitalization and extra spacing don’t matter.