# Toggle

Turn a setting on or off using a compact, button-shaped control.

```tsx
import { Toggle } from '@fluxui-dev/react';
import { BoltIcon } from '@fluxui-dev/react/icons';

<Toggle name="fast_mode" icon={BoltIcon} tooltip="Fast mode" />
```

## Saving changes

Save the new value in `onCheckedChange` and pass `loading` while the request runs, which shows a spinner and ignores further presses.

```tsx
import { router } from '@inertiajs/react';
import { LightBulbIcon } from '@fluxui-dev/react/icons';

const [saving, setSaving] = useState(false);

<Toggle
    icon={LightBulbIcon}
    tooltip="Active project"
    defaultChecked={project.active}
    loading={saving}
    onCheckedChange={(active) =>
        router.patch(`/projects/${project.id}`, { active }, {
            onStart: () => setSaving(true),
            onFinish: () => setSaving(false),
        })
    }
/>
```

## Sizes

Use the `size` prop to fit toggles into different interface densities.

```tsx
<Toggle icon={BoltIcon} />

<Toggle icon={BoltIcon} size="sm" />

<Toggle icon={BoltIcon} size="xs" />
```

## Variants

Choose a treatment that matches the surrounding controls.

```tsx
<Toggle icon={BoltIcon} />

<Toggle icon={BoltIcon} variant="filled" />

<Toggle icon={BoltIcon} variant="ghost" />

<Toggle icon={BoltIcon} variant="subtle" />
```

## Color

Use the `color` prop to customize the active state. The inactive state remains neutral.

```tsx
import { BellIcon, BoltIcon, HeartIcon } from '@fluxui-dev/react/icons';

<Toggle icon={BoltIcon} color="blue" defaultChecked />

<Toggle icon={HeartIcon} color="red" defaultChecked />

<Toggle icon={BellIcon} color="amber" defaultChecked />
```

## With label

Use the `label` prop to add short text when the setting benefits from a persistent visible label.

```tsx
<Toggle icon={BoltIcon} label="Fast mode" />
```

## Stateful icon

Use the `iconOn` and `iconOff` props to show a different icon for each state.

```tsx
import { SpeakerWaveIcon, SpeakerXMarkIcon } from '@fluxui-dev/react/icons';

<Toggle iconOn={SpeakerWaveIcon} iconOff={SpeakerXMarkIcon} tooltip="Toggle sound" />
```

## Custom icons

Pass an element to `iconOn` and `iconOff` for full control over each state's icon.

```tsx
import { Shrink, Expand } from 'lucide-react';

<Toggle
    tooltip="Toggle fullscreen"
    iconOn={<Shrink className="size-5 text-accent-content" />}
    iconOff={<Expand className="size-5 text-zinc-500/85 dark:text-zinc-300/80" />}
/>
```

## Stateful label

Use the `labelOn` and `labelOff` props to show different text for each state.

```tsx
<Toggle labelOn="Sound on" labelOff="Sound off" />
```

## Reference

### Toggle

**Prop:**

- `icon` - Icon component, drawn outline while off and solid while on. An element is rendered as is in both states.
- `iconOn` - Icon shown when on. Overrides `icon` for this state.
- `iconOff` - Icon shown when off. Use with `iconOn` or `icon`.
- `iconVariant` - Icon variant for both states. Options: `outline`, `solid`, `mini`, `micro`. Default: `solid` when on, `outline` when off.
- `label` - Label shown in both states. `children` may be used instead and take precedence.
- `labelOn` - Label shown when on. Overrides `label` for this state.
- `labelOff` - Label shown when off. Use with `labelOn`, `label`, or `children`.
- `size` - Control size. Options: `base` (default), `sm`, `xs`.
- `variant` - Visual treatment. Options: `outline` (default), `filled`, `ghost`, `subtle`.
- `color` - Color of the active icon. The inactive state and control surface remain neutral. Defaults to the application's accent color.
- `tooltip` - Tooltip text, or a `Tooltip` element without children to set its `position` and `kbd`. Recommended for icon-only toggles because it also supplies the accessible name.
- `checked` - Controlled checked state, paired with `onCheckedChange`.
- `defaultChecked` - Initial checked state when uncontrolled.
- `onCheckedChange` - Callback fired with the new checked state when the toggle is pressed.
- `name` - Name submitted with the form while the toggle is on.
- `value` - Value submitted while the toggle is on. Default: `on`.
- `uncheckedValue` - Value submitted while the toggle is off. By default nothing is submitted.
- `loading` - Shows a loading indicator and ignores presses while `true`.
- `inset` - Negative margins that cancel the toggle's padding on the given sides, for ghost and subtle toggles. `true` or sides such as `"top bottom"`.
- `disabled` - Prevents user interaction with the toggle.
- `className` - CSS classes applied to the toggle's button.

**Slot:**

- `children` - Visible label content.

**Attribute:**

- `data-flux-toggle` - Applied to the root element for styling and identification.
- `data-checked` - Applied when the toggle is on.
- `data-loading` - Applied while `loading` is `true`.
