# Toolbar

A row of controls that act on something else, such as an editor, a canvas or a table. The whole toolbar is one stop in the tab order; the arrow keys move between its controls.

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

```typescript title="Import"
import { Toolbar, ToolbarStart, ToolbarCenter, ToolbarEnd } from '@vigdx/ui/toolbar';
```

## Basic

Put kit or native controls inside and name the toolbar with `labels.ariaLabel`. Buttons, toggle buttons, button groups, split buttons and menus are items; a `vig-divider` separates them. `vigToolbarEnd` moves an item to the end.

```typescript title="toolbar-basic.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Button, ButtonIcon } from '@vigdx/ui/button';
import { ButtonGroup } from '@vigdx/ui/button-group';
import { Divider } from '@vigdx/ui/divider';
import { Icon } from '@vigdx/ui/icon';
import { ToggleButton } from '@vigdx/ui/toggle-button';
import { Toolbar, ToolbarEnd } from '@vigdx/ui/toolbar';

@Component({
  selector: 'app-toolbar-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, ButtonGroup, ButtonIcon, Divider, Icon, ToggleButton, Toolbar, ToolbarEnd],
  template: `
    <vig-toolbar [labels]="{ ariaLabel: 'Text formatting' }">
      <vig-button-group [labels]="{ ariaLabel: 'Style' }">
        <vig-toggle-button onLabel="Bold" [(value)]="bold" />
        <vig-toggle-button onLabel="Italic" [(value)]="italic" />
      </vig-button-group>
      <vig-divider orientation="vertical" />
      <vig-button variant="ghost" ariaLabel="Insert"
        ><vig-icon vigButtonIcon name="plus"
      /></vig-button>
      <vig-button vigToolbarEnd label="Share" variant="secondary" />
    </vig-toolbar>
  `,
})
export class ToolbarBasicDemo {
  protected readonly bold = signal(true);
  protected readonly italic = signal(false);
}
```

## Start, center and end

`vigToolbarStart` (or no marker), `vigToolbarCenter` and `vigToolbarEnd` place items in a region; the regions are logical, so the end is on the left in right-to-left pages. The toolbar draws no frame of its own: add one with classes on the host.

```typescript title="toolbar-regions.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Toolbar, ToolbarCenter, ToolbarEnd, ToolbarStart } from '@vigdx/ui/toolbar';

@Component({
  selector: 'app-toolbar-regions',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, Toolbar, ToolbarStart, ToolbarCenter, ToolbarEnd],
  template: `
    <vig-toolbar
      [labels]="{ ariaLabel: 'Report' }"
      class="rounded-card border border-border bg-subtle p-2"
    >
      <vig-button vigToolbarStart label="Back" variant="ghost" />
      <span vigToolbarCenter class="text-sm font-medium text-foreground">Quarterly report</span>
      <vig-button vigToolbarEnd label="Preview" variant="secondary" />
      <vig-button vigToolbarEnd label="Publish" />
    </vig-toolbar>
  `,
})
export class ToolbarRegionsDemo {}
```

## More actions

Keep the frequent actions in the toolbar and move the rest into a trailing `vig-menu` behind a “More actions” button. Actions without a visible result report it in a status message outside the toolbar.

```typescript title="toolbar-more.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Button, ButtonIcon } from '@vigdx/ui/button';
import { Icon } from '@vigdx/ui/icon';
import { Menu, MenuTrigger, type MenuEntry, type MenuItem } from '@vigdx/ui/menu';
import { Toolbar, ToolbarEnd } from '@vigdx/ui/toolbar';

@Component({
  selector: 'app-toolbar-more',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, ButtonIcon, Icon, Menu, MenuTrigger, Toolbar, ToolbarEnd],
  template: `
    <vig-toolbar [labels]="{ ariaLabel: 'Document' }">
      <vig-button label="Edit" variant="secondary" (click)="announce('Editing')" />
      <vig-button label="Comment" variant="secondary" (click)="announce('Comment added')" />
      <vig-menu vigToolbarEnd [items]="more" (selected)="ran($event)">
        <vig-button vigMenuTrigger variant="ghost" ariaLabel="More actions">
          <vig-icon vigButtonIcon name="more-horizontal" />
        </vig-button>
      </vig-menu>
    </vig-toolbar>
    <!-- Outside the toolbar, and always rendered, so the update is announced. -->
    <p role="status" class="mt-3 min-h-5 text-sm text-muted-foreground">{{ status() }}</p>
  `,
})
export class ToolbarMoreDemo {
  protected readonly more: MenuEntry[] = [
    { label: 'Duplicate' },
    { label: 'Export as PDF', icon: 'download' },
    'separator',
    { label: 'Delete', danger: true },
  ];
  protected readonly status = signal('');

  protected ran(item: MenuItem): void {
    // Let the focus return to "More actions" be announced first.
    setTimeout(() => this.announce(`${item.label} done`), 500);
  }

  protected announce(message: string): void {
    this.status.set(message);
  }
}
```

## Vertical

`orientation="vertical"` stacks the items, for a tool palette beside a canvas; ArrowUp and ArrowDown move between them and the end region sits at the bottom. Use horizontal dividers in it.

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

@Component({
  selector: 'app-toolbar-vertical',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, ButtonIcon, Divider, Icon, Toolbar, ToolbarEnd],
  template: `
    <vig-toolbar
      orientation="vertical"
      [labels]="{ ariaLabel: 'Canvas tools' }"
      class="h-64 rounded-card border border-border p-1"
    >
      <vig-button variant="ghost" ariaLabel="Add shape"
        ><vig-icon vigButtonIcon name="plus"
      /></vig-button>
      <vig-button variant="ghost" ariaLabel="Find"
        ><vig-icon vigButtonIcon name="search"
      /></vig-button>
      <vig-divider />
      <vig-button variant="ghost" ariaLabel="Download"
        ><vig-icon vigButtonIcon name="download"
      /></vig-button>
      <vig-button vigToolbarEnd variant="ghost" ariaLabel="Layout"
        ><vig-icon vigButtonIcon name="columns"
      /></vig-button>
    </vig-toolbar>
  `,
})
export class ToolbarVerticalDemo {}
```

## When the items do not fit

A horizontal toolbar wraps its items onto more lines; nothing is ever clipped or hidden, and the arrow keys follow the same order on every line. The toolbar does not move items into a menu by itself on purpose: a select, a text field or a radio group cannot become a menu item, so an automatic overflow would only work for plain buttons and would duplicate every action in two places.

For a single line on narrow screens, decide which actions are secondary and put them in a trailing `vig-menu`, as in the _More actions_ example. To hide an item only below a width, hide it with CSS (`class="max-sm:hidden"`) and list the same action in the menu; the toolbar skips hidden items, so the tab stop never lands on one.

## Toolbar or button group

A [button group](https://vigdx.com/components/button-group.md) only attaches a few buttons visually; each stays its own tab stop. Use a toolbar when there are several controls (three or more) that act on one piece of content, so keyboard users reach them with one Tab and move on with the next. A toolbar can hold button groups.

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

| Part | Element | data-state |
| --- | --- | --- |
| `root` | The `vig-toolbar` host (`role="toolbar"`); also carries `data-orientation` (`horizontal`, `vertical`). |   |
| `start` | The start region: `[vigToolbarStart]` and unmarked content. |   |
| `center` | The center region (`[vigToolbarCenter]`), hidden while empty. |   |
| `end` | The end region (`[vigToolbarEnd]`). |   |

## Accessibility

- Follows the WAI-ARIA APG toolbar pattern: `role="toolbar"` with an accessible name (`labels.ariaLabel`, “Actions” by default; an `aria-labelledby` you add on the host wins) and `aria-orientation`.
- One tab stop (a roving `tabindex`): Tab enters on the first item, or the one used last. ArrowRight and ArrowLeft move to the next and previous item, mirrored in right-to-left pages; a vertical toolbar uses ArrowDown and ArrowUp instead. Movement wraps at the ends, and Home and End jump to the first and last item.
- Natively disabled controls and hidden ones (by an attribute or by CSS, such as a breakpoint class) are skipped. Controls marked `aria-disabled` stay items, so a disabled action can still be discovered; that is how to disable one in a toolbar. Tab comes back to the item used last, also after the toolbar was hidden.
- Every item stays reachable. A text field keeps Home, End and the arrow keys for its caret, and hands the arrow keys over once the caret is at the edge they point past. A native radio group, such as `vig-select-button`, is one item: the arrow keys move its selection, and past its first or last option they move on; Home and End jump out of it.
- Controls that need every arrow key and cannot hand them over (native selects, number and date fields, listboxes, tabs) are not items: they keep their own tab stop, reached with Tab. Content of a dialog or popover declared inside the toolbar is not part of it either.
- A control that handles a key itself keeps it. In a vertical toolbar the toolbar takes ArrowUp and ArrowDown from a collapsed menu button, split button or select, which open with Enter or Space.
- The toolbar only writes the `tabindex` of its items, and puts back the one it replaced when a control stops being an item or the toolbar goes away.
- The server renders every control in the tab order; the browser narrows it to one tab stop once the page has hydrated.

## API

### Toolbar

Selector: `vig-toolbar` · Kind: component

A row (or column) of controls that act on something else, such as an editor or a table, following the APG toolbar pattern: one tab stop, arrow keys between the controls (mirrored in RTL), Home and End to the first and last. A text field hands the arrow keys over once the caret is at its edge, a radio group at its first and last radio; controls that need every arrow key (selects, number and date fields, listboxes, tabs) keep their own tab stop instead of joining the roving one.

Put kit or native controls straight inside; `vigToolbarStart`, `vigToolbarCenter` and `vigToolbarEnd` place them in a region.

**Example :**

```html
<vig-toolbar [labels]="{ ariaLabel: 'Text formatting' }">
  <vig-toggle-button onLabel="Bold" [(value)]="bold" />
  <vig-toggle-button onLabel="Italic" [(value)]="italic" />
  <vig-divider orientation="vertical" />
  <vig-button vigToolbarEnd label="Share" variant="secondary" />
</vig-toolbar>
```

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `labels` | `Partial<ToolbarLabels>` | — | Texts (i18n): the toolbar's accessible name; locale default "Actions". |
| `orientation` | `ToolbarOrientation` | `'horizontal'` | `horizontal` (default): ArrowLeft/ArrowRight move, mirrored in RTL, and the items wrap onto more lines when they do not fit. `vertical`: ArrowUp/ArrowDown move, the items stack in a column. |

### Templates and directives

| Selector | Export | Description |
| --- | --- | --- |
| `[vigToolbarStart]` | `ToolbarStart` | Projects content into the toolbar's start region (where unmarked content goes too). |
| `[vigToolbarCenter]` | `ToolbarCenter` | Projects content into the toolbar's center region. |
| `[vigToolbarEnd]` | `ToolbarEnd` | Projects content into the toolbar's end region (right in LTR, left in RTL). |
