# Switch

Immediate on/off settings built on Base UI.

Use Switch for a setting that takes effect immediately, such as auto-save or background sync. Its label should describe the setting, not change between “on” and “off.” Use [Checkbox](/components/checkbox/) for choices users review before submitting together.

<p className="zenos-base-ui-source">
  Backed by <a href="https://base-ui.com/react/components/switch">Base UI Switch</a> for upstream
  behavior and configuration. Zenos owns the public API, styling, and composition shown on this
  page.
</p>

```tsx
import { Field, Stack, Switch } from "@zentara-co/zenos-ui";

function SwitchExample() {
  return (
    <Field.Root className="w-full max-w-lg rounded-lg border border-border-default p-4">
      <Stack direction="row" gap="4" align="center" justify="between">
        <Stack gap="1">
          <Field.Label htmlFor="monitoring">Continuous monitoring</Field.Label>
          <Field.Description>Scan new assets as they are discovered.</Field.Description>
        </Stack>
        <Switch id="monitoring" defaultChecked />
      </Stack>
    </Field.Root>
  );
}
```

## Import

```tsx
import { Switch } from "@zentara-co/zenos-ui";
```

## Controlled state

Use `defaultChecked` for an uncontrolled setting. Pair `checked` with `onCheckedChange` when application state owns the setting. The callback receives the next boolean state and Base UI event details; the controlled owner must update `checked`.

```tsx
import { Field, Stack, Switch, Text } from "@zentara-co/zenos-ui";
import { useState } from "react";

function SwitchControlledExample() {
  const [checked, setChecked] = useState(true);

  return (
    <Stack gap="3" className="w-full max-w-lg">
      <Field.Root className="rounded-lg border border-border-default p-4">
        <Stack direction="row" gap="4" align="center" justify="between">
          <Stack gap="1">
            <Field.Label htmlFor="auto-save">Auto-save reports</Field.Label>
            <Field.Description>Save changes while you edit a report.</Field.Description>
          </Stack>
          <Switch id="auto-save" checked={checked} onCheckedChange={setChecked} />
        </Stack>
      </Field.Root>
      <Text size="s" color="subtle" aria-live="polite">
        Auto-save is {checked ? "enabled" : "disabled"}.
      </Text>
    </Stack>
  );
}
```

## Required and disabled

`required` participates in native form validation and requires the switch to be checked before submission. A disabled switch cannot be changed and contributes no form value. Pass `disabled` to the switch itself; use `Field.Root disabled` to keep the surrounding label and description consistent. Keep explanatory text visible when policy or permissions make a setting unavailable.

```tsx
import { Field, Separator, Stack, Switch } from "@zentara-co/zenos-ui";

function SwitchStatesExample() {
  return (
    <Stack gap="3" className="w-full max-w-lg">
      <Field.Root>
        <Stack direction="row" gap="4" align="center" justify="between">
          <Stack gap="1">
            <Field.Label htmlFor="required-alerts">Critical alerts</Field.Label>
            <Field.Description>Required for workspace administrators.</Field.Description>
          </Stack>
          <Switch id="required-alerts" name="critical-alerts" value="enabled" required />
        </Stack>
      </Field.Root>
      <Separator />
      <Field.Root disabled>
        <Stack direction="row" gap="4" align="center" justify="between">
          <Stack gap="1">
            <Field.Label htmlFor="managed-audit">Audit collection</Field.Label>
            <Field.Description className="text-text-subtle">
              Managed by your organization.
            </Field.Description>
          </Stack>
          <Switch id="managed-audit" defaultChecked disabled />
        </Stack>
      </Field.Root>
    </Stack>
  );
}
```

## Form value

Set `name` and `value` to include a checked switch in native `FormData`. An unchecked switch contributes no value, following native checkbox behavior.

```tsx
import { Button, Field, Stack, Switch, Text } from "@zentara-co/zenos-ui";
import { useState } from "react";

function SwitchFormExample() {
  const [submittedValue, setSubmittedValue] = useState("Not submitted");

  return (
    <form
      className="w-full max-w-lg"
      onSubmit={(event) => {
        event.preventDefault();
        const value = new FormData(event.currentTarget).get("notifications");
        setSubmittedValue(value?.toString() ?? "off");
      }}
    >
      <Stack gap="4">
        <Field.Root>
          <Stack direction="row" gap="4" align="center" justify="between">
            <Field.Label htmlFor="notification-setting">Weekly summary</Field.Label>
            <Switch id="notification-setting" name="notifications" value="weekly" defaultChecked />
          </Stack>
        </Field.Root>
        <Button type="submit">Save preference</Button>
        <Text size="s" color="subtle" aria-live="polite">
          Submitted value: {submittedValue}
        </Text>
      </Stack>
    </form>
  );
}
```

## Custom styling

Use the usual `className` prop to style the track and `classNameThumb` to style the automatically rendered thumb. No variant or color prop is needed. Use `data-[checked]` and `data-[disabled]` for state-specific classes, and keep a visible `focus-visible` treatment.

When changing dimensions, update the thumb size and checked translation together so it stays inside the track. The example below uses a 52px-wide track with a 2px border, a 24px thumb, and a 24px checked translation. Include matching `dark:` overrides when replacing built-in dark-theme colors.

```tsx
import { Field, Stack, Switch } from "@zentara-co/zenos-ui";

function SwitchCustomExample() {
  return (
    <Field.Root className="w-full max-w-lg">
      <Stack direction="row" gap="4" align="center" justify="between">
        <Stack gap="1">
          <Field.Label htmlFor="custom-sync">Background sync</Field.Label>
          <Field.Description>Keep your workspace up to date.</Field.Description>
        </Stack>
        <Switch
          id="custom-sync"
          defaultChecked
          className="h-7 w-13 rounded-md data-[checked]:bg-background-accent-teal-bolder-default dark:data-[checked]:bg-background-accent-teal-bolder-default"
          classNameThumb="size-6 rounded-sm data-[checked]:translate-x-6"
        />
      </Stack>
    </Field.Root>
  );
}
```

## Compound composition

`Switch` and `Switch.Root` share the same behavior and render a default thumb when no children are supplied. To style the thumb directly or compose its contents, provide `Switch.Thumb` explicitly. Children replace the automatic thumb; `classNameThumb` no longer applies, so use `className` on the explicit thumb instead.

Both parts preserve Base UI state attributes, native props, and refs. Keep `Switch.Thumb` in the composition so checked-state movement and styling continue to work.

```tsx
import { Field, Stack, Switch } from "@zentara-co/zenos-ui";

function SwitchCompoundExample() {
  return (
    <Field.Root className="w-full max-w-lg">
      <Stack direction="row" gap="4" align="center" justify="between">
        <Stack gap="1">
          <Field.Label htmlFor="compact-view">Compact view</Field.Label>
          <Field.Description>Show more items in each list.</Field.Description>
        </Stack>
        <Switch.Root
          id="compact-view"
          defaultChecked
          className="rounded-md data-[checked]:bg-background-accent-purple-bolder-default dark:data-[checked]:bg-background-accent-purple-bolder-default"
        >
          <Switch.Thumb className="rounded-sm" />
        </Switch.Root>
      </Stack>
    </Field.Root>
  );
}
```

## Anatomy

```text
Switch
Switch.Root
Switch.Thumb
```

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| checked | boolean | — | Controlled checked state. |
| defaultChecked | boolean | false | Initial uncontrolled checked state. |
| onCheckedChange | (checked: boolean, eventDetails) => void | — | Runs when state changes. |
| disabled / required | boolean | false | Native form semantics. |
| name / value | string | — | Form submission metadata. |
| className | string | — | Custom track styling. Override checked and disabled states with their data attributes, and preserve a visible focus treatment. |
| classNameThumb | string | — | Classes applied to the automatically rendered thumb. |
| children | ReactNode | — | Replaces the automatic thumb. Render Switch.Thumb explicitly to preserve its state attributes and movement. classNameThumb only applies to the automatic thumb. |
| native props | React.Base UI Switch props | — | Forwards native attributes, events, aria-*, data-*, className, style, and ref. |

Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props, including className, style, aria-*, and data-*.

## Accessibility

Associate every switch with a visible label using matching `id` and `htmlFor` values. Use `aria-label` when the surrounding visual context already explains the setting. Tab moves focus to the switch and Space toggles it; preserve this behavior when customizing its appearance.

Base UI exposes the current state through `role="switch"` and `aria-checked`; disabled and required semantics are preserved through its native form control. Keep the label stable, make both checked states visually distinguishable, and do not rely on track color alone. Preserve thumb movement and a visible keyboard focus indicator.

Source: https://zenos-ui.pages.dev/components/switch/