[ Forms ]
Switch
A toggle for settings that take effect immediately, like turning on notifications or dark mode. Works controlled or uncontrolled and can submit a value with forms.
5 examplesDark modeAccessible
Import#
import { Switch } from 'reusify-ui';When to use#
- Instant on/off settings where the change applies right away.
- If the choice only applies after pressing "Save", use a Checkbox instead.
Examples#
Basic#
<Switch label="Email notifications" defaultChecked />
<Switch label="Beta features" />
<Switch label="Locked setting" disabled />Controlled#
onChange receives the new boolean value.
Connected
const [on, setOn] = useState(true);
<Switch label="Wi-Fi" checked={on} onChange={setOn} />Colors#
color sets the track color when the switch is on.
<Switch color="primary" label="Primary" defaultChecked />
<Switch color="secondary" label="Secondary" defaultChecked />
<Switch color="success" label="Success" defaultChecked />
<Switch color="danger" label="Danger" defaultChecked />
<Switch color="warning" label="Warning" defaultChecked />
<Switch color="info" label="Info" defaultChecked />Sizes#
<Switch size="sm" label="Small" defaultChecked />
<Switch size="md" label="Medium" defaultChecked />
<Switch size="lg" label="Large" defaultChecked />In forms#
Give it a name and a hidden input submits "on" or "off" with the form.
<form action="/settings">
<Switch name="newsletter" label="Newsletter" />
</form>Used in designs#
Switch in real layouts: these 2 designs use it. Open one to try it and copy the code.
API reference#
<Switch />
Forwards its ref to the underlying <button role="switch">.
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | - | On state (controlled). |
defaultChecked | boolean | false | Initial state (uncontrolled). |
onChange | (checked: boolean) => void | - | Called with the new state. |
label | ReactNode | - | Clickable label next to the switch. |
size | 'sm' | 'md' | 'lg' | 'md' | Track and thumb size. |
color | 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'primary' | Track color when on. |
disabled | boolean | false | Disables the switch. |
name | string | - | Renders a hidden input so the value submits with forms. |
id | string | - | id for the switch button. |
checked-booleanOn state (controlled).
defaultCheckedfalsebooleanInitial state (uncontrolled).
onChange-(checked: boolean) => voidCalled with the new state.
label-ReactNodeClickable label next to the switch.
size'md''sm' | 'md' | 'lg'Track and thumb size.
color'primary''primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info'Track color when on.
disabledfalsebooleanDisables the switch.
name-stringRenders a hidden input so the value submits with forms.
id-stringid for the switch button.
Accessibility#
- Rendered as <button role="switch" aria-checked>, so screen readers announce "switch, on/off".
- Space and Enter toggle it; clicking the label also toggles it.