ContextMenu

ContextMenu displays an overlay menu to display actions related to an element.


import ContextMenu from 'primevue/contextmenu';

ContextMenu requires a collection of menuitems as its model and the show method needs to be called explicity using an event of the target like contextmenu to display the menu.

Logo

<img alt="Logo" src="/images/nature/nature2.jpg" class="w-full md:w-30rem border-round shadow-4" @contextmenu="onImageRightClick" aria-haspopup="true" />
<ContextMenu ref="menu" :model="items" />

Setting global property attaches the context menu to the document.

Right-Click anywhere on this page to view the global ContextMenu.


<ContextMenu global :model="items" />

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
        v-for="product in products"
        :key="product.id"
        :class="['p-2 hover:surface-hover border-round border-1 border-transparent transition-all transition-duration-200', { 'border-primary': selectedId === product.id }]"
        @contextmenu="onRightClick($event, product.id)"
    >
        <div class="flex flex-wrap p-2 align-items-center gap-3">
            <img class="w-4rem flex-shrink-0 border-round" :src="'/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>
<ContextMenu ref="menu" :model="items" @hide="selectedId = null">
    <template #item="{ item, props }">
        <a v-ripple class="flex align-items-center" v-bind="props.action">
            <span :class="item.icon" />
            <span class="ml-2">{{ item.label }}</span>
            <Badge v-if="item.badge" class="ml-auto" :value="item.badge" />
            <span v-if="item.shortcut" class="ml-auto border-1 surface-border border-round surface-100 text-xs p-1">{{ item.shortcut }}</span>
            <i v-if="item.items" class="pi pi-angle-right ml-auto"></i>
        </a>
    </template>
</ContextMenu>

The command property defines the callback to run when an item is activated by click or a key event.

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

<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
        v-for="user in users"
        :key="user.id"
        :class="['p-2 hover:surface-hover border-round border-1 border-transparent transition-all transition-duration-200 flex align-items-center justify-content-between', { 'border-primary': selectedUser?.id === user.id }]"
        @contextmenu="onRightClick($event, user)"
    >
        <div class="flex align-items-center gap-2">
            <img :alt="user.name" :src="`/images/avatar/${user.image}`" style="width: 32px" />
            <span class="font-bold">{{ user.name }}</span>
        </div>
        <Tag :value="user.role" :severity="getBadge(user)" />
    </li>
</ul>
<ContextMenu ref="menu" :model="items" @hide="selectedUser = null" />
<Toast />

Items with navigation are defined with templating to be able to use a router link component, an external link or programmatic navigation.


<span class="inline-flex align-items-center justify-content-center border-2 border-primary border-round w-4rem h-4rem" @contextmenu="onRightClick" aria-haspopup="true">
    <svg width="35" height="40" viewBox="0 0 35 40" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="..." fill="var(--primary-color)" />
        <path d="..." fill="var(--text-color)" />
    </svg>
</span>
<ContextMenu ref="routemenu" :model="items">
    <template #item="{ item, props }">
        <router-link v-if="item.route" v-slot="{ href, navigate }" :to="item.route" custom>
            <a v-ripple :href="href" v-bind="props.action" @click="navigate">
                <span :class="item.icon" />
                <span class="ml-2">{{ item.label }}</span>
            </a>
        </router-link>
        <a v-else v-ripple :href="item.url" :target="item.target" v-bind="props.action">
            <span :class="item.icon" />
            <span class="ml-2">{{ item.label }}</span>
        </a>
    </template>
</ContextMenu>

DataTable 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 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 and aria-expanded 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.
any printable characterMoves focus to the menuitem whose label starts with the characters being typed.