# Button group

Attaches related buttons into one shape, with shared borders and rounded outer corners, and groups them for assistive technology.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-button-group`
- Source: [libs/ui/src/lib/molecules/button-group/button-group.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/molecules/button-group/button-group.ts)

```typescript title="Import"
import { ButtonGroup } from '@vigdx/ui/button-group';
```

## Basic

Put `vig-button`s (or native buttons) inside and name the group with `labels.ariaLabel`. Bordered variants such as `secondary` and `outlined` share their borders.

```typescript title="button-group-basic.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { ButtonGroup } from '@vigdx/ui/button-group';

@Component({
  selector: 'app-button-group-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, ButtonGroup],
  template: `
    <vig-button-group [labels]="{ ariaLabel: 'Period' }">
      <vig-button label="Day" variant="secondary" />
      <vig-button label="Week" variant="secondary" />
      <vig-button label="Month" variant="secondary" />
    </vig-button-group>
  `,
})
export class ButtonGroupBasicDemo {}
```

## Toggle buttons

Independent on/off states, such as text styles, are `vig-toggle-button`s in a group; each reports its state with `aria-pressed`. The pressed one draws its border on top.

```typescript title="button-group-toggles.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { ButtonGroup } from '@vigdx/ui/button-group';
import { ToggleButton } from '@vigdx/ui/toggle-button';

@Component({
  selector: 'app-button-group-toggles',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ButtonGroup, ToggleButton],
  template: `
    <vig-button-group [labels]="{ ariaLabel: 'Text style' }">
      <vig-toggle-button onLabel="Bold" [(value)]="bold" />
      <vig-toggle-button onLabel="Italic" [(value)]="italic" />
      <vig-toggle-button onLabel="Underline" [(value)]="underline" />
    </vig-button-group>
  `,
})
export class ButtonGroupTogglesDemo {
  protected readonly bold = signal(true);
  protected readonly italic = signal(false);
  protected readonly underline = signal(false);
}
```

## Vertical and icon-only

`orientation="vertical"` stacks the buttons at full width. Icon-only buttons need an `ariaLabel`.

```typescript title="button-group-vertical.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Button, ButtonIcon } from '@vigdx/ui/button';
import { ButtonGroup } from '@vigdx/ui/button-group';
import { Icon } from '@vigdx/ui/icon';

@Component({
  selector: 'app-button-group-vertical',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, ButtonGroup, ButtonIcon, Icon],
  template: `
    <div class="flex flex-wrap items-start gap-6">
      <vig-button-group orientation="vertical" [labels]="{ ariaLabel: 'Sort' }">
        <vig-button label="Newest first" variant="secondary" />
        <vig-button label="Oldest first" variant="secondary" />
        <vig-button label="Most viewed" variant="secondary" />
      </vig-button-group>
      <vig-button-group [labels]="{ ariaLabel: 'Zoom' }">
        <vig-button variant="secondary" ariaLabel="Zoom out"
          ><vig-icon vigButtonIcon name="minus"
        /></vig-button>
        <vig-button variant="secondary" ariaLabel="Zoom in"
          ><vig-icon vigButtonIcon name="plus"
        /></vig-button>
      </vig-button-group>
    </div>
  `,
})
export class ButtonGroupVerticalDemo {}
```

## Button group, toolbar or select button

- A button group only attaches buttons: each one stays its own tab stop, with no arrow-key movement.
- Use a [toolbar](https://vigdx.com/components/toolbar.md) for a set of controls acting on one piece of content (an editor, a table): one tab stop and arrow keys between the controls. A toolbar can hold button groups, whose buttons then move with the arrow keys too.
- To pick one value out of a few, use a [select button](https://vigdx.com/components/select-button.md): it is a radio group with a value and form support, which a group of buttons is not.

## Customize

The outer corners use `--radius-button` and the borders come from each button's variant. Inner corners are square and neighbours overlap by one pixel, with logical properties, so the shape mirrors in right-to-left pages.

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="button-group"][data-part="root"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="button-group"]`

| Part | Element | data-state |
| --- | --- | --- |
| `root` | The `vig-button-group` host (`role="group"`); also carries `data-orientation` (`horizontal`, `vertical`). Style the buttons through their own `[data-scope="button"]` parts. |   |

## Accessibility

- The host is a `role="group"`, named by `labels.ariaLabel`. Leave it unnamed when the buttons explain themselves.
- The focused button is lifted above its neighbours, so its focus ring is drawn in full; it keeps the transparent outline that forced-colors mode turns visible.

## API

### ButtonGroup

Selector: `vig-button-group` · Kind: component

Buttons attached into one shape: shared borders, rounded only on the outer corners (`--radius-button`), in a `role="group"`. It only groups visually and semantically; each button keeps its own tab stop. For one tab stop with arrow keys between the controls, use `vig-toolbar` (which can hold button groups). For picking one value from a few, use `vig-select-button`; for independent on/off states, put `vig-toggle-button`s in a group.

**Example :**

```html
<vig-button-group [labels]="{ ariaLabel: 'History' }">
  <vig-button label="Undo" variant="secondary" />
  <vig-button label="Redo" variant="secondary" />
</vig-button-group>
```

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `labels` | `Partial<ButtonGroupLabels>` | — | Texts (i18n): the group's accessible name. |
| `orientation` | `ButtonGroupOrientation` | `'horizontal'` | `vertical` stacks the buttons, full width, with square inner corners. |
