useToggleSwitch
Hook that manages toggle switch state, checked/unchecked logic, and ARIA attributes.
Usage#
import { useToggleSwitch } from '@primereact/headless/toggleswitch';const { rootProps, inputProps, controlProps, thumbProps } = useToggleSwitch({
defaultChecked: false
});
<div {...rootProps}>
<input {...inputProps} />
<div {...controlProps}>
<span {...thumbProps} />
</div>
</div>;useToggleSwitch manages boolean toggle state with a hidden native checkbox using role="switch". See Primitive for a component-based API.
Features#
- Composable parts — spreads props onto
root,input,control, andthumbelements so the visual track and thumb are fully yours - Switch semantics — hidden native checkbox with
role="switch"preserves screen reader and keyboard behavior - Controlled or uncontrolled — drive via
checked/onCheckedChangeor let the hook manage state fromdefaultChecked - Validation flags —
invalidanddisabledemitaria-invalid,data-invalid, anddata-disabledacross the parts - Animation-ready —
data-checkedoncontrolandthumblets you transition the track color and thumb position via CSS
Working with callbacks#
Controlled toggle switch#
Pass checked and onCheckedChange to own the state externally — needed for settings pages, async toggles, or validation flows.
const [checked, setChecked] = React.useState(false);
useToggleSwitch({
checked,
onCheckedChange: (e) => setChecked(e.checked)
});The callback receives { originalEvent, checked } where checked is the new boolean.
Async confirmation before commit#
Intercept onCheckedChange to run a confirmation or network call before applying the new state.
const [checked, setChecked] = React.useState(false);
useToggleSwitch({
checked,
onCheckedChange: async (e) => {
if (await confirmChange(e.checked)) setChecked(e.checked);
}
});Invalid state tied to form validation#
Flip invalid from your form state so CSS can react via data-invalid.
useToggleSwitch({
invalid: formState.errors.notifications != null
});Styling with data attributes#
Every prop object includes data-scope="toggleswitch" and a data-part attribute. State-dependent attributes are added automatically.
| Scope | Part | States |
|---|---|---|
toggleswitch | root | data-checked, data-disabled, data-invalid |
toggleswitch | control | data-checked, data-disabled, data-invalid |
toggleswitch | thumb | data-checked, data-disabled, data-invalid |
[data-scope='toggleswitch'][data-part='control'] {
width: 2.5rem;
height: 1.5rem;
border-radius: 9999px;
background: #ccc;
transition: background 200ms;
}
[data-scope='toggleswitch'][data-part='control'][data-checked] {
background: var(--p-primary-color);
}
[data-scope='toggleswitch'][data-part='thumb'] {
width: 1rem;
height: 1rem;
border-radius: 9999px;
background: white;
transition: transform 200ms;
}
[data-scope='toggleswitch'][data-part='thumb'][data-checked] {
transform: translateX(1rem);
}
[data-scope='toggleswitch'][data-part='root'][data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}API#
useToggleSwitch#
| Name | Type | Default |
|---|---|---|
checked | boolean | — |
| When present, it specifies the input's checked state. | ||
defaultChecked | boolean | — |
| The default value for the input when not controlled by `checked` and `onCheckedChange` . | ||
disabled | boolean | false |
| Whether the toggle switch is disabled. | ||
invalid | boolean | false |
| When present, it specifies that the toggle switch value is invalid. | ||
tabIndex | number | — |
| Index of the element in tabbing order. | ||
ariaLabel | string | — |
| Establishes a string value that labels the component. | ||
ariaLabelledby | string | — |
| Establishes relationships between the component and label(s) where its value should be one or more element IDs. | ||
onFocus | FocusEventHandler<HTMLInputElement> | — |
| Callback function that is called when the toggle switch is focused. | ||
onBlur | FocusEventHandler<HTMLInputElement> | — |
| Callback function that is called when the toggle switch loses focus. | ||
onCheckedChange | (event: useToggleSwitchChangeEvent) => void | — |
| Callback fired when the toggle switch's checked state changes. | ||
Accessibility#
Space toggles the on/off state while focused, matching native checkbox keyboard behavior. See Primitive for full WAI-ARIA compliance details.