Skip to content

Material 3

Components

Button group

Button groups organize buttons and add interactions between them.

9:30
Schedule
9:00
Design reviewRoom 4
11:30
Lunch with SandraCafé Lucca
14:00
Sprint planningOnline
16:30
DentistMain Street

A button group holds related buttons in a row. Use one to:

  • Select one of several options, such as a view or a sort order
  • Turn several options on and off
  • Group related actions

For an action with a menu of related options, use a split button in place of a group.

import { Button, ButtonGroup } from '@material-web/react'

Basic button group

Put buttons in a ButtonGroup. To make a button one of the group's toggle buttons, give it an id and isSelectable.

import { Button, ButtonGroup } from '@material-web/react'

export default function Basic() {
  return (
    <ButtonGroup aria-label="View" defaultSelectedKeys={['week']}>
      <Button id="day" variant="tonal" isSelectable>
        Day
      </Button>
      <Button id="week" variant="tonal" isSelectable>
        Week
      </Button>
      <Button id="month" variant="tonal" isSelectable>
        Month
      </Button>
    </ButtonGroup>
  )
}

The ButtonGroup comes in two variants. Set variant to standard (the default) or connected.

Standard button group

A standard group is as wide as its buttons, with a space between them that the group's size sets.

import { Button, ButtonGroup } from '@material-web/react'

export default function Standard() {
  return (
    <ButtonGroup variant="standard" aria-label="View" defaultSelectedKeys={['week']}>
      <Button id="day" variant="outlined" isSelectable>
        Day
      </Button>
      <Button id="week" variant="outlined" isSelectable>
        Week
      </Button>
      <Button id="month" variant="outlined" isSelectable>
        Month
      </Button>
    </ButtonGroup>
  )
}

Connected button group

Use a connected group to select options, switch views or sort the elements of a page. It fills the width of its container, and its buttons grow to fill it. In this example the container is 480dp wide at most. The buttons sit close together, and the selected button is fully round. Avoid mixing color styles in a connected group, as it can make selection and emphasis unclear.

import { Button, ButtonGroup } from '@material-web/react'

const views = ['Day', 'Week', 'Month', 'Year']

export default function Connected() {
  return (
    <div className="w-full max-w-120">
      <ButtonGroup variant="connected" aria-label="View" defaultSelectedKeys={['Week']}>
        {views.map((view) => (
          <Button key={view} id={view} variant="tonal" isSelectable>
            {view}
          </Button>
        ))}
      </ButtonGroup>
    </div>
  )
}

Button variants

A group has no colors of its own. Set the color style on each button, such as filled, tonal or outlined, with the button's variant. Avoid text buttons and standard icon buttons, as they have no container.

import { Button, ButtonGroup } from '@material-web/react'

const variants = ['elevated', 'filled', 'tonal', 'outlined'] as const

export default function ButtonVariants() {
  return (
    <div className="flex flex-col items-center gap-space-300">
      {variants.map((variant) => (
        <ButtonGroup key={variant} aria-label={`View, ${variant}`} defaultSelectedKeys={['week']}>
          <Button id="day" variant={variant} isSelectable>
            Day
          </Button>
          <Button id="week" variant={variant} isSelectable>
            Week
          </Button>
          <Button id="month" variant={variant} isSelectable>
            Month
          </Button>
        </ButtonGroup>
      ))}
    </div>
  )
}

A toggle IconButton is selected by the group as a Button is.

import { ButtonGroup, Icon, IconButton } from '@material-web/react'

const styles = [
  { id: 'bold', icon: 'format_bold', label: 'Bold' },
  { id: 'italic', icon: 'format_italic', label: 'Italic' },
  { id: 'underlined', icon: 'format_underlined', label: 'Underline' },
]

export default function IconButtons() {
  return (
    <ButtonGroup
      variant="connected"
      className="w-auto"
      aria-label="Text style"
      selectionMode="multiple"
      defaultSelectedKeys={['bold']}
    >
      {styles.map(({ id, icon, label }) => (
        <IconButton key={id} id={id} variant="tonal" width="wide" aria-label={label} isSelectable>
          <Icon name={icon} />
        </IconButton>
      ))}
    </ButtonGroup>
  )
}

Selection

The group selects its toggle buttons by their id. With selectionMode="single" (the default), one button is selected at a time. Control the selection with selectedKeys and onSelectionChange, or set the initial selection with defaultSelectedKeys.

import { Button, ButtonGroup, Icon } from '@material-web/react'
import { useState } from 'react'
import type { Key } from 'react-aria-components'

const alignments = [
  { id: 'left', icon: 'format_align_left', label: 'Align left' },
  { id: 'center', icon: 'format_align_center', label: 'Align center' },
  { id: 'right', icon: 'format_align_right', label: 'Align right' },
]

export default function Single() {
  const [selected, setSelected] = useState(new Set<Key>(['left']))
  return (
    <ButtonGroup
      variant="connected"
      size="m"
      className="w-auto"
      aria-label="Text alignment"
      disallowEmptySelection
      selectedKeys={selected}
      onSelectionChange={setSelected}
    >
      {alignments.map(({ id, icon, label }) => (
        <Button key={id} id={id} size="m" variant="outlined" aria-label={label} isSelectable>
          <Icon name={icon} className={selected.has(id) ? 'icon-filled' : undefined} />
        </Button>
      ))}
    </ButtonGroup>
  )
}

A toggle button outside a group keeps its own selection. See Toggle button.

Multiple selection

Set selectionMode="multiple" to let each button toggle independently.

import { Button, ButtonGroup } from '@material-web/react'

export default function Multiple() {
  return (
    <ButtonGroup
      variant="connected"
      className="w-auto"
      aria-label="Filters"
      selectionMode="multiple"
      defaultSelectedKeys={['open']}
    >
      <Button id="open" variant="tonal" isSelectable>
        Open
      </Button>
      <Button id="assigned" variant="tonal" isSelectable>
        Assigned to me
      </Button>
      <Button id="mentioned" variant="tonal" isSelectable>
        Mentioned
      </Button>
    </ButtonGroup>
  )
}

Empty selection

A press on the selected button of a group deselects it, so a group can have no button selected. Set disallowEmptySelection to keep the last selected button from being deselected, as the text alignment group above does.

Actions

A Button without isSelectable is an action in the group. With selectionMode="multiple", the group is a toolbar of actions, as in this example.

import { Button, ButtonGroup } from '@material-web/react'

export default function Actions() {
  return (
    <ButtonGroup aria-label="Actions" selectionMode="multiple">
      <Button variant="outlined">Cancel</Button>
      <Button variant="tonal">Save draft</Button>
      <Button>Publish</Button>
    </ButtonGroup>
  )
}

Loading

Any button of the group can be pending, an action or a selectable one. Set isPending on the button, as outside a group. A pending selectable button keeps its selection, and pressing it does not change the group's. See Loading.

Disabled

Set isDisabled on the group to disable every button in it. A button with its own isDisabled keeps its place in the group, and the arrow keys skip it.

import { Button, ButtonGroup } from '@material-web/react'

const views = ['Day', 'Week', 'Month', 'Year']

export default function Disabled() {
  return (
    <div className="flex flex-col items-center gap-space-300">
      <ButtonGroup
        variant="connected"
        className="w-auto"
        aria-label="View, disabled"
        defaultSelectedKeys={['Week']}
        isDisabled
      >
        {views.map((view) => (
          <Button key={view} id={view} variant="outlined" isSelectable>
            {view}
          </Button>
        ))}
      </ButtonGroup>
      <ButtonGroup
        variant="connected"
        className="w-auto"
        aria-label="View, month disabled"
        defaultSelectedKeys={['Week']}
      >
        {views.map((view) => (
          <Button
            key={view}
            id={view}
            variant="outlined"
            isSelectable
            isDisabled={view === 'Month'}
          >
            {view}
          </Button>
        ))}
      </ButtonGroup>
    </div>
  )
}

Sizes

Set size to xs, s (the default), m, l or xl. It does not change the size of the buttons, so give each button the same size as the group.

In a standard group, size sets the space between the buttons: 18dp for xs, 12dp for s, and 8dp for m, l and xl.

import { Button, ButtonGroup } from '@material-web/react'

const sizes = ['xs', 's', 'm', 'l'] as const

export default function Sizes() {
  return (
    <div className="flex flex-col items-center gap-space-300">
      {sizes.map((size) => (
        <ButtonGroup
          key={size}
          size={size}
          aria-label={`View, ${size}`}
          defaultSelectedKeys={['week']}
        >
          <Button id="day" size={size} variant="tonal" isSelectable>
            Day
          </Button>
          <Button id="week" size={size} variant="tonal" isSelectable>
            Week
          </Button>
          <Button id="month" size={size} variant="tonal" isSelectable>
            Month
          </Button>
        </ButtonGroup>
      ))}
    </div>
  )
}

In a connected group, size sets the inner corners, which are larger for l and xl. The buttons of an xs or s connected group are at least 48dp wide. To make a connected group as wide as its buttons, set className="w-auto", as in this example.

import { Button, ButtonGroup } from '@material-web/react'

const sizes = ['xs', 's', 'm', 'l'] as const

export default function ConnectedSizes() {
  return (
    <div className="flex flex-col items-center gap-space-300">
      {sizes.map((size) => (
        <ButtonGroup
          key={size}
          variant="connected"
          size={size}
          className="w-auto"
          aria-label={`Rating, ${size}`}
          defaultSelectedKeys={['2']}
        >
          <Button id="1" size={size} variant="tonal" isSelectable>
            1
          </Button>
          <Button id="2" size={size} variant="tonal" isSelectable>
            2
          </Button>
          <Button id="3" size={size} variant="tonal" isSelectable>
            3
          </Button>
        </ButtonGroup>
      ))}
    </div>
  )
}

Shapes

Set shape to round (the default) or square. In a square connected group, the outer corners are squarer as well as the inner ones. In a standard group, each button keeps its own shape.

import { Button, ButtonGroup } from '@material-web/react'

const views = ['Day', 'Week', 'Month', 'Year']

export default function Square() {
  return (
    <div className="w-full max-w-120">
      <ButtonGroup
        variant="connected"
        shape="square"
        aria-label="View"
        defaultSelectedKeys={['Week']}
      >
        {views.map((view) => (
          <Button key={view} id={view} variant="tonal" isSelectable>
            {view}
          </Button>
        ))}
      </ButtonGroup>
    </div>
  )
}

In a toolbar

Put a ButtonGroup in a toolbar to group some of its elements. The group's toggle buttons take the toolbar's style, whatever their variant. The toolbar's arrow keys move through the group, on to the elements after it and back.

Customization

Pass classes in className to restyle the group. A class of yours replaces the group's own class for the same property. Each button keeps its own props and classes: see the Button page's Customization.

  • Spacing: To change the space between the buttons, pass a gap class, such as gap-space-50.
  • Density: Add density-1, density-2 or density-3 to the group to make every button in it shorter. See Density.
  • Functions: On a ButtonGroup, className, style and children can be functions of orientation, isDisabled and state. The state's type is exported as ButtonGroupRenderProps. See Children as a function.

Limitations

Vertical groups

A group lays out its buttons in a row. orientation="vertical" does not stack them: it changes only the arrow keys that move the focus, to Up and Down.