Switch
A toggle switch with role="switch" and aria-checked — semantically distinct from a checkbox.
Preview
Usage
import { Switch } from "anexui";
<Switch label="Dark mode" />
Copy codeControlled
import { useState } from "react";
import { Switch } from "anexui";
function Example() {
const [on, setOn] = useState(false);
return (
<Switch
label="Notifications"
checked={on}
onChange={(e) => setOn(e.target.checked)}
/>
);
}
Copy codeDefault on
<Switch label="Auto-save" defaultChecked />
Copy codeDisabled
<Switch label="Feature locked" disabled />
<Switch label="Always on" disabled defaultChecked />
Without label
<Switch aria-label="Toggle sidebar" />
Copy codeProps
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | — | Visible text label next to the toggle |
| checked | boolean | — | Controlled on/off state |
| defaultChecked | boolean | false | Uncontrolled initial state |
| disabled | boolean | false | Disables the switch |
| onChange | ChangeEventHandler | — | Change handler |
The type prop is fixed to "checkbox" internally. All other standard <input> attributes are accepted.
Accessibility
- Uses
role="switch"witharia-checkedto distinguish from a regular checkbox - When
labelis omitted, providearia-labeloraria-labelledbyon the component - Keyboard:
Spacetoggles the switch;Enteris not required (but works)