ContextMenu

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.

Logo

<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.

  • Bamboo Watch
    Bamboo Watch
    Accessories
    $65
  • Black Watch
    Black Watch
    Accessories
    $72
  • Blue Band
    Blue Band
    Fitness
    $79
  • Blue T-Shirt
    Blue T-Shirt
    Clothing
    $29
  • Bracelet
    Bracelet
    Accessories
    $15

<ul
    class="m-0 p-0 list-none border border-surface-200 dark:border-surface-700 rounded p-4 flex flex-col gap-2 w-full md:w-[30rem]"
>
    <li
        *ngFor="let product of data"
        class="p-2 hover:bg-surface-100 dark:hover:bg-surface-800 rounded border border-transparent transition-all transition-duration-200"
        [ngClass]="{ 'border-primary': selectedId === product.id }"
        (contextmenu)="onContextMenu($event)"
    >
        <div class="flex flex-wrap p-2 items-center gap-4">
            <img
                class="w-16 shrink-0 rounded"
                src="https://primefaces.org/cdn/primeng/images/demo/product/{{ product.image }}"
                [alt]="product.name"
            />
            <div class="flex-1 flex flex-col gap-1">
                <span class="font-bold">{{ product.name }}</span>
                <div class="flex items-center gap-2">
                    <i class="pi pi-tag text-sm"></i>
                    <span>{{ product.category }}</span>
                </div>
            </div>
            <span class="font-bold ml-8">&#36;{{ product.price }}</span>
        </div>
    </li>
</ul>

<p-contextmenu #cm [model]="items" (onHide)="onHide()">
    <ng-template pTemplate="item" let-item>
        <a pRipple class="flex items-center p-contextmenu-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
            >
            <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.

  • Amy ElsnerAmy Elsner
    Admin
  • Anna FaliAnna Fali
    Member
  • Asiya JavayantAsiya Javayant
    Member
  • Bernardo DominicBernardo Dominic
    Guest
  • Elwin SharvillElwin Sharvill
    Member

<p-toast />
    <ul class="m-0 list-none border border-surface rounded p-4 flex flex-col gap-2 w-full sm:w-96">
        <li
            *ngFor="let user of users"
            class="p-2 hover:bg-emphasis rounded border border-transparent transition-all duration-200 flex items-center justify-content-between"
            [ngClass]="{ 'border-primary': selectedId === user.id }"
            (contextmenu)="onContextMenu($event, user)"
        >
            <div class="flex flex-1 items-center gap-2">
                <img
                    class="w-8 h-8"
                    [alt]="user.name"
                    [src]="'https://primefaces.org/cdn/primeng/images/demo/avatar/' + user.image"
                />
                <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 items-center justify-center border-2 border-primary rounded w-16 h-16"
    aria-haspopup="true"
>
    <svg width="31" height="33" viewBox="0 0 31 33" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path
            d="..."
            fill="var(--p-primary-color)"
        />
        <mask
            id="mask0_1_52"
            style="mask-type:luminance"
            maskUnits="userSpaceOnUse"
            x="0"
            y="0"
            width="31"
            height="33"
        >
            <path
                d="..."
                fill="var(--high-contrast-text-color)"
            />
        </mask>
        <g mask="url(#mask0_1_52)">
            <path
                fill-rule="evenodd"
                clip-rule="evenodd"
                d="..."
                fill="var(--p-primary-color)"
            />
        </g>
        <path
            d="..."
            fill="var(--high-contrast-text-color)"
        />
        <path
            d="..."
            fill="var(--high-contrast-text-color)"
        />
        <path
            fill-rule="evenodd"
            clip-rule="evenodd"
            d="..."
            fill="var(--high-contrast-text-color)"
        />
        <path
            d="..."
            fill="var(--high-contrast-text-color)"
        />
        <path
            d="..."
            fill="var(--high-contrast-text-color)"
        />
        <path
            fill-rule="evenodd"
            clip-rule="evenodd"
            d="..."
            fill="var(--high-contrast-text-color)"
        />
        <path
            d="..."
            fill="var(--high-contrast-text-color)"
        />
        <path
            d="..."
            fill="var(--high-contrast-text-color)"
        />
        <path
            d="..."
            fill="var(--high-contrast-text-color)"
        />
        <path
            d="..."
            fill="var(--high-contrast-text-color)"
        />
    </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-contextmenu-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-contextmenu-item-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.

Screen Reader

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.

Keyboard Support

KeyFunction
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.
escapeCloses 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 arrowOpens a submenu if there is one available and moves focus to the first item.
left arrowCloses a submenu and moves focus to the root item of the closed submenu.
homeMoves focus to the first menuitem within the submenu.
endMoves focus to the last menuitem within the submenu.