Skip to the content
human-kit design
43 — Layout

Accordion

Rows that each open on what they hold, ruled off from one another.

Ten days of daily injections, with a scan every other day.

Installation

pnpm dlx @human-kit/design add accordion
npx @human-kit/design add accordion
yarn dlx @human-kit/design add accordion
bunx @human-kit/design add accordion

The 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 outline for an accordion that stands on the page, and ghost inside something that already frames it, such as a dialog or a card.
  • Use filled where 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. Use multiple for 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. Set level on the trigger so it agrees with the headings above it.
  • The trigger carries aria-expanded and aria-controls; the panel is a region named by its trigger.
  • Tab reaches each trigger. The arrow keys move between triggers, Home and End go to the first and the last, and Enter or Space open and close the row.
  • A closed panel stays in the page, hidden and inert: 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

PropTypeDefault
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 value.

—
onChange(value: (string | number)[]) => void

Called with the open items after the user opens or closes a row.

—
controlledValueboolean

Stops the accordion writing back to value: it only reports through onChange, so the parent can refuse a change.

false
selectionMode'single' | 'multiple'

Whether one row can be open at a time, or several.

'single'
disallowEmptySelectionboolean

Keeps one row open at all times.

false
disabledboolean

Turns off every row.

false
orientation'vertical' | 'horizontal'

The axis of the arrow keys between the triggers.

'vertical'
loopboolean

From the last trigger, the arrow goes back to the first, and the other way.

true
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>.

Accordion.Item

PropTypeDefault
valuestring | number

Required. The name of the row, unique in the accordion.

—
disabledboolean

Turns off this row alone.

false

Plus every attribute of a <div>. It groups a trigger and its panel and draws nothing.

Accordion.Trigger

PropTypeDefault
level1 | 2 | 3 | 4 | 5 | 6

The level of the heading around the trigger.

3
elementHTMLButtonElement | null

The rendered button. Bindable.

—

Plus every attribute of a <button>, but type, disabled and the ARIA state, which the accordion sets.

Accordion.Panel

PropTypeDefault
forceMountboolean

Keeps a closed panel in the page, hidden, so it opens with its motion the first time too.

true
regionboolean

Makes the panel a region landmark. Turn it off past six or so rows.

true
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>. class goes on the inner box that pads the text.