# Split button

A primary action with a menu of related ones beside it, such as Save next to Save as and Save all.

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

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

## Basic

Give the primary action a `label` and the related ones as `items`, the same typed entries as `vig-menu` (`command`, `disabled`, `danger`, links and `'separator'`). `primaryClick` reports the primary action, `selected` a menu item.

```typescript title="split-button-basic.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { type MenuEntry, type MenuItem } from '@vigdx/ui/menu';
import { SplitButton } from '@vigdx/ui/split-button';

@Component({
  selector: 'app-split-button-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [SplitButton],
  template: `
    <div class="flex flex-wrap items-center gap-4">
      <vig-split-button
        label="Save"
        [items]="options"
        (primaryClick)="last.set('Save')"
        (selected)="ran($event)"
      />
      <span class="text-sm text-muted-foreground" aria-live="polite">
        Last action: {{ last() ?? 'none' }}
      </span>
    </div>
  `,
})
export class SplitButtonBasicDemo {
  protected readonly options: MenuEntry[] = [
    { label: 'Save as…' },
    { label: 'Save a copy' },
    'separator',
    { label: 'Save all' },
  ];
  protected readonly last = signal<string | undefined>(undefined);

  protected ran(item: MenuItem): void {
    this.last.set(item.label);
  }
}
```

## Variants, sizes and states

`variant` and `size` style both parts; a projected `vigButtonIcon` leads the primary action. `loading` shows a spinner on the primary action and blocks both buttons, since the menu holds variations of the running action; `disabled` turns off both. Without an enabled item there is no menu button.

```typescript title="split-button-variants.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { ButtonIcon } from '@vigdx/ui/button';
import { Icon } from '@vigdx/ui/icon';
import { type MenuEntry } from '@vigdx/ui/menu';
import { SplitButton } from '@vigdx/ui/split-button';

@Component({
  selector: 'app-split-button-variants',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ButtonIcon, Icon, SplitButton],
  template: `
    <div class="flex flex-col items-start gap-4">
      <div class="flex flex-wrap items-center gap-3">
        <vig-split-button label="Publish" [items]="options" />
        <vig-split-button label="Export" variant="secondary" [items]="options">
          <vig-icon vigButtonIcon name="download" />
        </vig-split-button>
        <vig-split-button label="Delete" variant="error" [items]="options" />
      </div>
      <div class="flex flex-wrap items-center gap-3">
        <vig-split-button label="Small" size="sm" variant="secondary" [items]="options" />
        <vig-split-button label="Large" size="lg" variant="secondary" [items]="options" />
      </div>
      <div class="flex flex-wrap items-center gap-3">
        <vig-split-button
          [label]="saving() ? 'Saving…' : 'Save'"
          [loading]="saving()"
          [items]="options"
          (primaryClick)="save()"
        />
        <vig-split-button label="Unavailable" [disabled]="true" [items]="options" />
      </div>
    </div>
  `,
})
export class SplitButtonVariantsDemo {
  protected readonly options: MenuEntry[] = [{ label: 'Schedule…' }, { label: 'Save as draft' }];
  protected readonly saving = signal(false);

  protected save(): void {
    this.saving.set(true);
    setTimeout(() => this.saving.set(false), 1500);
  }
}
```

## When to use it

Use a split button when one action is clearly the default and a few variations of it are rarer. When no action is the default, use a [menu](https://vigdx.com/components/menu.md) behind a single button; when the actions are unrelated, use separate buttons or a [toolbar](https://vigdx.com/components/toolbar.md).

## 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="split-button"][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="split-button"]`

| Part | Element | data-state |
| --- | --- | --- |
| `root` | The `vig-split-button` host. |   |
| `primary` | The `vig-button` of the primary action; style its `[data-scope="button"][data-part="control"]`. |   |
| `trigger` | The menu button (`aria-haspopup="menu"`); the open menu is a `[data-scope="menu"]` panel. |   |

## Accessibility

- Two buttons, two tab stops. The arrow button follows the WAI-ARIA APG menu button pattern: `aria-haspopup="menu"`, `aria-expanded` and, while open, `aria-controls`, also in the server-rendered HTML. Enter, Space or ArrowDown open the menu on the first item, ArrowUp on the last; Escape closes it and returns focus.
- The arrow button is named after the action, “More options for Save”, from `labels.menuAriaLabel` (`{0}` is the label), translated by the locale of `provideVigdxUi`.
- While `loading` both buttons are `aria-disabled` rather than disabled, so focus stays where it is; the primary one is also `aria-busy`. While `items` holds no enabled item the menu button is left out instead of announcing a menu that cannot open.
- The two parts are divided by a line of at least 3:1 against both (the text color on filled variants, the control border color on `secondary`), which forced-colors mode keeps. The focused part is drawn above the other so its focus ring shows in full, and the open menu button shows in the system Highlight color in forced-colors mode.

## API

### SplitButton

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

A primary action with a menu of related ones: a `vig-button` attached to a menu button that opens a `vig-menu` of typed `items`. The menu button follows the APG menu button pattern and is named after the action ("More options for Save"). Without an enabled item there is no menu button, and while `loading` it is unavailable too: its actions are variations of the one running.

**Example :**

```html
<vig-split-button label="Save" [items]="saveOptions" (primaryClick)="save()" />
```

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `disabled` | `boolean` | `false` | Disables the primary action and the menu button. |
| `items` | `readonly MenuEntry[]` | `[]` | Entries of the menu: items and `'separator'`s. While none is an enabled item, the menu button is left out. |
| `label` (required) | `string` | — | Text of the primary action; also names the menu button. |
| `labels` | `Partial<SplitButtonLabels>` | — | Texts (i18n): the menu button's name. |
| `loading` | `boolean` | `false` | Shows a spinner on the primary action and blocks it and the menu button, whose actions are variations of the one running. Both stay focusable (`aria-disabled`), so focus is not lost. |
| `size` | `SplitButtonSize` | `'md'` | Height and text size of both buttons. |
| `type` | `"button" \| "submit" \| "reset"` | `'button'` | `type` of the primary button, e.g. `submit` inside a form. |
| `variant` | `Variant` | `'primary'` | Tone of both buttons, from the shared variants. |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `primaryClick` | `MouseEvent` | Emitted when the primary action is clicked (not while `loading`). |
| `selected` | `MenuItem` | Emitted when a menu item is activated (its `command` also runs). |
