Accordion
Stacked disclosure sections on Base UI Accordion.
One at a time
Every second Tuesday at 10:00. A release that misses the cut waits for the next one.
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"
const questions = [
{
value: "schedule",
question: "When do releases go out?",
answer: "Every second Tuesday at 10:00. A release that misses the cut waits for the next one.",
},
{
value: "rollback",
question: "Can a release be rolled back?",
answer: "Yes. The previous build stays live for 24 hours, and Roll back restores it in under a minute.",
},
{
value: "notes",
question: "Who writes the release notes?",
answer: "The owner of each change adds a line when the change merges. The release manager edits the list.",
},
]
export default function AccordionOneAtATime() {
return (
<Accordion defaultValue={["schedule"]} className="max-w-md">
{questions.map(({ value, question, answer }) => (
<AccordionItem key={value} value={value}>
<AccordionTrigger>{question}</AccordionTrigger>
<AccordionContent>{answer}</AccordionContent>
</AccordionItem>
))}
</Accordion>
)
}
Several open
Invoices are issued on the first of the month and are due in 30 days.
Twelve seats are in use. Three more are included in the plan.
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"
const sections = [
{ value: "billing", title: "Billing", detail: "Invoices are issued on the first of the month and are due in 30 days." },
{ value: "members", title: "Members", detail: "Twelve seats are in use. Three more are included in the plan." },
{ value: "exports", title: "Exports", detail: "A CSV of every project is prepared each night and kept for a week." },
]
export default function AccordionSeveralOpen() {
return (
<Accordion multiple defaultValue={["billing", "members"]} className="max-w-md">
{sections.map(({ value, title, detail }) => (
<AccordionItem key={value} value={value}>
<AccordionTrigger>{title}</AccordionTrigger>
<AccordionContent>{detail}</AccordionContent>
</AccordionItem>
))}
</Accordion>
)
}
Disabled item
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"
export default function AccordionDisabledItem() {
return (
<Accordion className="max-w-md">
<AccordionItem value="summary">
<AccordionTrigger>Summary</AccordionTrigger>
<AccordionContent>Fourteen tasks closed this week and three moved to the next release.</AccordionContent>
</AccordionItem>
<AccordionItem value="audit" disabled>
<AccordionTrigger>Audit log</AccordionTrigger>
<AccordionContent>Available on the Business plan.</AccordionContent>
</AccordionItem>
<AccordionItem value="activity">
<AccordionTrigger>Activity</AccordionTrigger>
<AccordionContent>The last change was made to the invoice template on Monday.</AccordionContent>
</AccordionItem>
</Accordion>
)
}
Installation
npx shadcn@latest add @khaeli/accordionbunx --bun shadcn@latest add @khaeli/accordionInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
- @keyline-icons/react ^2.2.0
Keyboard
TabShift + Tab- Moves between the triggers, in order. The trigger of a
disableditem is a native disabled button and is skipped. EnterSpace- Opens or closes the focused section. Without
multiple, opening a section closes the one that was open.
Accessible name
- Each trigger is a button inside a heading and is named by its text, so write the text as the section's title. It exposes
aria-expanded. - An open panel is a region named by its trigger. Nothing else needs a name.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/accordion.json