# Accordion

> Accordion and AccordionItem stack collapsible sections with the APG accordion keyboard model, single or multiple expansion, cancellable changes, lazy panels that find-in-page can open, and nesting.

Canonical: https://salmonui.dev/docs/navigation/accordion

```tsx
import { Accordion, AccordionItem } from '@salmonui/navigation/accordion';
import '@salmonui/navigation/accordion/styles.css';

<Accordion type="single" defaultValue={['shipping']}>
  <AccordionItem value="shipping" header="Shipping">
    Orders ship within two business days.
  </AccordionItem>
  <AccordionItem value="returns" header="Returns" disabled>
    Return any unused item within 30 days.
  </AccordionItem>
</Accordion>
```

`@salmonui/navigation/accordion` is self-contained: it imports no other family,
and its stylesheet needs nothing besides `@salmonui/core/styles.css`, which
every app imports once.

## Items

Each `AccordionItem` is one section: `value` is its stable key,
`header` the header content, `icon` a decorative node before the header,
`disabled` makes the header `aria-disabled` and inert, and
`hidden` renders nothing. The children are the panel content. Build items
from data with `data.map(...)`; there is no separate data-source
and template API, because JSX children already cover both:

```tsx
data.map((d) => <AccordionItem key={d.id} value={d.id} header={d.title}>{d.body}</AccordionItem>)
```

`className` and `style` reach the item; `headerProps` and `panelProps` pass
any attribute, class, style or handler to the header `<button>` and the
panel region. `ref` on `Accordion` is the root `<div>`, on
`AccordionItem` the item `<div>`.

## Expansion

`type="multiple"` (default) lets any number of
items open; `type="single"` keeps at most one. In single mode `collapsible`
(default `true`) decides whether the open item can close; with
`collapsible={false}` its header is `aria-disabled`, as the APG pattern
recommends.

`value` / `defaultValue` / `onValueChange` hold the expanded item values,
always as an array, so the type does not change with `type`.
They are item values rather than indexes, because indexes shift when items
are added.

Without `value` the accordion is uncontrolled and starts from
`defaultValue`. With `value` it is controlled: a header press only reports
the proposed array through `onValueChange`, and the panels follow the prop.

```tsx
const [open, setOpen] = useState<readonly string[]>(['shipping']);

<Accordion value={open} onValueChange={setOpen}>
  …
</Accordion>
```

`disabled` on `Accordion` disables every item at once.

## Events

`onBeforeValueChange(event)` runs before every change with `item`,
`expanded`, the proposed `value`, `previousValue` and `source`
(`'pointer' | 'keyboard' | 'touch' | 'find'`); `event.preventDefault()`
cancels it. `onValueChange(value, event)` follows with
`{ item, expanded, source }`. A header click reaches `headerProps.onClick`, which runs
first; calling `event.preventDefault()` there stops the toggle. Event objects
are created only when a handler is bound and are frozen in development.

## Lazy panels and find-in-page

A panel's content mounts the first time it expands and stays mounted
(`keepMounted`, default `true`); `keepMounted={false}` unmounts it on
collapse, and `forceMount` renders it even while collapsed, including in
server HTML. Both props also exist per item.

A mounted collapsed panel carries `hidden="until-found"`: the browser's
find-in-page searches it, and on a match (`beforematch`) the item expands
with `source: 'find'`. React writes `hidden` as a boolean, so the component
sets the attribute itself after hydration; server HTML hides collapsed
panels through `data-state="closed"`.

Opening and closing animate the panel's `block-size` to and from `auto` with
`interpolate-size: allow-keywords`, with no JavaScript measurement.
Browsers without `interpolate-size` switch instantly; under
`prefers-reduced-motion` there is no transition. The animation is
this one fixed CSS transition, with no animation settings.

## Nesting

An `Accordion` inside a panel is independent: its own value, ids and
keyboard handling. Arrow keys in the inner accordion stay inside it, and the
outer accordion skips the inner headers.

## Keyboard and ARIA

| Key | Action |
| --- | --- |
| Enter, Space | Toggle the focused item |
| ArrowDown / ArrowUp | Next / previous header (wraps) |
| Home / End | First / last header |
| Tab | Every header is a tab stop, then the open panels' content |

Each header is a `<button>` inside a heading (`headingLevel`, default 3) with
`aria-expanded`, `aria-controls` and `aria-disabled` when disabled; each
panel is `role="region"` labelled by its header. The
accordion has no built-in text, so it needs no `localeText`.

## Styling

`.salmon-accordion` (`data-type`), `.salmon-accordion-item` (`data-state`
`open` / `closed`, `data-disabled`), `.salmon-accordion-heading`,
`.salmon-accordion-trigger`, `.salmon-accordion-icon`,
`.salmon-accordion-label`, `.salmon-accordion-indicator` (the chevron, turned
when open), `.salmon-accordion-panel` and `.salmon-accordion-content`. Colors
come from the theme; the CSS uses logical properties, so it follows `dir`.

## Server rendering

The accordion renders on the server with no browser API in render: expanded
panels carry their content, collapsed ones are hidden through
`data-state="closed"` and have no content unless `forceMount` is set, and
hydration reports no warnings. `hidden="until-found"` is added only
after hydration.

## Not included

Imperative methods for adding, removing, enabling, hiding, expanding or
selecting items are not offered: in React the item list belongs to
the application, and methods that mutate an internal copy let the UI and the
application state disagree. Add or remove `AccordionItem` children, set
`disabled` or `hidden` on them, and put an item's `value` into the `value`
array to expand it; focus a header with its DOM `focus()`.

## API reference

[`@salmonui/navigation/accordion`](https://salmonui.dev/docs/reference/navigation/accordion): `Accordion`,
`AccordionItem` and their prop and event types.

### Live demo: Accordion

Live demo: https://salmonui.dev/demos/navigation/accordion-basics

A controlled multiple / single Accordion with a disabled item, a change vetoed in onBeforeValueChange and a panel that find-in-page can reveal.

`Demo.tsx`:

```tsx
'use client';
import { useState } from 'react';
import { Accordion, AccordionItem } from '@salmonui/navigation/accordion';
import { Checkbox } from '@salmonui/inputs/checkbox';

export default function AccordionBasicsDemo() {
  const [single, setSingle] = useState(false);
  const [value, setValue] = useState<readonly string[]>(['shipping']);
  const [last, setLast] = useState('nothing yet');
  return (
    <div style={{ display: 'grid', gap: 12, maxWidth: 560 }}>
      <Checkbox checked={single} onCheckedChange={(checked) => setSingle(checked)}>
        Single mode
      </Checkbox>
      <Accordion
        type={single ? 'single' : 'multiple'}
        value={value}
        onBeforeValueChange={(event) => {
          if (event.item === 'locked') event.preventDefault();
        }}
        onValueChange={(next, event) => {
          setValue(next);
          setLast(`${event.item} ${event.expanded ? 'expanded' : 'collapsed'} (${event.source})`);
        }}
      >
        <AccordionItem value="shipping" header="Shipping">
          Orders ship within two business days. Tracking links arrive by email.
        </AccordionItem>
        <AccordionItem value="returns" header="Returns">
          Return any unused item within 30 days for a full refund.
        </AccordionItem>
        <AccordionItem value="warranty" header="Warranty" disabled>
          Two years on every device.
        </AccordionItem>
        <AccordionItem value="locked" header="Vetoed by onBeforeValueChange">
          Never shown.
        </AccordionItem>
        <AccordionItem value="search" header="Find me with Ctrl+F" forceMount>
          This panel is mounted while collapsed, so find-in-page can reveal the word marmalade.
        </AccordionItem>
      </Accordion>
      <p style={{ margin: 0 }}>
        Expanded: <code>{JSON.stringify(value)}</code> · last change: {last}
      </p>
    </div>
  );
}
```

### Live demo: Nested accordion

Live demo: https://salmonui.dev/demos/navigation/accordion-nested

A single-mode Accordion with icons, level-4 headings and a nested Accordion whose arrow keys stay inside it.

`Demo.tsx`:

```tsx
'use client';
import { Accordion, AccordionItem } from '@salmonui/navigation/accordion';

const glyph = (d: string) => (
  <svg viewBox="0 0 16 16" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <path d={d} />
  </svg>
);
const ACCOUNT = glyph('M8 8a2.75 2.75 0 1 0 0-5.5A2.75 2.75 0 0 0 8 8M2.75 14c0-2.9 2.35-4.5 5.25-4.5s5.25 1.6 5.25 4.5');
const BILLING = glyph('M1.75 3.5h12.5v9H1.75zM1.75 6.5h12.5M4.5 10h2.5');

export default function AccordionNestedDemo() {
  return (
    <Accordion type="single" defaultValue={['account']} headingLevel={4} style={{ maxWidth: 560 }}>
      <AccordionItem value="account" header="Account" icon={ACCOUNT}>
        <Accordion defaultValue={[]}>
          <AccordionItem value="profile" header="Profile">
            Name, avatar and time zone.
          </AccordionItem>
          <AccordionItem value="security" header="Security">
            Password and two-factor authentication.
          </AccordionItem>
        </Accordion>
      </AccordionItem>
      <AccordionItem value="billing" header="Billing" icon={BILLING} keepMounted={false}>
        Invoices are listed here; this panel unmounts when it collapses.
      </AccordionItem>
    </Accordion>
  );
}
```
