Switch
Toggles a setting that takes effect immediately. Renders a native checkbox with
role="switch", so keyboard and form behaviour come from the platform.
import { Switch } from "antlitz";
API
| Prop | Type | Default |
|---|---|---|
| radius | "none" | "sm" | "md" | "lg" | "full" | full |
| size | "sm" | "md" | "lg" | md |
Because it is a checkbox underneath, every input attribute applies: checked,
defaultChecked, disabled, name, onChange.
Usage
export function Basic() {
return (
<div className="flex items-center gap-2">
<Switch id="airplane" defaultChecked />
<Label htmlFor="airplane">Aeroplane mode</Label>
</div>
);
}Pair it with a Label or a Field — the switch renders no text, and an
unlabelled toggle is not usable with a screen reader.
Sizes
export function Sizes() {
return (
<>
<Switch size="sm" defaultChecked aria-label="Small" readOnly />
<Switch size="md" defaultChecked aria-label="Medium" readOnly />
<Switch size="lg" defaultChecked aria-label="Large" readOnly />
</>
);
}The thumb is inset by 2px at every size, so the proportions hold as the track grows. It starts at the inline start and travels toward the end, so the switch mirrors in a right-to-left page.
Radius
export function Radius() {
return RADII.map((radius) => (
<Switch
key={radius}
radius={radius}
defaultChecked
aria-label={radius}
readOnly
/>
));
}radius defaults to full rather than none, as on Radio: a pill is what
makes a switch read as a switch. The thumb takes the same radius as the track,
so a square switch has a square thumb.
States
export function States() {
return (
<>
<Switch aria-label="Off" />
<Switch defaultChecked aria-label="On" readOnly />
<Switch disabled aria-label="Disabled" />
<Switch disabled defaultChecked aria-label="Disabled on" readOnly />
<Switch aria-invalid aria-label="Invalid" />
<Switch aria-invalid defaultChecked aria-label="Invalid on" readOnly />
</>
);
}Controlled
Notifications are off.
export function Controlled() {
const [enabled, setEnabled] = useState(false);
return (
<>
<Field orientation="horizontal" label="Email notifications">
<Switch
checked={enabled}
onChange={(event) => setEnabled(event.target.checked)}
/>
</Field>
<Text size="sm" tone="subtle">
Notifications are {enabled ? "on" : "off"}.
</Text>
</>
);
}State is ordinary React — there is no context or provider involved.