# Collapsible

A button that shows or hides a region under it.

A button that shows or hides a region under it: details, advanced settings, a log. A disclosure: a trigger (a real button with any content) and a region that reveals under it, pushing the rows below. One caret convention: a right caret that turns down, mirrored right-to-left. Three trigger shapes cover the system's disclosures: a whole row (task rows), an inline header on the text line ("Thought for 12s") and a section header (sidebar "Chats"). Behaviour and ARIA come from the Zag collapsible machine, through Ark UI in React, Vue and Svelte. This is the only place the reveal recipe lives.

Kind: component · Category: Display · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `collapsible` machine, through Ark UI

## Add

```bash
npx fndn add collapsible
```

It writes the folder `collapsible/` into the project's ui folder (`index.ts`, `collapsible.tsx`, `Collapsible.vue`, `Collapsible.svelte`, `collapsible.recipe.ts`). First time: [Installation](/installation.md).

## Usage

```tsx
import { Collapsible } from "@/components/ui/collapsible";
```

```vue
import { Collapsible } from "@/components/ui/collapsible";
```

```svelte
import { Collapsible } from "$lib/components/ui/collapsible";
```

## Parts

In the DOM the root carries `data-slot="collapsible"` and each part `data-slot="collapsible-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `collapsible` |  | data-state=open\|closed from the engine. Hosts add their own class and restyle the parts by data-slot. The trigger and region are its direct children, or descendants in no nested collapsible (a trigger in a footer under the region); a host whose trigger is another component's button uses the engine's Trigger anywhere inside the Root (recommendation-card). |
| `trigger` | `button` | `collapsible-trigger` |  | Direct child of the root. aria-expanded, aria-controls, data-state=open\|closed, data-disabled from the engine. |
| `caret` | `span` | `collapsible-caret` | optional | Right caret (ph caret-right) that turns down 90deg while open; first or last child of the trigger; carries kbd-instant |
| `label` | `span` | `collapsible-label` |  | The trigger's content. Any markup may go inside. In a row it wraps (the caret, the meta and its first line share a 24px first line); in inline and section it truncates, with a title while cut. |
| `meta` | `span` | `collapsible-meta` | optional | Trailing muted note (step count, duration) |
| `content` | `div` | `collapsible-content` |  | The region. Direct child of the root; carries kbd-instant. Closed: data-state=closed and hidden (out of the layout and the tab order). Settled open: no data-state. |
| `body` | `div` | `collapsible-body` |  | The one child of the region; it clips while the region moves. Put the host's content here. |
| `text` | `p` | `collapsible-text` | optional | Plain detail text, inset to line up with the trigger's label |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | `false` | Bindable (React: `defaultOpen` and `onOpenChange`; Vue: `v-model:open`; Svelte: `bind:open`). Open or closed. React: open / defaultOpen / onOpenChange; Vue: v-model:open; Svelte: bind:open. |
| `disabled` | `boolean` | `false` | The trigger does nothing and dims. |
| `variant` | `"row" \| "inline" \| "section"` | `"row"` | row: the whole row is the trigger (44px, caret in a 28px box). inline: a text-sized header on the text line that bleeds 6px. section: a 28px quiet header (12px/500, ink-2, ink on hover). |
| `caret` | `"start" \| "end" \| "none"` | `"end"` | Where the caret sits in the trigger. Sections put it first. |
| `label` | `node (children / slot)` |  | The trigger's content: text, or markup (React node, Vue slot `label`, Svelte snippet). |
| `meta` | `string` |  | Trailing muted note in the trigger. |
| `detail` | `string` |  | Plain detail text shown in the region, inset like the trigger. |
| `keepLayout` | `boolean` | `false` | A closed region stays laid out at zero height (invisible, out of the tab order) instead of display: none, so entrances inside it do not replay when it reopens (thinking-state, tool-chips). |
| `triggerClass` | `string` |  | A class added to the trigger (a host's hook). |
| `labelClass` | `string` |  | A class added to the label part (a host's hook). |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `openChange` | (open: boolean) |

## Slots

| Slot | Content |
| --- | --- |
| `default` | The region's content, after the detail text. |
| `label` | Vue: the trigger's content when it is more than text (React: label as a node; Svelte: label as a snippet). |

## States

`hover`, `press`, `focus-visible`, `open`, `disabled`

## Keyboard and accessibility

Role: button (aria-expanded, aria-controls) + region.

Keyboard:

- Tab reaches the trigger; nothing inside a closed region is reachable
- Enter / Space on the trigger toggle the region; the change is instant (no reveal, no caret turn)
- A disabled trigger stays in the tab order and ignores Enter, Space and clicks

Notes:

- The trigger is a real button; its whole content is the accessible name, so keep the label short or give it aria-label.
- The region leaves the layout (hidden) once the exit has played, which also takes it out of the accessibility tree.

## Motion

- reveal: grid-template-rows 0fr -> 1fr over duration.reveal easing.out-strong, opacity over duration.base ease; rows below move with it
- exit: the same transition back; the engine waits for the fndn-collapsible-exit hold (duration.reveal) before it hides the region
- caret: rotate 0 -> 90deg (right-to-left 180 -> 90deg) over duration.move easing.out-strong
- keyboard: region and caret are instant (kbd-instant)
- first paint: nothing plays

## Tokens

`color.ink`, `color.ink-2`, `color.ink-3`, `color.hover`, `color.hover-2`, `radius.control`, `text.xs`, `text.sm`, `duration.fast`, `duration.base`, `duration.move`, `duration.reveal`, `easing.out-strong`

## Notes

- Composition: the trigger and the region are direct children of the root, so collapsibles nest. A host puts its own class on the root (className / class) and styles its parts through `.<host> > [data-slot=collapsible-trigger]`.
- Set it from outside by controlling `open` (React open / onOpenChange, Vue v-model:open, Svelte bind:open).

## Examples

### Deploy step with details

React · `build-step.tsx`

```tsx
import { Collapsible } from "@/components/ui/collapsible";

export default function BuildStep() {
  return (
    <Collapsible
      variant="row"
      defaultOpen
      label="Build"
      meta="48s"
      detail="Installed 1,284 packages, compiled 312 modules and uploaded 14.2 MB of assets for dpl_7f3a9c."
      style={{ width: 320 }}
    />
  );
}
```

Vue · `build-step.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Collapsible } from "@/components/ui/collapsible";

const open = ref(true);
</script>

<template>
  <Collapsible
    variant="row"
    v-model:open="open"
    label="Build"
    meta="48s"
    detail="Installed 1,284 packages, compiled 312 modules and uploaded 14.2 MB of assets for dpl_7f3a9c."
    style="width: 320px"
  />
</template>
```

Svelte · `build-step.svelte`

```svelte
<script lang="ts">
  import { Collapsible } from "$lib/components/ui/collapsible";

  let open = $state(true);
</script>

<Collapsible
  variant="row"
  bind:open={open}
  label="Build"
  meta="48s"
  detail="Installed 1,284 packages, compiled 312 modules and uploaded 14.2 MB of assets for dpl_7f3a9c."
  style="width: 320px"
/>
```

### Warnings under a message

React · `build-warnings.tsx`

```tsx
import { Collapsible } from "@/components/ui/collapsible";

/* inline: a text-sized header on the line, for details under a message */
export default function BuildWarnings() {
  return (
    <div className="flex w-80 flex-col gap-1 text-md text-ink">
      <p>docs-site is live on Production.</p>
      <Collapsible
        variant="inline"
        label="Built with 2 warnings"
        detail="The image hero.png is 2.4 MB, over the 1 MB budget. The page /changelog has no description."
      />
    </div>
  );
}
```

Vue · `build-warnings.vue`

```vue
<script setup lang="ts">
import { Collapsible } from "@/components/ui/collapsible";
</script>

<!-- inline: a text-sized header on the line, for details under a message -->
<template>
  <div class="flex w-80 flex-col gap-1 text-md text-ink">
    <p>docs-site is live on Production.</p>
    <Collapsible
      variant="inline"
      label="Built with 2 warnings"
      detail="The image hero.png is 2.4 MB, over the 1 MB budget. The page /changelog has no description."
    />
  </div>
</template>
```

Svelte · `build-warnings.svelte`

```svelte
<script lang="ts">
  import { Collapsible } from "$lib/components/ui/collapsible";
</script>

<!-- inline: a text-sized header on the line, for details under a message -->
<div class="flex w-80 flex-col gap-1 text-md text-ink">
  <p>docs-site is live on Production.</p>
  <Collapsible
    variant="inline"
    label="Built with 2 warnings"
    detail="The image hero.png is 2.4 MB, over the 1 MB budget. The page /changelog has no description."
  />
</div>
```

### Sidebar section

React · `sidebar-section.tsx`

```tsx
import { Collapsible } from "@/components/ui/collapsible";

const projects = ["web-app", "billing-api", "mobile", "data-pipeline"];

/* section: a quiet header with the caret first, over a group of links */
export default function SidebarSection() {
  return (
    <Collapsible variant="section" caret="start" defaultOpen label="Projects" style={{ width: 240 }}>
      <ul className="flex flex-col px-2.5 pb-1 text-sm">
        {projects.map((project) => (
          <li key={project}>
            <a href={`#${project}`} className="block rounded-chip py-1 text-ink-2 hover:text-ink">{project}</a>
          </li>
        ))}
      </ul>
    </Collapsible>
  );
}
```

Vue · `sidebar-section.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Collapsible } from "@/components/ui/collapsible";

const projects = ["web-app", "billing-api", "mobile", "data-pipeline"];
const open = ref(true);
</script>

<!-- section: a quiet header with the caret first, over a group of links -->
<template>
  <Collapsible variant="section" caret="start" v-model:open="open" label="Projects" style="width: 240px">
    <ul class="flex flex-col px-2.5 pb-1 text-sm">
      <li v-for="project in projects" :key="project">
        <a :href="`#${project}`" class="block rounded-chip py-1 text-ink-2 hover:text-ink">{{ project }}</a>
      </li>
    </ul>
  </Collapsible>
</template>
```

Svelte · `sidebar-section.svelte`

```svelte
<script lang="ts">
  import { Collapsible } from "$lib/components/ui/collapsible";

  const projects = ["web-app", "billing-api", "mobile", "data-pipeline"];
  let open = $state(true);
</script>

<!-- section: a quiet header with the caret first, over a group of links -->
<Collapsible variant="section" caret="start" bind:open={open} label="Projects" style="width: 240px">
  <ul class="flex flex-col px-2.5 pb-1 text-sm">
    {#each projects as project (project)}
      <li>
        <a href={`#${project}`} class="block rounded-chip py-1 text-ink-2 hover:text-ink">{project}</a>
      </li>
    {/each}
  </ul>
</Collapsible>
```

### Locked for viewers

React · `locked-for-viewers.tsx`

```tsx
import { Collapsible } from "@/components/ui/collapsible";

export default function LockedForViewers() {
  return (
    <Collapsible
      variant="row"
      disabled
      label="Environment variables"
      meta="Admins only"
      detail="12 variables for Production, Staging and Preview."
      style={{ width: 320 }}
    />
  );
}
```

Vue · `locked-for-viewers.vue`

```vue
<script setup lang="ts">
import { Collapsible } from "@/components/ui/collapsible";
</script>

<template>
  <Collapsible
    variant="row"
    disabled
    label="Environment variables"
    meta="Admins only"
    detail="12 variables for Production, Staging and Preview."
    style="width: 320px"
  />
</template>
```

Svelte · `locked-for-viewers.svelte`

```svelte
<script lang="ts">
  import { Collapsible } from "$lib/components/ui/collapsible";
</script>

<Collapsible
  variant="row"
  disabled
  label="Environment variables"
  meta="Admins only"
  detail="12 variables for Production, Staging and Preview."
  style="width: 320px"
/>
```

## Recipe

`collapsible.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* collapsible — a disclosure: a trigger (a real button, any content) with a caret, and a region
 * that reveals below it. Behaviour and ARIA from the Zag collapsible machine.
 *
 *   row      a whole row is the trigger (task-rows): 44px, padding 10, the caret in a 28px box at the end;
 *            the label wraps, and the caret, the meta and the label's first line share a 24px first line
 *   inline   a text-sized trigger that sits on the text line (thinking-state "Thought for 12s"):
 *            fits its content, bleeds 6px so the label aligns with the text around it
 *   section  a section header (sidebar "Chats"): 28px, 12px/500 ink-2, ink on hover
 *
 * Everything is here, motion included (`docs motion`). This recipe owns the system's one
 * reveal: `grid-rows-[0fr]` appears nowhere else.
 *   reveal   grid-template-rows 0fr → 1fr at duration.reveal easing.out-strong, opacity at
 *            duration.base ease; the rows below move with it. Opening from closed starts from
 *            @starting-style (`starting:`; the region comes out of display: none); a settled open
 *            region has no data-state, so nothing plays at first paint.
 *   exit     the engine sets data-state="closed" and waits for the content's animation to end
 *            before it sets `hidden`: fndn-collapsible-exit (duration.reveal) holds the region while
 *            the transition runs back, then the region leaves the layout and the tab order.
 *   caret    rotate 0 → 90deg (right-to-left: 180 → 90deg) at duration.move easing.out-strong
 *   keyboard the region and the caret carry kbd-instant: a toggle from the keyboard is instant
 *   keepLayout a closed region stays laid out at zero rows, unseen and out of the tab order
 *            (data-keep-layout on the root; its `display: grid !important`, which must beat
 *            base.css's `[hidden]` rule, is in the theme layer of tailwind.css)
 * Parts are found by data-slot (collapsible, collapsible-trigger, collapsible-label, …); the root
 * slot is `base`. The exit's @keyframes are in base.css. */

export const collapsible = tv({
  slots: {
    base: "flex flex-col min-w-0",
    trigger: [
      "relative flex min-w-0 items-center text-start text-inherit",
      "data-disabled:opacity-50 data-disabled:cursor-default",
      /* press darkens */
      "active:not-data-disabled:bg-hover-2",
      "motion-colors-fast",
    ],
    /* one line; a longer label truncates at the end of its own text and carries the full text as
     * a title while it is cut (lib/truncate.ts). A row's label wraps instead. */
    label: "flex-[0_1_auto] min-w-0 overflow-hidden text-ellipsis whitespace-nowrap [unicode-bidi:plaintext]",
    /* "3 steps" keeps its own order on a right-to-left page */
    meta: "shrink-0 text-xs font-normal text-ink-2 whitespace-nowrap [unicode-bidi:plaintext]",
    /* a right caret that turns down; it points the reading direction while closed */
    caret: [
      "kbd-instant",
      "inline-flex shrink-0 items-center justify-center text-[14px] text-ink-3",
      "motion-transform-move [&:dir(rtl)]:[transform:rotate(180deg)] [[data-slot=collapsible-trigger][data-state=open]>&]:[transform:rotate(90deg)]",
    ],
    /* the region: open, closing (then closed: the engine adds `hidden` once the exit has run), and
     * opening from closed, which needs a start because it comes out of display: none */
    content: [
      "kbd-instant",
      "grid grid-rows-[1fr] opacity-100 motion-size-reveal motion-opacity-base",
      "data-[state=closed]:grid-rows-[0fr] data-[state=closed]:opacity-0",
      "data-[state=closed]:animate-[fndn-collapsible-exit_var(--fndn-duration-reveal)_linear]",
      "starting:data-[state=open]:grid-rows-[0fr] starting:data-[state=open]:opacity-0",
      /* from the keyboard the toggle is instant: the hold goes with the transition */
      "kbd:data-[state=closed]:[animation-duration:0s]",
      /* keepLayout: the closed region keeps its place, unseen */
      "[[data-keep-layout]>&[hidden]]:invisible",
    ],
    /* the clipping child */
    body: "min-w-0 min-h-0 overflow-hidden",
    text: "m-0 text-xs leading-normal text-ink-2 wrap-anywhere [unicode-bidi:plaintext] [&:dir(ltr)]:text-left [&:dir(rtl)]:text-right",
  },
  variants: {
    variant: {
      row: {
        /* a row may clip its content (rounded cards), so the ring sits inside the trigger */
        trigger: "w-full min-h-11 items-start gap-2.5 p-2.5 rounded-[inherit] text-sm focus-visible:outline-offset-[-2px] hover:not-data-disabled:bg-hover",
        /* plain text: the first line is 24px tall (centred on the caret), the next ones keep the body leading */
        label: "flex-[1_1_auto] min-h-6 overflow-visible wrap-anywhere whitespace-normal font-medium text-ink first-line:leading-6",
        meta: "leading-6",
        caret: "size-7 -mx-2 -my-0.5 text-[15px]",
        text: "px-2.5 pt-0 pb-2.5",
      },
      inline: {
        trigger: "w-fit max-w-[calc(100%_+_12px)] gap-2 -mx-1.5 px-1.5 py-1 rounded-control text-sm font-medium text-ink-2 hover:not-data-disabled:bg-hover",
        text: "py-1",
      },
      section: {
        trigger: "h-7 gap-1 px-1.5 rounded-control text-xs font-medium leading-none text-ink-2 hover:not-data-disabled:text-ink pointer-coarse:h-9",
        text: "px-1.5 py-1",
      },
    },
  },
  defaultVariants: { variant: "row" },
});

export type CollapsibleRecipeProps = VariantProps<typeof collapsible>;
```
