ContextMenu

A right-click menu for any element, opened at the cursor or from the keyboard with Shift+F10 or the Menu key.

Installation

npx dafink-ui add context-menu

Also installs: Menu

Demo

Last action: none

Props

NameTypeDefaultDescription
itemsMenuLeafEntry[]-Entries: { label, onSelect?, disabled?, selected?, detail? } or { separator: true }. Submenus are not supported.
childrenReactNode-The region that opens the menu on right-click, or on Shift+F10 / the Menu key when focus is inside it.
labelstring'Context menu'Accessible name for the menu.
disabledbooleanfalseNever opens; the browser's native menu shows instead.
onOpenChange(open: boolean) => void-Called when the menu opens or closes.
classNamestring''Additional classes on the menu panel.

Accessibility

Right-clicking anywhere in the wrapped region opens a role="menu" at the cursor; keyboard users open the same menu with Shift+F10 or the Menu key while focus is inside the region, anchored to the focused element. Focus moves into the menu, which uses aria-activedescendant for the active item, and isn't trapped: Escape, Tab, or choosing an item closes it and returns focus to the element that opened it. A pointer press outside, a scroll, or a resize closes it without moving focus. Items and their disabled/selected states render through the same code as Menu, so the two can't drift apart.

KeyFunction
shift+f10Opens the menu for the focused element.
menuOpens the menu for the focused element (the context menu key).
down arrowMoves to the next item, skipping disabled items and wrapping.
up arrowMoves to the previous item, wrapping.
homeMoves to the first item.
endMoves to the last item.
enterActivates the item and closes the menu.
spaceActivates the item and closes the menu.
escapeCloses the menu and returns focus.
tabCloses the menu and returns focus.

On touch screens, a half-second long-press opens the same menu at your finger; moving more than a few pixels cancels it, so scrolling and dragging still work.