---
title: ButtonGroup
---

import LibraryInfo from '@theme/LibraryInfo'
import { Feedback, TokensTable, PropsTable } from '../../src/components'
import { Users } from '@telus-uds/palette-allium/build/web/icons'
import { ButtonGroup, StackView, Typography } from '@telus-uds/components-web'
import { ButtonGroup as BaseDocs } from 'docs/@telus-uds/components-base'
import AvailableOnBoth from '../../partials/_platform-considerations-both.mdx'
import NoStylisticVariants from '../../partials/_variants-none.mdx'

# ButtonGroup

<LibraryInfo figmaLink="https://www.figma.com/embed?embed_host=share&url=https%3A%2F%2Fwww.figma.com%2Ffile%2F4zJn2Qn6uXOqPP1GVDipy6%2F%25F0%259F%258D%2580-Allium-Components%3Fnode-id%3D988%253A7565" />

```jsx live
<ButtonGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
/>
```

## Introduction

ButtonGroup allows the selection of (by default) one element from a selection of labelled buttons.

Spacing between buttons increases on wider viewports (`lg` and `xl`).

[Follow the appropriate instructions](importing-components) to add this component in to your app.

## Guidance

Use for sets of options with very simple, short labels, ideally one word or two.

Consider `RadioGroup` for items with labels of a few words in length and `RadioCard` for more complex options requiring a title and additional description content.

### `maxValues` prop

The prop `maxValues` may be passed to allow more than one item to be selected. Pass a number to set that
as the maximum, or `null` to have no maximum and allow any number of selections.

```jsx live
<ButtonGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
  maxValues={2}
/>
```

### `onChange` prop

An `onChange` function may be provided, which is called when a ButtonGroup item is toggled.

```jsx live
<ButtonGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
  onChange={(state) => setTimeout(() => alert(`ButtonGroup has selected: ${state}`), 100)}
/>
```

### Controlled usage with `values` prop

Pass an array of item ids to the `values` prop as well as `onChange` to control the ButtonGroup from a parent.

This is usually the easiest way to control sibling content with a ButtonGroup.

```jsx live
function ControlledButtonGroup() {
  const [values, setValues] = useState([])
  const text = values.length ? `Saved: "${values.join('", "')}".` : 'No selection saved'
  return (
    <StackView space={2}>
      <Typography variant={{ size: 'small' }}>{text}</Typography>
      <ButtonGroup
        values={values}
        onChange={setValues}
        items={[
          { label: 'First item', id: 'first' },
          { label: 'Second item', id: 'second' },
          { label: 'Third item', id: 'third' },
          { label: 'Fourth item', id: 'fourth' },
          { label: 'Fifth item', id: 'fifth' }
        ]}
      />
    </StackView>
  )
}
```

#### Ensuring at least one item should be selected

You can create your own validation logic to ensure at least one item is selected, see a suggestion below.

```jsx live
function ControlledButtonGroup() {
  const [values, setValues] = useState([])
  const [text, setText] = useState('')
  const handleChange = (values) => {
    if (values.length > 0) {
      setValues(values)
      setText(`Saved: "${values.join('", "')}".`)
    } else {
      setText('Please select at least one value')
    }
  }

  return (
    <StackView space={2}>
      <Typography variant={{ size: 'small' }}>{text}</Typography>
      <ButtonGroup
        values={values}
        onChange={handleChange}
        items={[
          { label: 'First item', id: 'first' },
          { label: 'Second item', id: 'second' },
          { label: 'Third item', id: 'third' },
          { label: 'Fourth item', id: 'fourth' },
          { label: 'Fifth item', id: 'fifth' }
        ]}
      />
    </StackView>
  )
}
```

#### Ensuring at least one item should be selected

You can define items to be selected by default by passing them as argument of the `useState` hook during the creation of the `values` array. The example below sets the second item as selected by default.

```jsx live
function ControlledButtonGroup() {
  const [values, setValues] = useState(['second'])
  const text = values.length ? `Saved: "${values.join('", "')}".` : 'No selection saved'
  return (
    <StackView space={2}>
      <Typography variant={{ size: 'small' }}>{text}</Typography>
      <ButtonGroup
        values={values}
        onChange={setValues}
        items={[
          { label: 'First item', id: 'first' },
          { label: 'Second item', id: 'second' },
          { label: 'Third item', id: 'third' },
          { label: 'Fourth item', id: 'fourth' },
          { label: 'Fifth item', id: 'fifth' }
        ]}
      />
    </StackView>
  )
}
```

### Uncontrolled usage

The ButtonGroup manages its own state internally if `values` is not passed.

A default selection may be set by passing an array of item IDs to `initialValues`. Do not use both `initialValues` and `values` props in the same `ButtonGroup`.

```jsx live
<ButtonGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
  initialValues={['second']}
/>
```

### Usage with icon props

To add icons on each `item` of a `ButtonGroup`:

- Import the icons you want to use.

```jsx
import { ExampleIcon } from '@telus-uds/palette-allium/build/web/icons'
```

- Add the property `icon` with the imported icon name as the value `icon: ExampleIcon` on each item and it should appear on left side of the label.
- You can use the `IconPosition` prop on the component to set the position of all icons to the right side of the labels.

```jsx live
<ButtonGroup
  items={[
    { label: 'First item', id: 'first', icon: ExampleIcon },
    { label: 'Second item', id: 'second', icon: ExampleIcon },
    { label: 'Third item', id: 'third', icon: ExampleIcon },
    { label: 'Fourth item', id: 'fourth', icon: ExampleIcon },
    { label: 'Fifth item', id: 'fifth', icon: ExampleIcon }
  ]}
  iconPosition="left"
/>
```

:::info
We use a icon wrapper called `Icons` in the documentation to avoid import conflicts. You can replace `ExampleIcon` by the combination `Icons.IconName` to view any palette icon on the code playground.
:::

```jsx live
<ButtonGroup
  items={[
    { label: 'First item', id: 'first', icon: Icons.List },
    { label: 'Second item', id: 'second', icon: Icons.User }
  ]}
/>
```

### Accessibility

By default, `ButtonGroup` is treated by accessibility tools as a group of radio buttons.

If `maxValues` prop is used to allow multiple selections, the buttons will be treated like checkboxes.

### Platform considerations

<AvailableOnBoth />

## Props

<PropsTable base={BaseDocs} />

## Tokens

<TokensTable id="ButtonGroup" base={BaseDocs} />

## Variants

### Contained

The `contained` variant is designed to fit neatly around the content, adjusting the width of the parent component to just what is needed. It removes the initial border width and color, giving it a cleaner look, and changes the background color of each button for better emphasis. This variant is great for grouping related options together in a way that makes them stand out while keeping everything tidy and consistent.

```jsx live
<ButtonGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
  variant={{ style: 'contained' }}
  initialValues={['third']}
/>
```

### Contained with icons

```jsx live
<ButtonGroup
  items={[
    { label: 'Stream & Live TV', id: 'first', icon: ExampleIcon },
    { label: 'Stream', id: 'second', icon: ExampleIcon },
    { label: 'Live TV', id: 'third', icon: ExampleIcon }
  ]}
  variant={{ style: 'contained' }}
  initialValues={['first']}
/>
```

### Width

#### Responsive width

- Just like [`width` variant of Button](button.mdx#width) but for all `Button` elements within `ButtonGroup`
- Default width expands based on the content
- Responsive `variant={{ width: 'responsive'}}` automatically applies full-width when is displayed on the XS viewport

```jsx live
<ButtonGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
  variant={{ width: 'responsive' }}
/>
```

#### Equal width

Divide all the available width equally among the ButtonGroup items.

```jsx live
<ButtonGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: '2nd item a little longer', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: '4th item shorter', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
  variant={{ width: 'equal' }}
/>
```

### Size

#### Size small

Buttons re-scaled to a small size to suit the user's needs

```jsx live
<ButtonGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
  variant={{ size: 'small' }}
/>
```

## Feedback

<Feedback component="ButtonGroup" />
