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.

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

<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">
                &#36;{{ 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.

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

NameElement
p-contextmenuContainer element.
p-menu-listList element.
p-menuitemMenuitem element.
p-menuitem-textLabel of a menuitem.
p-menuitem-iconIcon of a menuitem.
p-submenu-iconArrow icon of a submenu.

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
tabWhen focus is in the menu, closes the context menu and moves focus to the next focusable element in the page sequence.
enterIf menuitem has a submenu, toggles the visibility of the submenu otherwise activates the menuitem and closes all open overlays.
spaceIf menuitem has a submenu, toggles the visibility of the submenu otherwise activates the menuitem and closes all open overlays.
escapeCloses the context menu.
down arrowIf 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 arrowIf 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.