Bedform UIControls

Switch

Turn on or off.

Switch

Off
*
*
boolean
*
boolean
*
string
*

Usage

import { Switch } from "@bedform/ui/switch";
import { useState } from "react";

export function AutosaveSwitch() {
  const [checked, setChecked] = useState(false);

  return (
    <Switch
      checked={checked}
      name="autosave"
      value="enabled"
      variant="icons"
      onChange={(event) => {
        setChecked(event.currentTarget.checked);
      }}
    >
      Enable autosave
    </Switch>
  );
}

Switch renders a native checkbox with role="switch" inside a label, and its children become the label text.

Variants

<Switch>Default</Switch>
<Switch variant="icons">Icons</Switch>

Colors

Use color to change the checked state color.

<Switch color="primary" defaultChecked aria-label="Primary" />
<Switch color="secondary" defaultChecked aria-label="Secondary" />
<Switch color="destructive" defaultChecked aria-label="Destructive" />
<Switch color="dungeons" defaultChecked aria-label="Dungeons" />
<Switch color="legends" defaultChecked aria-label="Legends" />
<Switch color="realms" defaultChecked aria-label="Realms" />
<Switch color="gold" defaultChecked aria-label="Gold" />

Forms

Checked values are included in FormData, and reset restores the initial defaultChecked state.

Events

The native input dispatches input and change events after its checked state changes, so onChange works as it does on any checkbox.

API

Switch accepts every checkbox attribute, such as checked, defaultChecked, disabled, name, required and value. Its ref points to the input.

PropTypeDefault
color"primary" | "secondary" | "destructive" | "dungeons" | "legends" | "realms" | "gold""primary"
labelClassNamestring—
variant"default" | "icons""default"

CSS variables

VariablePurpose
--ore-switch-unitBase size unit
--ore-switch-track-backgroundInactive track background
--ore-switch-track-highlightInactive track highlight
--ore-switch-track-borderTrack border color
--ore-switch-thumb-backgroundThumb background
--ore-switch-thumb-highlightThumb highlight
--ore-switch-thumb-shadowThumb shadow
--ore-switch-thumb-borderThumb border color
--ore-switch-active-track-backgroundActive track background
--ore-switch-active-track-highlightActive track highlight
--ore-switch-iconThumb icon color

Accessibility

The native checkbox provides form and keyboard behavior, while role="switch" exposes switch semantics. Use a label to describe the control.

On this page