Loading component…
Loading component…
The menu a right-click opens, right at the pointer
To reuse this local React demo, copy all files from the Code tab into the same folder, then import ContextMenu from ./context-menu.
import { Copy01Icon, Delete02Icon, FileAddIcon, Link01Icon, ViewIcon } from "@hugeicons/core-free-icons";
import { ContextMenu } from "@/components/ui/context-menu";
export function Files({ files }: { files: { id: string; name: string }[] }) {
return (
<ContextMenu
aria-label="File actions"
items={(target) => {
const id = target.closest<HTMLElement>("[data-file]")?.dataset.file;
const file = files.find((f) => f.id === id);
if (!file) return [{ label: "New file", icon: FileAddIcon, shortcut: "⌘N", onSelect: () => create() }];
return [
{ label: "Open", icon: ViewIcon, onSelect: () => open(file.id) },
{ label: "Copy link", icon: Link01Icon, onSelect: () => copyLink(file.id) },
{ label: "Duplicate", icon: Copy01Icon, onSelect: () => duplicate(file.id) },
{ label: "Delete file", icon: Delete02Icon, danger: true, divider: true, onSelect: () => remove(file.id) },
];
}}
>
<ul>
{files.map((f) => (
<li key={f.id}>
<button type="button" data-file={f.id}>{f.name}</button>
</li>
))}
</ul>
</ContextMenu>
);
}| Name | Type | Default | Description |
|---|---|---|---|
| children | ReactElement | The area it opens on – exactly one element that takes a ref (a row, a card, a list). Its own handlers run first; one that calls preventDefault keeps the menu shut. The menu is a <div> right after it – wrap a <li> or <tr> in its list or table rather than the row itself | |
| items | ContextMenuItem[] \| (target: HTMLElement) => ContextMenuItem[] | The items, or a function that builds them for the element under the pointer (or the focused one) at the moment it opens – and again while it is open, so checks stay current. No items: the browser's own menu shows | |
| item | ContextMenuItem | { label, description?, icon?, logo?, checked?, type?, danger?, shortcut?, divider?, disabled?, keepOpen?, submenu?, onSelect? } – the same items as the Dropdown Menu | |
| item.icon | ReactNode \| HugeIcon | A 16px glyph before the label – an inline SVG, or a Hugeicons icon object passed as it is | |
| item.checked / item.type | boolean / "radio" \| "checkbox" | Makes the item a choice with a check on the right – one of a set (radio) or on and off on its own (checkbox) | |
| item.danger | boolean | Red label and icon, a red-tinted highlight – for Delete and the like | |
| item.shortcut | string | Shown on the right, e.g. "⌘C" – only shown, wire the keys up yourself | |
| item.divider / item.disabled / item.keepOpen | boolean | A hairline above the item · faint and skipped by the arrow keys · the menu stays open after the pick | |
| item.submenu | ContextMenuItem[] | Opens beside the item on hover, →, Enter or Space; ← or Esc steps back | |
| title | string | A small gray heading above the items – also the menu's accessible name | |
| aria-label | string | "Actions" | Names the menu when there is no title |
| onOpenChange | (open: boolean, target: HTMLElement \| null) => void | Called when it opens, with the element it opened on – e.g. to select the row it is about – and when it closes | |
| disabled | boolean | Never opens – the browser's own menu shows | |
| width | number \| string | The menu's width; by default it fits the items, at least 180px | |
| menuClassName | string | Classes on the menu panel |
Opens with its corner at the pointer and turns to the left or upward when there is no room; it lives in the top layer (popover), so no overflow or z-index cuts it
A press elsewhere, a scroll that moves the area, resizing or leaving the window closes it
Touch: a long press (half a second, without moving) opens it at the finger – the callout and the tap that would follow are held back; touch screens get 40px rows
In right-to-left text it opens to the left of the pointer
Dark mode built in: switches when an ancestor (usually <html>) has the class dark or data-theme="dark"
@import "tailwindcss"; in your main CSS file.