context-menu
Custom context menu for your stuff. Deliberately elegant.
Simple usage
Install
$ npm install context-menu
Setup
// index.js;;;;;; ContextMenu;; // Alternatively this also works (not recommended);
Use
// App.js ;;;; { return <div className="App" onContextMenu=thishandleOnContextMenu> <Stuff /> </div> ; } { const data = label: 'New File' { /** impl */ } label: 'New Folder' disabled: true { /** impl */ } label: 'Sort by' submenu: label: 'Name' { /** impl */ } label: 'Date' { /** impl */ } label: 'Size' { /** impl */ } ; const handle = ContextMenu; // Optional operations handle; handle; ; handle; } ;
Warning
Do not use this module if your app is not a React app and is published on the web. This module has React and ReactDOM as dependency, which if your app doesn't have already, can increase load times for your users.
However, it's great if you are using another framework or no framework at all, as long as your app is in packaged format like Electron app, Chrome extension etc. where few more kilo bytes wont matter much.
API
Although ContextMenu is built using React, the API can be used in any other setup, including vanilla JS.
ContextMenu.init(container, options)
Sets up ContextMenu for use in the specified container. options
is an optional object as:
theme: 'myCustomTheme' // built-in 'dark' | 'light', default 'light'
Use this sass
template to define theme:
// Applies to self and .submenu
(If SASS is not part of your workflow, you can use https://www.sassmeister.com/ to get through the day)
ContextMenu.showMenu(data, [position])
Deprecation: MouseEvents are now auto-captured thus passing them as second param is deprecated
Show context menu, usage as in example above.
ContextMenu.proxy(data)
Deprecated: ContextMenu#showMenu has been been made simple enough and MouseEvents are auto-captured.
Returns an event listener that can be bound to context-menu
event. Be cautious when using this, as every invocation returns a new function reference. Using this directly in cases like React as onContextMenu={ContextMenu.proxy(data)}
can confuse React and every render it'll remove and re-attach the listener.
It's best to call it once and store the returned function in a persistent and consistent variable or object property.
Important Note
ContextMenu
comes with z-index
set to 10
by default. If you have other absolutely/fixed positioned items in the DOM leading to conflicting overlaps, you can adjust the z-index
that ContextMenu
shall use for itself, by including this somewhere in your CSS.