Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Accordion

A list of questions or sections, each with a heading and a button that opens its answer. Readers open the items one by one. Each item opens and closes on its own.

Package
@kvirn-ui/react
Pattern
APG Accordion
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use it for a page of questions and answers, such as a FAQ about building permits, where most readers want one or two answers.
  • Write each button as the question or the section title. Screen reader users (people using software that reads the screen aloud) find the questions by heading, so the heading level has to fit your page’s outline.
  • Keep content that everyone needs in plain view. Hiding an answer behind a press costs every reader a step.
  • For a single button and panel, use a Disclosure. For a menu in the page header, use Navigation.
  • It has no “only one open” mode and no “show all” button. The arrow keys don’t move between the questions: each button is a Tab stop.

Example#

Use cases#

The first answer already open

Most readers need the first answer, so you start it open. The other items stay closed, and opening one never closes another.

Apply on My pages. Have your drawings and a site plan ready.

Props in this example:

A long FAQ that people search

On a long page of questions, residents search the page (Ctrl+F) for a word from an answer. Set hiddenUntilFound once on the root, and a browser that supports it opens the item that matches.

Props in this example:

Information: Did you know?

Chromium browsers such as Chrome and Edge can reveal a closed answer. In a browser that can’t, a match inside a closed answer isn’t found, and nothing else changes.

A few sections that people jump between

With region, each open answer is a landmark named by its question, so a screen reader user can jump between them.

Props in this example:

Information: Did you know?

Many open regions crowd a screen reader’s list of landmarks, and the heading already names each question. The ARIA Authoring Practices advise about six or fewer.

A list that says how many questions there are

If you want a screen reader to say how many questions there are, make the root a list and each item a list item. You choose the markup with as.

Props in this example:

  • as on Accordion.Root
  • as on Accordion.Item

Information: Did you know?

The root adds role="list" to a <ul> or <ol> itself, so it is still read as a list when the theme removes the bullets. Safari with VoiceOver drops the list otherwise.

Accessibility#

What it does for you#

  • APG pattern: Accordion: a stack of headings whose buttons each show and hide one section. Each item is a Disclosure: how its button, its hidden panel and find-in-page work is described on the Disclosure page.
  • Deviations: none. APG makes the arrow keys, Home and End between headers optional. This version doesn't use them: every button is a Tab stop, and those keys stay the page's own.
  • Native elements used: <div> (Root, Item), <h1> to <h6> (Heading, by level), <button type="button"> (Trigger), <div> (Panel).

An Accordion is a list of questions or sections, each a heading with a button, that a reader opens one by one. Items are independent: opening one never closes another. It has no "show all" button.

PartElement / roleARIA / stateNotes
Accordion.Root<div class="kv-accordion">, no role (role="list" on ul/ol)nonehiddenUntilFound is the default for every item. as="ul" or "ol" makes it a list that adds role="list" when it renders a ul or ol, because WebKit and VoiceOver drop the list role under list-style: none
Accordion.Item<div class="kv-accordion-item">, no roledata-open while openOwns its own state: open, defaultOpen, onOpenChange, disabled, focusableWhenDisabled, hiddenUntilFound
Accordion.Heading<h1> to <h6> by the required levelnoneClass kv-accordion-heading. The trigger is its only child. Screen reader users find the questions by heading
Accordion.Trigger<button type="button">aria-expanded, aria-controls. data-open, data-disabledDisclosure.Trigger with the class kv-accordion-trigger added: chevron down closed, up open, at the inline end
Accordion.Panel<div>, always rendered, hidden while closeddata-open. With region: role="region" and aria-labelledby the trigger's idDisclosure.Panel with the class kv-accordion-panel added. region is off by default: APG advises it for about six or fewer
useAccordionthe classes, for your own elementsrootProps, itemProps, headingPropsPair it with one useDisclosure() per item

What the automated tests prove:

  • Heading around the button. The trigger is the only child of a heading of the consumer's level, so the outline is theirs (1.3.1, 2.4.6).
  • Independent items. Opening one item changes no other. defaultOpen and open are per item.
  • Same wiring as a Disclosure. aria-controls points at the item's panel, a closed panel is hidden, and a disabled item can't be opened.
  • Find-in-page. hiddenUntilFound on the Root applies to every item, and an item can set its own.
  • Region is opt-in. Without region a panel has no role. With it, it is a region named by its trigger.
  • Development warnings, as for a Disclosure: a part outside an Item, or a Trigger that is not a <button>.

It announces nothing.

What you need to do#

  • Pick the heading level that fits the page's outline (a FAQ under an h2 uses 3). It is required: Accordion can't know where it sits (1.3.1 Info and Relationships, 2.4.6 Headings and Labels).
  • Name every button with the question or section title, and keep the same words open and closed (4.1.2 Name, Role, Value; 2.5.3 Label in Name).
  • Render each panel right after its heading, in the same item, so the reading order and the focus order match (2.4.3 Focus Order).
  • Use region only for a few sections. More than about six open regions crowd a screen reader user's list of landmarks (APG).
  • Keep content that must be seen at once out of an accordion. Hiding the answer behind a press costs every user a step.
  • For a numbered or bulleted list of items, use Accordion.Root as="ul" with Accordion.Item as="li". The root adds role="list" itself.
  • Everything the Disclosure and Button pages ask of you applies here too.

Warning: Don't forget

Choose the heading level that fits your page’s outline, and write each button as the question (1.3.1 Info and Relationships, 2.4.6 Headings and Labels). Keep the words the same open and closed (2.5.3 Label in Name).

Visual and modes#

  • Focus indicator: a ring on the button with at least 3:1 contrast against its background (2.4.7 Focus Visible, AA). It is drawn so the neighbouring item doesn't clip it. The default theme is designed to meet 2.4.13 Focus Appearance (AAA) as well.
  • Target size: the whole row is the button, much larger than the 24 by 24 CSS pixel minimum (2.5.8 Target Size (Minimum), AA).
  • Colour: the default theme keeps text at the minimum contrast on the page (1.4.3 Contrast (Minimum), AA), and no new colour pair is added. Thin lines between items are not the only boundary: the heading and the chevron mark each row. Open or closed is shown by the chevron and aria-expanded, never by colour alone (1.4.1 Use of Color).
  • Forced colours: the lines use the system's colours, and the chevron follows the text colour.
  • Reduced motion: nothing animates.
  • Reflow: at 320px the question wraps and the chevron stays at the end of the line (1.4.10 Reflow).
  • Right-to-left languages: the chevron moves to the left and the text starts at the right.

Focus management#

  • Initial focus: not moved. Opening or closing a section never moves focus.
  • Trap: no.
  • Restore to: not needed. Focus stays on the trigger.
  • Never hidden by: the sections are part of the page, so nothing covers the focused button except what your page adds, such as a sticky header. Keep the button in view with scroll-padding (2.4.11 Focus Not Obscured (Minimum), AA).

Warning: Don't forget

If your page has a sticky header, set scroll-padding-top so it doesn’t cover the focused button (2.4.11 Focus Not Obscured (Minimum)).

Allowed elements#

A tag outside the list changes the page's outline or semantics (1.3.1, 4.1.2). as is a string, so it works from a Server Component.

Part`as`Why
Rootdiv (default), ul, olA list of questions whose count is announced; the root adds role="list". No landmark element
Itemdiv (default), liA list item inside a ul or ol Root. No other element, so the heading and the panel stay its only children
Heading, Trigger, PanelnoneHeading is h1 to h6 by level, Trigger a <button>, Panel a <div> (4.1.2, 1.3.1)

WCAG success criteria#

  • 1.3.1 Info and Relationships (A): each question is a real heading with a native button inside it.
  • 2.4.6 Headings and Labels (AA): the heading level is yours, so the outline fits the page.
  • 4.1.2 Name, Role, Value (A): native buttons with aria-expanded and aria-controls.
  • 1.4.1 Use of Color (A): open or closed is a chevron and a state, not colour.
  • 1.4.10 Reflow (AA) and 1.4.12 Text Spacing (AA): the text wraps and nothing is clipped.
  • 2.1.1 Keyboard (A) and 2.1.2 No Keyboard Trap (A): every button is a Tab stop and focus can always leave.
  • 2.1.4 Character Key Shortcuts (A): the accordion adds no single-key shortcuts.
  • 2.4.3 Focus Order (A): the order of the buttons is the order of the page.
  • 2.4.7 Focus Visible (AA) and 2.4.11 Focus Not Obscured (Minimum) (AA): the focus ring shows and isn't clipped by the component.
  • 2.5.8 Target Size (Minimum) (AA): the whole row is the target.
  • 2.5.3 Label in Name (A): the button's name is its visible text, the same open and closed (accordion.test.tsx › a trigger keeps its name open and closed). Choosing words that match is yours.
  • 1.4.3 Contrast (Minimum) (AA) and 1.4.11 Non-text Contrast (AA): the text and the chevron use the page's text colour, and the theme adds no new colour pair. The text colours are checked automatically in every theme.
  • 3.2.1 On Focus (A) and 3.2.2 On Input (A): focusing or pressing a button opens its panel and changes nothing else.

Keyboard#

Focus
native
Selection follows focus
n/a
Arrows wrap
n/a
Shortcuts
none
KeyWhereWhat happens
Tabbefore the accordionMoves focus to the first trigger. Every trigger is its own Tab stop, in the order of the page
Test: accordion.test.tsx › Tab visits every trigger in order, and Shift+Tab goes back
Shift+Tabon a triggerMoves focus to the previous trigger, or out of the accordion from the first
Test: accordion.test.tsx › Tab visits every trigger in order, and Shift+Tab goes back
Enter / Spaceon a triggerOpens the section, or closes it when it is open. Focus stays on the trigger. Other items don't change
Test: accordion.test.tsx › Enter and Space toggle one item and leave the others
Tabon a trigger, section openMoves focus into the open section's content, then on to the next trigger
Test: accordion.test.tsx › Tab goes through an open section before the next trigger
ArrowDown / ArrowUp / Home / Endon a triggerNothing: APG's optional keys are not adopted, so they scroll the page as usual and the key is never prevented
Test: accordion.test.tsx › the arrow keys, Home and End are not handled

The accordion handles no other key. Escape is the page's own.

API reference#

import { Accordion, useAccordion } from '@kvirn-ui/react'

Accordion.Root

Renders <div> with no role, or a <ul> or <ol> with as. It takes every attribute of that element and passes ref to it. The default theme is described on Theming.

PropTypeDefaultDescription
hiddenUntilFoundbooleanfalseThe default for every item: keeps closed panels’ text findable with the browser’s find-in-page and #fragment links. An item can set its own.
as'div' | 'ul' | 'ol''div'A list of questions whose count is announced. Use ul or ol with Accordion.Item as="li", and the root adds role="list". Another tag warns once in development.
Class or attributeValuesMeaning
kv-accordionalwaysThe part class. The default theme styles it.
role="list"with as="ul" or as="ol"Added by the Accordion, so the list is kept when the theme removes the bullets.

Accordion.Item

Renders <div> with no role, or an <li> with as. It owns its own open state, so opening one item never closes another. It takes every attribute of that element and passes ref to it.

PropTypeDefaultDescription
openboolean–Controlled: whether this item is open. Pair it with onOpenChange, which only reports.
defaultOpenbooleanfalseUncontrolled: whether this item starts open.
onOpenChange(open: boolean, details: AccordionChangeDetails) => void–Called when the user opens or closes this item, with the new state and { reason, event }. The reason is "trigger-press" or "find-in-page".
hiddenUntilFoundbooleanthe Root’s valueKeeps this item’s closed panel findable with find-in-page.
disabledbooleanfalseBlocks opening and closing. Without focusableWhenDisabled the trigger is natively disabled and skipped by Tab.
focusableWhenDisabledbooleanfalseWith disabled, keeps the trigger in the Tab order with aria-disabled="true". Opening stays blocked.
as'div' | 'li''div'An li inside an Accordion.Root that is a ul or an ol.
Class or attributeValuesMeaning
kv-accordion-itemalwaysThe part class. The default theme styles it.
data-openpresent or absentThe item is open.

Accordion.Heading

Renders <h1> to <h6>, by level. The trigger goes inside it. It takes every attribute of a heading and passes ref to it.

PropTypeDefaultDescription
level1 | 2 | 3 | 4 | 5 | 6RequiredThe level your page’s outline needs: it renders <h1> to <h6>. Required, because an Accordion can’t know where it sits.
Class or attributeValuesMeaning
kv-accordion-headingalwaysThe part class. The default theme draws it as plain text: the trigger has the look.

Accordion.Trigger

Renders <button type="button"> with aria-expanded and aria-controls, and a chevron after its content. It is the Disclosure trigger with the accordion’s class added, and takes the same attributes.

Class or attributeValuesMeaning
kv-accordion-triggeralwaysThe part class, next to kv-disclosure-trigger. Your own class joins them. The chevron inside keeps kv-disclosure-icon.
aria-expanded"true" or "false"Whether the item is open. Set by the Accordion: you can’t pass it.
aria-controlsthe panel’s idPoints at the item’s panel. Set by the Accordion.
data-openpresent or absentThe item is open.
data-disabledpresent or absentThe item is disabled.
data-focus-visiblepresent or absentThe trigger has keyboard focus (:focus-visible).
aria-disabled"true" or absentSet when the item has disabled and focusableWhenDisabled both on.

Accordion.Panel

Renders <div>, always rendered and hidden while closed. It is the Disclosure panel with the accordion’s class added, and takes the same attributes. Render it right after the heading.

PropTypeDefaultDescription
regionbooleanfalseMakes the panel a region named by its trigger. Use it for about six sections or fewer: many open regions crowd a screen reader user’s landmark list.
Class or attributeValuesMeaning
kv-accordion-panelalwaysThe part class, next to kv-disclosure-panel. Your own class joins them.
hidden"" or "until-found", or absentSet while closed. With hiddenUntilFound the value is until-found.
data-openpresent or absentThe item is open.
role="region"with regionSet with aria-labelledby, which points at the trigger.

useAccordion

Takes no options. It gives the classes for your own elements: pair it with one useDisclosure() per item, and put each trigger inside a heading.

Result

PropTypeDefaultDescription
rootPropsAccordionPartProps<"accordion">–Spread on the container: the class kv-accordion.
itemPropsAccordionPartProps<"accordion-item">–Spread on an item’s wrapper: the class kv-accordion-item.
headingPropsAccordionPartProps<"accordion-heading">–Spread on the heading around the trigger: the class kv-accordion-heading.

Known issues#

  • No arrow-key navigation between headers. APG lists it as optional. Every button is a Tab stop, so a long FAQ costs a keyboard user more Tab presses.
  • No "show all" button and no "only one open" mode.
  • Find-in-page support differs between browsers. With hiddenUntilFound, Chromium browsers reveal a closed answer that matches. A browser without support treats the answer as hidden, so find-in-page misses it and nothing else breaks.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassaccordion.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassaccordion.test.tsx (axe and role, name and state): every item is a heading around a button, and no item or panel has a role
Keyboard contract rows (Vitest browser)passn/apass5 rows, each names a test in accordion.test.tsx
Focus order and managementpassn/apassaccordion.test.tsx asserts focus
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

What to check#

With a screen reader and the keyboard, try:

  • Each question is one Tab stop and appears in the headings list.
  • Press Enter on a question: "expanded" is read, and focus stays on it.
  • Opening a second question keeps the first open.
  • Tab from an open question goes into its answer, then to the next question.
  • The arrow keys, Home and End only scroll the page.
  • With region, an open answer appears in the landmarks list, named by its question.