# Toggle

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

Two-state buttons that can be toggled on or off. `Toggle` is a single button;
`ToggleGroup` groups multiple toggles with single or multiple selection modes.

```python
from refast.components.shadcn.controls import Toggle, ToggleGroup, ToggleGroupItem

# Single toggle
Toggle("Bold", icon="bold", pressed=True, on_pressed_change=ctx.callback(handle_toggle))

# ToggleGroup — single selection
ToggleGroup(
    type="single",
    children=[
        ToggleGroupItem("Left", icon="align-left", value="left"),
        ToggleGroupItem("Center", icon="align-center", value="center"),
        ToggleGroupItem("Right", icon="align-right", value="right"),
    ],
    on_value_change=ctx.callback(handle_alignment),
)
```

Toggle

```python
Toggle(
    label="Bold",
    icon="bold",
    variant="default",
    size="md",
    disabled=False,
    pressed=False,
    on_pressed_change=ctx.callback(handle_toggle),
)
```

ToggleGroup

Selected: []

```python
ToggleGroup(
    type="single",
    on_value_change=ctx.callback(handle_change),
    children=[
        ToggleGroupItem("Bold", icon="bold", value="bold"),
        ToggleGroupItem("Italic", icon="italic", value="italic"),
        ToggleGroupItem("Underline", icon="underline", value="underline"),
    ],
)
```

## Toggle Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `label` | `str` | `""` | Button text label |
| `icon` | `str \| None` | `None` | Lucide icon name |
| `pressed` | `bool \| None` | `None` | Controlled pressed state |
| `default_pressed` | `bool` | `False` | Initial pressed state (uncontrolled) |
| `disabled` | `bool` | `False` | Prevents interaction |
| `variant` | `"default" \| "outline"` | `"default"` | Visual style |
| `size` | `"sm" \| "md" \| "lg"` | `"md"` | Button size |
| `on_pressed_change` | `Callback \| None` | `None` | Called with new pressed state |
| `class_name` | `str` | `""` | Extra Tailwind classes |

## ToggleGroup Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `type` | `"single" \| "multiple"` | `"single"` | Selection mode |
| `value` | `str \| list[str] \| dict[str, bool] \| None` | `None` | Controlled value |
| `default_value` | `str \| list[str] \| dict[str, bool] \| None` | `None` | Initial value (uncontrolled) |
| `disabled` | `bool` | `False` | Disables all items |
| `variant` | `"default" \| "outline"` | `"default"` | Visual style applied to all items |
| `size` | `"sm" \| "md" \| "lg"` | `"md"` | Size applied to all items |
| `on_value_change` | `Callback \| None` | `None` | Called with the new value |
| `children` | `list` | *(required)* | `ToggleGroupItem` components |
| `class_name` | `str` | `""` | Extra Tailwind classes |

## ToggleGroupItem Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `label` | `str` | `""` | Item text |
| `icon` | `str \| None` | `None` | Lucide icon name |
| `value` | `str \| None` | `None` | Unique value for this item |
| `name` | `str \| None` | `None` | Alternative identifier |
| `disabled` | `bool` | `False` | Disables this specific item |

## Examples

```python
# Icon-only toolbar (alignment)
ToggleGroup(
    type="single",
    default_value="left",
    children=[
        ToggleGroupItem(icon="align-left", value="left"),
        ToggleGroupItem(icon="align-center", value="center"),
        ToggleGroupItem(icon="align-right", value="right"),
        ToggleGroupItem(icon="align-justify", value="justify"),
    ],
)

# Multiple selection (text formatting)
ToggleGroup(
    type="multiple",
    children=[
        ToggleGroupItem("B", icon="bold", value="bold"),
        ToggleGroupItem("I", icon="italic", value="italic"),
        ToggleGroupItem("U", icon="underline", value="underline"),
    ],
    on_value_change=ctx.callback(handle_format),
)

# Outline variant
Toggle("Mute", icon="volume-x", variant="outline", on_pressed_change=ctx.callback(handle_mute))
```

---

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).
