ToggleGroupControl is a form component that lets users choose options
represented in horizontal segments. To render options for this control use
ToggleGroupControlOption component.
This component is intended for selecting a single persistent value from a set of options,
similar to a how a radio button group would work. If you simply want a toggle to switch between views,
use a TabPanel instead.
Only use this control when you know for sure the labels of items inside won’t
wrap. For items with longer labels, you can consider a SelectControl or a
CustomSelectControl component instead.
import { __experimentalToggleGroupControl as ToggleGroupControl } from '@wordpress/components';
LinksPermalink to this section
PropsPermalink to this section
| Name | Default | Description |
|---|---|---|
help |
Additional description for the control. Only use for meaningful description or instructions for the control. An element containing the description will be programmatically associated to the BaseControl by the means of an | |
__nextHasNoMarginBottom |
Start opting into the new margin-free styles that will become the default in a future version. | |
label Required |
Label for the control. | |
hideLabelFromVision | false |
If true, the label will only be visible to screen readers. |
isAdaptiveWidth | false |
Determines if segments should be rendered with equal widths. |
isBlock | false |
Renders |
isDeselectable | false |
Whether an option can be deselected by clicking it again. |
onChange |
Callback when a segment is selected. | |
value |
The selected value. | |
children Required |
The options to render in the | |
disabled | false |
Whether the control is disabled. |
size |
The size variant of the control. | |
__next40pxDefaultSize |
Start opting into the larger default height that will become the default size in a future version. |
ExamplesPermalink to this section
DefaultPermalink to this section
const Default = ( {
onChange,
...props
} ) => {
const [ value, setValue ] = useState< ToggleGroupControlProps[ 'value' ] >(
props.value
);
return (
<ToggleGroupControl
{ ...props }
onChange={ ( ...changeArgs ) => {
setValue( ...changeArgs );
onChange?.( ...changeArgs );
} }
value={ value }
/>
);
};
With TooltipPermalink to this section
A tooltip can be shown for each option by enabling the showTooltip prop.
The aria-label will be used in the tooltip if provided. Otherwise, the
label will be used.
const WithTooltip = ( {
onChange,
...props
} ) => {
const [ value, setValue ] = useState< ToggleGroupControlProps[ 'value' ] >(
props.value
);
return (
<ToggleGroupControl
{ ...props }
onChange={ ( ...changeArgs ) => {
setValue( ...changeArgs );
onChange?.( ...changeArgs );
} }
value={ value }
/>
);
};
With IconsPermalink to this section
The ToggleGroupControlOptionIcon component can be used for icon options. A label is required
on each option for accessibility, which will be shown in a tooltip.
const WithIcons = ( {
onChange,
...props
} ) => {
const [ value, setValue ] = useState< ToggleGroupControlProps[ 'value' ] >(
props.value
);
return (
<ToggleGroupControl
{ ...props }
onChange={ ( ...changeArgs ) => {
setValue( ...changeArgs );
onChange?.( ...changeArgs );
} }
value={ value }
/>
);
};
DeselectablePermalink to this section
When the isDeselectable prop is true, the option can be deselected by clicking on it again.
const Deselectable = ( {
onChange,
...props
} ) => {
const [ value, setValue ] = useState< ToggleGroupControlProps[ 'value' ] >(
props.value
);
return (
<ToggleGroupControl
{ ...props }
onChange={ ( ...changeArgs ) => {
setValue( ...changeArgs );
onChange?.( ...changeArgs );
} }
value={ value }
/>
);
};
DisabledPermalink to this section
When the disabled prop is true, the control is unselectable.
const Disabled = ( {
onChange,
...props
} ) => {
const [ value, setValue ] = useState< ToggleGroupControlProps[ 'value' ] >(
props.value
);
return (
<ToggleGroupControl
{ ...props }
onChange={ ( ...changeArgs ) => {
setValue( ...changeArgs );
onChange?.( ...changeArgs );
} }
value={ value }
/>
);
};