Accordion

Slant / Layout

A group of disclosures that opens one section at a time, or several.

Demo

Usage

@tuttn/slant
import { Accordion, Text } from '@tuttn/slant'

// Uniform sections: an items array.
const items = [
  { id: 'offline', title: 'Does it work offline?', content: 'Yes, and it syncs when you reconnect.' },
  { id: 'data', title: 'Where is my data stored?', content: 'On your machine, in plain files.' },
  { id: 'sync', title: 'Sync', content: 'Not available yet.', disabled: true },
]

// Uncontrolled: starts from defaultValue (nothing open when omitted).
<Accordion items={items} defaultValue={['offline']} />
// Controlled: you hold the open ids. One at a time unless multiple.
const [open, setOpen] = useState<string[]>([])
<Accordion items={items} value={open} onValueChange={setOpen} />

// One item always open: its toggle can't close it, and says so with aria-disabled.
<Accordion items={items} defaultValue={['offline']} collapsible={false} />

// Rich sections: the compound part, several open at once, in the outline as h2s.
<Accordion multiple heading="h2">
  <Accordion.Item value="shortcuts" title="Keyboard shortcuts">
    <Text>Up and Down move between the toggles.</Text>
  </Accordion.Item>
  <Accordion.Item value="export" title="Export">
    <Text>Markdown, JSON, or a zip of the project folder.</Text>
  </Accordion.Item>
</Accordion>

Props

PropTypeDefaultDescription
itemsreadonly AccordionEntry[]—The sections, for uniform content. Leave it out and compose `Accordion.Item`s as children instead.
multiplebooleanfalseLets several items stay open at once. Without it, opening one closes the other.
valuestring[]—The open items' ids, when controlled. Without `multiple` it holds at most one.
defaultValuestring[][]The items open first, when uncontrolled.
onValueChange(value: string[]) => void—Called with the open items' ids whenever an item opens or closes.
collapsiblebooleantrueWithout `multiple`: whether the open item closes again when its toggle is pressed. When false, one item always stays open, and its toggle says so with `aria-disabled`.
headingAccordionHeading'h3'The element wrapping each toggle, so the items sit in the page's outline. An item's own `heading` wins.
Accordion.Item valuestring—Which item this is: the id the accordion's value carries.
Accordion.Item titleReactNode—The toggle's visible text.
Accordion.Item disabledboolean—Takes the item's toggle out of clicks, keys and the tab order.
Accordion.Item heading'h2' | 'h3' | 'h4' | 'div'—The element wrapping this item's toggle; the accordion's heading by default.
Accordion.Item togglePropsButtonHTMLAttributes—Extra attributes for the toggle button, as on Disclosure.
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref go on the root div, and on each Accordion.Item's own root div.

An item is { id, title, content, disabled? }. The value is always an array of open ids; without multiple it holds at most one, and opening an item closes the other. With collapsible={false} the open item stays open: its toggle carries aria-disabled="true", stays focusable, and a press does nothing.

Each item is a Disclosure, so its toggle sits in a heading (h3 by default; pick the level that fits the page with heading) and controls its panel with aria-expanded. Every toggle stays in the tab order. Up and Down move between this accordion's toggles and wrap at the ends; Home and End jump to the first and last. Disabled toggles are skipped, and a nested accordion's toggles keep their own keys.