# Switch

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

A sliding on/off toggle. Use when you want an immediate boolean setting that takes
effect without a form submission — e.g. feature flags, notification preferences.

```python
from refast.components.shadcn.controls import Switch

# Controlled — state lives on the server
Switch(
    checked=airplane_mode,
    on_checked_change=ctx.callback(handle_airplane_mode),
)

# Uncontrolled — initial state only
Switch(default_checked=True)

# With a label
Row(children=[
    Switch(id="notifications", on_checked_change=ctx.callback(handle_notifs)),
    Text("Enable notifications"),
])
```

```python
Switch(
    checked=False,
    disabled=False,
    on_checked_change=ctx.callback(handle_change),
)
```

## Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `checked` | `bool \| None` | `None` | Controlled checked state |
| `default_checked` | `bool` | `False` | Initial state (uncontrolled) |
| `disabled` | `bool` | `False` | Prevents interaction |
| `name` | `str \| None` | `None` | HTML input name for form submission |
| `on_checked_change` | `Callback \| None` | `None` | Called with new `bool` value on toggle |
| `id` | `str \| None` | `None` | Element ID — useful for pairing with a `<label>` |
| `class_name` | `str` | `""` | Extra Tailwind classes |

## Controlled vs Uncontrolled

```python
# Controlled — you own the state, server updates drive the UI
async def handle_change(ctx: Context, checked: bool):
    ctx.state.set("airplane_mode", checked)
    await ctx.refresh()

Switch(
    checked=ctx.state.get("airplane_mode", False),
    on_checked_change=ctx.callback(handle_change),
)

# Uncontrolled — React manages internal state, good for simple local toggles
Switch(default_checked=False)
```

## With a Visible Label

```python
Row(
    gap=2,
    class_name="items-center",
    children=[
        Switch(
            id="dark-mode",
            checked=is_dark,
            on_checked_change=ctx.callback(handle_dark_mode),
        ),
        Text("Dark mode", class_name="text-sm cursor-pointer"),
    ],
)
```

## Disabled State

```python
Switch(checked=True, disabled=True)   # locked on
Switch(checked=False, disabled=True)  # locked off
```

---

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