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#
Apply on My pages. Have your drawings and a site plan ready.
The fee depends on the size of the building. We work it out when your application arrives.
You get a decision within ten weeks of the application being complete.
import { Accordion } from '@kvirn-ui/react'
import { useAccordionTexts } from './texts.ts'
export function DefaultAccordion() {
const { texts } = useAccordionTexts()
return (
<Accordion.Root>
<Accordion.Item>
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.howToApply}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel>
<p>{texts.howToApplyText}</p>
</Accordion.Panel>
</Accordion.Item>
<Accordion.Item>
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.cost}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel>
<p>{texts.costText}</p>
</Accordion.Panel>
</Accordion.Item>
<Accordion.Item>
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.howLong}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel>
<p>{texts.howLongText}</p>
</Accordion.Panel>
</Accordion.Item>
</Accordion.Root>
)
}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.
The fee depends on the size of the building. We work it out when your application arrives.
import { Accordion } from '@kvirn-ui/react'
import { useAccordionTexts } from './texts.ts'
export function OpenByDefault() {
const { texts } = useAccordionTexts()
return (
<Accordion.Root>
<Accordion.Item defaultOpen>
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.howToApply}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel>
<p>{texts.howToApplyText}</p>
</Accordion.Panel>
</Accordion.Item>
<Accordion.Item>
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.cost}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel>
<p>{texts.costText}</p>
</Accordion.Panel>
</Accordion.Item>
</Accordion.Root>
)
}Props in this example:
defaultOpenon Accordion.Item
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.
Drawings, a site plan and a control plan.
Yes. The decision says how, and by when you must appeal.
Call the building office on weekdays from 9 to 11.
import { Accordion } from '@kvirn-ui/react'
import { useAccordionTexts } from './texts.ts'
export function FindInPage() {
const { texts } = useAccordionTexts()
return (
<Accordion.Root hiddenUntilFound>
<Accordion.Item>
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.documents}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel>
<p>{texts.documentsText}</p>
</Accordion.Panel>
</Accordion.Item>
<Accordion.Item>
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.appeal}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel>
<p>{texts.appealText}</p>
</Accordion.Panel>
</Accordion.Item>
<Accordion.Item>
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.contact}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel>
<p>{texts.contactText}</p>
</Accordion.Panel>
</Accordion.Item>
</Accordion.Root>
)
}Props in this example:
hiddenUntilFoundon Accordion.Root
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.
Drawings, a site plan and a control plan.
Yes. The decision says how, and by when you must appeal.
import { Accordion } from '@kvirn-ui/react'
import { useAccordionTexts } from './texts.ts'
export function Regions() {
const { texts } = useAccordionTexts()
return (
<Accordion.Root>
<Accordion.Item>
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.documents}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel region>
<p>{texts.documentsText}</p>
</Accordion.Panel>
</Accordion.Item>
<Accordion.Item>
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.appeal}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel region>
<p>{texts.appealText}</p>
</Accordion.Panel>
</Accordion.Item>
</Accordion.Root>
)
}Props in this example:
regionon Accordion.Panel
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.
The fee depends on the size of the building. We work it out when your application arrives.
You get a decision within ten weeks of the application being complete.
import { Accordion } from '@kvirn-ui/react'
import { useAccordionTexts } from './texts.ts'
export function AsAList() {
const { texts } = useAccordionTexts()
return (
<Accordion.Root as="ul" aria-label={texts.list}>
<Accordion.Item as="li">
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.cost}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel>
<p>{texts.costText}</p>
</Accordion.Panel>
</Accordion.Item>
<Accordion.Item as="li">
<Accordion.Heading level={3}>
<Accordion.Trigger>{texts.howLong}</Accordion.Trigger>
</Accordion.Heading>
<Accordion.Panel>
<p>{texts.howLongText}</p>
</Accordion.Panel>
</Accordion.Item>
</Accordion.Root>
)
}Props in this example:
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, bylevel),<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.
| Part | Element / role | ARIA / state | Notes |
|---|---|---|---|
| Accordion.Root | <div class="kv-accordion">, no role (role="list" on ul/ol) | none | hiddenUntilFound 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 role | data-open while open | Owns its own state: open, defaultOpen, onOpenChange, disabled, focusableWhenDisabled, hiddenUntilFound |
| Accordion.Heading | <h1> to <h6> by the required level | none | Class 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-disabled | Disclosure.Trigger with the class kv-accordion-trigger added: chevron down closed, up open, at the inline end |
| Accordion.Panel | <div>, always rendered, hidden while closed | data-open. With region: role="region" and aria-labelledby the trigger's id | Disclosure.Panel with the class kv-accordion-panel added. region is off by default: APG advises it for about six or fewer |
useAccordion | the classes, for your own elements | rootProps, itemProps, headingProps | Pair 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.
defaultOpenandopenare per item. - Same wiring as a Disclosure.
aria-controlspoints at the item's panel, a closed panel ishidden, and a disabled item can't be opened. - Find-in-page.
hiddenUntilFoundon the Root applies to every item, and an item can set its own. - Region is opt-in. Without
regiona panel has no role. With it, it is aregionnamed 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
levelthat fits the page's outline (a FAQ under anh2uses3). 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
regiononly 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"withAccordion.Item as="li". The root addsrole="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 |
|---|---|---|
| Root | div (default), ul, ol | A list of questions whose count is announced; the root adds role="list". No landmark element |
| Item | div (default), li | A list item inside a ul or ol Root. No other element, so the heading and the panel stay its only children |
| Heading, Trigger, Panel | none | Heading 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-expandedandaria-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
| Key | Where | What happens |
|---|---|---|
| Tab | before the accordion | Moves 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+Tab | on a trigger | Moves 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 / Space | on a trigger | Opens 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 |
| Tab | on a trigger, section open | Moves 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 / End | on a trigger | Nothing: 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.
| Prop | Type | Default | Description |
|---|---|---|---|
boolean | false | The 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 attribute | Values | Meaning |
|---|---|---|
kv-accordion | always | The 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.
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | – | Controlled: whether this item is open. Pair it with onOpenChange, which only reports. |
defaultOpen | boolean | false | Uncontrolled: 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". |
boolean | the Root’s value | Keeps this item’s closed panel findable with find-in-page. | |
disabled | boolean | false | Blocks opening and closing. Without focusableWhenDisabled the trigger is natively disabled and skipped by Tab. |
focusableWhenDisabled | boolean | false | With 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 attribute | Values | Meaning |
|---|---|---|
kv-accordion-item | always | The part class. The default theme styles it. |
data-open | present or absent | The 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.
| Prop | Type | Default | Description |
|---|---|---|---|
level | 1 | 2 | 3 | 4 | 5 | 6 | Required | The level your page’s outline needs: it renders <h1> to <h6>. Required, because an Accordion can’t know where it sits. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-accordion-heading | always | The 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 attribute | Values | Meaning |
|---|---|---|
kv-accordion-trigger | always | The 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-controls | the panel’s id | Points at the item’s panel. Set by the Accordion. |
data-open | present or absent | The item is open. |
data-disabled | present or absent | The item is disabled. |
data-focus-visible | present or absent | The trigger has keyboard focus (:focus-visible). |
aria-disabled | "true" or absent | Set 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.
| Prop | Type | Default | Description |
|---|---|---|---|
region | boolean | false | Makes 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 attribute | Values | Meaning |
|---|---|---|
kv-accordion-panel | always | The part class, next to kv-disclosure-panel. Your own class joins them. |
hidden | "" or "until-found", or absent | Set while closed. With hiddenUntilFound the value is until-found. |
data-open | present or absent | The item is open. |
role="region" | with region | Set 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
| Prop | Type | Default | Description |
|---|---|---|---|
rootProps | AccordionPartProps<"accordion"> | – | Spread on the container: the class kv-accordion. |
itemProps | AccordionPartProps<"accordion-item"> | – | Spread on an item’s wrapper: the class kv-accordion-item. |
headingProps | AccordionPartProps<"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 + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | accordion.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | accordion.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) | pass | n/a | pass | 5 rows, each names a test in accordion.test.tsx |
| Focus order and management | pass | n/a | pass | accordion.test.tsx asserts focus |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | pending | pending | pending | |
| VoiceOver + Safari + iOS | pending | pending | pending | |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | pending | pending | pending | |
| TalkBack + Chrome + Android | pending | pending | pending | |
| VoiceOver + Safari + macOS | pending | pending | pending |
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.