# Collapsible

> A disclosure that shows and hides one panel from a trigger.

```tsx title="hero.tsx"
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from '@langgenius/dify-ui/collapsible'

function SettingsSection({
  title,
  defaultOpen,
  settings,
}: {
  title: string
  defaultOpen?: boolean
  settings: { name: string; value: string }[]
}) {
  return (
    <Collapsible defaultOpen={defaultOpen} className="py-1 first:pt-0 last:pb-0">
      <CollapsibleTrigger className="group flex min-h-9 w-full items-center justify-between gap-2 rounded-lg px-2 text-start system-sm-semibold text-text-secondary transition-colors hover:bg-state-base-hover hover:text-text-primary active:bg-state-base-active data-panel-open:text-text-primary">
        {title}
        <span
          aria-hidden="true"
          className="i-ri-arrow-right-s-line size-4 shrink-0 text-text-tertiary transition-transform duration-150 ease-out group-data-panel-open:rotate-90 motion-reduce:transition-none [&:dir(rtl)]:-scale-x-100 [&:dir(rtl)]:group-data-panel-open:-rotate-90"
        />
      </CollapsibleTrigger>
      <CollapsiblePanel>
        <dl className="flex flex-col gap-2 px-2 pt-1 pb-2.5 system-sm-regular">
          {settings.map((setting) => (
            <div key={setting.name} className="flex items-baseline justify-between gap-4">
              <dt className="text-text-tertiary">{setting.name}</dt>
              <dd className="text-text-secondary tabular-nums">{setting.value}</dd>
            </div>
          ))}
        </dl>
      </CollapsiblePanel>
    </Collapsible>
  )
}

export function CollapsibleHeroDemo() {
  return (
    <div className="flex w-full max-w-80 flex-col divide-y divide-divider-subtle">
      <SettingsSection
        title="Retry on failure"
        defaultOpen
        settings={[
          { name: 'Max retries', value: '3' },
          { name: 'Retry interval', value: '1,000 ms' },
        ]}
      />
      <SettingsSection
        title="Error handling"
        settings={[
          { name: 'On failure', value: 'Default value' },
          { name: 'Fallback output', value: 'Empty string' },
        ]}
      />
    </div>
  )
}
```

## Usage guidelines

- Use `Collapsible` to hide secondary content, such as advanced settings or run details, behind a trigger that stays in place. Use `Tabs` to switch between peer panels, and `Popover` for content that floats above the page.
- Name the trigger with its visible text. An icon-only trigger composes `IconButton` through `render` and takes the `aria-label` that `IconButton` requires.
- `CollapsibleTrigger` draws only the focus ring. Compose `Button` or `IconButton` through `render` for a standard look, or style the native button for a full-width row.
- Show the state on the trigger with the `data-panel-open` attribute, for example to rotate a chevron, instead of reading `open` in React.
- `CollapsiblePanel` animates its height. Put padding on a child of the panel, not on the panel itself, so the closed panel collapses to zero height.
- Several collapsibles open independently. Each section owns its own root and trigger.

## Anatomy

```tsx
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from '@langgenius/dify-ui/collapsible'
```

```tsx
<Collapsible>
  <CollapsibleTrigger />
  <CollapsiblePanel />
</Collapsible>
```

## Examples

### Button trigger

Compose `Button` through `render` when the trigger reads as an action.

```tsx title="button-trigger.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from '@langgenius/dify-ui/collapsible'

const advancedOptions = [
  { name: 'Request timeout', value: '30 s' },
  { name: 'Max retries', value: '3' },
  { name: 'Retry interval', value: '1,000 ms' },
  { name: 'Follow redirects', value: 'Enabled' },
]

export function CollapsibleButtonTriggerDemo() {
  return (
    <Collapsible className="w-full max-w-80 items-start">
      <CollapsibleTrigger
        render={<Button variant="ghost" className="group active:bg-state-base-active" />}
      >
        Advanced options
        <span
          aria-hidden="true"
          className="i-ri-arrow-down-s-line size-4 transition-transform duration-150 ease-out group-data-panel-open:rotate-180 motion-reduce:transition-none"
        />
      </CollapsibleTrigger>
      <CollapsiblePanel className="self-stretch">
        <dl className="flex flex-col gap-2 px-3.5 pt-2 pb-1 system-sm-regular">
          {advancedOptions.map((option) => (
            <div key={option.name} className="flex items-baseline justify-between gap-4">
              <dt className="text-text-tertiary">{option.name}</dt>
              <dd className="text-text-secondary tabular-nums">{option.value}</dd>
            </div>
          ))}
        </dl>
      </CollapsiblePanel>
    </Collapsible>
  )
}
```

### Icon button trigger

An icon-only trigger composes `IconButton`, which takes the accessible name through `aria-label`.

```tsx title="icon-button-trigger.tsx"
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from '@langgenius/dify-ui/collapsible'
import { IconButton } from '@langgenius/dify-ui/icon-button'

export function CollapsibleIconButtonTriggerDemo() {
  return (
    <Collapsible className="w-full max-w-80">
      <div className="flex items-center gap-2">
        <span
          aria-hidden="true"
          className="i-ri-checkbox-circle-fill size-4 shrink-0 text-text-success"
        />
        <span className="min-w-0 flex-1 truncate system-sm-medium text-text-primary">
          Run #1842 succeeded
        </span>
        <CollapsibleTrigger
          render={
            <IconButton
              aria-label="Run details"
              className="group shrink-0 active:bg-state-base-active"
            >
              <span
                aria-hidden="true"
                className="i-ri-arrow-down-s-line size-4 transition-transform duration-150 ease-out group-data-panel-open:rotate-180 motion-reduce:transition-none"
              />
            </IconButton>
          }
        />
      </div>
      <CollapsiblePanel>
        <div className="pt-2.5">
          <dl className="grid grid-cols-3 gap-3 border-t border-divider-subtle pt-3 system-xs-regular">
            <div className="flex min-w-0 flex-col gap-0.5">
              <dt className="text-text-tertiary">Duration</dt>
              <dd className="system-sm-medium text-text-secondary tabular-nums">3.2 s</dd>
            </div>
            <div className="flex min-w-0 flex-col gap-0.5">
              <dt className="text-text-tertiary">Tokens</dt>
              <dd className="system-sm-medium text-text-secondary tabular-nums">1,024</dd>
            </div>
            <div className="flex min-w-0 flex-col gap-0.5">
              <dt className="text-text-tertiary">Nodes</dt>
              <dd className="system-sm-medium text-text-secondary tabular-nums">4</dd>
            </div>
          </dl>
        </div>
      </CollapsiblePanel>
    </Collapsible>
  )
}
```

### Hidden until found

`hiddenUntilFound` keeps the closed panel in the DOM with `hidden="until-found"`, so find-in-page searches its text and opens the panel on a match. Search this page for "EPUB" and step to the match inside the closed panel to try it.

```tsx title="hidden-until-found.tsx"
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from '@langgenius/dify-ui/collapsible'

export function CollapsibleHiddenUntilFoundDemo() {
  return (
    <Collapsible className="w-full max-w-80">
      <CollapsibleTrigger className="group flex min-h-9 w-full items-center justify-between gap-2 rounded-lg px-2 text-start system-sm-semibold text-text-secondary transition-colors hover:bg-state-base-hover hover:text-text-primary active:bg-state-base-active data-panel-open:text-text-primary">
        Supported file types
        <span
          aria-hidden="true"
          className="i-ri-arrow-right-s-line size-4 shrink-0 text-text-tertiary transition-transform duration-150 ease-out group-data-panel-open:rotate-90 motion-reduce:transition-none [&:dir(rtl)]:-scale-x-100 [&:dir(rtl)]:group-data-panel-open:-rotate-90"
        />
      </CollapsibleTrigger>
      <CollapsiblePanel hiddenUntilFound>
        <p className="px-2 pt-1 pb-2 system-sm-regular text-text-secondary">
          TXT, Markdown, PDF, HTML, EPUB, XLSX, DOCX, and CSV — up to 15 MB per file.
        </p>
      </CollapsiblePanel>
    </Collapsible>
  )
}
```

## Keyboard

| Key              | Action                                                 |
| ---------------- | ------------------------------------------------------ |
| `Enter`, `Space` | Opens or closes the panel. Focus stays on the trigger. |

## API reference

### Collapsible

Groups a disclosure trigger and collapsible panel with animated height transitions. Renders a `<div>` element.

Inherits `className`, `open`, `defaultOpen`, `onOpenChange`, `disabled`, `render`, `style` from [Base UI Collapsible.Root](https://base-ui.com/react/components/collapsible#root).

**Data attributes**

| Attribute             | Description                                       |
| --------------------- | ------------------------------------------------- |
| `data-open`           | Present when the collapsible is open.             |
| `data-closed`         | Present when the collapsible is closed.           |
| `data-starting-style` | Present when the collapsible begins animating in. |
| `data-ending-style`   | Present when the collapsible is animating out.    |

### CollapsiblePanel

A panel with the collapsible contents. Renders a `<div>` element.

Inherits `hiddenUntilFound`, `keepMounted`, `className`, `render`, `style` from [Base UI Collapsible.Panel](https://base-ui.com/react/components/collapsible#panel).

**Data attributes**

| Attribute             | Description                                   |
| --------------------- | --------------------------------------------- |
| `data-open`           | Present when the collapsible panel is open.   |
| `data-closed`         | Present when the collapsible panel is closed. |
| `data-starting-style` | Present when the panel begins animating in.   |
| `data-ending-style`   | Present when the panel is animating out.      |

**CSS variables**

| Variable                     | Type     | Description                     |
| ---------------------------- | -------- | ------------------------------- |
| `--collapsible-panel-height` | `number` | The collapsible panel's height. |
| `--collapsible-panel-width`  | `number` | The collapsible panel's width.  |

### CollapsibleTrigger

Toggles the collapsible panel open or closed; carries only focus-ring styles so it can be composed via `render` with `Button` or `IconButton`. Renders a `<button>` element.

Inherits `className`, `render`, `style`, `nativeButton` from [Base UI Collapsible.Trigger](https://base-ui.com/react/components/collapsible#trigger).

**Data attributes**

| Attribute         | Description                                 |
| ----------------- | ------------------------------------------- |
| `data-panel-open` | Present when the collapsible panel is open. |
