Git Product home page Git Product logo

rctx-contextmenu's Introduction

npm

Note: rctx-contextmenu Supports React 16.8 and above versions because this plugin solely uses React Hooks.

rctx-contextmenu

Context menu plugin for React. Live Demo | Example Code

RCTX Contextmenu

The problem 😯

There are lot of times when you want to customize the right click menu or Context Menu. May be you want to add more customized options or even sub options with same look and feel how a native context menu gives.

The solution 😄

rctx-contextmenu gives that flexibility to customize Context Menu. You can add options, sub-options with exactly the look and feel how you get when you use the native Context Menu.

Key features

  • Append to anywhere you want
  • Customize styling like however you want
  • In-built animation
  • Extensive list of APIs
  • Dynamically add/remove menus

Browser Support

  • IE 11 and Edge >= 12
  • FireFox >= 38
  • Chrome >= 47

Table of contents

Installation

npm install --save rctx-contextmenu

Sample Usage

import React from 'react';
import { ContextMenuTrigger, ContextMenu, ContextMenuItem } from 'rctx-contextmenu';

function App() {
  return (
    <div className="app">
      <ContextMenuTrigger
        id="my-context-menu-1"
      >
        <div className="box">
          Right Click On Me
        </div>
      </ContextMenuTrigger>

      <ContextMenu id="my-context-menu-1">
        <ContextMenuItem>Menu Item 1</ContextMenuItem>
        <ContextMenuItem>Menu Item 2</ContextMenuItem>
        <ContextMenuItem>Menu Item 3</ContextMenuItem>
        <ContextMenuItem>Menu Item 4</ContextMenuItem>
      </ContextMenu>
    </div>
  );
}

export default App;

Full example usage

<ContextMenu
  id="my-contextmenu"
  appendTo="body"
  animation="zoom"
  hideOnLeave={false}
  preventHideOnScroll={true}
  preventHideOnResize={true}
  attributes={{
    'aria-label': 'Some text',
    'aria-labelledby': 'Some text'
  }}
  className="my-context-menu"
  onMouseLeave={() => console.log('Mouse left')}
  onShow={() => console.log('I am visible!')}
  onHide={() => console.log('I am hidden!')}
/>
<ContextMenuItem
  disabled={true}
  preventClose={false}
  attributes={{
    'aria-label': 'Some text',
    'aria-labelledby': 'Some text'
  }}
  className="my-context-menu-item"
  onClick={() => console.log("I'm clicked!")}
/>
<ContextMenuTrigger
  id="my-contextmenu"
  disable={false}
  disableWhileShiftPressed={true}
  attributes={{
    'aria-label': 'Some text',
    'aria-labelledby': 'Some text'
  }}
  className="my-context-menu-trigger"
/>
<Submenu
  title="My submenu"
  attributes={{
    'aria-label': 'Some text',
    'aria-labelledby': 'Some text'
  }}
  className="my-context-menu-submenu"
/>

Live Demo

Live demo is available here.

Example Code

Example code is available here.

APIs

<ContextMenu />

Name Type Options Description
id String Should be a string. `ContextMenu` and `ContextMenuTrigger` id should match.
appendTo String Should be a string which contains element selector like body / #id / .class . Default: Enclosed element Append context menu inside required element.
animation String Enums: fade | zoom | pop | toTopLeft | rightToLeft Make the contextmenu visible in animated way.
hideOnLeave Boolean true / false. Default: false Hide contextmenu on blur or mouseOut.
attributes Object - Add additional attributes to the element.
className String - Additional classes.
preventHideOnResize Boolean true / false. Default: false Prevent hiding contextmenu from hiding on window resize.
preventHideOnScroll Boolean true / false. Default: false Prevent hiding contextmenu from hiding on scroll.
onShow Function () => {} Trigger this event when Context Menu is visible.
onHide Function () => {} Trigger this event when Context Menu is hidden.
onMouseLeave Function () => {} Invoke onMouseLeave on blur or mouseOut.

<ContextMenuItem />

Name Type Options Description
disabled Boolean true / False . Default False Disable menu item.
preventClose Boolean true / False . Default False Context menu is closed as soon as an item is clicked.
disableWhileShiftPressed Boolean true / false. Default: false Open browser default contextmenu if shift pressed while right click on trigger element.
attributes Object - Add additional attributes to the element.
className String - Additional classes.
onClick Function () => {} Fire onClick event.

<ContextMenuTrigger />

Name Type Options Description
id String Should be a string. `ContextMenu` and `ContextMenuTrigger` id should match.
attributes Object - Add additional attributes to the element.
disable true / false. Default: false false Disable contextmenu and open browser default contextmenu.
className String - Additional classes.

<Submenu />

Name Type Options Description
title String Should be a string. Title for the Submenu.
attributes Object - Add additional attributes to the element.
className String - Additional classes.

Styling

Styling is totally up to you. You can use CSS or even SCSS to customize the styling part. Below are the classes you can consider for styling

  • menu-trigger (Right click area wrapper)
  • contextmenu (The Context Menu)
  • contextmenu__item (Each Context Menu item)
  • contextmenu__item--disabled (Context Menu item)
  • submenu (Context Submenu)
  • submenu__item (Context Submenu item)

Contributions 🙏

Any kind of contribution would be much appreciated. It could be small change in the doc or even any kind of advice. You can create issues if you feel any feature which is missing which solves bigger problems.

Develop locally

First clone the repo

git clone https://github.com/reachtokish/rctx-contextmenu.git

Navigate inside rctx-contextmenu folder

cd rctx-contextmenu

Install dependencies

npm install

Then start server to run it locally

npm start

Now app should run on localhost:1234

Design inspiration Khalid Hasan Zibon / Dribble

License

MIT License

rctx-contextmenu's People

Contributors

node-kishore avatar reachtokish avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rctx-contextmenu's Issues

TypeScript support?

  • rctx-contextmenu version:
  • node version:
  • npm (or yarn) version:

Relevant code or config

What you did:

What happened:

Reproduction repository:

Problem description:

Suggested solution:

ContextMenuItem OnClick Doesn't Work For Me

  • rctx-contextmenu version:1.3.5
  • node version:v16.13.1
  • npm (or yarn) version:8.3.0

Relevant code or config

            id="treeContextMenu"
          >
          <div
            id="projectPageContainer"
            key="projectPageContainer"  
          >
          Page One
          </div>
          </ContextMenuTrigger>
<ContextMenu id="treeContextMenu" >
        <ContextMenuItem onClick={() => console.log("I'm clicked!")} >Set As HomePage</ContextMenuItem>
        <ContextMenuItem onClick={() => handlePageControlContextMenu('changeName')}>Change Page Name</ContextMenuItem>
        <ContextMenuItem onClick={() => handlePageControlContextMenu('moveToTop')}>Move To Top</ContextMenuItem>
        <ContextMenuItem onClick={() => handlePageControlContextMenu('moveToBottom')}>Move To Bottom</ContextMenuItem>
      </ContextMenu>```

Hello,

I like the UI. The issue I met is nothing happen when I left click the context menu item. 
As you see, even I click the first one console.log sth, it didn't show anything.

For your reference, I paste my other plugins version.

"@ant-design/charts": "^1.3.4",
    "@ant-design/icons": "^4.7.0",
    "@ant-design/pro-card": "^1.18.24",
    "@ant-design/pro-descriptions": "^1.10.34",
    "@ant-design/pro-form": "^1.52.12",
    "@ant-design/pro-layout": "^6.32.5",
    "@ant-design/pro-table": "^2.62.6",
    "@material-ui/core": "^4.12.3",
    "@material-ui/icons": "^4.11.2",
    "@types/react-color": "^3.0.6",
    "@umijs/plugin-openapi": "^1.3.2",
    "@umijs/route-utils": "^2.0.5",
    "antd": "^4.18.4",
    "async-mutex": "^0.3.2",
    "classnames": "^2.3.1",
    "echarts": "^5.3.0",
    "fbemitter": "^3.0.0",
    "keymaster": "^1.6.2",
    "leaflet": "^1.7.1",
    "leaflet.markercluster": "^1.5.3",
    "lodash": "^4.17.21",
    "long": "^5.2.0",
    "moment": "^2.29.1",
    "omit.js": "^2.0.2",
    "protobufjs": "^6.11.2",
    "rctx-contextmenu": "^1.3.5",
    "react": "^17.0.2",
    "react-color": "^2.19.3",
    "react-contexify": "^5.0.0",
    "react-dev-inspector": "^1.7.1",
    "react-dnd": "^14.0.5",
    "react-dnd-html5-backend": "^14.1.0",
    "react-dom": "^17.0.2",
    "react-grid-layout": "^1.3.1",
    "react-loading": "^2.0.3",
    "redux-logger": "^3.0.6",
    "redux-undo": "^1.0.1",
    "umi": "^3.5.20",
    "umi-request": "^1.4.0",
    "websocket": "^1.0.34"

Uncaught ReferenceError: arguments is not defined

  • rctx-contextmenu version:1.3.5
  • node version:v16.13.1
  • npm (or yarn) version:8.1.0

Relevant code or config

import { ContextMenuTrigger, ContextMenu, ContextMenuItem } from 'rctx-contextmenu';

What happened:

I get an error when I use create-react-app@5

image

module not linking correctly

noticed if you highlight from import rctx-contextmenu it would say module "rctx-contextmenu" and not the path to node_modules

Set menu icon

  • rctx-contextmenu version: 1.3.5
  • node version: 16.13.2
  • npm (or yarn) version: 8.1.2

Relevant code or config

<Submenu title="排序" icon={<EditOutlined />}>
  <ContextMenuItem></ContextMenuItem>
  <ContextMenuItem>升序</ContextMenuItem>
  <ContextMenuItem>降序</ContextMenuItem>
</Submenu>

What you did:
I want to add Icon to Submenu。

What happened:
Invalid

Reproduction repository:

Problem description:
I want to add Icon to Submenu。But it invalid.

Suggested solution:
In contentMenuItem

<div
  className={classnames(
    'contextmenu__item',
    {
      'contextmenu__item--disabled': disabled,
    },
    ...className.split(' ')
  )}
  onClick={handleClickEvent}
  {...attributes}
  ref={contextMenuItem}
>
  /* change next line */
  {icon && <div className="contextmenu__icon">{icon}</div>}
  {children}
</div>

In Submenu

<div
  className={classnames('submenu', ...className.split(' '))}
  onMouseOver={() => calculateSubmenuPos()}
  onMouseOut={() => hideSubmenu()}
  onFocus={() => null}
  onBlur={() => null}
  ref={submenuItem}
  {...attributes}
>
  /* change next line */
  <ContextMenuItem icon={icon}>{title}</ContextMenuItem>
  <div className="submenu__item" ref={submenuEl} style={submenuStyle}>
    {children}
  </div>
</div>

In index.scss

  &__icon {
    margin-right: 15px;
  }
  .submenu {
    $submenu: '.submenu';

It may work. Thanks!!!

Submenu close the contextmenu when click

  • rctx-contextmenu version: 1.4.1
  • node version: v19.9.0
  • npm (or yarn) version: 1.22.19

Relevant code or config

<ContextMenu
  id='node-context-menu'
  onHide={() => {
    setPreventClose(true);
  }}
>
  <Submenu title='Sub Menu'>
    <ContextMenuItem preventClose={preventClose}>
      <input />
      <Button
        onClick={() => {
          setPreventClose(false);
        }}
      >
        Submit
      </Button>
    </ContextMenuItem>
    <ContextMenuItem>Sub Item 2</ContextMenuItem>
    <ContextMenuItem>Sub Item 3</ContextMenuItem>
  </Submenu>
  <ContextMenuItem preventClose>add tags</ContextMenuItem>
  <ContextMenuItem>Menu Item 2</ContextMenuItem>
  <ContextMenuItem>Menu Item 3</ContextMenuItem>
  <ContextMenuItem>Menu Item 4</ContextMenuItem>
</ContextMenu>

What you did:
Click on "Sub menu"
image

What happened:
It closes the context menu
Expected result:
Will not close the context menu

Support for strict mode

Thank you so much for this wonderful library! It's doing exactly what I was looking for.

  • rctx-contextmenu version: "^1.3.7",
  • node version: 18.8.0
  • npm (or yarn) version: 1.22.19
  • react version: 18.2.0

I just saw that it does not yet support StrictMode. I think the reason is the dependency react-transition-group that hasn't been updated since they removed their dependency on findDomNode which is the one throwing the error.

https://github.com/reactjs/react-transition-group/releases/tag/v4.4.0

Type Issue for children props in index.d.ts

  • rctx-contextmenu version: 1.3.6
  • node version: v16.17.0
  • npm (or yarn) version: 8.15.0

Relevant code or config




Right Click On Me

  <ContextMenu id="my-context-menu-1">
    <ContextMenuItem>Menu Item 1</ContextMenuItem>
    <ContextMenuItem>Menu Item 2</ContextMenuItem>
    <ContextMenuItem>Menu Item 3</ContextMenuItem>
    <ContextMenuItem>Menu Item 4</ContextMenuItem>
  </ContextMenu>
</div>

What you did:

I just tried to run sample code on react-typescript project.

"react": "^18.2.0",
"typescript": "^4.7.4",

What happened:

Type '{ children: Element[]; id: string; appendTo: string; animation: string; }' is not assignable to type 'IntrinsicAttributes & ContextMenu'.
Property 'children' does not exist on type 'IntrinsicAttributes & ContextMenu'.
3 | const CustomContextMenu = () => {
4 | return (

5 | <ContextMenu
| ^^^^^^^^^^^


Problem description:

Type '{ children: Element[]; id: string; appendTo: string; animation: string; }' is not assignable to type 'IntrinsicAttributes & ContextMenu'.

Suggested solution:
I solved this problem like this:

rctx-contextmenu/src/index.d.ts

export interface ContextMenu {
id: string,
appendTo?: string,
animation?: string,
hideOnLeave?: boolean,
attributes?: object,
className?: string,
preventHideOnResize?: boolean,
preventHideOnScroll?: boolean,
onShow?: {(event: any): void},
onHide?: {(event: any): void},
children?: ReactNode, --------------------------------------->
onMouseLeave?: {(event: any): void}
}

export interface ContextMenuItem {
disabled?: boolean,
preventClose?: boolean,
disableWhileShiftPressed?: boolean,
attributes?: object,
className?: string,
children?: ReactNode, ---------------------------------------->
onClick?: {(event: any): void}
}

export interface ContextMenuTrigger {
id: string,
attributes?: object,
disable?: boolean,
className?: string,
children?: ReactNode ------------------------------------------->
}

export interface Submenu {
title: string,
attributes?: object,
className?: boolean,
children?: ReactNode -------------------------------------------->
}

I would like you to solve this issues as soon as possible.
Best Regards

Evgenii

Nextjs implementation problem?

It seems that it is not supported with nextjs. It throws a document undefined error in nextjs. Have any example with nextjs?

Feature Request: Left click toggle context-menu

I'd really like to have an optional toggle to make the context-menu trigger on left-click, technically not a "context-menu" anymore, but the feature would help bring down a lot of bloat in my projects, as currently some menus that I would like to add now have to be done through different packages.

👍 Love the package, I will give it an attempt to add it myself and open a PR, but I'm not confident.

Bug reported in dinamic items

  • gatsby-remark-embedder version: ?
  • node version: v12.18.3
  • npm (or yarn) version: npm 6.14.6

Relevant code or config

Failed to execute 'removeEventListener' on 'EventTarget': 2 arguments required, but only 1 present

What you did: I fixed this problem by adding a second parameter to the removeEventLister function like this: removeEventListener('resize', ()=>{}) but I'm not sure if this is the right thing I resolved

What happened: I'm using reactjs with class components, I'm adding dynamically several ContextMenuTrigger and ContextMenu with dynamic id's, when I add new ones to the state it works fine, but when I delete one and update the state the error occurs, I clarify that I'm using redux so what I do is delete it from the store and the store itself updates the state of the component

Reproduction repository: At this time I cannot

Problem description: I'm using reactjs with class components, I'm adding dynamically several ContextMenuTrigger and ContextMenu with dynamic id's, when I add new ones to the state it works fine, but when I delete one and update the state the error occurs, I clarify that I'm using redux so what I do is delete it from the store and the store itself updates the state of the component

Suggested solution: I fixed this problem by adding a second parameter to the removeEventLister function like this: removeEventListener('resize', ()=>{}) but I'm not sure if this is the right thing I resolved

IE support

Need to check if this plugin supports IE or not. If not then it should at least support IE

Bug with positioning

Just go to live demo page and click on the bottom of any card (Basic example section). You'll see that context menu is out of the viewport. This only happens if your scroll position is on top.

image

Build size

  • gatsby-remark-embedder version:
  • node version:
  • npm (or yarn) version:

Relevant code or config

What you did:

What happened:

Reproduction repository:

Problem description:

Suggested solution:

Warning: De: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.

  • rctx-contextmenu version: ^1.4.1
  • node version: v20.13.1
  • npm (or yarn) version: 10.5.2

Vite - ^5.3.1

Using
import { ContextMenu, ContextMenuItem, ContextMenuTrigger } from 'rctx-contextmenu';?

is giving warnings in console
chunk-RY6NLCXT.js?v=9a219bf8:521 Warning: Me: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.
at Me (http://localhost:5173/node_modules/.vite/deps/rctx-contextmenu.js?t=1722055804221&v=9a219bf8:797:15)

image

Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of t which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-find-node

Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of t which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-find-node

Nested Triggers

  • rctx-contextmenu version: 1.3.4
  • node version: 16.3.0
  • npm (or yarn) version: npm: 7.15.1

Relevant code or config

<>
<ContextMenuTrigger id={menuOne}>
       ....
              <ContextMenuTrigger id={menuTwo}>
              .....
              </ContextMenuTrigger>
          ...
</ContextMenuTrigger>
      <ContextMenu id={menuOne}>
        ... menu 1
      </ContextMenu>
      <ContextMenu id={menuTwo}>
        .... menu 2
      </ContextMenu>
</>

What you did:
I'm trying to have a trigger inside another trigger, i figured the nested item would get the click event first.
What happened:
When tried to right click the trigger 2 menu 1 comes up
Problem description:
When tried to right click the trigger 2 menu 1 comes up.
Suggested solution:
Have nested triggers work properly inside other triggers.

Menu closes if onMouseMove or onPointerMove event is called on Parent

Hi.
I have ContextMenuTrigger under a div.
On this parent div, I am using events like onPointerMove and onMouseMove.

If I don't use these event callbacks, the context menu opens and works fine. But as soon as I use these functions, the right menu closes as soon as I move my mouse.

Screen.Recording.2023-03-06.at.7.02.01.PM.mov

Open menu Onlick

Hello, thanks for this wonderful package :), please i have this issue in which i will like to open the context menu when the users clicks on a item.

Bug in menu height

If the height of context-menu is bigger than window height then it's hiding at the top. Need fix the height issue. Best to make it scrollable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.