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, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/en/components/collapse.md.

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.

Project information and collaboration notes.
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 },
      ]}
    />
  );
}

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.

Project information and collaboration notes.
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 },
      ]}
    />
  );
}

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.

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>
  );
}

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.

Unread
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>
  );
}

API

Collapse API

Configuration for panel entries, expanded state and accordion mode.

PropertyTypeDefaultDescription
itemsreadonly CollapseItem[]RequiredPanel items
activeKeystring | readonly string[]—Controlled open keys as a string or array; accordion mode keeps only the first.
defaultActiveKeystring | 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.
accordionbooleanfalseExpand one panel at a time
size'sm' | 'md' | 'lg''md'Header size
borderedbooleantrueShow border
destroyInactivebooleanfalseUnmount 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.

PropertyTypeDefaultDescription
keystringRequiredUnique key
labelReactNodeRequiredExpandable item heading.
childrenReactNodeRequiredContents shown when expanded.
disabledbooleanfalsePrevent heading toggles without disabling independent extra actions.
extraReactNode—Header actions