Component guideEditable source

Context Menu

Contextual commands opened from a target region.

Original component docs
On this page4 sectionsBack to top

Use Context Menu for commands associated with an object or region. A context menu must not be the only way to discover an essential action. Offer an ordinary button or menu for touch users and people unfamiliar with right-click behavior.

Live preview
Loading preview...

#Installation

Complete the base setup. For manual installation:

npx shadcn@latest add https://neobrutal-ui.andongmin.com/r/context-menu.json

#Usage

"use client";
import { useRef, useState } from "react";
import {
  ContextMenu,
  ContextMenuTrigger,
  ContextMenuContent,
  ContextMenuItem,
} from "@/components/ui/context-menu";

export default function FileCommands() {
  const triggerRef = useRef<HTMLDivElement>(null);
  const [message, setMessage] = useState("No action selected.");
  return (
    <div className="grid gap-3">
      <ContextMenu>
        <ContextMenuTrigger
          ref={triggerRef}
          className="block rounded-base border-2 border-dashed border-border p-8"
          tabIndex={0}
        >
          Right-click this file or press Shift+F10.
        </ContextMenuTrigger>
        <ContextMenuContent finalFocus={triggerRef}>
          <ContextMenuItem onClick={() => setMessage("Duplicate selected in this preview.")}>
            Duplicate
          </ContextMenuItem>
          <ContextMenuItem disabled>Move to archive</ContextMenuItem>
        </ContextMenuContent>
      </ContextMenu>
      <p role="status">{message}</p>
    </div>
  );
}

#API reference

Prop / partPurposeDefault
ContextMenuRoot state and callbacksClosed
ContextMenuTriggerTarget region and render compositionPrimitive trigger
ContextMenuContentPositioned menu surfacePrimitive-managed
ContextMenuContent.finalFocusElement ref, function, or boolean controlling focus after closeTrigger or previously focused element
ContextMenuItem.onClick / disabledAction and availability— / false
ContextMenuCheckboxItem / ContextMenuRadioGroupStateful choicesControlled or uncontrolled
ContextMenuSubNested commands—
ContextMenuContent.side / alignPreferred popup placement and alignmentright / start
ContextMenuContent.alignOffset / sideOffsetAlignment adjustment and trigger gap, in pixels4 / 0
ContextMenuContent.arrowPadding / collisionPaddingArrow and collision-boundary spacing, in pixels0
ContextMenuItem.variantItem appearance: default or destructivedefault
ContextMenuCheckboxItem.closeOnClick / ContextMenuRadioItem.closeOnClickClose after selecting a stateful itemtrue

#Accessibility

Make the relevant target keyboard reachable when it needs keyboard context-menu access. Test Shift+F10, arrow navigation, and Escape. Keep another discoverable route to essential commands. Avoid unexpectedly replacing the browser's context menu across the entire page.

Menu content is bounded by Base UI's available height. Long menus scroll so pointer users and keyboard users can reach every item.

Right-clicking does not focus the target in every browser. When closing the menu should return focus to its target, pass that target's ref to ContextMenuContent.finalFocus, as in the example. Base UI handles the focus return.