Context Menu

The React Context Menu component displays a menu when you right-click an element or long-press on a touch device. It lets users access actions related to that specific item or area.

It helps provide quick actions without adding buttons to the UI. For example, you can right-click on a file to see options like rename, delete, or copy.

This component is built on top of Base UI's Context Menu and styled with Tailwind CSS. It supports keyboard navigation and accessibility, and it works well across different screen sizes.

Right click here

Installation

Install the component using the Tailgrids CLI.

npx @tailgrids/cli add context-menu

Anatomy

Import the component parts and combine them to create a context-aware menu.

import {
  ContextMenu,
  ContextMenuCheckboxItem,
  ContextMenuContent,
  ContextMenuGroup,
  ContextMenuItem,
  ContextMenuLabel,
  ContextMenuPortal,
  ContextMenuRadioGroup,
  ContextMenuRadioItem,
  ContextMenuSeparator,
  ContextMenuShortcut,
  ContextMenuSub,
  ContextMenuSubContent,
  ContextMenuSubTrigger,
  ContextMenuTrigger
} from "@/components/context-menu";

export const ContextMenuExample = () => (
  <ContextMenu>
    <ContextMenuTrigger>Right click me</ContextMenuTrigger>
    <ContextMenuContent>
      <ContextMenuItem>Action</ContextMenuItem>
      <ContextMenuSeparator />
      <ContextMenuSub>
        <ContextMenuSubTrigger>Submenu</ContextMenuSubTrigger>
        <ContextMenuSubContent>
          <ContextMenuItem>Sub Action</ContextMenuItem>
        </ContextMenuSubContent>
      </ContextMenuSub>
    </ContextMenuContent>
  </ContextMenu>
);

Examples

File Manager

A common use case for context menus is providing file-specific actions in a file explorer or dashboard.

Right click on file

Text Selection

Enhance text editors or content areas with context menus for formatting and transformation tools.

Right click to format text

Settings & Preferences

Use ContextMenuCheckboxItem and ContextMenuRadioGroup to allow users to toggle settings or select preferences directly from the context menu.

Right click for settings

API Reference

ContextMenu

The root component that provides the context for the menu. It doesn't render any HTML element.

PropTypeDefaultDescription
defaultOpenbooleanfalseWhether the menu is initially open.
openboolean-Whether the menu is currently open.
onOpenChange(open: boolean) => void-Event handler called when the menu is opened or closed.
disabledbooleanfalseWhether the component should ignore user interaction.
modalbooleantrueWhether the menu should be rendered as a modal, preventing interaction with the rest of the page.

ContextMenuTrigger

The element that triggers the context menu on right-click or long-press.

PropTypeDefaultDescription
disabledbooleanfalseWhether the trigger should ignore user interaction.
renderelement-Replace the default div with a custom element.
Data AttributeValuesDescription
data-popup-open-Present when the menu is open.
data-pressed-Present when the trigger is being pressed.

ContextMenuPortal

A portal element that moves the popup to a different part of the DOM, usually body.

PropTypeDefaultDescription
containerelementbodyA parent element to render the portal element into.
keepMountedbooleanfalseWhether to keep the portal mounted in the DOM while the popup is hidden.

ContextMenuContent

The container for the context menu items.

PropTypeDefaultDescription
align'start' | 'center' | 'end''start'The alignment of the popup relative to the anchor.
alignOffsetnumber4Additional offset along the alignment axis in pixels.
side'top' | 'bottom' | 'left' | 'right''right'The side of the anchor the popup should be positioned on.
sideOffsetnumber0Distance between the anchor and the popup in pixels.
collisionBoundaryelement | element[]viewportThe boundary that the popup should be constrained to.
collisionPaddingnumber | object0The distance from the boundary edge to keep the popup within.
hideWhenDetachedbooleanfalseWhether to hide the popup when its anchor is off-screen.
Data AttributeValuesDescription
data-state"open" | "closed"The current state of the popup.
data-side"top" | "bottom" | "left" | "right"The side the popup is currently positioned on.
data-align"start" | "center" | "end"The current alignment of the popup.

ContextMenuItem

An individual interactive item in the menu.

PropTypeDefaultDescription
variant'default' | 'destructive''default'The visual style of the item.
insetbooleanfalseIf true, adds left padding to align with items that have icons/indicators.
disabledbooleanfalseWhether the item should ignore user interaction.
onClick(event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void-Event handler called when the item is clicked.
closeOnClickbooleantrueWhether to close the menu when the item is clicked.
Data AttributeValuesDescription
data-highlighted-Present when the item is focused or hovered.
data-disabled-Present when the item is disabled.

ContextMenuCheckboxItem

An item that can be toggled between a checked and unchecked state.

PropTypeDefaultDescription
checkedboolean | 'indeterminate'-The checked state of the item.
onCheckedChange(checked: boolean) => void-Event handler called when the checked state changes.
onClick(event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => boolean-Event handler called when the checked state changes.
insetbooleanfalseIf true, adds left padding to align with items that have icons/indicators.
disabledbooleanfalseWhether the item should ignore user interaction.
closeOnClickbooleanfalseWhether to close the menu when the item is clicked.
Data AttributeValuesDescription
data-checked-Present when the item is checked.
data-unchecked-Present when the item is unchecked.
data-highlighted-Present when the item is focused or hovered.
data-disabled-Present when the item is disabled.

ContextMenuRadioGroup

Used to group multiple ContextMenuRadioItem components.

PropTypeDefaultDescription
valueany-The currently selected value in the group.
onValueChange(value: any) => void-Event handler called when the selection changes.
disabledbooleanfalseWhether the entire group should be disabled.

ContextMenuRadioItem

An item within a radio group that can be selected.

PropTypeDefaultDescription
valueany-The unique value for this radio item.
insetbooleanfalseAdds padding for consistent alignment.
disabledbooleanfalseWhether the item should be disabled.
closeOnClickbooleanfalseWhether to close on click.
Data AttributeValuesDescription
data-checked-Present when the item is selected.
data-unchecked-Present when the item is not selected.
data-highlighted-Present when the item is focused or hovered.
data-disabled-Present when the item is disabled.

ContextMenuSub, Trigger & Content

Used to create nested submenus.

ContextMenuSub

Context menu sub component.

PropTypeDefaultDescription
defaultOpenbooleanfalseWhether the submenu is open by default.
openboolean-Whether the submenu is currently open.
onOpenChange(open: boolean) => void-Event handler called when the submenu state changes.
disabledbooleanfalseWhether the submenu should be disabled.
highlightItemOnHoverbooleantrueWhether to highlight the item on hover.

ContextMenuSubTrigger

Context menu sub trigger component.

PropTypeDefaultDescription
insetbooleanfalseAdds padding for consistent alignment.
disabledbooleanfalseWhether the trigger should be disabled.
openOnHoverbooleantrueWhether the submenu should open when hovered.
delaynumber100Delay before opening on hover (in ms).
Data AttributeValuesDescription
data-popup-open-Present when the submenu is open.
data-highlighted-Present when the trigger is focused or hovered.
data-disabled-Present when the trigger is disabled.

ContextMenuSubContent

Context menu sub content component.

Same props as ContextMenuContent.

ContextMenuGroup

Groups related menu items.

PropTypeDescription
childrenReactNodeThe group content.

ContextMenuLabel

A non-interactive label used to group items.

PropTypeDefaultDescription
insetbooleanfalseAdds padding for consistent alignment.
childrenReactNode-The label text or content.

ContextMenuSeparator

A decorative line used to separate groups of items.

PropTypeDefaultDescription
orientation"horizontal" | "vertical""horizontal"The orientation of the separator.
classNamestring-Additional CSS classes.
renderelement-Custom element to render.

ContextMenuShortcut

A decorative element used to display keyboard shortcuts.

PropTypeDescription
classNamestringAdditional CSS classes for styling.
childrenReactNodeThe shortcut content (e.g., ⌘C).

Accessibility

  • Semantic structure: The component uses proper menu roles like menu and menuitem, so screen readers can understand the structure.
  • Keyboard support: Users can move between items using Arrow keys, open items with Enter or Space, and close the menu with Escape.
  • Focus management: Focus moves correctly between menu items and submenus, and returns to the trigger when the menu is closed.
  • Trigger behavior: The menu opens on right-click or long-press, and also works with keyboard interaction.
  • Screen reader support: Active and selected items are announced clearly, including nested menus and grouped items.
  • Disabled items: Disabled items are skipped during keyboard navigation and are announced properly.