A Toggle Button can be used to group related options.
To emphasize groups of related Toggle buttons,
a group should share a common container.
The ToggleButtonGroup
controls the selected state of its child buttons when given its own value
prop.
With exclusive selection, selecting one option deselects any other.
In this example, text justification toggle buttons present options for left, center, right, and fully justified text (disabled), with only one item available for selection at a time.
Note: Exclusive selection does not enforce that a button must be active. For that effect see enforce value set.
{{ādemoā: āToggleButtons.jsā}}
Multiple selection allows for logically-grouped options, like bold, italic, and underline, to have multiple options selected.
{{ādemoā: āToggleButtonsMultiple.jsā}}
For larger or smaller buttons, use the size
prop.
{{ādemoā: āToggleButtonSizes.jsā}}
{{ādemoā: āColorToggleButton.jsā}}
The buttons can be stacked vertically with the orientation
prop set to āverticalā.
{{ādemoā: āVerticalToggleButtons.jsā}}
If you want to enforce that at least one button must be active, you can adapt your handleChange function.
const handleAlignment = (event, newAlignment) => {
if (newAlignment !== null) {
setAlignment(newAlignment);
}
};
const handleDevices = (event, newDevices) => {
if (newDevices.length) {
setDevices(newDevices);
}
};
{{ādemoā: āToggleButtonNotEmpty.jsā}}
{{ādemoā: āStandaloneToggleButton.jsā}}
Here is an example of customizing the component. You can learn more about this in the overrides documentation page.
import * as React from 'react';
import { styled } from '@mui/material/styles';
import FormatAlignLeftIcon from '@mui/icons-material/FormatAlignLeft';
import FormatAlignCenterIcon from '@mui/icons-material/FormatAlignCenter';
import FormatAlignRightIcon from '@mui/icons-material/FormatAlignRight';
import FormatAlignJustifyIcon from '@mui/icons-material/FormatAlignJustify';
import FormatBoldIcon from '@mui/icons-material/FormatBold';
import FormatItalicIcon from '@mui/icons-material/FormatItalic';
import FormatUnderlinedIcon from '@mui/icons-material/FormatUnderlined';
import FormatColorFillIcon from '@mui/icons-material/FormatColorFill';
import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
import Divider from '@mui/material/Divider';
import Paper from '@mui/material/Paper';
import ToggleButton from '@mui/material/ToggleButton';
import ToggleButtonGroup, {
toggleButtonGroupClasses,
} from '@mui/material/ToggleButtonGroup';
const StyledToggleButtonGroup = styled(ToggleButtonGroup)(({ theme }) => ({
[`& .${toggleButtonGroupClasses.grouped}`]: {
margin: theme.spacing(0.5),
border: 0,
borderRadius: theme.shape.borderRadius,
[`&.${toggleButtonGroupClasses.disabled}`]: {
border: 0,
},
},
[`& .${toggleButtonGroupClasses.middleButton},& .${toggleButtonGroupClasses.lastButton}`]:
{
marginLeft: -1,
borderLeft: '1px solid transparent',
},
}));
export default function CustomizedDividers() {
const [alignment, setAlignment] = React.useState('left');
const [formats, setFormats] = React.useState(() => ['italic']);
const handleFormat = (
event: React.MouseEvent<HTMLElement>,
newFormats: string[],
) => {
setFormats(newFormats);
};
const handleAlignment = (
event: React.MouseEvent<HTMLElement>,
newAlignment: string,
) => {
setAlignment(newAlignment);
};
return (
<div>
<Paper
elevation={0}
sx={(theme) => ({
display: 'flex',
border: `1px solid ${theme.palette.divider}`,
flexWrap: 'wrap',
})}
>
<StyledToggleButtonGroup
size="small"
value={alignment}
exclusive
onChange={handleAlignment}
aria-label="text alignment"
>
<ToggleButton value="left" aria-label="left aligned">
<FormatAlignLeftIcon />
</ToggleButton>
<ToggleButton value="center" aria-label="centered">
<FormatAlignCenterIcon />
</ToggleButton>
<ToggleButton value="right" aria-label="right aligned">
<FormatAlignRightIcon />
</ToggleButton>
<ToggleButton value="justify" aria-label="justified" disabled>
<FormatAlignJustifyIcon />
</ToggleButton>
</StyledToggleButtonGroup>
<Divider flexItem orientation="vertical" sx={{ mx: 0.5, my: 1 }} />
<StyledToggleButtonGroup
size="small"
value={formats}
onChange={handleFormat}
aria-label="text formatting"
>
<ToggleButton value="bold" aria-label="bold">
<FormatBoldIcon />
</ToggleButton>
<ToggleButton value="italic" aria-label="italic">
<FormatItalicIcon />
</ToggleButton>
<ToggleButton value="underlined" aria-label="underlined">
<FormatUnderlinedIcon />
</ToggleButton>
<ToggleButton value="color" aria-label="color" disabled>
<FormatColorFillIcon />
<ArrowDropDownIcon />
</ToggleButton>
</StyledToggleButtonGroup>
</Paper>
</div>
);
}
role="group"
. You should provide an accessible label with aria-label="label"
, aria-labelledby="id"
or <label>
.aria-pressed="<bool>"
according to the button state. You should label each button with aria-label
.At present, toggle buttons are in DOM order. Navigate between them with the tab key. The button behavior follows standard keyboard semantics.