Switch
A composable boolean switch with controlled and uncontrolled checked state, hidden input form support, and a headless thumb part for custom styling.
Anatomy
The switch is assembled from two parts. Switch.Root renders the track (and a hidden checkbox input for form integration); Switch.Thumb renders the moving indicator.
<script>
import { Switch } from '@human-kit/ui';
</script>
<Switch.Root aria-label="Enable notifications">
<Switch.Thumb />
</Switch.Root>States
Every visual state is exposed through data-* attributes on both parts, so all styling — including disabled and readonly — is done with plain CSS or utility classes.
Forms
Switch.Root keeps a hidden checkbox in sync: give it a name and value and the pair is submitted when the switch is on. value only affects form submission, never the visual state.
Usage guidelines
- Use
checked/defaultCheckedfor the switch state. - Use
onCheckedChangeto react to user-driven state changes. - Use
valueonly for form submission through the hidden native input; it does not represent the visual state. - Wrap the switch in a native
<label>for the simplest accessible labeling pattern, or point a siblinglabel[for]at the switchid.
Accessibility
Switch.Rootexposesrole="switch"witharia-checked="true" | "false".- Press
Spaceto toggle the switch. readonlykeeps the switch focusable while preventing state changes.
API reference
Root
The switch control. Renders a span with role="switch" and keeps a hidden checkbox input in sync for form integration.
* required. Native HTML attributes of the underlying element are also accepted.
Thumb
The moving indicator. Mirrors the root state through data attributes for styling.
* required. Native HTML attributes of the underlying element are also accepted.