# Collapsible

- **Section:** Components
- **URL:** https://refast.fastapicloud.dev/docs/components/collapsible
- **Markdown:** https://refast.fastapicloud.dev/docs/components/collapsible.md
- **Source:** https://github.com/idling-mind/refast-docs/blob/main/docs/pages/components/cards/collapsible.py
- **Live examples:** the widgets on this page need a browser; open the URL above to try them.
- **Applies to:** refast 0.12.2

An expandable section that reveals or hides content. Supports both
**uncontrolled** (browser manages state) and **controlled** (Python manages
state via a callback) usage.

```python
from refast.components import Collapsible, CollapsibleTrigger, CollapsibleContent

# Uncontrolled — browser manages open state
Collapsible(
    default_open=False,
    children=[
        CollapsibleTrigger(children=[Button("Show details")]),
        CollapsibleContent(children=[Text("The details are here.")]),
    ],
)
```

Click the button to toggle (Python manages open state via on_open_change):

Toggle section ↕

This content is revealed when the collapsible is open.

You can put any components here.

```python
Collapsible(
    open=False,
    disabled=False,
    children=[
        CollapsibleTrigger(
            children=[Button("Toggle section", on_click=ctx.callback(toggle))]
        ),
        CollapsibleContent(
            children=[Text("Hidden content")]
        ),
    ],
)
```

## Collapsible Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `open` | `bool \| None` | `None` | Controlled open state. Omit for uncontrolled. |
| `default_open` | `bool` | `False` | Initial open state (uncontrolled only). |
| `on_open_change` | `Callback \| None` | `None` | Fired when the open state changes. |
| `disabled` | `bool` | `False` | Prevents toggling. |
| `children` | `list` | `[]` | A `CollapsibleTrigger` and a `CollapsibleContent`. |
| `class_name` | `str` | `""` | Extra Tailwind classes. |

## CollapsibleTrigger Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `children` | `list` | `[]` | The element that triggers open/close. |
| `as_child` | `bool` | `True` | Merges props onto the child element (Radix `asChild`). |
| `class_name` | `str` | `""` | Extra Tailwind classes. |

## CollapsibleContent Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `children` | `list` | `[]` | Content revealed when open. |
| `class_name` | `str` | `""` | Extra Tailwind classes. |

## Uncontrolled usage

```python
# Browser manages state — no server round-trip needed
Collapsible(
    default_open=True,
    children=[
        CollapsibleTrigger(children=[Button("Toggle")]),
        CollapsibleContent(children=[Text("Expanded by default.")]),
    ],
)
```

## Controlled usage

```python
async def toggle(ctx: Context):
    current = ctx.state.get("is_open", False)
    ctx.state.set("is_open", not current)
    await ctx.refresh()

def render(ctx: Context):
    is_open = ctx.state.get("is_open", False)
    return Collapsible(
        open=is_open,
        children=[
            CollapsibleTrigger(
                children=[Button("Toggle", on_click=ctx.callback(toggle))]
            ),
            CollapsibleContent(children=[Text("Controlled content.")]),
        ],
    )
```

## on_open_change callback

```python
async def on_open_change(ctx: Context):
    # ctx.event_data is the new boolean open state
    ctx.state.set("is_open", ctx.event_data)
    await ctx.refresh()

Collapsible(
    open=is_open,
    on_open_change=ctx.callback(on_open_change),
    children=[
        CollapsibleTrigger(children=[Button("Toggle")]),
        CollapsibleContent(children=[Text("Content.")]),
    ],
)
```

---

Part of the Refast documentation: [all pages](https://refast.fastapicloud.dev/llms.txt), [everything on one page](https://refast.fastapicloud.dev/llms-full.txt), [search](https://refast.fastapicloud.dev/api/search?q=save_prop).
