Menu is a navigation / command component that supports dynamic and static positioning.
import { Menu } from 'primeng/menu';
Menu requires a collection of menuitems as its model.
<p-menu [model]="items" />
Menu supports one level of nesting by defining children with items property.
<p-menu [model]="items" />
Popup mode is enabled by setting popup property to true and calling toggle method with an event of the target.
<p-toast />
<p-menu #menu [model]="items" [popup]="true" />
<p-button (click)="menu.toggle($event)" icon="pi pi-ellipsis-v"/>
Menu offers item customization with the item template that receives the menuitem instance from the model as a parameter. The submenu header has its own submenuheader template, additional slots named start and end are provided to embed content before or after the menu.
<p-menu [model]="items" class="flex justify-center" styleClass="w-full md:w-60">
<ng-template pTemplate="start">
<span class="inline-flex items-center gap-1 px-2 py-2">
<svg width="33" height="35" viewBox="0 0 33 35" fill="none" xmlns="http://www.w3.org/2000/svg" class="block mx-auto">
<path d="..." fill="var(--primary-color)" />
</svg>
<span class="text-xl font-semibold">
PRIME<span class="text-primary">APP</span>
</span>
</span>
</ng-template>
<ng-template pTemplate="submenuheader" let-item>
<span class="text-primary font-bold">{{ item.label }}</span>
</ng-template>
<ng-template pTemplate="item" let-item>
<a pRipple class="flex items-center p-menu-item-link">
<span [class]="item.icon"></span>
<span class="ml-2">{{ item.label }}</span>
<p-badge *ngIf="item.badge" class="ml-auto" [value]="item.badge" />
<span *ngIf="item.shortcut" class="ml-auto border border-surface rounded bg-emphasis text-muted-color text-xs p-1">
{{ item.shortcut }}
</span>
</a>
</ng-template>
<ng-template pTemplate="end">
<button pRipple class="relative overflow-hidden w-full border-0 bg-transparent flex items-start p-2 pl-4 hover:bg-surface-100 dark:hover:bg-surface-800 rounded-none cursor-pointer transition-colors duration-200">
<p-avatar image="https://primefaces.org/cdn/primeng/images/demo/avatar/amyelsner.png" class="mr-2" shape="circle" />
<span class="inline-flex flex-col">
<span class="font-bold">Amy Elsner</span>
<span class="text-sm">Admin</span>
</span>
</button>
</ng-template>
</p-menu>
The function to invoke when an item is clicked is defined using the command property.
<p-toast />
<p-menu [model]="items" />
Navigation is specified using url property for external links and with routerLink for internal ones. If a menuitem has an active route, p-menuitem-link-active style class is added as an indicator. Active route link can be configured with routerLinkActiveOptions property of MenuItem API.
<p-menu [model]="items">
<ng-template #item let-item>
<ng-container *ngIf="item.route; else elseBlock">
<a [routerLink]="item.route" class="p-menu-item-link">
<span [class]="item.icon"></span>
<span class="ml-2">{{ item.label }}</span>
</a>
</ng-container>
<ng-template #elseBlock>
<a [href]="item.url" class="p-menu-item-link">
<span [class]="item.icon"></span>
<span class="ml-2">{{ item.label }}</span>
</a>
</ng-template>
</ng-template>
</p-menu>
Menu component uses the menu role and the value to describe the menu can either be provided with aria-labelledby or aria-label props. Each list item has a presentation role whereas anchor elements have a menuitem role with aria-label referring to the label of the item and aria-disabled defined if the item is disabled. A submenu within a Menu uses the group role with an aria-labelledby defined as the id of the submenu root menuitem label.
In popup mode, the component implicitly manages the aria-expanded, aria-haspopup and aria-controls attributes of the target element to define the relation between the target and the popup.
Key | Function |
---|---|
tab | Add focus to the first item if focus moves in to the menu. If the focus is already within the menu, focus moves to the next focusable item in the page tab sequence. |
shift + tab | Add focus to the last item if focus moves in to the menu. If the focus is already within the menu, focus moves to the previous focusable item in the page tab sequence. |
enter | Activates the focused menuitem. If menu is in overlay mode, popup gets closes and focus moves to target. |
space | Activates the focused menuitem. If menu is in overlay mode, popup gets closes and focus moves to target. |
escape | If menu is in overlay mode, popup gets closes and focus moves to target. |
down arrow | Moves focus to the next menuitem. |
up arrow | Moves focus to the previous menuitem. |
home | Moves focus to the first menuitem. |
end | Moves focus to the last menuitem. |