Accordion

Stacked sections. Each heading opens or closes its own panel.

onChange called 0 time(s)

Orders leave within two working days.

import { accordion, lucide } from '@markless/ui';export default function AccordionBasic() @{	const questions = [		{			value: 'ship',			question: 'When does my order ship?',			answer: 'Orders leave within two working days.',		},		{			value: 'returns',			question: 'How do I return something?',			answer: 'Start a return from your orders page.',		},		{			value: 'overseas',			question: 'Do you ship overseas?',			answer: 'Everywhere but Antarctica.',		},	];	<accordion.root class="accordion" value="ship">		@for (const q of questions; key q.value) {			<accordion.item class="section" value={q.value}>				<accordion.itemlabel class="heading">					<accordion.itemtrigger class="trigger">						<lucide.chevronright class="trigger-icon" aria-hidden="true" />						{q.question}					</accordion.itemtrigger>				</accordion.itemlabel>				<accordion.itemcontent class="panel">{q.answer}</accordion.itemcontent>			</accordion.item>;		}	</accordion.root>;}

Use an accordion when a page has several sections and a reader wants one or two at a time. A help page full of questions is the usual case. For one panel on its own, use collapsible.

Each section has a name, the value you write on accordion.item. The root's value names the section that is open. Opening another section closes the first one. Add multiple and the root's value becomes a list, so several sections can stay open.

Examples

Four pages, each using the accordion a different way: one step always open, several days open at once, answers opened by a search, and a plan nobody can open.

notes.app/welcome

Welcome to Notes

Name, email address and a password.

One always open. With collapsible={false}, pressing the open step does nothing; another step replaces it.

mytrips.app/lisbon-to-porto

Lisbon to Porto

Pastéis in Belém, then one last tram ride as the lights come on.

Several open at once. With multiple, value is a list, so your own buttons can open or close every day.

beanshop.dev/help

Beanshop help

Opened from outside. Typing sets value to the answers that match, and they open marked. Closed answers stay in the page, so the browser's own find-in-page opens them too.

notes.app/pricing

Plans

Unlimited notebooks and a full year of history.

A section nobody can open. disabled on one item locks it, and the arrow keys skip it.

Anatomy

The parts nest like this: accordion.root holds each accordion.item; an item holds an accordion.itemlabel, which holds the accordion.itemtrigger, and an accordion.itemcontent panel.

accordion.itemlabel renders an <h3>, and the trigger goes inside it. That heading also names the panel: the panel is a role="region" whose aria-labelledby points at it. If you leave the heading out, the panel has no name.

Keyboard

Orders leave within two working days.

  • Focus the next trigger.
  • Focus the previous trigger.
  • Focus the first trigger.
  • Focus the last trigger.
  • orOpen or close the section.

The arrow keys move focus between triggers. They open nothing. At the last trigger, Down goes back to the first, and there is no prop to stop that. The walk skips sections that are disabled.

Closed panels stay in the page

A closed panel keeps its content. It gets hidden="until-found", so the browser's find-in-page still searches it. When find-in-page lands on a word inside, the section opens and onChange runs.

If you set disableUntilFound, a closed panel gets plain hidden instead. Then find-in-page skips it. A disabled section always gets plain hidden, so a search can never open it.

Styling

The family ships no CSS. It writes these attributes for your selectors:

ui-openon item, itemtrigger, itemcontent
The section is open.
ui-closedon item, itemtrigger, itemcontent
The section is closed.
ui-disabledon root, item
The accordion or this section is disabled.
ui-multipleon root
multiple is set.
ui-valueon item
Always. It holds the section's own value.

ui-value lets one rule reach one section:

.section[ui-value='returns'][ui-open] {
	border-color: rebeccapurple;
}

When the last open section closes, onChange gets an empty string. With multiple it gets an empty list. Your own onClick on a trigger runs after the section has changed, so it reads the new state.

API reference

accordion.root

valuestring | readonly string[]
Which section is showing. A plain string names one section; with multiple it is the list of sections that are showing. Omit it and everything starts closed. Default ''.
multipleboolean
Several sections may show at once. Without it, opening one closes the rest. Default false.
collapsibleboolean
Whether the section that is showing may be closed again, leaving nothing open. On by default. Default true.
disabledboolean
Nothing opens or closes while this is set. Default false.
disableUntilFoundboolean
Closed panels are hidden outright rather than with until-found, so the browser's find-in-page cannot reach the text inside them. Default false.
onChange(value: string | readonly string[]) => void
Called with the new value when a person opens or closes a section. Omit it and the accordion still works; the call site simply does nothing.

accordion.item

valuestring
Required. This section's own name, and what the root's value names.
disabledboolean
This one section opens and closes for nobody. Default false.

accordion.itemlabel

No props of its own.

accordion.itemtrigger

No props of its own.

accordion.itemcontent

No props of its own.