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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-context-menu-hooks

v0.4.0

Published

React Context Menu Hooks is an easy to use and feature rich scalable custom context menu solution for react.

Downloads

645

Readme

react-context-menu-hooks

React Context Menu Hooks is an easy to use and feature rich scalable custom context menu solution for react.

Check out the Demo

Features

  • All complexity such as handlers and registers are hidden and abstracted to provide a simple and easy to use API
  • Built using modern react hooks patterns, making it both performant and easy to use with modern react > 16.8
  • Dynamic directional menus - The context and expand menus will be dynamically positioned so they are always on the screen
  • Scalability - Works well in small apps with a single context menu and is able to scale up to very large apps with various context menu layouts
  • CSS based easy to control, style, update and themes
  • Dark mode theme

Think carefully when adding custom context menus to your app, context menu is not always accessible mobile users, so if you are targeting mobile users for your app, you should not have functionality that is only accessible in context menus.

Install

$ npm install react-context-menu-hooks

Usage

import the react-context-menu-hooks css in your app.

import '../node_modules/react-context-menu-hooks/src/ContextMenu.css';

Bridge

The bridge is where define your data structure and your default data.

It also does the job of binding your context menu to the trigger area/s. You will see further down that you will pass the returned value from createBridge to your context menu and trigger areas to create a link.

The first argument of create bridge function is where you define the default data for your context menu. The user will never see this data but it is used for the first render of the menu while it is hidden.

import { createBridge } from 'react-context-menu-hooks';

export const myContextMenuBridge = createBridge({
  changeColor: () => { /* do nothing */ },
  color: 'none',
});

Trigger Area

A trigger area is the area of the page that will trigger the opening of the context menu.

The TriggerArea component has two required props, bridge, and data. Provide the bridge prop with a Bridge instance created using the createBridge function as above. And the data prop is the data that will be provided to your context menu when the user right clicks in the Trigger Area.

You can pass any div element props to the TriggerElement component which will be rendered on the underlying div element. In the snippit below you can see we pass the common HTML prop className to the TriggerArea component.

import React, { useState } from 'react';
import { ContextMenuTriggerArea } from 'react-context-menu-hooks';

import { myContextMenuBridge } from './myContextMenuBridge';

const Shape = () => {
  const [color, setColor] = useState('blue');

  return (
    <ContextMenuTriggerArea
      bridge={myContextMenuBridge}
      className={`shape ${color}`}
      data={{
        color,
        changeColor: (newColor) => { setColor(newColor) },
      }}
    >
      <p>Right click to change the color!</p>
    </ContextMenuTriggerArea>
  );
};

ContextMenu

The ContextMenu component is the layout component for your ContextMenu. ContextMenu has property components; Option, Select, and Divider which can be used to easily layout your menu.

In the ContextMenu component the bridge prop is required which you provide with the Bridge instance created using the createBridge function as above.

The useContextMenu hook is used to get the context data provided from the trigger area and render the menu accordingly.

import React from 'react';
import { ContextMenu, useContextMenu } from 'react-context-menu-hooks';

import { myContextMenuBridge } from './myContextMenuBridge';

function MyContextMenu() {
  const { changeColor, color } = useContextMenu(myContextMenuBridge);

  const handleColorSelect = (action) => { changeColor(action); };

  return (
    <ContextMenu bridge={myContextMenuBridge} onSelect={handleColorSelect}>
      <ContextMenu.Option disabled={color === 'red'} select="red">Change to Red</ContextMenu.Option>
      <ContextMenu.Option disabled={color === 'blue'} select="blue">Change to Blue</ContextMenu.Option>
    </ContextMenu>
  );
}

TypeScript

The Library is fully typescript integrated. If you are using typescript in your project you can pass your data interface as the first type argument to createBridge. Your type will then be inferred in the useContextMenu hook and the TriggerArea component props.

import { createBridge } from 'react-context-menu-hooks';

export interface MyContextMenuTriggerData {
  changeColor: (color: string) => void;
  color: string;
}

export const myContextMenuBridge = createBridge<MyContextMenuTriggerData>({
  changeColor: () => { /* do nothing */ },
  color: 'none',
});

Issues

If you find any bugs please raise them in the github issues section. Also if you are in need of support / or the documentation is unclear please also raise a ticket there.

Contribute

Contributors welcome. Just Fork, Hack and Create a Pull Request! :)