Overlays
Context Menu
Right-click menu anchored at the pointer; Shift+F10 or the Menu key for keyboard users.
react-aria-components
Preview
Right-click here (or focus and press Shift+F10)
No action yet.
"use client";
import { Copy, Download, List, Send, Trash2 } from "lucide-react";
import { useState } from "react";
import { ContextMenu } from "@/components/ui/context-menu";
import { MenuItem } from "@/components/ui/menu";
export default function Example() {
const [last, setLast] = useState<string | null>(null);
return (
<div className="flex w-full max-w-md flex-col gap-3">
<ContextMenu
onAction={(key) => setLast(String(key))}
target={
<div className="flex h-36 items-center justify-center rounded-lg border border-dashed border-line-strong bg-surface text-base text-fg-muted">
Right-click here (or focus and press Shift+F10)
</div>
}
>
<MenuItem id="list" icon={<List />} shortcut="L">Add to list</MenuItem>
<MenuItem id="sequence" icon={<Send />} shortcut="S">Start sequence</MenuItem>
<MenuItem id="copy" icon={<Copy />} shortcut="⌘C">Copy email</MenuItem>
<MenuItem id="export" icon={<Download />}>Export</MenuItem>
<MenuItem id="delete" icon={<Trash2 />} danger>Delete</MenuItem>
</ContextMenu>
<p className="text-sm text-fg-muted">{last ? `Last action: ${last}` : "No action yet."}</p>
</div>
);
}Installation
Components are copied into your project — no runtime package.
Install the following dependencies:
terminalnpm install react-aria-componentsCreate a
context-menu.tsxfile and paste the following code into it.src/components/ui/context-menu.tsx"use client"; import { type KeyboardEvent, type MouseEvent, type ReactNode, useRef, useState } from "react"; import { Menu, type MenuProps, Popover } from "react-aria-components"; import { cx } from "@/utils/cx"; export interface ContextMenuProps<T extends object> extends Omit<MenuProps<T>, "className" | "children"> { /** The area that responds to right-click (or Shift+F10 / the Menu key). */ target: ReactNode; /** <MenuItem>s from ./menu. */ children: ReactNode; className?: string; } /** Right-click menu anchored at the pointer. Keyboard users open it with Shift+F10 or the Menu key. */ export function ContextMenu<T extends object>({ target, children, className, onAction, ...props }: ContextMenuProps<T>) { const [open, setOpen] = useState(false); const [pos, setPos] = useState({ x: 0, y: 0 }); const anchor = useRef<HTMLSpanElement>(null); const area = useRef<HTMLDivElement>(null); const openAt = (x: number, y: number) => { const box = area.current!.getBoundingClientRect(); setPos({ x: x - box.left, y: y - box.top }); setOpen(true); }; const onContextMenu = (e: MouseEvent<HTMLDivElement>) => { e.preventDefault(); openAt(e.clientX, e.clientY); }; const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => { if ((e.shiftKey && e.key === "F10") || e.key === "ContextMenu") { e.preventDefault(); const box = area.current!.getBoundingClientRect(); openAt(box.left + 16, box.top + 16); } }; return ( <div ref={area} onContextMenu={onContextMenu} onKeyDown={onKeyDown} tabIndex={0} aria-haspopup="menu" className={cx("relative outline-none focus-visible:shadow-focus", className)}> {target} <span ref={anchor} aria-hidden className="pointer-events-none absolute size-0" style={{ left: pos.x, top: pos.y }} /> <Popover triggerRef={anchor} isOpen={open} onOpenChange={setOpen} placement="bottom start" offset={2} className="min-w-48 rounded-md bg-surface p-1 shadow-popover outline-none"> <Menu {...props} aria-label="Context menu" autoFocus="first" onAction={(...args: Parameters<NonNullable<typeof onAction>>) => { setOpen(false); onAction?.(...args); }} className="outline-none" > {children} </Menu> </Popover> </div> ); }Create a
menu.tsxfile and paste the following code into it.src/components/ui/menu.tsx"use client"; import type { ReactNode } from "react"; import { Menu as AriaMenu, MenuItem as AriaMenuItem, type MenuItemProps as AriaMenuItemProps, type MenuProps as AriaMenuProps, MenuTrigger, Popover, Separator, } from "react-aria-components"; import { cx } from "@/utils/cx"; import { Kbd } from "./kbd"; export { MenuTrigger }; export function Menu<T extends object>({ className, ...props }: Omit<AriaMenuProps<T>, "className"> & { className?: string }) { return ( <Popover placement="bottom end" offset={6} className="min-w-48 rounded-md bg-surface p-1 shadow-popover outline-none"> <AriaMenu {...props} className={cx("outline-none", className)} /> </Popover> ); } export interface MenuItemProps extends Omit<AriaMenuItemProps, "children" | "className"> { icon?: ReactNode; shortcut?: string; danger?: boolean; children: ReactNode; className?: string; } /** 36px items, radius 8, 16px icon, optional shortcut. */ export function MenuItem({ icon, shortcut, danger, children, className, ...props }: MenuItemProps) { const textValue = props.textValue ?? (typeof children === "string" ? children : undefined); return ( <AriaMenuItem {...props} textValue={textValue} className={cx( "flex h-9 cursor-default items-center gap-2.5 rounded-sm px-2.5 text-base outline-none [&_svg]:size-4 [&_svg]:shrink-0", danger ? "text-error-text data-focused:bg-error-soft [&_svg]:text-error" : "text-fg data-focused:bg-muted [&_svg]:text-fg-secondary", "data-disabled:text-fg-disabled", className, )} > {icon} <span className="flex-1">{children}</span> {shortcut && <Kbd className="border-0 bg-transparent">{shortcut}</Kbd>} </AriaMenuItem> ); } export function MenuSeparator() { return <Separator className="mx-1 my-1 border-t border-line" />; }Create a
kbd.tsxfile and paste the following code into it.src/components/ui/kbd.tsximport { cx } from "@/utils/cx"; /** Marks keyboard shortcuts. */ export function Kbd({ className, children }: { className?: string; children: React.ReactNode }) { return ( <kbd className={cx( "inline-flex h-5 min-w-5 items-center justify-center rounded-[6px] border border-line bg-surface px-1.5 font-sans text-xs font-medium text-fg-muted", className, )} > {children} </kbd> ); }Add the theme tokens and the
cxhelper from Installation if you haven’t yet, then update the import paths to match your project setup.