Skip to content
+

Toggle Button

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.

Exclusive selection

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.

Multiple selection

Multiple selection allows for logically-grouped options, like bold, italic, and underline, to have multiple options selected.

Size

For larger or smaller buttons, use the size prop.

Press Enter to start editing
Press Enter to start editing

Vertical buttons

The buttons can be stacked vertically with the orientation prop set to "vertical".

Press Enter to start editing

Enforce value set

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);
  }
};
Press Enter to start editing

Customization

Here is an example of customizing the component. You can learn more about this in the overrides documentation page.

Spacing

The demos below show how to adjust spacing between toggle buttons in horizontal and vertical orientations.

Horizontal Spacing

Vertical Spacing

Accessibility

ARIA

  • ToggleButtonGroup has role="group". You should provide an accessible label with aria-label="label", aria-labelledby="id" or <label>.
  • ToggleButton sets aria-pressed="<bool>" according to the button state. You should label each button with aria-label.

Keyboard

The group uses a single tab stop. Use the left and right arrow keys to move focus in a horizontal group, or the up and down arrow keys in a vertical group. The Home and End keys move focus to the first and last enabled buttons. Focus movement wraps, skips disabled buttons, and does not change the selected value. Press Space or Enter to toggle the focused button.

API

See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.