# Accordion (`<z-accordion>`)

> Collapsible content panels with single or multiple open items, animated height transitions, and keyboard navigation.

- **Kind:** controller
- **Category:** layout
- **Inherits:** `Controller`, `Base`

## Attributes

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `multiple` | `boolean` | `false` | — | Allows several panels to stay open at once; otherwise opening one closes the others. |
| `no-collapsible` | `boolean` | `false` | — | Keeps at least one panel open: the last open panel cannot be closed, and the first enabled panel opens automatically. |
| `active` | `string` | — | — | Open panels as a comma- or space-separated list of indices, values, or ids; `-1` closes all. Kept in sync with the open state. |
| `duration` | `number` | `150` | — | Panel transition duration in milliseconds; `0` disables animation. |
| `no-animation` | `boolean` | `false` | — | Skips panel transitions entirely, showing and hiding content instantly. |
| `targets` | `string` | `''` | — | CSS selector for item elements within the container, replacing the default item detection. |
| `trigger` | `string` | `''` | — | CSS selector for the clickable trigger within each item, replacing `.z-accordion-title` detection. |
| `content` | `string` | `''` | — | CSS selector for the collapsible panel within each item, replacing `.z-accordion-content` detection. |

### Inherited from `Base`

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `cls` | `string` | `''` | `Base` | Adds classes to named parts as `key: classes` pairs; a plain string targets the default part. |
| `stl` | `string` | `''` | `Base` | Adds inline styles to named parts as `key: styles` pairs; a plain string targets the default part. |

## Styling Parts (`cls` / `stl`)

Default `cls` target: `open`. Default `stl` target: `host-inner`.

| Key | Default class | cls | stl | Description |
| --- | --- | --- | --- | --- |
| `open` | `z-open` | ✓ | — | Class applied to open items; the default `cls` target for this component. |

## CSS Variables

| Variable | Default | Scope | Affects | Description |
| --- | --- | --- | --- | --- |
| `--z-accordion-border` | `var(--z-border)` | `.z-accordion` | `border-color` | Border color of the accordion items. |
| `--z-accordion-border-o` | `var(--z-border-o)` | `.z-accordion` | `border-color` | Opacity applied to the item border color. |
| `--z-accordion-border-style` | `solid` | `.z-accordion > li, .z-accordion > div` | `border-style` | Border style of each item. |
| `--z-accordion-border-w` | `0 0 1px 0` | `.z-accordion > li, .z-accordion > div` | `border-width` | Border widths per side of each item; the default draws only a bottom hairline. |
| `--z-accordion-transition` | `150ms cubic-bezier(0.16, 1, 0.3, 1)` | `.z-accordion` | `transition` | Transition timing used for the accordion's animated state changes. |
| `--z-disabled-opacity` | `0.5` | `.z-accordion > .z-disabled > .z-accordion-title, .z-accordion-title.z-disabled, .z-accordion-title:disabled` | `opacity` | Opacity of disabled triggers. |

## Events

| Event | Cancelable | Detail | Description |
| --- | --- | --- | --- |
| `z-accordion:before-hide` | ✓ | `{ index: number; item: HTMLElement; trigger: HTMLElement; content: HTMLElement; value: string }` | Fires before a panel closes; `preventDefault()` cancels the change. |
| `z-accordion:before-show` | ✓ | `{ index: number; item: HTMLElement; trigger: HTMLElement; content: HTMLElement; value: string }` | Fires before a panel opens; `preventDefault()` cancels the change. |
| `z-accordion:change` | — | `{ activeIndices: number[]; index: number; open: boolean; item: HTMLElement \| null; trigger: HTMLElement \| null; content: HTMLElement \| null; value: string \| null }` | Fires after any open-state change; detail carries the active indices and the primarily affected item. |
| `z-accordion:hidden` | — | `{ index: number; item: HTMLElement; trigger: HTMLElement; content: HTMLElement; value: string }` | Fires after a panel's close transition completes. |
| `z-accordion:hide` | — | `{ index: number; item: HTMLElement; trigger: HTMLElement; content: HTMLElement; value: string }` | Fires when a panel starts closing. |
| `z-accordion:mount` | — | `Record<string, unknown>` | Fires when deferred template content is mounted into the container. |
| `z-accordion:show` | — | `{ index: number; item: HTMLElement; trigger: HTMLElement; content: HTMLElement; value: string }` | Fires when a panel starts opening. |
| `z-accordion:shown` | — | `{ index: number; item: HTMLElement; trigger: HTMLElement; content: HTMLElement; value: string }` | Fires after a panel's open transition completes. |

## Methods

| Method | Params | Returns | Description |
| --- | --- | --- | --- |
| `activeIndices()` | — | `number[]` | Returns the indices of open panels, sorted ascending. |
| `close()` | `target: number \| string`, `animate?: boolean = true` | `Promise<boolean>` | Closes the given panel; refuses to close the last one when `no-collapsible` is set. |
| `closeAll()` | `animate?: boolean = true` | `Promise<boolean>` | Closes all panels, keeping one open when `no-collapsible` is set. |
| `open()` | `target: number \| string`, `animate?: boolean = true` | `Promise<boolean>` | Opens the panel at the given index, value, or id; resolves `true` when open. |
| `openAll()` | `animate?: boolean = true` | `Promise<boolean>` | Opens every enabled panel; with `multiple` off, opens only the first enabled one. |
| `refresh()` | — | `void` | Re-scans the DOM and rebinds listeners after items change. |
| `toggle()` | `target: number \| string`, `animate?: boolean = true` | `Promise<boolean>` | Opens the given panel if closed, otherwise closes it. |

## Data Hooks

| Attribute | Values | Applies to | Role | Description |
| --- | --- | --- | --- | --- |
| `data-accordion-ignore` | — | `descendant` | `config` | Excludes the element from item detection. |
| `data-accordion-item` | `close-all`, `next`, `open-all`, `previous` | `descendant` | `trigger` | Turns the element into an accordion control: `open-all`, `close-all`, `next`, `previous`, or a target index, value, or id to toggle. |
| `data-fn` | `template` | `descendant` | `config` | Marks a deferred `<template>` child whose content is mounted into the container on demand. |
| `data-part` | `list` | `descendant` | `marker` | Marks the list container (`list`) when the host itself is not the accordion list. |
| `data-value` | — | `descendant` | `read-only` | Identifies the item for `active` and toggle targets; falls back to the item id or index. |

## CSS Classes

| Class | Role | Applies to | Description |
| --- | --- | --- | --- |
| `.z-accordion` | `root` | `z-accordion` | Root list container. |
| `.z-accordion-content` | `element` | `.z-accordion` | Collapsible panel associated with a trigger. |
| `.z-accordion-icon` | `element` | `.z-accordion` | Indicator icon in the trigger, styled to reflect the open state. |
| `.z-accordion-title` | `element` | `.z-accordion` | Clickable trigger that toggles its panel. |
| `.z-open` | `state` | `.z-accordion` | State class on open items. |

## Keyboard

| Keys | Action | When |
| --- | --- | --- |
| `ArrowDown` / `ArrowUp` | Move focus to the next / previous enabled trigger, wrapping at the ends. | Focus is on a trigger |
| `Home` / `End` | Move focus to the first / last enabled trigger. | Focus is on a trigger |
| ` ` | Toggle the focused panel when the trigger is not a native button. | Focus is on a non-button trigger |
| `Enter` | Toggle the focused panel when the trigger is neither a button nor a link. | Focus is on a non-button, non-link trigger |

## Accessibility

> Triggers receive `aria-expanded` and `aria-controls`; panels receive `role="region"` and `aria-labelledby` unless they are lists or already carry a role.

> With `no-collapsible`, the last open trigger is marked `aria-disabled` because it cannot be closed.

## Notes

- Items are detected from direct children with a `.z-accordion-title` trigger and a `.z-accordion-content` panel; nav lists (`.z-nav`) use links and sublists instead.
- Panel animation is skipped when the user prefers reduced motion.

## Guides

- [Styling with cls and stl](https://next.0build.dev/docs/guides/styling.md)
- [Translating Components](https://next.0build.dev/docs/guides/i18n.md)
- [Icon Resolution](https://next.0build.dev/docs/guides/icons.md)
- [The data-source Convention](https://next.0build.dev/docs/guides/data-source.md)
- [Openers & Triggers](https://next.0build.dev/docs/guides/triggers.md)
- [Dialog Behavior](https://next.0build.dev/docs/guides/dialogs.md)
- [Utilities](https://next.0build.dev/docs/guides/utilities.md)
- [Runtime](https://next.0build.dev/docs/guides/runtime.md)
