Back to Onyx

Switch

web/lib/opal/src/components/inputs/switch/README.md

4.1.01.3 KB
Original Source

Switch

An accessible toggle switch with hover, focus, and disabled states.

Supports both controlled and uncontrolled modes.

Usage

tsx
import { Switch } from "@opal/components";

// Uncontrolled
<Switch defaultChecked onCheckedChange={(checked) => console.log(checked)} />

// Controlled
<Switch checked={value} onCheckedChange={setValue} />

// Disabled
<Switch disabled />

Props

PropTypeDefaultDescription
checkedbooleanundefinedControlled checked state.
defaultCheckedbooleanfalseInitial state for uncontrolled mode.
onCheckedChange(checked: boolean) => voidundefinedCalled with the new value when toggled.
disabledbooleanfalseDisables interaction and applies muted styles.

All other <button> attributes (except style, className, and onChange) are forwarded.

Visual States

StateTrack colorThumb color
Offbackground-tint-03background-neutral-light-00
Onaction-link-05background-neutral-light-00
Disabled offbackground-neutral-04background-neutral-03
Disabled onaction-link-03background-neutral-03

Hover darkens the track by one step; focus adds a border ring.