Skip to content
Components

Context Menu

Context Menu with thoughtful defaults, semantic styling, and editable source.

Open ↗

Make it yours.

Install the editable source and its dependencies into your React + Tailwind project.

Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/context-menu.json

Use it

Scroll horizontally for long lines
"use client";
import {useState} from 'react';
import {ContextMenu} from '@/components/jez-ui/ui/context-menu';

export default function Example(){
const [notice,setNotice]=useState('');
const actions=[{label:'Duplicate',onSelect:()=>setNotice('A copy is ready.')},{label:'Archive',onSelect:()=>setNotice('Moved to archive.')}];
return <><ContextMenu trigger={<div tabIndex={0} className="rounded-xl border border-dashed border-border p-10 text-sm">Right-click for actions</div>} items={actions}/><p role="status">{notice}</p></>;
}

Props & types

Scroll horizontally for long lines
{
  trigger: React.ReactNode;
  items: { label: string; onSelect: () => void; disabled?: boolean }[];
  className?: string;
}

Source

View 2 source files

registry/ui/context-menu.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
import { ContextMenu as Primitive } from "radix-ui";
export function ContextMenu({
  trigger,
  items,
  className,
}: {
  trigger: React.ReactNode;
  items: { label: string; onSelect: () => void; disabled?: boolean }[];
  className?: string;
}) {
  return (
    <Primitive.Root>
      <Primitive.Trigger asChild>{trigger}</Primitive.Trigger>
      <Primitive.Portal>
        <Primitive.Content
          className={cn(
            "z-50 min-w-44 rounded-xl border border-border bg-background p-1 text-foreground shadow-lg",
            className,
          )}
        >
          {items.map((i, n) => (
            <Primitive.Item
              key={n}
              disabled={i.disabled}
              onSelect={i.onSelect}
              className="cursor-pointer rounded-lg px-3 py-2 text-sm outline-none data-[highlighted]:bg-muted data-[disabled]:opacity-40"
            >
              {i.label}
            </Primitive.Item>
          ))}
        </Primitive.Content>
      </Primitive.Portal>
    </Primitive.Root>
  );
}

registry/ui/utils.ts

Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
  return twMerge(clsx(values));
}

Dependencies

radix-ui@1.6.7 · clsx@2.1.1 · tailwind-merge@3.5.0

Accessibility & behaviour

Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.

Read the accessibility and performance guide →