oakoss/ui
Components

Switch

A control that turns a setting on or off right away.

SourceReact Aria
Turns off wireless connections.

Installation

npx shadcn@latest add @oakoss/switch

Usage

import { Switch } from '#/components/ui/inputs/switch';

<Switch description="Turns off wireless connections.">Airplane mode</Switch>;

Examples

Settings row

labelPlacement="start" puts the label first and the switch at the row's end, as in a settings list.

Plays a sound for each new message.

Sizes

size takes sm and md.

Accessibility

React Aria renders a native checkbox with role="switch" inside a <label>, so the label is the switch's name and its click target, and screen readers announce it as on or off. Space toggles it.

  • Description and errors: description and the error describe the switch, with no ids to wire. The error shows only while it's invalid.
  • Required: the asterisk after a required label is hidden from screen readers, and the switch is marked required, but Chrome doesn't expose that state for it and reports it as invalid until it's on, as it does any native one; where it matters, say so in the label or description. validationBehavior="aria" avoids the early "invalid", but then the switch no longer blocks a native form submit.
  • Target size: the track's hit area grows to 44px. Set targetSize={false} where switches sit closer than that; the row stays at least 24px tall.
  • Contrast: the off track reaches 3:1 against the page. In Windows High Contrast the track keeps its outline and the thumb takes system colors, so it stays visible on and off. Right to left, the thumb travels the other way and stays inside the track.

API

Switch

Renders React Aria's SwitchField, with its label in a SwitchButton. Children are the label.

Props

Prop

Type

Also accepts React Aria SwitchField props.

Data attributes

AttributePresent when
data-slotAlways: switch-field on the root, switch on the label, switch-track and switch-thumb
data-sizeOn the label; sm or md
data-selectedIt's on
data-invalidIt's invalid
data-disabledIt's disabled
data-readonlyIt's read-only
data-requiredIt's required
data-hoveredOn the label, while a pointer hovers it
data-pressedOn the label, while it's pressed
data-focus-visibleOn the label, while it has keyboard focus