# Collapsible
One section shown and hidden by a person or an agent.
Source page: https://dinachi.dev/docs/components/collapsible

## Installation
```bash
npx @dinachi/cli@latest add collapsible
```
Adds to your project:
- `components/ui/collapsible/collapsible.tsx`
- `components/ui/collapsible/index.ts`
Installs:
- `@base-ui/react` ^1.9.0

## Usage
```tsx
import { ChevronDown } from "lucide-react"
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "@/components/ui/collapsible"

export function ShippingDetails() {
  return (
    <Collapsible className="max-w-sm">
      <CollapsibleTrigger>
        <span>Shipping details</span>
        <ChevronDown className="h-4 w-4 transition-transform" />
      </CollapsibleTrigger>
      <CollapsiblePanel>
        <p className="px-4 pb-4 text-muted-foreground">Ships in 2 to 3 working days.</p>
      </CollapsiblePanel>
    </Collapsible>
  )
}
```

## Anatomy
```tsx
<Collapsible>
  <CollapsibleTrigger />
  <CollapsiblePanel />
</Collapsible>
```
- `Collapsible`: Holds the open state.
- `CollapsibleTrigger`: The full-width button that toggles the panel.
- `CollapsiblePanel`: The content. Height animates open and closed.

## Examples

### Default
A section that opens from its trigger.
```tsx
import { Collapsible, CollapsibleTrigger, CollapsiblePanel } from '@/components/ui/collapsible';
import { ChevronDown } from 'lucide-react';

export function Example() {
  return (
    <Collapsible className="w-full max-w-sm">
      <CollapsibleTrigger>
        <span>View more details</span>
        <ChevronDown className="h-4 w-4 transition-transform" />
      </CollapsibleTrigger>
      <CollapsiblePanel>
        <div className="px-4 pb-4 text-sm text-muted-foreground">
          This is the collapsible content. It can contain any elements you need,
          including text, images, or other components.
        </div>
      </CollapsiblePanel>
    </Collapsible>
  );
}
```

### Controlled
The open state lives in your state.
```tsx
import React from 'react';
import { Collapsible, CollapsibleTrigger, CollapsiblePanel } from '@/components/ui/collapsible';
import { ChevronDown } from 'lucide-react';

export function Example() {
  const [open, setOpen] = React.useState(false);

  return (
    <div className="w-full max-w-sm space-y-2">
      <Collapsible open={open} onOpenChange={setOpen}>
        <CollapsibleTrigger>
          <span>{open ? 'Hide' : 'Show'} settings</span>
          <ChevronDown className="h-4 w-4 transition-transform" />
        </CollapsibleTrigger>
        <CollapsiblePanel>
          <div className="space-y-2 px-4 pb-4">
            <div className="rounded-md border px-4 py-3 text-sm">Setting 1</div>
            <div className="rounded-md border px-4 py-3 text-sm">Setting 2</div>
            <div className="rounded-md border px-4 py-3 text-sm">Setting 3</div>
          </div>
        </CollapsiblePanel>
      </Collapsible>
      <p className="text-xs text-muted-foreground px-4">
        State: {open ? 'Open' : 'Closed'}
      </p>
    </div>
  );
}
```

## Notes

### Chevron
The trigger rotates a direct child `svg` 180 degrees while open. Put the icon directly inside `CollapsibleTrigger`, unwrapped, with `transition-transform` to animate it.

### Several sections
For related sections sharing one open state, use [Accordion](/docs/components/accordion).

## API reference
Props marked **agent** change what an agent reads or may do.

### Collapsible
Holds the open state.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | - | Controlled open state. |
| `defaultOpen` | `boolean` | `false` | Starting state when uncontrolled. |
| `onOpenChange` **agent** | `(open, details) => void` | - | Runs for people and agents. `details.cancel()` refuses the change. |
| `disabled` **agent** | `boolean` | `false` | Blocks the trigger, for people and agents. |

### CollapsibleTrigger
The full-width button that toggles the panel.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` **agent** | `ReactNode` | - | Also the name the agent reads. |
| `render` | `ReactElement` | - | Swap the element, keep the behaviour. |

### CollapsiblePanel
The content. Height animates open and closed.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `keepMounted` | `boolean` | `false` | Keeps the content in the DOM while closed. |
| `hiddenUntilFound` | `boolean` | `false` | Lets find in page reach the content and open the panel. |

## Agent
Inside an `AgentProvider`, Collapsible appears in `agent.snapshot()` as `disclosure`.
Reads:
- `label`: The trigger's text, or its aria-label.
- `open`: Whether the panel is showing.
- `disabled`: True when the collapsible is disabled.
Actions:
- `open() / close()`: Whichever fits the current state. Panel content is listed once open.
An agent's change runs through `onOpenChange` like a click on the trigger. `isAgentChange(details)` is true for it, and `details.cancel()` refuses it; the agent is told the app refused. A disabled collapsible refuses on its own.
```ts
await agent.act({
  steps: [{ ref: "View more details", action: "open" }],
})
```

## Accessibility
- The trigger is a button with aria-expanded, and aria-controls on the panel while open.
- The trigger's text is its name, for screen readers and the agent.
- Reduced motion turns off the height animation.
### Keyboard
| Keys | Does |
| --- | --- |
| `Enter` / `Space` | Opens or closes the panel |

## Source
`components/ui/collapsible/collapsible.tsx`
```tsx
"use client"

import * as React from "react"
import { Collapsible as BaseCollapsible } from "@base-ui/react/collapsible"
import { cn } from "@/lib/utils"
import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"

const CollapsibleAgentContext = React.createContext<AgentOpenParts | null>(null)

/** Base UI's root, holding its own open state so an agent can open and close it. */
const Collapsible = React.forwardRef<
  React.ComponentRef<typeof BaseCollapsible.Root>,
  React.ComponentPropsWithoutRef<typeof BaseCollapsible.Root>
>(({ className, open, defaultOpen, onOpenChange, ...props }, ref) => {
  const [state, change, parts] = useAgentOpen("disclosure", open, defaultOpen, onOpenChange)
  return (
    <CollapsibleAgentContext.Provider value={useHasAgent() ? parts : null}>
      <BaseCollapsible.Root data-slot="collapsible" ref={ref} className={cn("w-full", className)} {...props} open={state} onOpenChange={change} />
    </CollapsibleAgentContext.Provider>
  )
})
Collapsible.displayName = "Collapsible"

const CollapsibleTrigger = React.forwardRef<
  React.ComponentRef<typeof BaseCollapsible.Trigger>,
  React.ComponentPropsWithoutRef<typeof BaseCollapsible.Trigger>
>(({ className, ...props }, ref) => (
  <BaseCollapsible.Trigger
    data-slot="collapsible-trigger"
    ref={useMergedRef(ref, React.useContext(CollapsibleAgentContext)?.trigger)}
    className={cn(
      "flex w-full items-center justify-between rounded-md p-4 font-medium transition-colors",
      "hover:bg-accent hover:text-accent-foreground",
      "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
      "disabled:pointer-events-none disabled:opacity-50",
      "[&[data-panel-open]>svg]:rotate-180",
      className
    )}
    {...props}
  />
))
CollapsibleTrigger.displayName = "CollapsibleTrigger"

const CollapsiblePanel = React.forwardRef<
  React.ComponentRef<typeof BaseCollapsible.Panel>,
  React.ComponentPropsWithoutRef<typeof BaseCollapsible.Panel>
>(({ className, ...props }, ref) => (
  <BaseCollapsible.Panel
    data-slot="collapsible-panel"
    ref={ref}
    className={cn(
      "overflow-hidden text-sm h-[var(--collapsible-panel-height)]",
      "transition-[height] duration-[var(--motion-duration-base,200ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
      "data-[ending-style]:h-0 data-[starting-style]:h-0",
      "motion-reduce:transition-none",
      className
    )}
    {...props}
  />
))
CollapsiblePanel.displayName = "CollapsiblePanel"

export type CollapsibleProps = React.ComponentPropsWithoutRef<typeof BaseCollapsible.Root>
export type CollapsibleTriggerProps = React.ComponentPropsWithoutRef<typeof BaseCollapsible.Trigger>
export type CollapsiblePanelProps = React.ComponentPropsWithoutRef<typeof BaseCollapsible.Panel>

export {
  Collapsible,
  CollapsibleTrigger,
  CollapsiblePanel,
}
```
