Context Menu
FreeNewA right-click menu that opens at the pointer, with icons, shortcuts, submenus, and checkbox and radio items.
Installation
pnpm dlx shadcn@latest add @viewport/context-menu
First time? Add the @viewport registry to components.json. Setup takes one minute.
Usage
The code behind the preview above.
"use client";
import { useState } from "react";
import { ClipboardPaste, Copy, Download, Image as ImageIcon, Scissors, Share2, Trash2 } from "lucide-react";
import {
ContextMenu,
ContextMenuCheckboxItem,
ContextMenuContent,
ContextMenuItem,
ContextMenuLabel,
ContextMenuRadioGroup,
ContextMenuRadioItem,
ContextMenuSeparator,
ContextMenuSub,
ContextMenuSubContent,
ContextMenuSubTrigger,
ContextMenuTrigger,
} from "@/components/viewport/context-menu";
export default function ContextMenuDemo() {
const [grid, setGrid] = useState(true);
const [fit, setFit] = useState("cover");
const [last, setLast] = useState("Right-click the image, or press and hold on a touch screen.");
return (
<div className="flex w-full max-w-sm flex-col gap-3">
<ContextMenu>
<ContextMenuTrigger tabIndex={0} aria-label="Cover image. Open the context menu for actions." className="relative block h-52 overflow-hidden rounded-2xl border border-border bg-card">
<span
aria-hidden
className="absolute inset-0 bg-[radial-gradient(120%_90%_at_20%_10%,oklch(0.85_0.09_75),transparent_60%),radial-gradient(90%_80%_at_85%_90%,oklch(0.62_0.12_45),transparent_65%)] opacity-80"
style={{ backgroundSize: fit === "cover" ? "100% 100%" : "60% 60%", backgroundPosition: "center", backgroundRepeat: "no-repeat" }}
/>
{grid ? <span aria-hidden className="absolute inset-0 bg-[linear-gradient(to_right,rgb(0_0_0/0.08)_1px,transparent_1px),linear-gradient(to_bottom,rgb(0_0_0/0.08)_1px,transparent_1px)] bg-[size:24px_24px]" /> : null}
<span className="absolute bottom-3 start-3 inline-flex items-center gap-1.5 rounded-md bg-background/80 px-2 py-1 text-xs font-medium backdrop-blur-sm">
<ImageIcon className="size-3.5" aria-hidden />
cover.png
</span>
</ContextMenuTrigger>
<ContextMenuContent className="w-56">
<ContextMenuItem icon={<Scissors aria-hidden />} shortcut="⌘X" onSelect={() => setLast("Cut")}>
Cut
</ContextMenuItem>
<ContextMenuItem icon={<Copy aria-hidden />} shortcut="⌘C" onSelect={() => setLast("Copied")}>
Copy
</ContextMenuItem>
<ContextMenuItem icon={<ClipboardPaste aria-hidden />} shortcut="⌘V" disabled>
Paste
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuCheckboxItem checked={grid} onCheckedChange={setGrid}>
Show grid
</ContextMenuCheckboxItem>
<ContextMenuSub>
<ContextMenuSubTrigger>Image fit</ContextMenuSubTrigger>
<ContextMenuSubContent>
<ContextMenuLabel>Image fit</ContextMenuLabel>
<ContextMenuRadioGroup value={fit} onValueChange={setFit}>
<ContextMenuRadioItem value="cover">Fill the frame</ContextMenuRadioItem>
<ContextMenuRadioItem value="contain">Fit inside</ContextMenuRadioItem>
</ContextMenuRadioGroup>
</ContextMenuSubContent>
</ContextMenuSub>
<ContextMenuSeparator />
<ContextMenuItem icon={<Download aria-hidden />} onSelect={() => setLast("Download started")}>
Download
</ContextMenuItem>
<ContextMenuItem icon={<Share2 aria-hidden />} onSelect={() => setLast("Share link copied")}>
Copy share link
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem icon={<Trash2 aria-hidden />} variant="destructive" shortcut="⌫" onSelect={() => setLast("Moved to trash")}>
Move to trash
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
<p role="status" className="text-center text-xs text-muted-foreground">
{last}
</p>
</div>
);
}Props
<ContextMenuItem>
| Prop | Type | Default |
|---|---|---|
| icon | React.ReactNodeAn icon shown before the label. | — |
| shortcut | stringA keyboard shortcut shown at the end of the row. Display only. | — |
| variant | "default" | "destructive"`destructive` colors the row for actions that cannot be undone. | "default" |
<ContextMenuSubTrigger>
| Prop | Type | Default |
|---|---|---|
| icon | React.ReactNode | — |
Accessibility
- Built on Radix Context Menu: opens on right click, long press, Shift+F10 and the menu key.
- Arrow keys, typeahead, Home and End; Escape closes and returns focus.
- Stays inside the viewport and scrolls when it runs out of room.
- No scale animation under reduced motion.