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 (onClick)="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.
Name | Element |
---|---|
p-menu | Container element. |
p-menu-list | List element. |
p-menuitem | Menuitem element. |
p-menuitem-text | Label of a menuitem. |
p-menuitem-icon | Icon of a menuitem. |
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. |
API defines helper props, events and others for the PrimeNG Menu module.
Menu is a navigation / command component that supports dynamic and static positioning.
Defines the input properties of the component.
name | type | default | description | ||
---|---|---|---|---|---|
MenuItem[] | |||||
boolean | |||||
Object | |||||
string | |||||
any | |||||
boolean | |||||
number | |||||
string | |||||
string | |||||
string | |||||
string | |||||
string | |||||
number |
Defines emit that determine the behavior of the component based on a given condition or report the actions that the component takes.
name | parameters | description | |
---|---|---|---|
value : any | |||
value : any | |||
event : Event | |||
event : Event |
Defines methods that can be accessed by the component's reference.
name | parameters | description | |
---|---|---|---|
event : Event | |||
event : any | |||
Defines the templates used by the component.
name | parameters | description | |
---|---|---|---|
context : {
$implicit: MenuItem, // Item instance.
} | |||
context : {
$implicit: MenuItem, // Item instance.
} |
Defines the custom interfaces used by the module.
MenuItem provides the following properties. Note that not all of them may be utilized by the tabmenu component.
name | type | description |
---|---|---|
string | ||
string | ||
- | ||
string | ||
MenuItem[] | ||
boolean | ||
boolean | ||
boolean | ||
string | ||
boolean | ||
any | ||
boolean | ||
string | ||
string | ||
string | ||
string | ||
Object | ||
string | ||
string | ||
string | ||
any | ||
string | ||
any | ||
Object | ||
string | ||
QueryParamsHandling | ||
boolean | ||
boolean | ||
boolean | ||
Object | ||
string | ||
Object | ||
TooltipOptions |