# Menu

A list of actions or links that opens from a trigger button, with submenus, headed groups and checkbox or radio items. Use it to tuck away secondary actions, such as row actions in a table or an overflow menu in a toolbar. The same items drive the menubar and the context menu.

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

```typescript title="Import"
import { Menu, MenuTrigger, MenuItemTemplate } from '@vigdx/ui/menu';
```

## Basic

Project a trigger marked `vigMenuTrigger` — a `vig-button` or a native button — and pass the entries as `items`. `'separator'` draws a divider, `danger` styles destructive items, and `selected` emits the activated item.

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

import { MENU_ICONS } from './menu-icons';

@Component({
  selector: 'app-menu-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, Menu, MenuTrigger],
  providers: [MENU_ICONS],
  template: `
    <div class="flex flex-wrap items-center gap-4">
      <vig-menu [items]="actions" (selected)="last.set($event)">
        <vig-button vigMenuTrigger label="Actions" variant="secondary" />
      </vig-menu>
      <span class="text-sm text-muted-foreground" aria-live="polite">
        Last action: {{ last()?.label ?? 'none' }}
      </span>
    </div>
  `,
})
export class MenuBasicDemo {
  protected readonly actions: MenuEntry[] = [
    { label: 'Edit', icon: 'pencil' },
    { label: 'Duplicate', icon: 'copy' },
    'separator',
    { label: 'Delete', icon: 'trash', danger: true },
  ];
  protected readonly last = signal<MenuItem | undefined>(undefined);
}
```

```typescript title="menu-icons.ts"
import { provideVigdxIcons } from '@vigdx/ui/icon';

/**
 * App glyphs registered next to the built-in icons, so menu items can refer
 * to them by name. Usually provided once in `app.config.ts`.
 */
export const MENU_ICONS = provideVigdxIcons({
  pencil: { paths: ['M17 3a2.8 2.8 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z'] },
  copy: {
    paths: [
      'M11 9h9a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-9a2 2 0 0 1-2-2v-9a2 2 0 0 1 2-2Z',
      'M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1',
    ],
  },
  trash: {
    paths: ['M3 6h18M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2m3 0v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6'],
  },
  'more-vertical': {
    paths: [
      'M11 5a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
      'M11 12a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
      'M11 19a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
    ],
  },
});
```

## Submenus and shortcuts

An item with `items` opens a submenu to the inline end (the left in right-to-left pages), flipping at the viewport's edge. `shortcut` shows a key combination at the end of the row; it is display only. Activating any item closes every open menu.

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

import { MENU_ICONS } from './menu-icons';

@Component({
  selector: 'app-menu-submenus',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, Menu, MenuTrigger],
  providers: [MENU_ICONS],
  template: `
    <div class="flex flex-wrap items-center gap-4">
      <vig-menu [items]="file" (selected)="last.set($event)">
        <vig-button vigMenuTrigger label="File" variant="secondary" />
      </vig-menu>
      <span class="text-sm text-muted-foreground" aria-live="polite">
        Last action: {{ last()?.label ?? 'none' }}
      </span>
    </div>
  `,
})
export class MenuSubmenusDemo {
  protected readonly file: MenuEntry[] = [
    { label: 'New file', icon: 'pencil', shortcut: 'Ctrl+N' },
    {
      label: 'Open recent',
      items: [
        { label: 'report.pdf' },
        { label: 'notes.txt' },
        'separator',
        { label: 'Older', items: [{ label: 'budget-2025.xlsx' }, { label: 'plan.docx' }] },
      ],
    },
    { label: 'Duplicate', icon: 'copy', shortcut: 'Ctrl+D' },
    'separator',
    { label: 'Delete', icon: 'trash', danger: true, shortcut: 'Del' },
  ];
  protected readonly last = signal<MenuItem | undefined>(undefined);
}
```

```typescript title="menu-icons.ts"
import { provideVigdxIcons } from '@vigdx/ui/icon';

/**
 * App glyphs registered next to the built-in icons, so menu items can refer
 * to them by name. Usually provided once in `app.config.ts`.
 */
export const MENU_ICONS = provideVigdxIcons({
  pencil: { paths: ['M17 3a2.8 2.8 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z'] },
  copy: {
    paths: [
      'M11 9h9a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-9a2 2 0 0 1-2-2v-9a2 2 0 0 1 2-2Z',
      'M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1',
    ],
  },
  trash: {
    paths: ['M3 6h18M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2m3 0v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6'],
  },
  'more-vertical': {
    paths: [
      'M11 5a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
      'M11 12a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
      'M11 19a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
    ],
  },
});
```

## Groups, checkbox and radio items

Consecutive items with the same `group` are listed under that heading. `type: 'checkbox'` or `'radio'` with `checked` makes checkable items; your app owns the state, so update it from `run` (or the `checkedChange` output). Space toggles an item and keeps the menu open, Enter and a click toggle and close it.

```typescript title="menu-checkable.ts"
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Menu, MenuTrigger, type MenuItem } from '@vigdx/ui/menu';

type Density = 'compact' | 'comfortable' | 'spacious';

@Component({
  selector: 'app-menu-checkable',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, Menu, MenuTrigger],
  template: `
    <div class="flex flex-wrap items-center gap-4">
      <vig-menu [items]="view()">
        <vig-button vigMenuTrigger label="View" variant="secondary" />
      </vig-menu>
      <span class="text-sm text-muted-foreground" aria-live="polite">
        Word wrap {{ wrap() ? 'on' : 'off' }}, {{ density() }} density
      </span>
    </div>
  `,
})
export class MenuCheckableDemo {
  protected readonly wrap = signal(true);
  protected readonly density = signal<Density>('comfortable');

  // The app owns `checked`: each item's `run` updates the state it shows.
  protected readonly view = computed<MenuItem[]>(() => [
    {
      label: 'Word wrap',
      type: 'checkbox',
      checked: this.wrap(),
      run: () => this.wrap.update((on) => !on),
    },
    this.densityItem('Compact', 'compact'),
    this.densityItem('Comfortable', 'comfortable'),
    this.densityItem('Spacious', 'spacious'),
  ]);

  private densityItem(label: string, value: Density): MenuItem {
    return {
      label,
      type: 'radio',
      group: 'Density',
      checked: this.density() === value,
      run: () => this.density.set(value),
    };
  }
}
```

## Row actions

An icon-only trigger per row, named with `ariaLabel`. Each item's `run` performs the action, with the row in scope. The panel opens below the trigger's end edge and flips when space runs out.

```typescript title="menu-row-actions.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 } from '@vigdx/ui/menu';

import { MENU_ICONS } from './menu-icons';

interface Project {
  id: number;
  name: string;
}

@Component({
  selector: 'app-menu-row-actions',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, ButtonIcon, Icon, Menu, MenuTrigger],
  providers: [MENU_ICONS],
  template: `
    <ul class="w-full max-w-md divide-y divide-border rounded-card border border-border">
      @for (project of projects(); track project.id) {
        <li class="flex items-center justify-between gap-3 py-1.5 ps-4 pe-2 text-sm">
          {{ project.name }}
          <vig-menu [items]="actionsFor(project)">
            <vig-button
              vigMenuTrigger
              appearance="text"
              variant="secondary"
              size="sm"
              [ariaLabel]="'Actions for ' + project.name"
            >
              <vig-icon vigButtonIcon name="more-vertical" />
            </vig-button>
          </vig-menu>
        </li>
      } @empty {
        <li class="px-4 py-3 text-sm text-muted-foreground">No projects left.</li>
      }
    </ul>
  `,
})
export class MenuRowActionsDemo {
  protected readonly projects = signal<Project[]>([
    { id: 1, name: 'Website relaunch' },
    { id: 2, name: 'Mobile app' },
    { id: 3, name: 'Annual report' },
  ]);

  // `run` performs the action with the row in scope.
  protected actionsFor(project: Project): MenuEntry[] {
    return [
      { label: 'Duplicate', icon: 'copy', run: () => this.duplicate(project) },
      'separator',
      { label: 'Delete', icon: 'trash', danger: true, run: () => this.remove(project) },
    ];
  }

  private duplicate(project: Project): void {
    this.projects.update((list) => [
      ...list,
      { id: Math.max(...list.map((p) => p.id)) + 1, name: `${project.name} (copy)` },
    ]);
  }

  private remove(project: Project): void {
    this.projects.update((list) => list.filter((p) => p.id !== project.id));
  }
}
```

```typescript title="menu-icons.ts"
import { provideVigdxIcons } from '@vigdx/ui/icon';

/**
 * App glyphs registered next to the built-in icons, so menu items can refer
 * to them by name. Usually provided once in `app.config.ts`.
 */
export const MENU_ICONS = provideVigdxIcons({
  pencil: { paths: ['M17 3a2.8 2.8 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z'] },
  copy: {
    paths: [
      'M11 9h9a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-9a2 2 0 0 1-2-2v-9a2 2 0 0 1 2-2Z',
      'M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1',
    ],
  },
  trash: {
    paths: ['M3 6h18M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2m3 0v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6'],
  },
  'more-vertical': {
    paths: [
      'M11 5a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
      'M11 12a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
      'M11 19a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
    ],
  },
});
```

## Links, groups and disabled items

Items with an `href` render as links; `target="_blank"` adds `rel="noopener noreferrer"`. For router navigation, give the link a `run` that calls the router: a plain click goes to `run`, while Ctrl or Cmd and middle clicks still open the URL in a new tab. Separators group related entries, and `disabled` items stay visible but are skipped by the arrow keys.

```typescript title="menu-links.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Menu, MenuTrigger, type MenuEntry } from '@vigdx/ui/menu';

@Component({
  selector: 'app-menu-links',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, Menu, MenuTrigger],
  template: `
    <vig-menu [items]="help">
      <vig-button vigMenuTrigger label="Help" variant="secondary" />
    </vig-menu>
  `,
})
export class MenuLinksDemo {
  // Separators split the menu into groups.
  protected readonly help: MenuEntry[] = [
    { label: 'Getting started', icon: 'home', href: '/docs/getting-started' },
    { label: 'Theming', icon: 'file', href: '/docs/theming' },
    { label: 'Components', icon: 'search', href: '/components' },
    'separator',
    { label: 'Angular docs', icon: 'external-link', href: 'https://angular.dev', target: '_blank' },
    'separator',
    { label: 'Contact support', icon: 'user', href: '/support', disabled: true },
  ];
}
```

## Custom item template

An `ng-template vigMenuItem` replaces the icon and label of every item; `let-item` is the menu item. Here it adds keyboard shortcut hints.

```typescript title="menu-custom-item.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Icon } from '@vigdx/ui/icon';
import { Menu, MenuItemTemplate, MenuTrigger, type MenuEntry } from '@vigdx/ui/menu';

import { MENU_ICONS } from './menu-icons';

@Component({
  selector: 'app-menu-custom-item',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, Icon, Menu, MenuItemTemplate, MenuTrigger],
  providers: [MENU_ICONS],
  template: `
    <vig-menu [items]="actions">
      <vig-button vigMenuTrigger label="Edit" variant="secondary" />
      <ng-template vigMenuItem let-item>
        @if (item.icon) {
          <vig-icon [name]="item.icon" />
        }
        <span class="truncate">{{ item.label }}</span>
        <kbd class="ms-auto ps-6 font-mono text-xs text-muted-foreground">
          {{ shortcuts[item.label] }}
        </kbd>
      </ng-template>
    </vig-menu>
  `,
})
export class MenuCustomItemDemo {
  protected readonly actions: MenuEntry[] = [
    { label: 'Copy', icon: 'copy' },
    { label: 'Rename', icon: 'pencil' },
    'separator',
    { label: 'Delete', icon: 'trash', danger: true },
  ];
  protected readonly shortcuts: Record<string, string> = {
    Copy: 'Ctrl+C',
    Rename: 'F2',
    Delete: 'Del',
  };
}
```

```typescript title="menu-icons.ts"
import { provideVigdxIcons } from '@vigdx/ui/icon';

/**
 * App glyphs registered next to the built-in icons, so menu items can refer
 * to them by name. Usually provided once in `app.config.ts`.
 */
export const MENU_ICONS = provideVigdxIcons({
  pencil: { paths: ['M17 3a2.8 2.8 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z'] },
  copy: {
    paths: [
      'M11 9h9a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-9a2 2 0 0 1-2-2v-9a2 2 0 0 1 2-2Z',
      'M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1',
    ],
  },
  trash: {
    paths: ['M3 6h18M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2m3 0v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6'],
  },
  'more-vertical': {
    paths: [
      'M11 5a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
      'M11 12a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
      'M11 19a1 1 0 1 0 2 0a1 1 0 1 0-2 0',
    ],
  },
});
```

## What changed in 2.1

Existing menus look and behave a little differently since 2.1:

- Panels fade in and out (with a small slide when motion is not reduced), in about 120 ms. While a menu closes, its panel stays in the page with `data-closing` until the exit ends; tests that expect it gone at once should wait for it.
- Typing a letter moves to the next item that starts with it; a Space typed within such a search belongs to it ("save as"), otherwise Space activates the item when the key comes up.
- A long menu is capped at the viewport's height and scrolls inside itself.
- After a pointer activation, focus returns to the trigger without a focus ring.

## 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="panel"] { … }`: 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 |
| --- | --- | --- |
| `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 menu button pattern: the trigger gets `aria-haspopup="menu"`, `aria-expanded` and `aria-controls`; the panel is a `role="menu"` labelled by the trigger, with `role="menuitem"` buttons or links.
- Enter, Space or ArrowDown on the trigger opens the menu on the first item, ArrowUp on the last, and so do a touch tap and a screen reader's double-tap. A mouse click opens it without moving focus from the trigger, where the arrow keys then enter it. Inside, ArrowUp and ArrowDown wrap, Home and End jump, typing a letter moves to the next item starting with it, and disabled items are skipped. Keyboard focus shows an outline on the item, which forced-colors mode keeps.
- Submenu items have `aria-haspopup="menu"` and `aria-expanded`. ArrowRight, Enter or Space opens a submenu on its first item; ArrowLeft and Escape close one level and return to its item; both arrows are mirrored in right-to-left pages. Hovering an item opens its submenu after a short delay, and while the pointer travels diagonally toward it, the items it crosses do not take over (a safe triangle).
- Checkable items are `menuitemcheckbox` and `menuitemradio` with `aria-checked`; groups are `role="group"` named by their heading. Shortcuts are hidden from assistive technology: they work outside the menu, not in it.
- Escape closes the menu and returns focus to the trigger. Tab closes every menu and continues from the trigger's place in the page (Shift+Tab lands on the trigger), even though menus render at the end of the page; a click outside closes them and lets focus move on. Activating an item closes every menu, so none stays open after navigation.
- Separators are native `<hr>` elements, which menus expose as separators.
- Icon-only triggers have no visible text: give them an `ariaLabel` that names the object, e.g. “Actions for Mobile app”.
- Item icons are decorative. Keep custom `vigMenuItem` templates free of interactive elements; the item itself is the button or link.

## API

### Menu

Selector: `vig-menu` · Kind: component

Action menu behind a trigger (PrimeNG popup p-menu and TieredMenu territory), following the APG menu-button pattern: opening moves focus into the menu (a mouse click leaves it on the trigger, where arrow keys enter), arrow keys wrap, typing jumps to an item, Escape returns to the trigger. Items may open submenus (`items`), sit in headed groups, and be checkbox or radio items; every menu closes once an item is activated.

**Example :**

```html
<vig-menu [items]="actions" (selected)="run($event)">
  <vig-button vigMenuTrigger label="Actions" />
</vig-menu>
```

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 |
| --- | --- | --- | --- |
| `items` | `readonly MenuEntry[]` | `[]` | — |

#### Outputs

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

#### Methods

| Signature | Description |
| --- | --- |
| `close(refocus = true): void` | Closes the menu and its submenus. Focus returns to the trigger when asked to, and always when it is still in a menu or was dropped from one (scroll auto-close, a click on a blank area), so it never falls back to `<body>`. |
| `onTriggerFocusOut(event: FocusEvent): void` | Focus left a trigger whose menu a mouse opened, for somewhere other than the menu. |
| `onTriggerKeydown(event: KeyboardEvent): void` | — |
| `onTriggerPointerDown(event: PointerEvent): void` | Remembers which kind of pointer pressed the trigger. |
| `openMenu(fromEnd = false, enter = true): void` | Opens the menu; `enter` moves focus to the first (or, `fromEnd`, last) item. |
| `toggle(event?: MouseEvent): void` | Opens or closes the menu from a trigger click. Only a mouse click leaves focus on the trigger (no item is focused, the arrow keys enter). Every other open enters the menu: the keyboard (Enter or Space), touch, a pen, and a screen reader's double-tap, whose user could not otherwise reach a menu that renders at the end of the page. |

### Templates and directives

| Selector | Export | Description |
| --- | --- | --- |
| `[vigMenuTrigger]` | `MenuTrigger` | Wires a projected element as the menu trigger. Works on a native button or on a component wrapping one (e.g. vig-button): the ARIA state lands on the actual interactive element. |
| `ng-template[vigMenuItem]` | `MenuItemTemplate` | Custom rendering for the items of a `vig-menu`, replacing the default icon + label inside each `role="menuitem"` button. **Example :** `<vig-menu [items]="actions"> <vig-button vigMenuTrigger label="Actions" /> <ng-template vigMenuItem let-item> <i [class]="item.icon" aria-hidden="true"></i> {{ item.label }} </ng-template> </vig-menu>` |
