Context Menu
SaveA right-click menu with submenus, checkbox and radio items, shortcuts, and destructive items, built on Base UI Context Menu with the Dropdown Menu surface and items.
- Category
- Overlays
- Version
- 0.1.1
- Frameworks
- Next.js, Vite + React
- Styling
- Tailwind CSS v4
Preview
Context Menu preview
Changed filesRight click a file
src/limits.ts2.1 KB
src/handler.ts5.8 KB
docs/rate-limits.md1.3 KB
tests/limits.test.ts3.4 KB
Installation
npx @shakenui/cli add context-menuReview the plan first. A dry run lists every file and package without changing anything:
npx @shakenui/cli add context-menu --dry-runUsage
context-menu-example.tsx
"use client";
import { useState } from "react";
import {
ContextMenu,
ContextMenuCheckboxItem,
ContextMenuContent,
ContextMenuGroup,
ContextMenuItem,
ContextMenuLabel,
ContextMenuRadioGroup,
ContextMenuRadioItem,
ContextMenuSeparator,
ContextMenuShortcut,
ContextMenuSub,
ContextMenuSubContent,
ContextMenuSubTrigger,
ContextMenuTrigger,
} from "@/components/shaken/ui/context-menu";
const icon = { "aria-hidden": true, viewBox: "0 0 16 16", fill: "none" } as const;
const stroke = { stroke: "currentColor", strokeWidth: 1.5, strokeLinecap: "round", strokeLinejoin: "round" } as const;
const files = [
{ name: "src/limits.ts", size: "2.1 KB", modified: "4 min ago" },
{ name: "src/handler.ts", size: "5.8 KB", modified: "4 min ago" },
{ name: "docs/rate-limits.md", size: "1.3 KB", modified: "Yesterday" },
{ name: "tests/limits.test.ts", size: "3.4 KB", modified: "Yesterday" },
];
function FileRow({ file }: { file: (typeof files)[number] }) {
const [inContext, setInContext] = useState(true);
const [access, setAccess] = useState("write");
return (
<ContextMenu>
<ContextMenuTrigger
tabIndex={0}
aria-label={`${file.name}. Press Shift F10 for actions.`}
className={
"flex cursor-default items-center gap-3 px-3 py-2.5 text-sm outline-none select-none " +
"transition-colors duration-150 ease-out motion-reduce:transition-none " +
"hover:bg-zinc-950/[0.03] data-[popup-open]:bg-zinc-950/[0.05] dark:hover:bg-white/[0.04] dark:data-[popup-open]:bg-white/[0.08] " +
"focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-sky-500"
}
>
<svg {...icon} className="size-4 shrink-0 text-zinc-500">
<path d="M4 2.5h5l3 3v8H4v-11Z" {...stroke} />
<path d="M9 2.5v3h3" {...stroke} />
</svg>
<span className="min-w-0 flex-1 truncate font-mono text-xs text-zinc-950 dark:text-white">{file.name}</span>
{access !== "write" || !inContext ? (
<span className="text-xs text-zinc-500">{!inContext ? "Hidden from agent" : "Read only"}</span>
) : null}
<span className="w-16 text-right font-mono text-xs text-zinc-500 tabular-nums max-sm:hidden">{file.size}</span>
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem>
<svg {...icon}>
<path d="M9.5 2.5h4v4M13.5 2.5 7.5 8.5M11.5 9.5v3a1 1 0 0 1-1 1h-7a1 1 0 0 1-1-1v-7a1 1 0 0 1 1-1h3" {...stroke} />
</svg>
Open
<ContextMenuShortcut>↵</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem>
<svg {...icon}>
<path d="M9.5 3.5l3 3-7 7h-3v-3l7-7z" {...stroke} />
</svg>
Rename
<ContextMenuShortcut>F2</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem>
<svg {...icon}>
<rect x="5.5" y="5.5" width="8" height="8" rx="1.5" {...stroke} />
<path d="M10.5 3.5v-.25A.75.75 0 0 0 9.75 2.5h-6.5a.75.75 0 0 0-.75.75v6.5c0 .41.34.75.75.75h.25" {...stroke} />
</svg>
Copy path
<ContextMenuShortcut>⌥⌘C</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuSub>
<ContextMenuSubTrigger
icon={
<svg {...icon}>
<path d="M2.5 4.5a1 1 0 0 1 1-1h3l1.5 1.5h4.5a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1h-9a1 1 0 0 1-1-1v-7.5Z" {...stroke} />
</svg>
}
>
Move to
</ContextMenuSubTrigger>
<ContextMenuSubContent>
<ContextMenuItem>src</ContextMenuItem>
<ContextMenuItem>src/middleware</ContextMenuItem>
<ContextMenuItem>docs</ContextMenuItem>
<ContextMenuItem>tests</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem>New folder…</ContextMenuItem>
</ContextMenuSubContent>
</ContextMenuSub>
<ContextMenuSeparator />
<ContextMenuCheckboxItem checked={inContext} onCheckedChange={setInContext}>
Include in agent context
</ContextMenuCheckboxItem>
<ContextMenuRadioGroup value={access} onValueChange={setAccess}>
<ContextMenuLabel>Agent access</ContextMenuLabel>
<ContextMenuRadioItem value="write">Read and write</ContextMenuRadioItem>
<ContextMenuRadioItem value="read">Read only</ContextMenuRadioItem>
</ContextMenuRadioGroup>
<ContextMenuSeparator />
<ContextMenuGroup>
<ContextMenuItem destructive>
<svg {...icon}>
<path d="M3 4.5h10M6.5 4.5V3a.5.5 0 0 1 .5-.5h2a.5.5 0 0 1 .5.5v1.5M4.5 4.5l.6 8.1a1 1 0 0 0 1 .9h3.8a1 1 0 0 0 1-.9l.6-8.1" {...stroke} />
</svg>
Delete file
<ContextMenuShortcut>⌘⌫</ContextMenuShortcut>
</ContextMenuItem>
</ContextMenuGroup>
</ContextMenuContent>
</ContextMenu>
);
}
export default function ContextMenuExample() {
return (
<div className="w-full max-w-md">
<div className="overflow-hidden rounded-xl border border-zinc-950/10 bg-white dark:border-white/10 dark:bg-black">
<div className="flex items-center justify-between border-b border-zinc-950/[0.07] px-3 py-2 dark:border-white/[0.08]">
<span className="text-xs font-medium text-zinc-500">Changed files</span>
<span className="text-xs text-zinc-500">Right click a file</span>
</div>
<div className="divide-y divide-zinc-950/[0.07] dark:divide-white/[0.08]">
{files.map((file) => (
<FileRow key={file.name} file={file} />
))}
</div>
</div>
</div>
);
}API
| Prop | Type | Default | Description |
|---|---|---|---|
| ContextMenu open / defaultOpen / onOpenChange | boolean / boolean / (open, details) => void | — | Root state. |
| ContextMenuTrigger | Base UI ContextMenu.Trigger props | — | The area that opens the menu on right click, long press, Shift+F10, or the Menu key. Renders a div; add tabIndex={0} and a label when it is not focusable. |
| ContextMenuContent sideOffset / alignOffset | number | 2 / 2 | Offset from the pointer, in pixels. |
| ContextMenuItem destructive / disabled | boolean | false | Red styling for delete-style actions; disabled keeps the item visible but skips it. |
| ContextMenuCheckboxItem checked / defaultChecked / onCheckedChange | boolean / boolean / (checked) => void | — | A toggle with a check in the leading slot. Stays open after a click. |
| ContextMenuRadioGroup value / onValueChange | any / (value) => void | — | Exactly one ContextMenuRadioItem is chosen; the chosen one shows a dot. |
| ContextMenuSub / ContextMenuSubTrigger / ContextMenuSubContent | Base UI SubmenuRoot / SubmenuTrigger / Dropdown Menu content props | — | A nested menu. The trigger takes an optional icon; the panel opens beside it with its first item level with the trigger. |
| ContextMenuGroup / ContextMenuLabel / ContextMenuSeparator / ContextMenuShortcut | Dropdown Menu parts | — | Grouping, section labels, dividers, and shortcut hints, shared with Dropdown Menu. |
Accessibility
- The panel has role="menu"; items are menuitem, menuitemcheckbox, or menuitemradio with aria-checked.
- Right click and long press open the menu at the pointer; on a focused trigger, Shift+F10 and the Menu key open it at the start of the area.
- Arrow keys move the highlight, Home and End jump, typing a letter jumps to a matching item, and Escape closes and returns focus.
- Submenu triggers set aria-haspopup and open with the right arrow key, closing with the left arrow key.
- Disabled items set aria-disabled and stay reachable with the arrow keys.
- The highlight changes color instantly with no animated movement; the panel grows from the pointer in 160ms and fades out in 110ms, and reduced motion removes both.
Files
Installed into src/components/shaken, or components/shaken when the project has no src directory.
src/components/shaken/ui/context-menu.tsxcontext-menu- Also installs dropdown-menu@0.2.1registry dependency
- npm packages: @base-ui/react@^1.9.0React and Tailwind CSS v4 required
Source
The exact source the CLI installs, from /r/items/react/context-menu/0.1.1.json.
ui/context-menu.tsxShowHide
ui/context-menu.tsx
"use client";
import { ContextMenu as BaseContextMenu } from "@base-ui/react/context-menu";
import { Menu as BaseMenu } from "@base-ui/react/menu";
import type { ReactNode } from "react";
import {
DropdownMenuContent,
type DropdownMenuContentProps,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuShortcut,
} from "./dropdown-menu";
type ClassName<State> = string | ((state: State) => string | undefined) | undefined;
function cx(...classes: (string | false | null | undefined)[]) {
return classes.filter(Boolean).join(" ");
}
/** Prepends base classes while keeping Base UI's state-function form of className working. */
function withBase<State>(base: string, className: ClassName<State>) {
return typeof className === "function" ? (state: State) => cx(base, className(state)) : cx(base, className);
}
/** Groups the context menu parts and holds its open state. */
export const ContextMenu = BaseContextMenu.Root;
/**
* The area that opens the menu on right click, on long press with touch, and with Shift+F10 or
* the Menu key when it has focus. Renders a `<div>`; give it `tabIndex={0}` and a label if it is
* not already focusable, so keyboard users can reach the menu too.
*/
export function ContextMenuTrigger({ onKeyDown, ...props }: BaseContextMenu.Trigger.Props) {
return (
<BaseContextMenu.Trigger
onKeyDown={(event) => {
onKeyDown?.(event);
if (event.defaultPrevented) return;
if (!((event.shiftKey && event.key === "F10") || event.key === "ContextMenu")) return;
// Browsers disagree on whether these keys fire `contextmenu`, and where. Open the menu
// at the start of the focused area every time instead.
event.preventDefault();
const target = event.currentTarget as HTMLElement;
const rect = target.getBoundingClientRect();
target.dispatchEvent(
new MouseEvent("contextmenu", {
bubbles: true,
cancelable: true,
button: 2,
clientX: rect.left + Math.min(16, rect.width / 2),
clientY: rect.top + Math.min(rect.height, 32),
}),
);
}}
{...props}
/>
);
}
export interface ContextMenuContentProps extends Omit<DropdownMenuContentProps, "side" | "align"> {}
/** The menu panel, opened at the pointer. Shares its surface and motion with Dropdown Menu. */
export function ContextMenuContent({ sideOffset = 2, alignOffset = 2, ...props }: ContextMenuContentProps) {
return <DropdownMenuContent side="bottom" align="start" sideOffset={sideOffset} alignOffset={alignOffset} {...props} />;
}
/** An action. `destructive` styles it for deleting; `disabled` keeps it visible but skips it. */
export const ContextMenuItem = DropdownMenuItem;
/** Groups related items. Put a ContextMenuLabel inside to name the group. */
export const ContextMenuGroup = DropdownMenuGroup;
/** Names a ContextMenuGroup or ContextMenuRadioGroup. Must be rendered inside the group it labels. */
export const ContextMenuLabel = DropdownMenuLabel;
/** A divider between groups of items. */
export const ContextMenuSeparator = DropdownMenuSeparator;
/** A keyboard shortcut hint shown at the end of an item. */
export const ContextMenuShortcut = DropdownMenuShortcut;
// The same row recipe as Dropdown Menu items, so checkbox, radio, and submenu rows line up with them.
const row = cx(
"flex h-8 cursor-default items-center gap-2.5 rounded-md px-2 text-sm text-zinc-700 outline-none select-none",
"[&_svg]:size-4 [&_svg]:shrink-0 [&_svg]:text-zinc-500",
"data-[highlighted]:bg-zinc-950/[0.05] data-[highlighted]:text-zinc-950 data-[highlighted]:[&_svg]:text-zinc-950",
"dark:text-zinc-300 dark:[&_svg]:text-zinc-500 dark:data-[highlighted]:bg-white/[0.08] dark:data-[highlighted]:text-white dark:data-[highlighted]:[&_svg]:text-white",
"data-[disabled]:pointer-events-none data-[disabled]:opacity-45",
);
// Indicators appear and disappear without motion: toggling is a high-frequency action.
const indicatorSlot = "flex size-4 shrink-0 items-center justify-center";
export interface ContextMenuCheckboxItemProps extends BaseMenu.CheckboxItem.Props {}
/**
* An item that toggles a setting on and off, with a check in the leading slot when on. Stays
* open after a click by default so several settings can be changed in a row.
*/
export function ContextMenuCheckboxItem({ className, children, ...props }: ContextMenuCheckboxItemProps) {
return (
<BaseMenu.CheckboxItem className={withBase(row, className)} {...props}>
<span className={indicatorSlot}>
<BaseMenu.CheckboxItemIndicator className="flex">
<svg aria-hidden="true" viewBox="0 0 16 16" fill="none">
<path d="m3.75 8.25 2.75 2.75 5.75-6" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
</svg>
</BaseMenu.CheckboxItemIndicator>
</span>
{children}
</BaseMenu.CheckboxItem>
);
}
/** Groups radio items so exactly one is chosen. Pass `value`/`onValueChange` to control it. */
export const ContextMenuRadioGroup = BaseMenu.RadioGroup;
export interface ContextMenuRadioItemProps extends BaseMenu.RadioItem.Props {}
/** One choice in a ContextMenuRadioGroup, with a filled dot in the leading slot when chosen. */
export function ContextMenuRadioItem({ className, children, ...props }: ContextMenuRadioItemProps) {
return (
<BaseMenu.RadioItem className={withBase(row, className)} {...props}>
<span className={indicatorSlot}>
<BaseMenu.RadioItemIndicator className="size-1.5 rounded-full bg-current" />
</span>
{children}
</BaseMenu.RadioItem>
);
}
/** Groups a submenu trigger with its panel. */
export const ContextMenuSub = BaseMenu.SubmenuRoot;
export interface ContextMenuSubTriggerProps extends BaseMenu.SubmenuTrigger.Props {
/** Optional icon before the label. */
icon?: ReactNode;
}
/** An item that opens a submenu: on hover, on click, or with the right arrow key. */
export function ContextMenuSubTrigger({ className, icon, children, ...props }: ContextMenuSubTriggerProps) {
return (
<BaseMenu.SubmenuTrigger
className={withBase(
cx(
row,
"data-[popup-open]:bg-zinc-950/[0.05] data-[popup-open]:text-zinc-950 dark:data-[popup-open]:bg-white/[0.08] dark:data-[popup-open]:text-white",
),
className,
)}
{...props}
>
{icon}
{children}
<svg aria-hidden="true" viewBox="0 0 16 16" fill="none" className="-mr-0.5 ml-auto">
<path d="m6.25 4.5 3.5 3.5-3.5 3.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
</svg>
</BaseMenu.SubmenuTrigger>
);
}
export interface ContextMenuSubContentProps extends Omit<DropdownMenuContentProps, "side" | "align"> {}
/** The submenu panel. Opens beside its trigger with its first item level with the trigger row. */
export function ContextMenuSubContent({ sideOffset = 2, alignOffset = -7, ...props }: ContextMenuSubContentProps) {
return <DropdownMenuContent side="inline-end" align="start" sideOffset={sideOffset} alignOffset={alignOffset} {...props} />;
}