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.jsonAurora.jpg
Right-click the photo.
ContextMenuContextMenuContentContextMenuTriggerdefaultContextMenuLabels- +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
import { Button } from "@/components/sheenui/button/button";
import { ContextMenu, ContextMenuContent, ContextMenuTrigger } from "@/components/sheenui/context-menu/context-menu";
import { CopyIcon } from "@/components/sheenui/description-list/copy-button";
import { Menu, MenuContent, MenuItem, MenuSeparator, MenuSub, MenuSubContent, MenuSubTrigger, MenuTrigger } from "@/components/sheenui/menu/menu";
// Package build (after the first npm release, or a packed tarball): import { Button, ContextMenu, ContextMenuContent, ContextMenuTrigger, CopyIcon, Menu, MenuContent, MenuItem, MenuSeparator, MenuSub, MenuSubContent, MenuSubTrigger, MenuTrigger } from "@sheenui/theme";
import { Ellipsis as DotsIcon, Pencil as PencilIcon, Share2 as ShareIcon, Trash2 as TrashIcon } from "lucide-react";
// This example also uses button, description-list: npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json https://lokendra-builds.github.io/sheenui/r/description-list.json
function PhotoActions({ name }: { name: string }) {
return (
<>
<MenuItem icon={<PencilIcon />} shortcut="enter" onClick={() => console.log("rename", name)}>
Rename
</MenuItem>
<MenuItem icon={<CopyIcon />} shortcut="mod+d">Duplicate</MenuItem>
<MenuSub>
<MenuSubTrigger icon={<ShareIcon />}>Share</MenuSubTrigger>
<MenuSubContent>
<MenuItem>Copy link</MenuItem>
<MenuItem>Email</MenuItem>
</MenuSubContent>
</MenuSub>
<MenuSeparator />
<MenuItem icon={<TrashIcon />} tone="danger">Move to bin</MenuItem>
</>
);
}
export function Example() {
return (
<>
<ContextMenu>
<ContextMenuTrigger tabIndex={0} role="group" aria-label="Aurora.jpg" aria-describedby="hint">
<img src="aurora.jpg" alt="" />
</ContextMenuTrigger>
<ContextMenuContent><PhotoActions name="Aurora.jpg" /></ContextMenuContent>
</ContextMenu>
{/* The same actions behind a visible button */}
<Menu>
<MenuTrigger render={<Button variant="glass" size="icon" aria-label="Actions for Aurora.jpg" />}>
<DotsIcon />
</MenuTrigger>
<MenuContent align="end"><PhotoActions name="Aurora.jpg" /></MenuContent>
</Menu>
</>
);
}Files to copy
9 folders · 10 files · 4 packages
- From
theme/src/ components/context-menu/components/menu/components/drawer/parts/sheet.tsxcomponents/kbd/components/status-icons/hooks/{use-latest.ts, use-media-query.ts, use-preferences.ts}motion/tokens.tslib/{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.tsxcomponents/spinner/hooks/use-magnetic.tsmotion/gate.tslib/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 ShiftF10Shift F10
import { useRef, useState } from "react";
import { ContextMenu, ContextMenuContent, ContextMenuTrigger } from "@/components/sheenui/context-menu/context-menu";
import { MenuCheckboxItem, MenuItem, MenuLabel, MenuRadioGroup, MenuRadioItem, MenuSeparator, MenuSub, MenuSubContent, MenuSubTrigger } from "@/components/sheenui/menu/menu";
// Package build (after the first npm release, or a packed tarball): import { ContextMenu, ContextMenuContent, ContextMenuTrigger, MenuCheckboxItem, MenuItem, MenuLabel, MenuRadioGroup, MenuRadioItem, MenuSeparator, MenuSub, MenuSubContent, MenuSubTrigger } from "@sheenui/theme";
import { Plus as PlusIcon } from "lucide-react";
// onOpenChange gets the event that opened the menu: a MouseEvent for a right-click or
// Shift+F10, the TouchEvent for a long-press (iOS sends no contextmenu event).
export function Example() {
const point = useRef({ x: 0, y: 0 });
const [snap, setSnap] = useState(true);
const [zoom, setZoom] = useState("100");
const addSquare = () => console.log("add square at", point.current);
function rememberPoint(open: boolean, { event }: { event: Event }) {
const at = event instanceof TouchEvent ? event.touches[0] : (event as MouseEvent);
if (open && at) point.current = { x: at.clientX, y: at.clientY };
}
return (
<ContextMenu onOpenChange={rememberPoint}>
<ContextMenuTrigger tabIndex={0} role="group" aria-label="Canvas">
Right-click, press Shift+F10 or long-press here
</ContextMenuTrigger>
<ContextMenuContent>
<MenuSub>
<MenuSubTrigger icon={<PlusIcon />}>Add shape</MenuSubTrigger>
<MenuSubContent>
<MenuItem onClick={addSquare}>Square</MenuItem>
</MenuSubContent>
</MenuSub>
<MenuItem shortcut="mod+v" disabled>Paste</MenuItem>
<MenuSeparator />
<MenuCheckboxItem checked={snap} onCheckedChange={setSnap}>Snap to grid</MenuCheckboxItem>
<MenuRadioGroup value={zoom} onValueChange={(value) => setZoom(value as string)}>
<MenuLabel>Zoom</MenuLabel>
<MenuRadioItem value="100">100%</MenuRadioItem>
</MenuRadioGroup>
</ContextMenuContent>
</ContextMenu>
);
}API
ContextMenu
ContextMenuTrigger
ContextMenuContent
Keyboard
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
Related pages
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .