npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-use-context-menu

v0.1.4

Published

React hook for managing custom context menus!

Readme

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install --save react-use-context-menu

or

yarn add react-use-context-menu

Features

  • Supports Keyboard navigation
  • Fully customizable
  • Fully accessible
  • No dependencies
  • Only 1kb gzipped
  • RTL support
  • supports touch screen with hold to display!
  • Detects the size of the menu and always places it inside the viewport when clicking near the borders.
  • ESM and CommonJS dist

Usage

Basic usage:

import React from 'react'
import ReactDOM from 'react-dom'
import useContextMenu from 'react-use-context-menu'

function App() {
  const [
    bindMenu,
    bindMenuItems,
    useContextTrigger
  ] = useContextMenu();
  const [bindTrigger] = useContextTrigger(});
  return (
    <div className="App">
      <h1>useContextMenu</h1>
      <h2 {...bindTrigger}>Right click me to see some magic happen!</h2>
      <nav {...bindMenu}>
        <div {...bindMenuItems}>First action</div>
        <div {...bindMenuItems}>Second action</div>
        <hr/>
        <div {...bindMenuItems}>Last action</div>
      </nav>
    </div>
  );
}

API

const [
    bindMenu,
    bindMenuItems,
    useContextTrigger,
    {
        data, // the data collected by the collect function of useContextTrigger
        coords, // a 2d Array [x, y] returning the coords of the point where the right click was triggered
        setCoords, // manually set the coords of the menu
        isVisible, // Boolean indicating if the menu is visible or not
        setVisible, // manually set the context menu visible with setVisible(true) or hidden with setVisible(false)
    }
  ] = useContextMenu({
      rtl: false, // Optional, set to true to enable RightToLeft menu,
      handleElementSelect: el => el.focus() // Handles the element being selected with keyboard. Optional, focus is the default behaviour
  });

bindMenu

The first element of the result array is an Object used to bind the context menu element. It has 4 properties:

{
    style, // Mandatory
    ref, // Mandatory
    role: "menu", // Optional, if you don't care about a11y 😠
    tabIndex: -1 // same as above, also this is needed for keayboard navigation
}

Keep in mind that you need to spread this over an actual element (like a div or a nav), if you spread it to a Component, then the Component should take care of passing the props to the underlying wrapper element. If you as well need to access the ref of the element you can simply do:

<div {...bindMenu} ref={(el) => {
    bindMenu.ref(el);
    // you logic here
}}

bindMenuItems

The second element is an Object with 3 props:

{
    ref, // Mandatory
    role: "menuitem", // Optional, if you don't care about a11y 😠
    tabIndex: -1 // same as above, also this is needed for keayboard navigation
}

used to bind the menu items. You can spread it or assign the props one by one. Same as above applies.

useContextTrigger

The third element of the result array is another hook which you should use to bind the trigger component(s), which is the component which will trigger the context menu when right clicked. It accepts an optional config object for fine tuning and advanced usage

const [bindTrigger] = useContextTrigger({
// those are the default values 
  disable: false, // disable the trigger
  holdToDisplay: 1000, // time in ms after which the context menu will appear when holding the touch
  posX: 0, // distance in pixel from which the context menu will appear related to the right click X coord
  posY: 0, // distance in pixel from which the context menu will appear related to the right click y coord
  mouseButton: MOUSE_BUTTON.RIGHT, // set to 0 for triggering the context menu with the left click
  disableIfShiftIsPressed: false, // Self explanatory 😺
  collect: () => 'useContextMenu is cool!' // collect data to be passed to the context menu, see the example to see this in action
});

Examples:

You can check the example folder or this codesandbox for more advanced examples.

Comparison with other similar packages

This is the first package implemented using hooks so far for what I've seen! Other packages are using components which requires lot of configuration and are way bigger in size. Also this is the smallest and most configurable 💃

Gratitude

This package have been deeply inspired by https://github.com/vkbansal/react-contextmenu, thanks a lot to @vkbansal! 🙏

LICENSE

MIT