Switch
A control that allows the user to toggle between checked and not checked.Usage
1import { Switch } from "@raystack/apsara";
Switch Props
Prop
Type
Examples
State
The Switch component supports various states to handle different interaction scenarios.
1<Flex gap="large" align="center">2 <Switch />3 <Switch defaultChecked />4 <Switch disabled />5 <Switch disabled defaultChecked />6 <Switch required />7</Flex>
Size Variants
The Switch component comes in two sizes: large (default) and small.
1<Flex gap="large" align="center">2 <Switch size="large" />3 <Switch size="large" defaultChecked />4 <Switch size="small" />5 <Switch size="small" defaultChecked />6</Flex>
Controlled Usage
Use the Switch component in a controlled manner to manage its state externally.
1(function ControlledSwitch() {2 const [checked, setChecked] = React.useState(false);3 return <Switch checked={checked} onCheckedChange={setChecked} />;4})
Accessibility
The Switch component follows WAI-ARIA guidelines for toggle buttons:
- Uses proper ARIA attributes (
aria-checked,aria-required,aria-label) - Supports keyboard navigation (Space and Enter to toggle)
- Includes proper labeling and description support
- Changes cursor to 'not-allowed' when disabled
- Associates labels with the switch using htmlFor