Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right Click Menu using React JS

I'd like to know if there is a best practice/correct way to setup a right-click menu for a React component.

I currently have this...

// nw is nw.gui from Node-Webkit componentWillMount: function() {     var menu = new nw.Menu();     menu .append(new nw.MenuItem({         label: 'doSomething',         click: function() {             // doSomething         }     }));      // I'd like to know if this bit can be done in a cleaner/more succinct way...     // BEGIN     var that = this;     addEventListener('contextmenu', function(e){         e.preventDefault();         // Use the attributes property to uniquely identify this react component          // (so different elements can have different right click menus)         if (e.target.attributes[0].nodeValue == that.getDOMNode().attributes[0].nodeValue) {             menu.popup(e.x, e.y);         }     })     // END }, 

This works, but it feels a little messy and I was wondering if there was another approach I might be able to use, any information would be greatly appreciated,

Thanks!

like image 346
Tom Avatar asked Feb 21 '15 16:02

Tom


People also ask

How do you handle right click in React?

To distinguish left and right click events in React, we can set the onClick prop and onContextMenu to handle left click and right click events respectively. to add the handleClick function to handle both left and right click events. We check which type of click it is with the e. type propety.

How do you create a context menu?

Open the app -> Java -> Package -> Mainactivity.In this step, add the code to show the ContextMenu. Whenever the app will strat make a long click on a text and display the number of options to select of them for specific purposes. Comments are added inside the code to understand the code in more detail.

How do I open a new tab in React?

To open a link in a new tab in React, use the <a> element and set its target prop to _blank , e.g. <a href="https://google.com" target="_blank" rel="noopener noreferrer"></a> . The _blank value means that the resource is loaded into a new tab. Copied!


2 Answers

UPDATE:

Figured it out - here is what you can do

var addMenu;  componentWillMount: function() {     addMenu = new nw.Menu();     addMenu.append(new nw.MenuItem({         label: 'doSomething',         click: function() {             // doSomething         }     })); },  contextMenu: function(e) {     e.preventDefault();     addMenu.popup(e.clientX, e.clientY); },  render: function(){     return <button onClick={this.handleClick} onContextMenu={this.contextMenu}>SomethingUseful</button> } 

In render you can pass a function to onContextMenu for when a right click occurs for this react component.

like image 199
Tom Avatar answered Sep 22 '22 14:09

Tom


There are few things to take care about with popup menus:

  1. it should be rendered away from its parent and siblings - preferably in an overlay which is the last child of document.body
  2. special logic should take care that it's always displayed on screen and never cropped by screen edges
  3. if there is a hierarchy involved, child popups should be aligned to items from previous popup (opener).

I created a library which you can use to accomplish all of these:

http://dkozar.github.io/react-data-menu/

like image 34
Danko Kozar Avatar answered Sep 21 '22 14:09

Danko Kozar