Blocks Design System

Switch

An on/off setting on Base UI, for choices that take effect as a setting rather than a selection. Pass the label as children. Use Checkbox when the choice is part of a form the reader submits as a list of options.

tsx
import { Switch } from '@blocks/design-system'

Switch is a client component. A Server Component can render it uncontrolled with defaultChecked; checked and onCheckedChange belong in a client-authored caller.

In a native form

With a name, a checked switch submits its value (on unless you set one); an unchecked switch submits nothing, like a checkbox.

Source · switch/public-station.tsx
switch/public-station.tsx
// A Server Component: an uncontrolled switch submits with a native form.
import { Button, Stack, Switch } from '@blocks/design-system'

export default function PublicStation() {
  return (
    <Stack as="form" action="/components/switch" method="get" gap="sm" aria-label="Station sharing">
      <Switch name="public" value="yes" defaultChecked>
        Show on the public map
      </Switch>
      <div>
        <Button type="submit" variant="primary">
          Save sharing
        </Button>
      </div>
    </Stack>
  )
}
Station sharing

Controlled

Own the state when something else on the page depends on it, and say what the setting is now.

Digest emails are off.

Source · switch/digest.tsx
switch/digest.tsx
'use client'

import { Body, Stack, Switch } from '@blocks/design-system'
import { useState } from 'react'

export default function Digest() {
  const [enabled, setEnabled] = useState(false)
  return (
    <Stack gap="xs">
      <Switch checked={enabled} onCheckedChange={setEnabled}>
        Weekly snow digest
      </Switch>
      <Body margin="none" role="status">
        Digest emails are {enabled ? 'on' : 'off'}.
      </Body>
    </Stack>
  )
}
Weekly digest

Inside a Field

Without children, Switch takes its label and description from a surrounding Field.

Turn off to save battery on solar-powered stations.

Source · switch/in-field.tsx
switch/in-field.tsx
import { Field, FieldDescription, FieldLabel, Switch } from '@blocks/design-system'

export default function InField() {
  return (
    <Field name="autoSync">
      <FieldLabel>
        <Switch defaultChecked />
        Sync readings every hour
      </FieldLabel>
      <FieldDescription>Turn off to save battery on solar-powered stations.</FieldDescription>
    </Field>
  )
}
Hourly sync

Props

Switch props
PropTypeDefaultDescription
checkedboolean—Whether the switch is currently active. To render an uncontrolled switch, use the `defaultChecked` prop instead.
childrenReactNode—The switch's label, rendered next to it inside a `<label>`.
defaultCheckedbooleanfalseWhether the switch is initially active. To render a controlled switch, use the `checked` prop instead.
disabledbooleanfalseWhether the component should ignore user interaction.
formstring—Identifies the form that owns the hidden input. Useful when the switch is rendered outside the form.
idstring—The id of the hidden input element. When `nativeButton` is `true`, the id is applied to the root element.
inputRefRef<HTMLInputElement>—A ref to access the hidden `<input>` element.
namestring—Identifies the field when a form is submitted.
nativeButtonbooleantrueWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).
onCheckedChange((checked: boolean, eventDetails: { … }) => void)—Event handler called when the switch is activated or deactivated.
readOnlybooleanfalseWhether the user should be unable to activate or deactivate the switch.
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, SwitchRootState>—Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render.
requiredbooleanfalseWhether the user must activate the switch before submitting a form.
uncheckedValuestring—The value submitted with the form when the switch is off. By default, unchecked switches do not submit any value, matching native checkbox behavior.
valuestring—The value submitted with the form when the switch is on. By default, switch submits the "on" value, matching native checkbox behavior.

Also accepts styling escape hatches and a ref, and the native attributes of the element it renders.

Accessibility

  • Switch has the switch role and announces “on” or “off”. Space and Enter toggle it; clicking the label does too.
  • Label the setting, not the action: “Weekly snow digest”, not “Turn on digest”.
  • If turning a switch on has a delay or can fail, show the outcome nearby in words.