Skip to content
caliper ui docs

Components

Context menu

A menu opened with a right-click or long press on a piece of content.

Free
pnpm dlx shadcn@latest add @caliper/context-menu

Board pack

Uploaded by Hannah Brooks · Monday

quarterly-report.pdfPDF · 3.1 MB

Right-click the file, or long-press on touch.

Installs components/ui/context-menu.tsxDepends on @radix-ui/react-context-menuMotion none in component source

When to use

Use it for

  • Actions tied to the thing under the pointer, like a file or a row.
  • As a secondary route to actions that are also in a visible menu.

Skip it when

  • It is the only route to an action; right-click is not discoverable.

Installation

terminal
pnpm dlx shadcn@latest add @caliper/context-menu

Adds its 3 files to components/ui/, each with its own stylesheet, and installs their packages. No account needed. The first part also adds Caliper UI’s tokens; import them once as in Installation.

Usage

This is the code behind the preview above. It is built from ContextMenu, ContextMenuTrigger, ContextMenuContent and ContextMenuItem.

example.tsx
import { FileText } from "lucide-react"
import { useState, type ReactNode } from "react"
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "@/components/ui/context-menu"
import { cn } from "@/components/ui/lib/cn"

function Panel({ title, context, children, className }: { title: ReactNode; context: ReactNode; children: ReactNode; className?: string }) {
  return (
    <section className={cn("grid w-full max-w-md gap-5 rounded-overlay border border-border bg-background p-6 shadow-resting", className)}>
      <div className="grid gap-1">
        <p className="font-medium">{title}</p>
        <p className="text-sm text-muted-foreground">{context}</p>
      </div>
      {children}
    </section>
  );
}

export function FileContext() {
  const [last, setLast] = useState("Right-click the file, or long-press on touch.");
  return (
    <Panel title="Board pack" context="Uploaded by Hannah Brooks · Monday" className="max-w-sm">
      <ContextMenu>
        <ContextMenuTrigger asChild>
          <div className="flex items-center gap-3 rounded-control border border-border px-4 py-3 text-sm">
            <FileText aria-hidden="true" className="size-5 text-muted-foreground" />
            <span className="grid min-w-0 flex-1">
              <span className="truncate font-medium">quarterly-report.pdf</span>
              <span className="text-muted-foreground">PDF · 3.1 MB</span>
            </span>
          </div>
        </ContextMenuTrigger>
        <ContextMenuContent>
          <ContextMenuItem onSelect={() => setLast("Opened quarterly-report.pdf.")}>Open</ContextMenuItem>
          <ContextMenuItem onSelect={() => setLast("Renaming quarterly-report.pdf…")}>Rename</ContextMenuItem>
          <ContextMenuItem onSelect={() => setLast("quarterly-report.pdf moved to Trash.")}>Delete</ContextMenuItem>
        </ContextMenuContent>
      </ContextMenu>
      <p role="status" className="text-sm text-muted-foreground">{last}</p>
    </Panel>
  );
}

API reference

ContextMenu

component

No props of its own; it accepts the native props of its root element.

ContextMenuTrigger

component

No props of its own; it accepts the native props of its root element.

ContextMenuContent

component

No props of its own; it accepts the native props of its root element.

ContextMenuItem

component

No props of its own; it accepts the native props of its root element.

Accessibility

It is built on native elements, so roles and focus come from the browser.

Keyboard and focus handling comes from Radix context-menu: arrow keys move through items, Enter and Space select, and Escape closes and returns focus.

Motion

It declares no animation in its component source. Any transition comes from its stylesheet, which uses the duration tokens that collapse to instant under prefers-reduced-motion.

All Actions examples