Back to Base UIYou are on Context Menu.

Context Menu

A menu anchored to the pointer with cascading items and menu handoff.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks

Libraries

Base UI

Motion

Spring + short tween

Reduced motion

Fade in place
context-menu.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/context-menu.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/context-menu.json
npmthe default on every machine

First component? Set up your project first

Frameworks

The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.

5 runtimes
context-menu.tsx
"use client";

import * as React from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";

export type ContextMenuItem = {
  label: string;
  shortcut?: string;
  onSelect?: () => void;
  separator?: boolean;
};

export type ContextMenuProps = {
  items: ContextMenuItem[];
  children?: React.ReactNode;
};

export function ContextMenu({ items, children = "Right click here" }: ContextMenuProps) {
  const [position, setPosition] = React.useState<{ x: number; y: number } | null>(null);
  const reduce = useReducedMotion();

  React.useEffect(() => {
    if (!position) return;
    const close = () => setPosition(null);
    const onKey = (event: KeyboardEvent) => {
      if (event.key === "Escape") close();
    };
    document.addEventListener("pointerdown", close);
    document.addEventListener("keydown", onKey);
    return () => {
      document.removeEventListener("pointerdown", close);
      document.removeEventListener("keydown", onKey);
    };
  }, [position]);

  return (
    <>
      <div
        onContextMenu={(event) => {
          event.preventDefault();
          setPosition({ x: event.clientX, y: event.clientY });
        }}
        className="flex h-24 w-64 items-center justify-center rounded-xl border border-dashed text-[13px] text-muted-foreground"
      >
        {children}
      </div>
      <AnimatePresence>
        {position && (
          <motion.div
            role="menu"
            initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.96 }}
            animate={reduce ? { opacity: 1 } : { opacity: 1, scale: 1 }}
            exit={{ opacity: 0, transition: { duration: 0.15, ease: [0, 0, 0.58, 1] } }}
            transition={{ duration: 0.22, ease: [0.215, 0.61, 0.355, 1] }}
            style={{ left: position.x, top: position.y }}
            className="fixed z-50 min-w-[190px] origin-top-left rounded-xl border bg-background p-1 shadow-xl"
            onPointerDown={(event) => event.stopPropagation()}
          >
            {items.map((item, index) =>
              item.separator ? (
                <div key={`sep-${index}`} role="separator" className="my-1 h-px bg-border" />
              ) : (
                <motion.button
                  key={item.label}
                  type="button"
                  role="menuitem"
                  initial={reduce ? false : { opacity: 0, y: -4, filter: "blur(2px)" }}
                  animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
                  transition={{ duration: 0.2, ease: [0.215, 0.61, 0.355, 1], delay: index * 0.03 }}
                  onClick={() => {
                    item.onSelect?.();
                    setPosition(null);
                  }}
                  className="flex w-full items-center justify-between gap-6 rounded-md px-2 py-1.5 text-left text-[13px] hover:bg-accent"
                >
                  <span>{item.label}</span>
                  {item.shortcut && <span className="font-mono text-[13px] text-muted-foreground">{item.shortcut}</span>}
                </motion.button>
              ),
            )}
          </motion.div>
        )}
      </AnimatePresence>
    </>
  );
}

Install

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/context-menu.json
npmthe default on every machine

Dependencies

motion lucide-react

The registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.

02

Usage

Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.

context-menu.tsx
import { ContextMenu } from "@/components/prequelui/context-menu";

export default function Demo() {
  return (
    <ContextMenu>
      {/* compose the parts exactly like the underlying primitive */}
    </ContextMenu>
  );
}
import { ContextMenu } from "@/components/prequelui/context-menu";

export function Variant() {
  return (
    <ContextMenu tempo={1.2}>
      …
    </ContextMenu>
  );
}
03

Animation

This primitive keeps the API and semantics of the underlying component and adds motion at the surface edges. Entrances are slower and more settled than exits, so closing never feels like waiting.

  • Springs for position, scale and size; tweens for colour and opacity.
  • The exit is shorter than the entrance, and keeps the current position as its starting point.
  • All measurements are taken again when content or font metrics change.
  • With reduced motion, the surface fades in place and never travels.
spring 300 / 30exit 150 msblur 4 pxheight 450 ms
04

API Reference

Every part takes exactly the props of the Context Menu primitive. The animated surface adds only motion props, so the accessibility tree and keyboard behaviour stay the same.

Context Menu

PropTypeDefault
childrenReactNode—
disabledbooleanfalse
transitionTransitionspring.press
classNamestring—

Next component

Dialog

A modal that tips into view from its edge while the page blurs behind it.