Accordion
Rows that each open on what they hold, ruled off from one another.
Installation
pnpm dlx @human-kit/design add accordionnpx @human-kit/design add accordionyarn dlx @human-kit/design add accordionbunx @human-kit/design add accordionThe command copies the files to src/lib/components/ui/accordion. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Accordion from '$lib/components/ui/accordion';
</script>
<Accordion.Root>
<Accordion.Item value="fees">
<Accordion.Trigger>What does the fee cover?</Accordion.Trigger>
<Accordion.Panel>The visits, the scans and the procedure.</Accordion.Panel>
</Accordion.Item>
</Accordion.Root> Guidelines
- Use
outlinefor an accordion that stands on the page, andghostinside something that already frames it, such as a dialog or a card. - Use
filledwhere a white card would weigh too much, such as a dense panel or a form: the rows on one soft grey panel with no edge, as a filled field. - Keep the default
selectionMode="single"when the rows are steps or alternatives, read one at a time. Usemultiplefor a list of questions, where the reader compares answers. - Write each trigger as the question or the name of what it holds, short enough for one line: its button is 32px tall and does not wrap well.
- Do not hide what most readers need behind a row. An accordion saves room for what only some of them open.
- Past six or so rows, set
region={false}on each panel, so a screen reader’s list of landmarks is not flooded.
Accessibility
- Each trigger is a
<button>inside a heading,<h3>by default. Setlevelon the trigger so it agrees with the headings above it. - The trigger carries
aria-expandedandaria-controls; the panel is aregionnamed by its trigger. Tabreaches each trigger. The arrow keys move between triggers,HomeandEndgo to the first and the last, andEnterorSpaceopen and close the row.- A closed panel stays in the page,
hiddenandinert: its content is not read and cannot take the focus. - A disabled item keeps its row in sight and leaves the arrow-key order.
API reference
Accordion.Root
| Prop | Type | Default |
|---|---|---|
variant | 'outline' | 'filled' | 'ghost' On a card of its own, on a soft grey panel with no edge, or the rules between the rows only. | 'outline' |
value | (string | number)[] The values of the open items. Bindable. | — |
defaultValue | (string | number)[] The items open at the start, when you give no | — |
onChange | (value: (string | number)[]) => void Called with the open items after the user opens or closes a row. | — |
controlledValue | boolean Stops the accordion writing back to | false |
selectionMode | 'single' | 'multiple' Whether one row can be open at a time, or several. | 'single' |
disallowEmptySelection | boolean Keeps one row open at all times. | false |
disabled | boolean Turns off every row. | false |
orientation | 'vertical' | 'horizontal' The axis of the arrow keys between the triggers. | 'vertical' |
loop | boolean From the last trigger, the arrow goes back to the first, and the other way. | true |
element | HTMLDivElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>.
Accordion.Item
| Prop | Type | Default |
|---|---|---|
value | string | number Required. The name of the row, unique in the accordion. | — |
disabled | boolean Turns off this row alone. | false |
Plus every attribute of a <div>. It groups a trigger and its panel and draws nothing.
Accordion.Trigger
| Prop | Type | Default |
|---|---|---|
level | 1 | 2 | 3 | 4 | 5 | 6 The level of the heading around the trigger. | 3 |
element | HTMLButtonElement | null The rendered button. Bindable. | — |
Plus every attribute of a <button>, but type, disabled and the ARIA state, which the accordion sets.
Accordion.Panel
| Prop | Type | Default |
|---|---|---|
forceMount | boolean Keeps a closed panel in the page, hidden, so it opens with its motion the first time too. | true |
region | boolean Makes the panel a | true |
element | HTMLDivElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>. class goes on the inner box that pads the text.