Turns a setting on or off, taking effect immediately.
import { Switch } from "@rdloom/react";
export default function SwitchBasicExample() {
return (
<Switch defaultSelected>Email notifications</Switch>
);
}Installation
npx rdloom add switchCopies the source into src/components/rdloom. Edit it freely: rdloom upgrade merges later versions into your changes. It needs react-aria-components; add --install to install them.
Use another registry client
npx shadcn@latest add https://rdloom.vimalbhatt.com/r/switch.jsonWorks, but without upgrade tracking.
Usage
import { Switch } from "@rdloom/react";
<Switch defaultSelected>Email notifications</Switch>Sizes
import { Switch } from "@rdloom/react";
export default function SwitchSizesExample() {
return (
<div className="flex flex-col gap-3">
<Switch size="sm">Compact mode</Switch>
<Switch size="md">Dark mode</Switch>
</div>
);
}Disabled
import { Switch } from "@rdloom/react";
export default function SwitchDisabledExample() {
return (
<div className="flex flex-col gap-3">
<Switch isDisabled>Auto-update</Switch>
<Switch isDisabled defaultSelected>
Backups
</Switch>
</div>
);
}API Reference
Defined by the spec. Components also accept the props of the React Aria component they wrap.
| Prop | Type | Default |
|---|---|---|
childrenrequiredVisible label describing the setting. | node | none |
isSelectedControlled on/off state. | boolean | none |
defaultSelectedInitial state when uncontrolled. | boolean | false |
sizeTrack and thumb size. | "sm" | "md" | "md" |
isDisabledPrevents interaction and dims the switch. | boolean | false |
onChangeCalled with the new state. | (isSelected: boolean) => void | none |
Accessibility
Role switch, WCAG 2.2 AA. Tested with axe and keyboard tests; screen reader checks are in the audit checklist.
Keyboard
- Space toggles
Screen readers announce
- Announced as a switch (or toggle button) with its label and on or off
- Toggling announces the new state
What your code must do
- Label is part of the click target
- State is not conveyed by color alone (thumb position also changes)
Guidelines
Use it when
- Settings that apply instantly, like dark mode or notifications
Avoid it when
- Choices that only apply after pressing Save: use Checkbox
Don't
- Labels that change with the state, like 'On'/'Off'
Design tokens
The semantic tokens this component uses. Change them once and every component follows; see Design tokens.
--rd-color-action-primary--rd-color-border-strong--rd-color-surface-default--rd-color-text-default--rd-color-focus-ring