Skip to content

React Context Menu

A glass React context menu that opens on right-click, on long-press on touch screens, or with Shift+F10, right where you pressed.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/context-menu.json
Aurora.jpg

Right-click the photo.

  • ContextMenu
  • ContextMenuContent
  • ContextMenuTrigger
  • defaultContextMenuLabels
  • +2 more

Photo grid

Right-click (or long-press) a photo. Every action is also behind the visible ⋯ button: a context menu should be a shortcut, never the only way in.

  • Aurora.jpg
  • Dunes.jpg
  • Lagoon.jpg
  • Bloom.jpg
  • Right-click, long-press or press Shift+F10 for actions.

Files to copy

9 folders · 10 files · 4 packages
From theme/src/
  • components/context-menu/
  • components/menu/
  • components/drawer/parts/sheet.tsx
  • components/kbd/
  • components/status-icons/
  • hooks/{use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, form.ts, glide-highlight.tsx, popup-a11y.ts, popup-motion.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/context-menu.jsonRegistry item: context-menu. The styles need the Tailwind v4 glass UI setup.
The examples also use
  • components/button/
  • components/description-list/copy-button.tsx
  • components/spinner/
  • hooks/use-magnetic.ts
  • motion/gate.ts
  • lib/state-light.tsx
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json https://lokendra-builds.github.io/sheenui/r/description-list.json
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

Canvas

A submenu that adds shapes where you pressed (right-click, long-press or Shift+F10), toggles that keep the menu open, a zoom choice and a disabled item. It opens at the pointer and flips to stay on screen near the edges.

Right-click or long-press anywhereor focus the canvas and press Shift F10

Right-click, long-press or press Shift+F10 to add shapes and change the view.

API

ContextMenu

PropTypeDefaultDescription
open / defaultOpen / onOpenChangeboolean / (open, details) => voidNoneControlled or uncontrolled open state. details.event is what opened it: a MouseEvent (right-click, Shift+F10) or a TouchEvent (long-press).
disabledbooleanfalseLet the browser's own menu through.
childrenReactNodeNoneTakes a ContextMenuTrigger and a ContextMenuContent.
...propsBase UI propsNoneThe rest is passed to the underlying Base UI part (for example closeParentOnEsc, orientation, loopFocus, highlightItemOnHover, onOpenChangeComplete, onItemHighlighted).

ContextMenuTrigger

PropTypeDefaultDescription
tabIndexnumberNoneSet 0 on a non-control area so keyboard users can focus it and press Shift+F10 or the Menu key. The menu then opens at the focused element, not the screen corner.
className / renderstring / ReactElementNoneStyle it, or render it as your own element. Text selection and the iOS callout are off while long-pressing.
...propsBase UI propsNoneThe rest is passed to the underlying Base UI part (for example style).

ContextMenuContent

PropTypeDefaultDescription
side"top" | "bottom" | "left" | "right" | "inline-start" | "inline-end""bottom"Preferred side of the trigger; it flips when there is no room.
align"center" | "end" | "start""start"Sets the alignment along that side.
sideOffsetnumber2Gap between the trigger and the menu, in px (each part has its own default).
alignOffsetnumber0Shift along the alignment axis, in px (each part has its own default).
collisionPaddingnumber12Space kept from the viewport edges, in px.
positionerClassNamestringNoneClasses for the positioner (the fixed element around the popup).
labelsPartial<ContextMenuLabels>defaultContextMenuLabelsSpoken text: menu names the popup (it is announced when the menu opens) and submenu names a nested popup. Merged over defaultContextMenuLabels. Every other string, for translation: labels={{ menu: "…" }}; omitted keys keep the English of defaultContextMenuLabels. All 2 keys of ContextMenuLabels are listed in the key table under the props table.
ContextMenuLabels keys and English defaults (2)
Pass any key in labels; the rest keep the defaults of defaultContextMenuLabels.
KeyEnglish default
menuContext menu (Shift+F10)
submenusubmenu: (level: number) => string;

Keyboard

KeysAction
Right-click/Long-press/Shift + F10/Menu keyOpen the menu at the pointer or the focused element.
Arrow Down/Arrow UpMove between items; the list wraps.
Home/EndJump to the first or last item.
Type a letterJump to the next item that starts with it (typeahead).
Enter/SpaceRun the highlighted item (a checkbox or radio item toggles and keeps the menu open).
EscapeClose and return focus to the trigger.

Accessibility

  • A context menu is a shortcut, never the only way to an action: also show the same actions in a visible menu button.
  • Give the trigger an accessible name that mentions the shortcut; role="menu" with menuitem children, and focus moves into the menu on open.
  • Text selection and the iOS callout are disabled on the trigger so a long press opens the menu.

All exports on this page

  • ContextMenu — Root of a context menu: holds the open state for a ContextMenuTrigger and its ContextMenuContent.
  • ContextMenuContent — The menu popup.
  • ContextMenuTrigger — The area that opens the menu.
  • defaultContextMenuLabels — English defaults of ContextMenuLabels: the popup is "Context menu (Shift+F10)".

When to use the context menu

  • Use it as a right-click menu for a file list or photo grid where right-click on an item offers Duplicate, Rename and Delete, sharing the same MenuItem parts as a visible three-dot menu button.
  • Use it for a canvas or whiteboard where the menu should open at the exact pointer position, flipping to stay on screen when the click is near an edge.
  • Use it on touch devices where a 500 millisecond long-press opens the menu, with text selection and the iOS callout disabled on the trigger so the press is not stolen.
  • Skip it when an action must be discoverable; a context menu is a shortcut, never the only way, so show the same actions in a Dropdown menu button on the item too.
  • Pair it with Dropdown menu parts such as MenuItem, MenuSeparator and MenuCheckboxItem, with KbdShortcut for shown key combinations, and AlertDialog to confirm destructive entries like Delete.

Frequently asked questions

67 components pages in all: browse them on All components.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .