useToggleButton
Hooks that manage toggle button pressed state and group value selection with single or multiple modes.
Usage#
import { useToggleButton } from '@primereact/headless/togglebutton';
import { useToggleButtonGroup } from '@primereact/headless/togglebuttongroup';const toggle = useToggleButton();
<button {...toggle.getRootProps()}>
<span {...toggle.getIndicatorProps()}></span>
</button>;useToggleButton manages a boolean pressed state and returns prop getter functions for root and indicator elements. useToggleButtonGroup coordinates multiple toggle buttons with single or multiple selection. See Primitive for a component-based API.
Features#
- Prop getter API —
getRootPropsandgetIndicatorPropsaccept resolved disabled and invalid state at call time - Controlled or uncontrolled —
pressed/onPressedChangefor owned state,defaultPressedfor hook-managed state - Group coordination —
useToggleButtonGroupmanages shared value and exposesisPressed/updateChangefor children - Single or multiple — group switches between radio-like and multi-select behavior via the
multipleflag - Minimum selection — group
allowEmptyguarantees at least one button stays pressed when set tofalse
Working with callbacks#
Controlled pressed state#
Pass pressed and onPressedChange to own the state externally — required when toggles drive other UI or persist across reloads.
const [pressed, setPressed] = React.useState(false);
useToggleButton({
pressed,
onPressedChange: (e) => setPressed(e.pressed)
});Dynamic disabled / invalid#
getRootProps(disabled, invalid) accepts booleans so you can compute state at render time from form context.
const rootProps = toggle.getRootProps(isSubmitting, hasError);Group as a formatting toolbar#
Use useToggleButtonGroup with multiple: true to build a toolbar where each button represents an independent flag.
const group = useToggleButtonGroup({ multiple: true });
const bold = useToggleButton({
pressed: group.isPressed(group.state.value, 'bold'),
onPressedChange: (e) => group.updateChange({ ...e, value: 'bold' })
});Require a selection#
Set allowEmpty: false when the group represents a mandatory choice like view mode or alignment.
const group = useToggleButtonGroup({ allowEmpty: false, defaultValue: 'left' });Read live state for custom UI#
Expose state.pressed or state.value to show counts, icons, or status text derived from the current selection.
const group = useToggleButtonGroup({ multiple: true });
// group.state.value: unknown[]
const toggle = useToggleButton();
// toggle.state.pressed: booleanStyling with data attributes#
The prop getters include data attributes for styling.
| Scope | Part | States |
|---|---|---|
togglebutton | root | data-pressed, data-disabled |
togglebuttongroup | root | data-multiple |
button[data-pressed] {
background-color: var(--p-primary-color);
color: var(--p-primary-contrast-color);
}
button[data-disabled] {
pointer-events: none;
opacity: 0.6;
}
[data-scope='togglebuttongroup'][data-multiple] {
/* styles for multi-select group */
}API#
useToggleButton#
| Name | Type | Default |
|---|---|---|
pressed | boolean | — |
| When present, it specifies that the ToggleButton should be pressed. | ||
defaultPressed | boolean | — |
| The default pressed value when not controlled by `pressed` and `onPressedChange` . | ||
disabled | boolean | false |
| When present, it specifies that the element should be disabled. | ||
invalid | boolean | false |
| When present, it specifies that the component should have invalid state style. | ||
onPressedChange | (event: useToggleButtonChangeEvent) => void | — |
| Callback fired when the ToggleButton's pressed state changes. | ||
useToggleButtonGroup#
| Name | Type | Default |
|---|---|---|
value | unknown | — |
| Value of the ToggleButtonGroup. | ||
defaultValue | unknown | — |
| The default value of the ToggleButtonGroup. | ||
onValueChange | (event: useToggleButtonGroupChangeEvent) => void | — |
| Callback fired when the ToggleButtonGroup's value state changes. | ||
multiple | boolean | false |
| When present, it specifies that the ToggleButton group allows multiple selections. | ||
allowEmpty | boolean | true |
| When present, it specifies that the ToggleButton group allows empty selection. | ||
disabled | boolean | false |
| When present, it specifies that the ToggleButton group is disabled. | ||
invalid | boolean | false |
| When present, it specifies that the ToggleButton group value is invalid. | ||
Accessibility#
Space or Enter toggles the pressed state while focused. See Primitive for full WAI-ARIA compliance details.