> Leaf UI 0.17.3 · Package: @ifhover/leaf-ui · Source: c3221834f84f2f48481a2054026b713456f8e5dc
> Browser documentation: https://ifhover.github.io/leaf-ui/en/components/collapse.html

> For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/en/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/en/llms-full.txt.

# Collapse

Expand or collapse a group of sections on demand.


## Basic usage

Provide headers and content through `items`, and set the initial expanded panels with `defaultActiveKey`. Multiple panels can be expanded by default. `disabled` prevents toggling that header.


About the project

Project information and collaboration notes.

Updates

Archived

basic.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/collapse/basic.tsx"
import { Collapse } from '@ifhover/leaf-ui';

export function CollapseBasic() {
  return (
    <Collapse
      defaultActiveKey="about"
      items={[
        {
          key: 'about',
          label: 'About the project',
          children: 'Project information and collaboration notes.',
        },
        {
          key: 'updates',
          label: 'Updates',
          children: 'Review recent feature and content updates.',
        },
        { key: 'archived', label: 'Archived', children: null, disabled: true },
      ]}
    />
  );
}
```

Expand code


## Examples

### Accordion

Set `accordion` to allow at most one expanded panel; all panels can also be closed. For controlled state, use `activeKey` and `onChange`. The callback always returns `string[]`, including in accordion mode.


About the project

Project information and collaboration notes.

Updates

Archived

accordion.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/collapse/accordion.tsx"
import { Collapse } from '@ifhover/leaf-ui';

export function CollapseAccordion() {
  return (
    <Collapse
      accordion
      defaultActiveKey="about"
      items={[
        {
          key: 'about',
          label: 'About the project',
          children: 'Project information and collaboration notes.',
        },
        {
          key: 'updates',
          label: 'Updates',
          children: 'Review recent feature and content updates.',
        },
        { key: 'archived', label: 'Archived', children: null, disabled: true },
      ]}
    />
  );
}
```

Expand code

### Preserve or destroy content

Content mounts when first opened and remains mounted after closing by default. Enter a draft, close the panel and reopen it to continue editing. With `destroyInactive`, content unmounts after the closing animation, so internal uncontrolled values return to their initial state on reopening. Controlled state outside the panel remains application-owned.


Destroy collapsed content

Draft

Project name

preserve-content.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/collapse/preserve-content.tsx"
import { Collapse, Flex, Form, FormField, Input, Switch } from '@ifhover/leaf-ui';
import { useState } from 'react';

export function CollapsePreserveContent() {
  const [destroy, setDestroy] = useState(false);
  return (
    <Flex direction="column" gap="md">
      <Flex>
        <Switch checked={destroy} onChange={(event) => setDestroy(event.target.checked)}>
          Destroy collapsed content
        </Switch>
      </Flex>
      <Collapse
        defaultActiveKey="draft"
        destroyInactive={destroy}
        items={[
          {
            key: 'draft',
            label: 'Draft',
            children: (
              <Form layout="vertical" onSubmit={(event) => event.preventDefault()}>
                <FormField label="Project name">
                  <Input name="project" placeholder="Leaf Garden" />
                </FormField>
              </Form>
            ),
          },
        ]}
      />
    </Flex>
  );
}
```

Expand code

### Header actions

Use `extra` for an independent action beside the header. Extra buttons do not toggle the panel; this example only updates its read status.


Notice

Mark read

Unread

extra.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/collapse/extra.tsx"
import { Button, Collapse, Flex, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';

export function CollapseExtra() {
  const [read, setRead] = useState(false);
  return (
    <Flex direction="column" gap="md">
      <Collapse
        items={[
          {
            key: 'notice',
            label: 'Notice',
            children: 'The documentation has been updated.',
            extra: (
              <Button size="sm" disabled={read} onClick={() => setRead(true)}>
                Mark read
              </Button>
            ),
          },
        ]}
      />
      <Text role="status" tone="muted">
        {read ? 'Read' : 'Unread'}
      </Text>
    </Flex>
  );
}
```

Expand code


## API

### Collapse API

Configuration for panel entries, expanded state and accordion mode.

| Property           | Type                                           | Default  | Description                                                                                                                       |
| ------------------ | ---------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `items`            | `readonly `[`CollapseItem`](#collapseitem)`[]` | Required | Panel items                                                                                                                       |
| `activeKey`        | `string \| readonly string[]`                  | —        | Controlled open keys as a string or array; accordion mode keeps only the first.                                                   |
| `defaultActiveKey` | `string \| readonly string[]`                  | `[]`     | Initial open keys for uncontrolled usage.                                                                                         |
| `onChange`         | `(keys: string[]) => void`                     | —        | Always return a string array of open keys; an empty array means all are closed.                                                   |
| `accordion`        | `boolean`                                      | `false`  | Expand one panel at a time                                                                                                        |
| `size`             | `'sm' \| 'md' \| 'lg'`                         | `'md'`   | Header size                                                                                                                       |
| `bordered`         | `boolean`                                      | `true`   | Show border                                                                                                                       |
| `destroyInactive`  | `boolean`                                      | `false`  | Unmount closed contents after the exit animation; visited contents are preserved by default, with unvisited items mounted lazily. |

### CollapseItem

The title, content and state of a collapsible panel entry.

| Property   | Type        | Default  | Description                                                          |
| ---------- | ----------- | -------- | -------------------------------------------------------------------- |
| `key`      | `string`    | Required | Unique key                                                           |
| `label`    | `ReactNode` | Required | Expandable item heading.                                             |
| `children` | `ReactNode` | Required | Contents shown when expanded.                                        |
| `disabled` | `boolean`   | `false`  | Prevent heading toggles without disabling independent extra actions. |
| `extra`    | `ReactNode` | —        | Header actions                                                       |
