Skip to content
Browse documentation

Actions where you right-click: the menu grows from the exact point, a long press opens it on touch, and Shift+F10 from the keyboard.

Used forFile and asset gridsCanvas and boardsTablesText editors

Install with the shadcn CLI

$ npx shadcn@latest add @hairline/context-menu

Older shadcn CLI, or a network that blocks ui.shadcn.com? Install by URL: shadcn add https://hairlineui.com/r/context-menu.json. More

Templates

Need the whole page?

65 Next.js landing pages, each designed from scratch. Try any of them live; one is $39, all of them $159, and 8 are free.

Where it’s used

  • File and asset grids

    Rename, move, delete on a right-click.

  • Canvas and boards

    Actions for the item under the pointer.

  • Tables

    Row actions without a "…" column.

  • Text editors

    Custom actions on a selection.

What it handles for you

A right-click menu that only works with a mouse leaves phones and keyboards out; this one has a way in for each.

Instead of wiring upRadix ContextMenulong-press handlingviewport collision
  1. Three ways in

    Right-click, long press (500ms, cancelled by movement) and Shift+F10.

  2. From the point

    Grows from where you clicked; flips and shifts to fit.

  3. Same rows as the dropdown

    Gliding highlight, typeahead and checkable rows.

  4. Callout suppressed

    iOS doesn't show its own menu on the long press.

Overview

Custom right-click menus often forget touch and the keyboard. This one opens at the pointer on right-click, after a 500ms long press on touch (the area dims slightly while it waits), and on Shift+F10 or the menu key. It grows from the exact point, stays inside the window, and right-clicking elsewhere moves it there.

States

waiting
On touch, the area eases to 0.985 and 80% while the long press counts down.
open
The menu rises from the point (320ms).
pick
It closes and focus returns to the area.

Usage

usage.tsx
import { ContextMenu } from "@/components/context-menu";<ContextMenu items={[{ id: "open", label: "Open" }, { id: "delete", label: "Delete", destructive: true, onSelect: remove }]}>  <FileCard file={file} /></ContextMenu>

Depends on lucide-react, installed for you by the CLI along with the keyframes it needs.

Works with

Props

children
ReactNode
The area with the menu.
items
MenuEntry[]
Same rows as the dropdown menu.
label= "Actions"
string
Accessible name of the menu.
onOpenChange
(open) => void
Opened or closed.

Accessibility and motion

  • Placement, presence and dismissal live in floating.ts, shared by tooltip, popover, dropdown and context menus: they all flip, shift, enter and leave the same way.
  • The area takes focus (tabIndex 0) so Shift+F10 can reach it.
  • Reduced motion: it fades; the area doesn't dim.

Requirements

React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

Source

"use client";import * as React from "react";import { createPortal } from "react-dom";import { enterStyle, themeVars, useDismiss, useFloating, usePresence } from "./floating";import { MenuList, type MenuEntry } from "./menu-list";import { useInteractionReducedMotion } from "./ui-interaction-motion";/* ───────────────────────────────────────────────────────── * CONTEXT MENU: actions where you right-click * *   open     right-click, a long press on touch (500ms, the area *            dims a touch while it waits) or Shift+F10 / the menu *            key; the menu grows from the exact point *   again    right-clicking elsewhere while open moves it there *   place    it flips and shifts to stay inside the window * * The rows are the same as the dropdown menu's: a gliding * highlight, keys, typeahead, checkable rows. * ───────────────────────────────────────────────────────── */export type { MenuEntry } from "./menu-list";export interface ContextMenuProps {  /** The area that has the menu. */  children: React.ReactNode;  items: MenuEntry[];  label?: string;  onOpenChange?: (open: boolean) => void;  className?: string;}export function ContextMenu({ children, items, label = "Actions", onOpenChange, className = "" }: ContextMenuProps) {  const reduced = useInteractionReducedMotion();  const area = React.useRef<HTMLDivElement>(null);  const surface = React.useRef<HTMLDivElement>(null);  const [point, setPoint] = React.useState<{ x: number; y: number } | null>(null);  const [byKey, setByKey] = React.useState(false);  const [holding, setHolding] = React.useState(false);  const press = React.useRef<{ timer: number; x: number; y: number } | null>(null);  const open = point !== null;  const { mounted, shown } = usePresence(open, 180);  const place = useFloating({ open: mounted, point, floating: surface, side: "bottom", offset: 2 });  const [vars, setVars] = React.useState<React.CSSProperties>({});  const openAt = (x: number, y: number, keyboard = false) => {    setByKey(keyboard);    setPoint({ x, y });    onOpenChange?.(true);  };  const close = (refocus = false) => {    setPoint(null);    onOpenChange?.(false);    if (refocus) area.current?.focus();  };  useDismiss(open, (reason) => close(reason === "escape"), [surface]);  React.useLayoutEffect(() => {    if (mounted) setVars(themeVars(area.current));  }, [mounted]);  const cancelPress = () => {    if (press.current) window.clearTimeout(press.current.timer);    press.current = null;    setHolding(false);  };  return (    <>      <div        ref={area}        tabIndex={0}        aria-haspopup="menu"        onContextMenu={(e) => {          e.preventDefault();          cancelPress();          openAt(e.clientX, e.clientY);        }}        onKeyDown={(e) => {          if ((e.shiftKey && e.key === "F10") || e.key === "ContextMenu") {            e.preventDefault();            const r = e.currentTarget.getBoundingClientRect();            openAt(r.left + 16, r.top + 16, true);          }        }}        onPointerDown={(e) => {          if (e.pointerType === "mouse") return;          const { clientX: x, clientY: y } = e;          setHolding(true);          press.current = { x, y, timer: window.setTimeout(() => {            setHolding(false);            openAt(x, y);          }, 500) };        }}        onPointerMove={(e) => {          const p = press.current;          if (p && Math.hypot(e.clientX - p.x, e.clientY - p.y) > 8) cancelPress();        }}        onPointerUp={cancelPress}        onPointerCancel={cancelPress}        className={`outline-none transition-[scale,opacity] duration-500 ease-out focus-visible:ring-2 focus-visible:ring-ring/40 ${holding && !reduced ? "scale-[0.985] opacity-80" : ""} ${className}`}        style={{ WebkitTouchCallout: "none" }}      >        {children}      </div>      {mounted &&        createPortal(          <div            ref={surface}            className="fixed left-0 top-0 z-[55] min-w-[200px] overflow-hidden rounded-[14px] bg-popover text-popover-foreground shadow-[0_0_0_1px_var(--border)]"            style={{ ...vars, left: place.x, top: place.y, transformOrigin: "0 0", ...enterStyle(shown, place.side, reduced) }}          >            <MenuList key={`${point?.x}-${point?.y}`} items={items} label={label} autoFocus={byKey ? "first" : "none"} onClose={() => close(true)} />          </div>,          document.body,        )}    </>  );}export default ContextMenu;

Registry item: /r/context-menu.json