# Switch

> A toggle for an on or off setting that takes effect immediately.

Use a switch for a setting that applies as soon as it changes, like turning on notifications. If the change only applies after a form is submitted, use a [checkbox](/docs/components/checkbox).

```tsx
import { Switch } from "@aloxe/react";

export default function SwitchBasic() {
  return (
    <div className="flex flex-col gap-4">
      <Switch label="Dark mode" />
      <Switch label="Show line numbers" defaultChecked />
    </div>
  );
}
```

## Installation

```bash
npm i @aloxe/react @aloxe/tokens
```

Then add the CSS imports from [Installation](/docs/getting-started/installation).

## Usage

```tsx
import { Switch } from "@aloxe/react";

<Switch label="Weekly report" checked={enabled} onCheckedChange={setEnabled} />
```

Write the label as the setting's name, not as a question or an action.

## Examples

### States

A description under the label, and disabled switches, on and off.

```tsx
import { Switch } from "@aloxe/react";

export default function SwitchStates() {
  return (
    <div className="flex max-w-sm flex-col gap-4">
      <Switch label="Weekly report" description="Sent every Monday at 9:00." defaultChecked />
      <Switch
        label="Single sign-on"
        description="Managed by your identity provider."
        defaultChecked
        disabled
      />
      <Switch label="Audit log export" disabled />
    </div>
  );
}
```

### Controlled

Pass `checked` and `onCheckedChange` and apply the setting in the handler.

```tsx
"use client";

import { Switch } from "@aloxe/react";
import { useState } from "react";

export default function SwitchControlled() {
  const [paused, setPaused] = useState(false);

  return (
    <div className="flex flex-col gap-3">
      <Switch label="Pause notifications" checked={paused} onCheckedChange={setPaused} />
      <p className="aloxe-text-body-sm text-ax-text-secondary">
        {paused ? "Notifications are paused." : "You will be notified as things happen."}
      </p>
    </div>
  );
}
```

## Accessibility

| Key | Action |
| --- | --- |
| Tab | Moves focus to the switch. |
| Space | Turns it on or off. |
| Enter | Turns it on or off. |

- Renders `role="switch"` with `aria-checked`, so screen readers announce it as on or off.
- The on state is shown by the thumb position as well as the colour.
- The track is 36 × 20 px with a 24 px tall hit area; with a visible label the whole row is clickable.

## API reference

Every part accepts `className` and renders a `data-aloxe` attribute you can target in CSS, such as `[data-aloxe="switch"]`.

**Switch** (extends Base UI Switch.Root)

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `label` | `ReactNode` |  | Yes | The visible label and the switch's accessible name. |
| `checked` | `boolean` |  |  | Controlled state. Pair with onCheckedChange. |
| `defaultChecked` | `boolean` | `false` |  | Initial state when uncontrolled. |
| `onCheckedChange` | `(checked: boolean, details) => void` |  |  | Called when the user toggles the switch. Apply the setting right away. |
| `showLabel` | `boolean` | `true` |  | false hides the label visually only. |
| `description` | `ReactNode` |  |  | Supporting text under the label, linked with aria-describedby. |
| `error` | `ReactNode` |  |  | Error message. Sets aria-invalid. |
| `disabled` | `boolean` | `false` |  | Disables the switch and dims its label. |
| `name` | `string` |  |  | Form field name. |
| `className` | `string` |  |  | Classes for the wrapper. |

## Figma

Matches the **Switch** component. Figma `Checked` (`Off`, `On`) maps to `checked`; `Label` and `Show label` map to `label` and `showLabel`.

See [Figma](/docs/getting-started/figma) for where to get the file and how its names map to code.
