Cómo funciona
The menu opens to the right of and below the point, flips left or up when it would leave the area, and scales from the corner nearest the point. Coordinates are converted to layout pixels, so a scaled ancestor does not offset it. A long press shows a ring that closes under the finger and cancels if the finger moves more than 8px.
Úsalo para
- Secondary actions on items in a grid, list or canvas.
- Desktop-like apps where users expect right-click.
Busca otra opción cuando
- The only way to reach an action: also offer a visible button.
- Primary actions: show them directly.
Uso
import { ContextMenu } from './ContextMenu.jsx'
export function FileGrid({ files }) {
return (
<ContextMenu
label="File actions"
items={[
{ id: 'open', label: 'Open', shortcut: '↵', onSelect: open },
{ id: 'rename', label: 'Rename', onSelect: rename },
{ type: 'separator' },
{ id: 'trash', label: 'Move to trash', danger: true, onSelect: trash },
]}
>
{files.map((f) => <FileTile key={f.id} file={f} tabIndex={0} />)}
</ContextMenu>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
children | ReactNode | — | The area that answers right-click and long press; it becomes the positioning context. |
items | ({ id, label, icon?, shortcut?, danger?, disabled?, onSelect? } | { type: 'separator' } | { type: 'label', label })[] | — | Menu entries. |
label | string | — | Accessible name of the menu. |
onOpen | (target: Element) => void | — | Called with the element that was right-clicked or pressed, so you know what the menu is for. |
onOpenChange | (open: boolean) => void | — | Called when the menu opens or closes. |
longPress | number | 480 | Touch hold time in ms. |
defaultOpenAt | { x, y } | null | null | Open on first render at this point in the area (without taking focus). |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the area. |
Accesibilidad
- role menu with role menuitem rows and role separator; disabled items carry aria-disabled.
- Shift+F10 or the Menu key on a focused element opens it with focus on the first item; Up, Down, Home, End and typeahead move; Enter chooses.
- Escape or Tab closes the menu and returns focus to the element it was opened from; a click outside, scrolling or leaving the window also closes it.
- Items are 34px high; the destructive item uses red text and an icon, not colour alone.
- With reduced motion the menu fades without scaling and the long-press ring is shown without animating.
Movimiento
Menu 160ms cubic-bezier(0.2, 0.9, 0.3, 1.1) from 0.94 scale at the corner nearest the pointer; long-press ring fills linearly over the hold time.
Añádelo a tu proyecto
Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.
ContextMenu.jsxcontext-menu.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

