Accordion
Stacked sections. Each heading opens or closes its own panel.
onChange called 0 time(s)
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>;}
.accordion { display: grid; gap: 0.7em; width: min(31rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5;}.section { border: 1px solid var(--ink); border-radius: 6px; overflow: clip;}.heading { margin: 0; font-size: inherit; font-weight: 400; line-height: inherit; text-shadow: none;}.trigger { display: flex; gap: 0.75em; align-items: center; width: 100%; padding: 0.85em 1em; border: 0; background: transparent; color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif; text-align: left; cursor: pointer;}.trigger-icon { display: inline-flex; width: 1em; height: 1em; flex: none; transition: transform 140ms ease;}.trigger[ui-open] .trigger-icon { transform: rotate(90deg);}.trigger[ui-open] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.trigger:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.trigger:focus-visible { outline: 2px solid var(--ink); outline-offset: -5px;}.trigger:disabled { cursor: not-allowed; opacity: 0.55;}.section[ui-open] .panel { padding: 1em; border-block-start: 1px solid var(--ink); font-family: 'Shantell Sans', system-ui, sans-serif;}.panel[hidden] { display: none;}@media (prefers-reduced-motion: reduce) { .trigger-icon { transition: none; }}
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.
Welcome to Notes
One always open. With collapsible={false}, pressing the open step does nothing; another step replaces it.
Lisbon to Porto
Several open at once. With multiple, value is a list, so your own buttons can open or close every day.
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.
Plans
A section nobody can open. disabled on one item locks it, and the arrow keys skip it.
Anatomy
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
- 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; withmultipleit is the list of sections that are showing. Omit it and everything starts closed.Default''.multiplebooleanSeveral sections may show at once. Without it, opening one closes the rest.Defaultfalse.collapsiblebooleanWhether the section that is showing may be closed again, leaving nothing open. On by default.Defaulttrue.disabledbooleanNothing opens or closes while this is set.Defaultfalse.disableUntilFoundbooleanClosed panels are hidden outright rather than withuntil-found, so the browser's find-in-page cannot reach the text inside them.Defaultfalse.onChange(value: string | readonly string[]) => voidCalled 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
valuestringRequired.This section's own name, and what the root'svaluenames.disabledbooleanThis one section opens and closes for nobody.Defaultfalse.
accordion.itemlabel
No props of its own.
accordion.itemtrigger
No props of its own.
accordion.itemcontent
No props of its own.