Skip to content

shadcn.io is an independent project built on shadcn/ui

Free

Context Menu with Labels for React

A right-click menu with My Account and Support sections, then a destructive Log Out action.

The dashed area reads Right click here. My Account holds Profile and Settings. Support holds Help Center. Log Out is last and uses the destructive color.

Use labels when a longer menu should be scanned by section. Log Out can confirm in an Alert Dialog before the session ends. In React, Base UI does not sign anyone out.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Context Menu anatomy: trigger, item, sub trigger, separator, item indicator, content

Usage

<ContextMenu>
  <ContextMenuTrigger className="flex h-[200px] w-full max-w-lg items-center justify-center rounded-md border border-dashed text-sm">
    <span className="pointer-fine:inline-block hidden">Right click here</span>
    <span className="pointer-coarse:inline-block hidden">
      Long press here
    </span>
  </ContextMenuTrigger>
  <ContextMenuContent className="w-56">
    <ContextMenuGroup>
      <ContextMenuGroupLabel>My Account</ContextMenuGroupLabel>
      <ContextMenuItem>
        <UserIcon aria-hidden="true" />
        Profile
      </ContextMenuItem>
      <ContextMenuItem>
        <SettingsIcon aria-hidden="true" />
        Settings
      </ContextMenuItem>
    </ContextMenuGroup>
    <ContextMenuSeparator />
    <ContextMenuGroup>
      <ContextMenuGroupLabel>Support</ContextMenuGroupLabel>
      <ContextMenuItem>
        <CircleHelpIcon aria-hidden="true" />
        Help Center
      </ContextMenuItem>
    </ContextMenuGroup>
    <ContextMenuSeparator />
    <ContextMenuGroup>
      <ContextMenuItem variant="destructive">
        <LogOutIcon aria-hidden="true" />
{/* The rest of the file is under Installation. */}

FAQ

How are the section labels added?

Each label is a ContextMenuGroupLabel at the top of a ContextMenuGroup with the items it names. The label isn't selectable; screen readers announce it as the group's name.

Where do the separators go?

Between the sections and before Log Out, each a ContextMenuSeparator outside the groups.

How is the destructive item styled?

variant="destructive" on ContextMenuItem colors it with the destructive token, icon included. Keep it last, after a separator, and confirm with an Alert Dialog when it can't be undone.

How are the icons added?

A Lucide icon as the first child of each item, hidden from assistive tech since the label names the action. The menu styles size and space it.

How do I run an action when an item is chosen?

Pass onClick to each ContextMenuItem.

The menu closes after the action runs.

What opens the menu?

A right-click inside the dashed area, which reads Right click here on a mouse and Long press here on a touch screen. The area is ContextMenuTrigger; a long press on it opens the menu too. Wrap your own row, file or canvas in the trigger instead.

API Reference

Context Menu

Sets variant="destructive". The rest is on the Context Menu page.