Quick Overview
jQuery-contextMenu is a lightweight, feature-rich context menu plugin for jQuery. It allows developers to easily create customizable right-click menus for web applications, enhancing user interaction and providing quick access to contextual actions.
Pros
- Easy to implement and customize
- Supports keyboard navigation and accessibility features
- Offers various themes and styling options
- Compatible with both desktop and mobile devices
Cons
- Requires jQuery as a dependency
- May have performance issues with large, complex menus
- Limited built-in animation options
Code Examples
- Basic context menu:
$.contextMenu({
selector: '.context-menu-one',
callback: function(key, options) {
var m = "clicked: " + key;
window.console && console.log(m) || alert(m);
},
items: {
"edit": {name: "Edit", icon: "edit"},
"cut": {name: "Cut", icon: "cut"},
"copy": {name: "Copy", icon: "copy"},
"paste": {name: "Paste", icon: "paste"},
"delete": {name: "Delete", icon: "delete"},
"sep1": "---------",
"quit": {name: "Quit", icon: function($element, key, item){ return 'context-menu-icon context-menu-icon-quit'; }}
}
});
- Dynamic menu items:
$.contextMenu({
selector: '.context-menu-two',
build: function($trigger, e) {
return {
callback: function(key, options) {
var m = "clicked: " + key;
window.console && console.log(m) || alert(m);
},
items: {
"edit": {name: "Edit", icon: "edit"},
"cut": {name: "Cut", icon: "cut"},
"copy": {name: "Copy", icon: "copy"},
"paste": {name: "Paste", icon: "paste"},
"delete": {name: "Delete", icon: "delete"},
"sep1": "---------",
"quit": {name: "Quit", icon: function($element, key, item){ return 'context-menu-icon context-menu-icon-quit'; }}
}
};
}
});
- Submenu example:
$.contextMenu({
selector: '.context-menu-three',
callback: function(key, options) {
var m = "clicked: " + key;
window.console && console.log(m) || alert(m);
},
items: {
"edit": {name: "Edit", icon: "edit"},
"cut": {name: "Cut", icon: "cut"},
"copy": {name: "Copy", icon: "copy"},
"paste": {name: "Paste", icon: "paste"},
"delete": {name: "Delete", icon: "delete"},
"sep1": "---------",
"quit": {name: "Quit", icon: function($element, key, item){ return 'context-menu-icon context-menu-icon-quit'; }},
"fold1": {
"name": "Sub group",
"items": {
"fold1-key1": {"name": "Sub 1"},
"fold2": {
"name": "Sub 2",
"items": {
"fold2-key1": {"name": "Sub 2.1"},
"fold2-key2": {"name": "Sub 2.2"}
}
}
}
}
}
});
Getting Started
- Include jQuery and jQuery-contextMenu in your HTML:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.9.2/jquery.contextMenu.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
jQuery contextMenu plugin & polyfill
$.contextMenu
is a management facility for - you guessed it - context menus. It was designed for an application where there are hundreds of elements that may show a context menu - so intialization speed and memory usage are kept fairly small. It also allows to register context menus without providing actual markup, as $.contextMenu
generates DOMElements as needed.
features - demo - documentation
Dependencies
- jQuery >=1.8.2
- jQuery UI position (optional but recommended)
Usage
register contextMenu from javascript:
$.contextMenu({
// define which elements trigger this menu
selector: ".with-cool-menu",
// define the elements of the menu
items: {
foo: {name: "Foo", callback: function(key, opt){ alert("Foo!"); }},
bar: {name: "Bar", callback: function(key, opt){ alert("Bar!") }}
}
// there's more, have a look at the demos and docs...
});
have a look at the demos.
Version 3.0 beta
Version 3.0 is a restructure of the javascript into something more sane written in ES6. It consolidates all API's so callbacks are better documented and more concise. The basics are still the same, but all callbacks are structured differently.
The goal of this refactor was mostly to make the ContextMenu easier to maintain, and make the API's more consise. It also adds JSdoc comments so the API documentation is generated from the code and it enables code completion.
Check out the 3.x branch, or install with npm install jquery-contextmenu@next
.
HTML5 Compatibility
Firefox 8 implemented contextmenu using the <menuitem> tags for menu-structure. The specs however state that <command> tags should be used for this purpose. $.contextMenu accepts both.
Firefox 8 does not yet fully implement the contextmenu specification (Ticket #617528). The elements a, button, input and option usable as commands are being ignored altogether. It also doesn't (optically) distinguish between checkbox/radio and regular commands (Bug #705292).
Note: While the specs note <option>s to be renderd as regular commands, $.contextMenu will render an actual <select>. import contextMenu from HTML5 <menu>:
$.contextMenu("html5");
Interaction Principles
You're (obviously) able to use the context menu with your mouse. Once it is opened, you can also use the keyboard to (fully) navigate it.
- â (up) previous item in list, will skip disabled elements and wrap around
- â (down) next item in, will skip disabled elements and wrap around
- â (right) dive into sub-menu
- â (left) rise from sub-menu
- âµ (return) invoke command
- ⥠(tab) next item or input element, will skip disabled elements and wrap around
- ⪠⥠(shift tab) previous item or input element, will skip disabled elements and wrap around
- â (escape) close menu
- â´ (space) captured and ignore to avoid page scrolling (for consistency with native menus)
- â (page up) captured and ignore to avoid page scrolling (for consistency with native menus)
- â (page down) captured and ignore to avoid page scrolling (for consistency with native menus)
- â (home) first item in list, will skip disabled elements
- â (end) last item in list, will skip disabled elements
Besides the obvious, browser also react to alphanumeric key strokes. Hitting r
in a context menu will make Firefox (8) reload the page immediately. Chrome selects the option to see infos on the page, Safari selects the option to print the document. Awesome, right? Until trying the same on Windows I did not realize that the browsers were using the access-key for this. I would've preferred typing the first character of something, say "s" for "save" and then iterate through all the commands beginning with s. But that's me - what do I know about UX? Anyways, $.contextMenu now also supports accesskey handling.
Authors
- Björn Brala
- Rodney Rehm (original creator)
- Christiaan Baartse (single callback per menu)
- Addy Osmani (compatibility with native context menu in Firefox 8)
License
$.contextMenu
is published under the MIT license
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest youâll be creating employment for local families and restoring wildlife habitats.
Special thanks
Font-Awesome icons used from encharm/Font-Awesome-SVG-PNG.
SWIS :heart: Open Source
SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot