Skip to main content

Context Menu

A zero-dependency right-click context menu anchored to cursor coordinates, with an origin-aware reveal, spring-gliding item highlight, stroke-draw checkboxes, and keyboard navigation.

Right-click or long-press heregrid:on · guides:on · @linh
action=ready

Customize

Duration (ms)170
Motion blur (px)4
Viewport padding (px)8
Speed1

Installation

npx shadcn@latest add @kinetic/context-menu

Usage

Wrap a region in ContextMenuTrigger. Right-clicking inside it opens ContextMenuContent at the pointer position. The ContextMenu key or Shift+F10 opens it from the keyboard, centered on the focused trigger, with the first item focused.

import {
  ContextMenu,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuSeparator,
  ContextMenuShortcut,
  ContextMenuTrigger,
} from "@/components/kinetic/ui/context-menu";

export function CanvasArea() {
  return (
    <ContextMenu>
      <ContextMenuTrigger className="grid h-40 place-items-center rounded-lg border">
        Right-click here
      </ContextMenuTrigger>
      <ContextMenuContent>
        <ContextMenuItem>
          Copy <ContextMenuShortcut>Ctrl+C</ContextMenuShortcut>
        </ContextMenuItem>
        <ContextMenuSeparator />
        <ContextMenuItem variant="destructive">Delete</ContextMenuItem>
      </ContextMenuContent>
    </ContextMenu>
  );
}

Trigger options

ContextMenuTrigger accepts disabled, asChild to render your own element, and longPressDelay to set how long a touch must be held before the menu opens (default 500 ms, clamped between 200 and 1500, and 0 disables long press).

<ContextMenuTrigger asChild longPressDelay={600}>
  <section>...</section>
</ContextMenuTrigger>

Positioning

The menu is anchored to the cursor coordinates and its reveal grows from the anchored corner. Use collisionPadding on ContextMenu or ContextMenuContent to keep the menu away from the viewport edges.

Portal

ContextMenuContent portals to document.body by default. Wrap it in ContextMenuPortal with a container prop when mounting inside a specific DOM node without creating a duplicate portal.

import {
  ContextMenu,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuPortal,
  ContextMenuTrigger,
} from "@/components/kinetic/ui/context-menu";

<ContextMenu>
  <ContextMenuTrigger>Right-click</ContextMenuTrigger>
  <ContextMenuPortal container={portalContainer}>
    <ContextMenuContent>
      <ContextMenuItem>Inspect</ContextMenuItem>
    </ContextMenuContent>
  </ContextMenuPortal>
</ContextMenu>

Items

ContextMenuItem supports variant (default or destructive), inset, disabled, closeOnSelect and onSelect. Use ContextMenuLabel, ContextMenuGroup and ContextMenuSeparator to organize them.

<ContextMenuItem inset onSelect={() => rename()}>
  Rename
</ContextMenuItem>

Checkbox and radio items

ContextMenuCheckboxItem takes checked with onCheckedChange, or defaultChecked. Wrap ContextMenuRadioItem elements, each with a value, in a ContextMenuRadioGroup.

<ContextMenuCheckboxItem checked={showHidden} onCheckedChange={setShowHidden}>
  Show hidden files
</ContextMenuCheckboxItem>

<ContextMenuRadioGroup value={sort} onValueChange={setSort}>
  <ContextMenuRadioItem value="name">Name</ContextMenuRadioItem>
  <ContextMenuRadioItem value="date">Date</ContextMenuRadioItem>
</ContextMenuRadioGroup>

Nest ContextMenuSub, ContextMenuSubTrigger and ContextMenuSubContent inside the content.

<ContextMenuSub>
  <ContextMenuSubTrigger>Share</ContextMenuSubTrigger>
  <ContextMenuSubContent>
    <ContextMenuItem>Copy link</ContextMenuItem>
  </ContextMenuSubContent>
</ContextMenuSub>

Motion preferences

Tune the animation with speed, stiffness, damping and blurPx on ContextMenu. Timing follows the current Kinetic tempo, and motion is simplified when reduced motion is enabled.

API Reference

ContextMenu

Zero-dependency context menu root managing pointer coordinates, open state, and spring highlight physics.

PropTypeDefaultDescription
durationMsnumber170Base reveal duration in milliseconds.
speednumber1Playback speed multiplier for reveal and highlight spring.
stiffness / dampingnumber420 / 32Spring parameters controlling highlight tracking.
collisionPaddingnumber8Viewport edge margin in pixels used for auto corner flipping.

ContextMenuTrigger / ContextMenuContent

Right-click / Shift+F10 target area and portaled floating menu anchored to the cursor corner.

PropTypeDefaultDescription
disabled (ContextMenuTrigger)booleanfalsePrevents opening the context menu on secondary click.
asChild (ContextMenuTrigger)booleanfalseMerges context-menu listeners onto the child element.

ContextMenuPortal

Optional explicit portal wrapper controlling where ContextMenuContent mounts.

PropTypeDefaultDescription
containerElement | nulldocument.bodyDOM target element for portaled menu content.

Notes

Always wrap ContextMenuItem inside ContextMenuGroup. Provide a visible button or DropdownMenu fallback for essential actions on touch devices.