# Menubar

An application menu bar such as File, Edit and View: a row of menus that act as one widget, with one Tab stop and the arrow keys between them. Its menus are the same as the Menu's, and on narrow layouts the bar collapses into a single Menu button.

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

```typescript title="Import"
import { Menubar } from '@vigdx/ui/menu';
```

## Basic

Pass the top-level menus as `items`; each one's `items` is its menu, with the same entries as `vig-menu`: submenus, separators, shortcuts, groups and checkable items. A top-level item without `items` acts directly. `selected` emits every activated item, and each item's `run` runs too.

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

@Component({
  selector: 'app-menubar-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Menubar],
  template: `
    <div class="flex w-full flex-col gap-3">
      <vig-menubar [items]="menus" (selected)="last.set($event.label)" />
      <span class="text-sm text-muted-foreground" aria-live="polite">
        Last command: {{ last() ?? 'none' }}
      </span>
    </div>
  `,
})
export class MenubarBasicDemo {
  protected readonly menus: MenuItem[] = [
    {
      label: 'File',
      items: [
        { label: 'New', shortcut: 'Ctrl+N' },
        { label: 'Open…', shortcut: 'Ctrl+O' },
        { label: 'Open recent', items: [{ label: 'report.pdf' }, { label: 'notes.txt' }] },
        'separator',
        { label: 'Save', shortcut: 'Ctrl+S' },
      ],
    },
    {
      label: 'Edit',
      items: [
        { label: 'Undo', shortcut: 'Ctrl+Z' },
        { label: 'Redo', shortcut: 'Ctrl+Y', disabled: true },
        'separator',
        { label: 'Cut' },
        { label: 'Copy' },
        { label: 'Paste' },
      ],
    },
    { label: 'Help', items: [{ label: 'Keyboard shortcuts' }, { label: 'About' }] },
  ];
  protected readonly last = signal<string | undefined>(undefined);
}
```

## Collapsing on narrow layouts

When the bar itself is narrower than `collapse` (`sm` 30rem by default, `md` 40rem, `lg` 48rem, or `never`), it shows one Menu button that opens the same items, the top-level menus as submenus. It is a CSS container query, so a server-rendered page shows the right form before any script runs. The bar fills the space it gets (`flex: 1` in a flex row); inside a shrink-to-fit parent, give it a width.

```typescript title="menubar-collapse.ts"
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import { Menubar, type MenuItem } from '@vigdx/ui/menu';
import { SelectButton } from '@vigdx/ui/select-button';

@Component({
  selector: 'app-menubar-collapse',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Menubar, SelectButton],
  template: `
    <div class="flex w-full flex-col gap-4">
      <vig-select-button label="Space for the bar" [options]="widths" [(value)]="width" />
      <!-- The menubar measures its own width (a container query): give it the space it may use. -->
      <div class="max-w-full" [style.width]="width()">
        <vig-menubar [items]="menus()" collapse="md" />
      </div>
    </div>
  `,
})
export class MenubarCollapseDemo {
  protected readonly widths = [
    { value: '100%', label: 'Wide' },
    { value: '18rem', label: 'Narrow' },
  ];
  protected readonly width = signal('100%');
  private readonly wrap = signal(true);

  protected readonly menus = computed<MenuItem[]>(() => [
    { label: 'File', items: [{ label: 'New' }, { label: 'Save' }] },
    { label: 'Edit', items: [{ label: 'Undo' }, { label: 'Redo' }] },
    {
      label: 'View',
      items: [
        {
          label: 'Word wrap',
          type: 'checkbox',
          checked: this.wrap(),
          run: () => this.wrap.update((on) => !on),
        },
      ],
    },
    { label: 'Help', items: [{ label: 'About' }] },
  ]);
}
```

## 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="menu"][data-part="menubar"] { … }`: 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="menu"]`

| Part | Element | data-state |
| --- | --- | --- |
| `menubar` | The `role="menubar"` row of top-level items. |   |
| `menubar-item` | A top-level `<button>` or `<a>` (`role="menuitem"`). | `open, closed` |
| `menubar-trigger` | The button a narrow menubar collapses into (`collapse`). |   |
| `panel` | A `role="menu"` panel (also each submenu), in the overlay container. |   |
| `item` | A menu item, `<a>` or `<button>`: `open`/`closed` with a submenu, `checked`/`unchecked` when checkable. | `open, closed, checked, unchecked` |
| `icon` | The item icon. |   |
| `label` | The item text. |   |
| `indicator` | The check mark or radio dot of a checkable item. |   |
| `shortcut` | The `<kbd>` of an item's `shortcut`. |   |
| `submenu-indicator` | The chevron of an item that opens a submenu. |   |
| `group` | A `role="group"` of items sharing a `group` heading. |   |
| `group-heading` | The heading of a group. |   |
| `separator` | A separator `<hr>`. |   |

| Attribute | Meaning |
| --- | --- |
| `data-opening` | On the overlay element (panel or `<dialog>`) while its entrance transition plays; removed once it settles (2s at most). Style the entrance with `data-opening:` utilities. |
| `data-closing` | On the overlay element while its exit transition plays, before it closes; removed once it settles (2s at most). Style the closed look with `data-closing:` utilities. |

## Accessibility

- Follows the WAI-ARIA APG menubar pattern: a `role="menubar"` named by `labels.ariaLabel` (“Application menu” by default) of `role="menuitem"` items; those with a menu have `aria-haspopup="menu"` and `aria-expanded`, and their menu is labelled by them.
- The bar is one Tab stop (a roving tab index). ArrowLeft and ArrowRight move between its items and wrap, Home and End jump, typing a letter moves to the next item starting with it; in right-to-left pages the arrows are mirrored. ArrowDown or Enter opens a menu on its first item, ArrowUp on its last.
- While a menu is open, ArrowLeft and ArrowRight close it and open the adjacent one with focus on its bar item (ArrowDown then enters it), from the bar or from inside the menu; pointing at another bar item switches to its menu, without animation. Escape closes the menu and returns to its bar item; Tab closes it and leaves the bar.
- Inside the menus everything works as in the Menu: submenus, typeahead, checkbox and radio items, and every menu closes once an item is activated.
- The collapsed form is a menu button (`aria-haspopup="menu"`) named by `labels.triggerLabel`.
- Use a menubar for application commands. For site navigation, prefer links your users Tab through, such as the Sidebar.

## API

### Menubar

Selector: `vig-menubar` · Kind: component

Application menu bar (File, Edit, View) following the APG menubar pattern: one Tab stop, ArrowLeft/ArrowRight between the top-level items (mirrored in RTL), ArrowDown opens a menu, typing jumps to an item, and once a menu is open, the arrow keys and the pointer switch between menus. Its menus are the same panels as `vig-menu`'s, with submenus, groups and checkbox and radio items. Narrower than `collapse`, it becomes a single "Menu" button opening the same items, decided by CSS alone, so a server-rendered page shows the right one from the start.

**Example :**

```html
<vig-menubar [items]="menus" (selected)="log($event)" />
```

While it plays its entrance or exit transition, the animating element carries `data-opening` or `data-closing` (stable hooks for styling and for waiting in tests; see the theming guide). data-opening data-closing

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `collapse` | `MenubarCollapse` | `'sm'` | Bar width below which it collapses into one button. |
| `items` | `readonly MenuItem[]` | `[]` | Top-level items; each one's `items` is its menu. Others act directly. |
| `labels` | `Partial<MenubarLabels>` | — | Texts of the bar and its collapsed button (i18n); locale defaults. |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `checkedChange` | `MenuCheckedChange` | A checkbox or radio item was activated: update its `checked`. |
| `selected` | `MenuItem` | Emitted when an item is activated (its `command` and `run` also run). |
