---
title: "Collapsible · UI SDK — StyleX Blocks"
description: "Show and hide a region of content behind a trigger, with a bare content slot to style freely."
url: "https://stylexblocks.com/components/ui-sdk/collapsible/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Collapsible

Show and hide a region of content behind a trigger, with a bare content slot to style freely.

- Behavior engine: UI SDK
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/collapsible.md), [Base UI](https://stylexblocks.com/components/base-ui/collapsible.md)
- Category: Display
- Used in blocks: [AI Chat Conversation](https://stylexblocks.com/block/ui-sdk/ai-chat.md), [AI Chat Landing](https://stylexblocks.com/block/ui-sdk/ai-chat-landing.md), [Checkout Form](https://stylexblocks.com/block/ui-sdk/checkout-form.md), [Checkout Wizard](https://stylexblocks.com/block/ui-sdk/checkout-wizard.md), [Collapsible Table](https://stylexblocks.com/block/ui-sdk/collapsible-table.md), [Inbox Table](https://stylexblocks.com/block/ui-sdk/inbox-table.md), [Order Detail](https://stylexblocks.com/block/ui-sdk/order-detail.md), [Side Nav Shell](https://stylexblocks.com/block/ui-sdk/shell-side-nav.md)
- HTML page: https://stylexblocks.com/components/ui-sdk/collapsible/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style ui-sdk
```

Then add the item as source you own:

```sh
npx stylexblocks add collapsible
```

Equivalent: `npx shadcn add @stylexblocks/collapsible`. Registry item JSON: https://stylexblocks.com/r/ui-sdk/collapsible.json

## Usage

```tsx
import { Collapsible } from "#components/ui/ui-sdk/collapsible.tsx";
import { Button } from "#components/ui/ui-sdk/button.tsx";

export function Example() {
  return (
    // the engine wires the trigger and animates the panel; the panel slot is
    // deliberately bare — style it freely via the style prop
    <Collapsible.Root defaultOpen>
      <Collapsible.Trigger render={<Button variant="surface" />}>
        Show advanced options
      </Collapsible.Trigger>
      <Collapsible.Panel>
        Custom domains, edge caching, and preview deployments.
      </Collapsible.Panel>
    </Collapsible.Root>
  );
}
```

## Source

`components/ui/ui-sdk/collapsible.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import {
  Collapsible as UiSdkCollapsible,
  CollapsiblePanel as UiSdkCollapsiblePanel,
  CollapsibleTrigger as UiSdkCollapsibleTrigger,
} from "@ui-sdk/react/collapsible";
import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";
import { defineSlotRecipe } from "#lib/recipe-props.ts";
import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'

const collapsibleContent = stylex.create({
  base: {
    overflow: 'hidden',
    animationName: {
      default: null,
      ':is([data-open], [data-state="open"])': 'expand-height, fade-in',
      ':is([data-closed], [data-state="closed"])': 'collapse-height, fade-out',
    },
    animationDuration: {
      default: null,
      ':is([data-open], [data-state="open"])': durations.moderate,
      ':is([data-closed], [data-state="closed"])': durations.moderate,
    },
  },
})

// The panel transitions UI SDK's measured height instead of replaying the
// recipe's keyframes. The recipe already matches UI SDK's `data-open` /
// `data-closed`.
const chrome = stylex.create({
  panel: {
    // UI SDK's documented panel animation: transition the measured height
    // to and from zero on data-starting-style / data-ending-style. A panel
    // that starts open just appears (the shared keyframes replayed on every
    // page load and before hydration the measured var is unset).
    animationName: "none",
    height: {
      default: "var(--collapsible-panel-height, auto)",
      ":is([data-starting-style], [data-ending-style])": "0px",
    },
    opacity: {
      default: 1,
      ":is([data-starting-style], [data-ending-style])": 0,
    },
    transitionProperty: "height, opacity",
    transitionDuration: {
      default: "200ms",
      "@media (prefers-reduced-motion: reduce)": "0s",
    },
    transitionTimingFunction: "ease",
  },
});

const collapsibleRecipe = defineSlotRecipe({
  slots: {
    content: { styles: collapsibleContent },
  },
  defaultVariants: {},
});

const { withContext } = createSlotRecipeContext(collapsibleRecipe);

/**
 * Collapsible — UI SDK's collapsible wearing the collapsible slot recipe:
 * trigger wiring, height animation hooks, and open state all come from the
 * engine (Root → Trigger → Panel). The panel slot carries only the
 * open/close height animation — style the rest freely per use. Every part
 * composes via `render`.
 */
export const Collapsible = {
  Root: UiSdkCollapsible,
  Trigger: UiSdkCollapsibleTrigger,
  Panel: withContext(
    UiSdkCollapsiblePanel,
    () => [collapsibleContent.base, chrome.panel],
    "CollapsiblePanel",
  ),
};
```
