React Context Menu

2.14.0 · maintenance · verified Sun Apr 19

react-contextmenu is a React component library that provides accessible context menus (right-click menus) for web applications. It offers a declarative API to define context menu triggers and their associated menus, complete with individual menu items and dividers. The current stable version is 2.14.0. While there have been recent minor releases, the project is explicitly "Looking for maintainers," indicating a slowed or potentially halted development cadence without new contributors. Key differentiators include built-in accessibility support and a straightforward component-based API for integrating context menus into React applications, differentiating it from lower-level DOM manipulation or older jQuery-based solutions. It supports a wide range of browser versions, including older IE 11.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up a basic context menu with a trigger element and several menu items, including a divider and a disabled item. It highlights the crucial unique ID pairing between ContextMenuTrigger and ContextMenu components.

import React from "react";
import ReactDOM from "react-dom";
import { ContextMenu, MenuItem, ContextMenuTrigger } from "react-contextmenu";

function handleClick(e, data) {
  console.log(`Clicked on item with foo: ${data.foo}`);
}

function MyAppComponent() {
  return (
    <div>
      <p>Right-click the box below to see a custom context menu.</p>
      {/* NOTICE: id must be unique between EVERY <ContextMenuTrigger> and <ContextMenu> pair */}
      {/* NOTICE: inside the pair, <ContextMenuTrigger> and <ContextMenu> must have the same id */}

      <ContextMenuTrigger id="my_unique_menu_id">
        <div style={{ border: '1px solid gray', padding: '20px', width: '200px', cursor: 'context-menu' }}>
          Right click me!
        </div>
      </ContextMenuTrigger>

      <ContextMenu id="my_unique_menu_id">
        <MenuItem data={{ foo: 'item1_data' }} onClick={handleClick}>
          Menu Item One
        </MenuItem>
        <MenuItem data={{ foo: 'item2_data' }} onClick={handleClick}>
          Menu Item Two
        </MenuItem>
        <MenuItem divider />
        <MenuItem data={{ foo: 'item3_data' }} onClick={handleClick}>
          Menu Item Three (with separator)
        </MenuItem>
        <MenuItem disabled>
          Disabled Item
        </MenuItem>
      </ContextMenu>

    </div>
  );
}

// Assuming a root element with id='root' in your HTML
ReactDOM.render(<MyAppComponent />, document.getElementById("root"));

view raw JSON →