Switch
Toggle a boolean value with an animated pill switch and optional label and help text.
Switch is a client component that renders a controlled toggle built on the Headless UI Switch, with the thumb animated by motion/react. Use it for on/off settings. It does not manage its own state; pass value and update it from onChange.
Import#
import { Switch } from "@reactberry/system/blocks";
Usage#
import { useState } from "react";
import { Switch } from "@reactberry/system/blocks";
import { Box } from "@reactberry/system/elements";
export default function NotificationSettings() {
const [enabled, setEnabled] = useState(false);
return (
<Box width="20rem">
<Switch
value={enabled}
onChange={setEnabled}
label="Email notifications"
help="Receive a summary of activity once a day."
/>
</Box>
);
}
Examples#
Without a visible label#
When neither label nor help is provided, only the switch track is rendered. Pass ariaLabel to label it for assistive technology.
Custom row layout#
Spread props override the row defaults; here flexDirection="row-reverse" places the switch before its label.
API#
Switch#
| Prop | Type | Default | Description |
|---|---|---|---|
value | boolean | false | Whether the switch is on. Controls the track colour ( |
onChange | ((newValue: boolean) => void) | — | Called with the inverted |
label | string | — | Bold primary text shown before the switch |
help | string | — | Secondary text shown below the label in a smaller size |
ariaLabel | string | — | Accessible label for the switch. Falls back to |
Any other props are spread onto the outer row (a Text rendered as a div with display="flex", gap="small", alignItems="center", justifyContent="space-between" and width="100%"), so layout and style props can override those defaults.
Accessibility#
- The inner switch element is given
role="checkbox",aria-checked, andtabIndex=0. Itsaria-labelisariaLabel, falling back tolabel. - A hidden native checkbox input (
display: none) is also rendered and kept in sync withvalue. - Keyboard toggling is handled by the Headless UI
Switch, but noonChangeis passed to it directly, so keyboard activation may not call youronChange. Verify keyboard behaviour if it is required.
Notes#
- The whole row is clickable;
onChangefires from the row'sonClick, so clicks on the label and help text also toggle the value.