Toggle Buttons
Toggle buttons can be used to group related options.
To emphasize groups of related Toggle buttons, a group should share a common container.
The ToggleButtonGroup
will control the selected state of its child buttons when
given its own value
prop.
Exclusive Selection
Text justification toggle buttons present options for left, right, center, full, and justified text with only one item available for selection at a time. Selecting one option deselects any other.
Multiple Selection
Logically-grouped options, like Bold, Italic, and Underline, allow multiple options to be selected.
Enforce value set
If you want to enforce at least one button to be active, you can adapt your handleChange function.
const handleFormat = (event, newFormats) => {
if (newFormats.length) {
setFormats(newFormats);
}
};
const handleAlignment = (event, newAlignment) => {
if (newAlignment !== null) {
setAlignment(newAlignment);
}
};
<ToggleButton
value="check"
selected={selected}
onChange={() => {
setSelected(!selected);
}}
>
<CheckIcon />
</ToggleButton>
Customized toggle button
Here is an example of customizing the component. You can learn more about this in the overrides documentation page.