ContextMenu displays an overlay menu on right click of its target.
import { ContextMenuModule } from 'primeng/contextmenu';
ContextMenu can be attached to a particular element whose local template variable name is defined using the target property.
<img
#img
src="https://primefaces.org/cdn/primeng/images/demo/nature/nature2.jpg"
alt="Logo"
aria-haspopup="true"
class="max-w-full" />
<p-contextMenu [target]="img" [model]="items" />
Setting global property to true attaches the context menu to the document.
Right-Click anywhere on this page to view the global ContextMenu.
<p-contextMenu [model]="items" [global]="true" />
ContextMenu offers item customization with the item template that receives the menuitem instance from the model as a parameter.
<ul class="m-0 p-0 list-none border-1 surface-border border-round p-3 flex flex-column gap-2 w-full md:w-30rem">
<li
*ngFor="let product of data"
class="p-2 hover:surface-hover border-round border-1 border-transparent transition-all transition-duration-200"
(contextmenu)="onContextMenu($event)">
<div class="flex flex-wrap p-2 align-items-center gap-3">
<img
class="w-4rem flex-shrink-0 border-round"
src="https://primefaces.org/cdn/primeng/images/product/{{ product.image }}"
alt="product.name" />
<div class="flex-1 flex flex-column gap-1">
<span class="font-bold">
{{ product.name }}
</span>
<div class="flex align-items-center gap-2">
<i class="pi pi-tag text-sm"></i>
<span>
{{ product.category }}
</span>
</div>
</div>
<span class="font-bold text-900 ml-5">
${{ product.price }}
</span>
</div>
</li>
</ul>
<p-contextMenu #cm [model]="items" (onHide)="onHide()">
<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>
<i *ngIf="item.items" class="pi pi-angle-right ml-auto"></i>
</a>
</ng-template>
</p-contextMenu>
The function to invoke when an item is clicked is defined using the command property.
<p-toast />
<ul class="m-0 p-0 list-none border-1 surface-border border-round p-3 flex flex-column gap-2 w-full md:w-30rem">
<li
*ngFor="let user of users"
[class]="[
'p-2',
'hover:surface-hover',
'border-round',
'border-1',
'border-transparent',
'transition-all',
'transition-duration-200',
'flex',
'align-items-center',
'justify-content-between',
selectedUser?.id === user?.id ? 'border-primary' : ''
]"
(contextmenu)="onContextMenu($event, user)">
<div class="flex align-items-center gap-2">
<img
[alt]="user.name"
[src]="'https://primefaces.org/cdn/primeng/images/demo/avatar/' + user.image"
style="width: 32px" />
<span class="font-bold">
{{ user.name }}
</span>
</div>
<p-tag [value]="user.role" [severity]="getBadge(user)" />
</li>
</ul>
<p-contextMenu #cm [model]="items" (onHide)="onHide()" />
ContextMenu can be attached to a particular element whose local template variable name is defined using the target property.
<span #span class="inline-flex align-items-center justify-content-center border-2 border-primary border-round w-4rem h-4rem" aria-haspopup="true">
<svg width="33" height="35" viewBox="0 0 33 35" fill="none" xmlns="http://www.w3.org/2000/svg">
<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>
<p-contextMenu [target]="span" [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-contextMenu>
Table has built-in support for ContextMenu, see the ContextMenu demo for an example.
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-contextmenu | 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. |
p-submenu-icon | Arrow icon of a submenu. |
ContextMenu component uses the menubar role with aria-orientation set to "vertical" 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 ContextMenu uses the menu role with an aria-labelledby defined as the id of the submenu root menuitem label. In addition, menuitems that open a submenu have aria-haspopup, aria-expanded and aria-controls to define the relation between the item and the submenu.
Key | Function |
---|---|
tab | When focus is in the menu, closes the context menu and moves focus to the next focusable element in the page sequence. |
enter | If menuitem has a submenu, toggles the visibility of the submenu otherwise activates the menuitem and closes all open overlays. |
space | If menuitem has a submenu, toggles the visibility of the submenu otherwise activates the menuitem and closes all open overlays. |
escape | Closes the context menu. |
down arrow | If focus is not inside the menu and menu is open, add focus to the first item. If an item is already focused, moves focus to the next menuitem within the submenu. |
up arrow | If focus is not inside the menu and menu is open, add focus to the last item. If an item is already focused, moves focus to the next menuitem within the submenu. |
right arrow | Opens a submenu if there is one available and moves focus to the first item. |
left arrow | Closes a submenu and moves focus to the root item of the closed submenu. |
home | Moves focus to the first menuitem within the submenu. |
end | Moves focus to the last menuitem within the submenu. |
API defines helper props, events and others for the PrimeNG ContextMenu module.
ContextMenu displays an overlay menu on right click of its target. Note that components like Table has special integration with ContextMenu.
Defines the input properties of the component.
name | type | default | description | ||
---|---|---|---|---|---|
MenuItem[] | |||||
string | |||||
string | HTMLElement | |||||
boolean | |||||
Object | |||||
string | |||||
any | |||||
boolean | |||||
number | |||||
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 : null | |||
value : null |
Defines the templates used by the component.
name | parameters | description | |
---|---|---|---|
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 |