Accordion

Stacked sections that open and close under their headings.

Source
$npx @dinachi/cli@latest add accordion

Yes. It adheres to the WAI-ARIA design pattern.

multiple
disabled

Usage

import {
  Accordion,
  AccordionHeader,
  AccordionItem,
  AccordionPanel,
  AccordionTrigger,
} from "@/components/ui/accordion"

export function Faq() {
  return (
    <Accordion aria-label="FAQ" defaultValue={["returns"]}>
      <AccordionItem value="returns">
        <AccordionHeader>
          <AccordionTrigger>Returns</AccordionTrigger>
        </AccordionHeader>
        <AccordionPanel>Send it back within 30 days for a full refund.</AccordionPanel>
      </AccordionItem>
      <AccordionItem value="shipping">
        <AccordionHeader>
          <AccordionTrigger>Shipping</AccordionTrigger>
        </AccordionHeader>
        <AccordionPanel>Orders leave the warehouse within two working days.</AccordionPanel>
      </AccordionItem>
    </Accordion>
  )
}

Examples

Yes. It adheres to the WAI-ARIA design pattern and uses semantic HTML elements.

One item open at a time.
import { Accordion, AccordionItem, AccordionHeader, AccordionTrigger, AccordionPanel } from '@/components/ui/accordion';

export function Example() {
  return (
    <Accordion defaultValue={["item-1"]} className="w-full">
      <AccordionItem value="item-1">
        <AccordionHeader>
          <AccordionTrigger>Is it accessible?</AccordionTrigger>
        </AccordionHeader>
        <AccordionPanel>
          Yes. It adheres to the WAI-ARIA design pattern.
        </AccordionPanel>
      </AccordionItem>
      <AccordionItem value="item-2">
        <AccordionHeader>
          <AccordionTrigger>Is it styled?</AccordionTrigger>
        </AccordionHeader>
        <AccordionPanel>
          Yes. It comes with default styles.
        </AccordionPanel>
      </AccordionItem>
    </Accordion>
  );
}

Notes

Values are arrays

value and defaultValue are always arrays, even when multiple is false. When controlled, give each item a value.

Horizontal layouts

The bundled styles are vertical. A horizontal accordion needs its own styling.

API

valueany[]
Controlled open items. Always an array, even when only one can be open.
defaultValueany[] = []
Starting open items when uncontrolled.
onValueChange
agent
(value, details) => void
Runs for people and agents. details.cancel() refuses the change.
multiple
agent
boolean = false
Lets several items stay open. Otherwise opening one closes the rest.
disabled
agent
boolean = false
Blocks every item, for people and agents.
keepMountedboolean = false
Keeps closed panels in the DOM. Also settable per panel.
hiddenUntilFoundboolean = false
Lets find-in-page reveal text in a closed panel. Overrides keepMounted.
aria-label
agent
string
Names the accordion. Its items report it to the agent as in.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as disclosure.

Reads

label

The item's trigger text.

open

Whether the item is open.

value

The panel's text while open. Nothing while closed.

in

The Accordion's aria-label, when it has one.

disabled

True when the item or the whole accordion is disabled.

Actions

open()

Opens the item while closed. In a single accordion it closes the open one.

close()

Closes the item while open. Other open items stay open.

Each item is listed on its own. An agent's change runs through the root's onValueChange like anyone's. isAgentChange(details) is true for it; details.cancel() keeps the item as it was and the agent is told the app refused. A disabled item or a disabled accordion refuses on its own.

await agent.act({
  steps: [{ ref: "Is it styled?", action: "open" }],
})

Accessibility

  • Each trigger is a button with aria-expanded, and aria-controls on its panel while open.
  • Each panel is a region labelled by its trigger.
  • Disabled items keep their place but cannot be opened.
  • AccordionHeader renders an h3. Pass render to match the page's heading level.