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-2ordensity-3to the group to make every button in it shorter. See Density. - Functions: On a
ButtonGroup,className,styleandchildrencan be functions oforientation,isDisabledandstate. The state's type is exported asButtonGroupRenderProps. 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.
Use cases
Material lists what people should be able to do with assistive technology:
- Navigate to and interact with each button in the group
- Identify when buttons are selected
Interaction and style
- States: Each button shows its states as it does outside a group: a state layer on hover, a ripple on a press, and a focus ring for keyboard focus alone.
- Touch target: Each button keeps its touch target of at least 48dp. In a connected group, the buttons of the
xsandssizes are at least 48dp wide, so that their targets do not overlap across the 2dp between them. In a standard group, Material advises keeping the larger space of thexsandssizes, 18dp and 12dp, which is there for accessible targets: don't pass them a smaller gap class. - Density: A density class on the group makes every button shorter, and their targets stay 48dp.
- Motion: With reduced motion, the buttons' changes of shape and color, a connected group's inner corners included, are instant.
- Forced colors: In forced colors, each button keeps the edge it has outside a group, and a selected toggle button is filled with the system's highlight color.
- Right to left: A connected group's corners are mirrored in a right-to-left layout.
Focus
- The group is not a tab stop. Tab moves the focus to its first button, as Material advises, and the arrow keys move it between the buttons.
- When Tab brings the focus back to the group, it returns to the button that had it last.
- A disabled button cannot be focused, and the arrow keys skip it.
isDisabledon the group disables every button.
Keyboard navigation
| Keys | Actions |
|---|---|
| Tab | Move the focus into the group, or out of it to the next element |
| Left or Right arrow | Move the focus to the previous or next button |
| Space or Enter | Select or deselect the focused button, or press an action button |
- The arrow keys move the focus without selecting, and stop at the first and the last button.
- Left and Right arrow are reversed in a right-to-left locale, which is set with an
I18nProvider, not withdir. See Internationalization. - With
orientation="vertical", Up and Down arrow move the focus in place of Left and Right. See Vertical groups. - In a toolbar, the toolbar's arrow keys move through the group and on to the elements after it.
Labeling elements
selectionModesets how the group is announced: as a radio group withsingle, and as a toolbar of toggle buttons withmultiple. In a radio group, each toggle button is a radio that reports whether it is selected witharia-checked. In a toolbar, a toggle button reports it witharia-pressed.- Name the group with an
aria-labelor anaria-labelledby, such as View. - Label each button as you would outside a group, as Material advises: give a button with an icon alone an
aria-label. See Button and Icon button. - A disabled group reports it with
aria-disabled.
variant'standard' | 'connected'Default'standard'Two variants: standard and connected
standard: Standard button groups add interaction between adjacent buttons so they respond to each other. When a button in a standard group is selected, the selected button changes shape and width, and adjacent buttons move and temporarily change width. Standard button group inner padding: XS 18dp, S 12dp, M 8dp, L 8dp, XL 8dp.connected: Connected button groups help people select options, switch views, or sort elements in a page. They behave similarly to standard groups, except they don’t affect adjacent buttons. For all connected button groups, use 2dp padding. Avoid mixing color styles in connected button groups; it can make selection and emphasis unclear.
size's' | 'xs' | 'm' | 'l' | 'xl'Default's'Works with all button sizes: XS, S, M, L, and XL
shape'round' | 'square'Default'round'Two default shapes: round and square
Props of ButtonGroup (17)
classNameClassNameOrFunction<ToggleButtonGroupRenderProps>Default'react-aria-ToggleButtonGroup'The CSS className for the element. A function may be provided to compute the class based on component state.
orientation'horizontal' | 'vertical'Default'horizontal'The orientation of the the toggle button group.
selectionMode'single' | 'multiple'Default'single'Whether single or multiple selection is enabled.
disallowEmptySelectionbooleanWhether the collection allows empty selection.
selectedKeysIterable<Key>The currently selected keys in the collection (controlled).
defaultSelectedKeysIterable<Key>The initial selected keys in the collection (uncontrolled).
onSelectionChange(keys: Set<Key>) => voidHandler that is called when the selection changes.
isDisabledbooleanWhether all items are disabled.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
childrenChildrenOrFunction<T>The children of the component. A function may be provided to alter the children based on component state.
styleCSSProperties | (values: ToggleButtonGroupRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties | undefinedThe inline style for the element. A function may be provided to compute the style based on component state.
renderDOMRenderFunction<'div', ToggleButtonGroupRenderProps>Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.
Requirements:
- You must render the expected element type (e.g. if
<button>is expected, you cannot render an<a>). - Only a single root DOM element can be rendered (no fragments).
- You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
- You must render the expected element type (e.g. if
slotstring | nullA slot name for the component. Slots allow the component to receive props from a parent component. An explicit
nullvalue indicates that the local props completely override all props received from a parent.refRef<HTMLDivElement>
ButtonGroup also takes the 70 attributes and events of the element it renders.