Menu

Menu is a navigation / command component that supports dynamic and static positioning.


import { MenuModule } 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" styleClass="w-full md:w-15rem">
    <ng-template pTemplate="start">
        <span class="inline-flex align-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)" />
                <mask id="mask0_1_36" style="mask-type:luminance" maskUnits="userSpaceOnUse" x="0" y="0" width="31" height="33">
                    <path d="..." fill="var(--primary-color-text)" />
                </mask>
                <g mask="url(#mask0_1_36)">
                    <path fill-rule="evenodd" clip-rule="evenodd" d="..." fill="var(--primary-color)" />
                </g>
                <path d="..." fill="var(--primary-color-text)" />
                <path d="..." fill="var(--primary-color-text)" />
                <path
                    fill-rule="evenodd"
                    clip-rule="evenodd"
                    d="..."
                    fill="var(--primary-color-text)"
                />
                <path d="..." fill="var(--primary-color-text)" />
                <path d="..." fill="var(--primary-color-text)" />
                <path
                    fill-rule="evenodd"
                    clip-rule="evenodd"
                    d="..."
                    fill="var(--primary-color-text)"
                />
                <path d="..." fill="var(--primary-color-text)" />
                <path d="..." fill="var(--primary-color-text)" />
                <path d="..." fill="var(--primary-color-text)" />
                <path d="..." fill="var(--primary-color-text)" />
            </svg>
            <span class="font-medium 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 align-items-center p-menuitem-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-1 surface-border border-round surface-100 text-xs p-1">
                {{ item.shortcut }}
            </span>
        </a>
    </ng-template>
    <ng-template pTemplate="end">
        <button pRipple class="relative overflow-hidden w-full p-link flex align-items-center p-2 pl-3 text-color hover:surface-200 border-noround">
            <p-avatar image="https://primefaces.org/cdn/primeng/images/demo/avatar/amyelsner.png" class="mr-2" shape="circle" />
            <span class="inline-flex flex-column">
                <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 pTemplate="item" let-item>
        <ng-container *ngIf="item.route; else elseBlock">
            <a [routerLink]="item.route" class="p-menuitem-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-menuitem-link">
                <span [class]="item.icon"></span>
                <span class="ml-2">{{ item.label }}</span>
            </a>
        </ng-template>
    </ng-template>
</p-menu>

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
p-menuContainer element.
p-menu-listList element.
p-menuitemMenuitem element.
p-menuitem-textLabel of a menuitem.
p-menuitem-iconIcon of a menuitem.

Screen Reader

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.

Keyboard Support

KeyFunction
tabAdd 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 + tabAdd 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.
enterActivates the focused menuitem. If menu is in overlay mode, popup gets closes and focus moves to target.
spaceActivates the focused menuitem. If menu is in overlay mode, popup gets closes and focus moves to target.
escapeIf menu is in overlay mode, popup gets closes and focus moves to target.
down arrowMoves focus to the next menuitem.
up arrowMoves focus to the previous menuitem.
homeMoves focus to the first menuitem.
endMoves focus to the last menuitem.