ContextMenu displays an overlay menu on right click of its target.
import { ContextMenu } from 'primereact/contextmenu';
ContextMenu requires a collection of menuitems as its model and the show method needs to be called explicity using the onContextMenu event of the target to display the menu.
<ContextMenu model={items} ref={cm} breakpoint="767px" />
<img src="/images/nature/nature3.jpg" alt="Logo" className="max-w-full" onContextMenu={(e) => cm.current.show(e)} />
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} breakpoint="767px" />
ContextMenu offers item customization with the items template property that receives the item instance and returns an element.
<div className="card flex md:justify-content-center">
<ul className="m-0 p-0 list-none border-1 surface-border border-round p-3 flex flex-column gap-2 w-full md:w-30rem">
{products.map((product) => (
<li
key={product.id}
className={`p-2 hover:surface-hover border-round border-1 border-transparent transition-all transition-duration-200 ${selectedId === product.id && 'border-primary'}`}
onContextMenu={(e) => onRightClick(e, product.id)}
>
<div className="flex flex-wrap p-2 align-items-center gap-3">
<img className="w-4rem shadow-2 flex-shrink-0 border-round" src={`/images/product/${product.image}`} alt="product.name" />
<div className="flex-1 flex flex-column gap-1">
<span className="font-bold">{product.name}</span>
<div className="flex align-items-center gap-2">
<i className="pi pi-tag text-sm"></i>
<span>{product.category}</span>
</div>
</div>
<span className="font-bold text-900 ml-5">{product.price}</span>
</div>
</li>
))}
</ul>
<ContextMenu model={items} ref={cm} breakpoint="767px" onHide={() => setSelectedId(undefined)} />
</div>
The command property defines the callback to run when an item is activated by click or a key event.
<ul className="m-0 p-0 list-none border-1 surface-border border-round p-3 flex flex-column gap-2 w-full md:w-30rem">
{users.map((user) => (
<li
key={user.id}
className={`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'}`}
onContextMenu={(event) => onRightClick(event, user)}
>
<div className="flex align-items-center gap-2">
<img alt="user.name" src={`https://primefaces.org/cdn/primereact/images/avatar/${user.image}`} style={{ width: '32px' }} />
<span className="font-bold">{user.name}</span>
</div>
<Tag value={user.role} severity={getBadge(user)} />
</li>
))}
</ul>
<ContextMenu ref={cm} model={items} onHide={() => setSelectedUser(undefined)} />
<Toast ref={toast} />
Items with navigation are defined with command and url property to be able to use a router link component, an external link or programmatic navigation.
<span className="inline-flex align-items-center justify-content-center border-2 border-primary border-round w-4rem h-4rem" onContextMenu={(event) => onRightClick(event)} aria-haspopup="true">
<img alt="logo" src="https://primefaces.org/cdn/primereact/images/logo.png" height="40"></img>
</span>
<ContextMenu model={items} ref={cm} />
DataTable has built-in support for ContextMenu, see the Context Menu demo for an example.
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. |