JavaScript Context Menu

The LemonadeJS Context Menu is a reactive JavaScript plugin to create dynamic and interactive menu navigation on web applications. It provides a customizable and user-friendly experience, featuring intelligent configurations that automatically adjust the menu’s position to prevent it from disappearing off-screen, thus enhancing both the user experience and the responsive design of your application.

This plugin allows for the customization of the menu’s behaviour to align with the specific needs of your application. Whether executing direct actions or unveiling nested options for an in-depth selection process, the Context Menu adapts effortlessly to various user interactions.

With a strong emphasis on responsive design and adaptability, the LemonadeJS Context Menu is a vital tool for web development. Its ease of integration with major frameworks like React, Vue, Angular, and others makes it an invaluable asset for creating sophisticated and user-friendly navigation experiences in modern web applications.

Documentation

Installation

npm install @lemonadejs/contextmenu

Settings

PropertyDescription
options: Option[]An array of option objects describing the rendering options. Each item should follow the structure defined in the ‘Option Details’ section.
root?: HTMLElementCustom root element for the contextmenu event listener. Defaults to the parent element.

Events

EventDescription
onclose?: (instance) => voidCallback executed when the menu closes.

Option Details

PropertyDescription
submenu?: Option[]An optional array containing options displayed as a sub-menu.
title?: StringThe title text associated with the option.
type?: StringThe type of the option: ‘line’ for a separator, ‘inline’ for a custom inline element.
onclick?: () => voidThe function executed upon selecting the option.
icon?: StringThe name of the Material Icon associated with the option.
render?: () => voidThe function executed when rendering the option.
onopen?: () => voidThe function executed when the submenu is opened.
disabled?: BooleanWhen true, the option is disabled and cannot be selected.
shortcut?: StringDisplay text for a keyboard shortcut hint (e.g., “Ctrl+C”).
tooltip?: StringTooltip text displayed on hover.

Methods

MethodDescription
openAt(event: Event): voidOpen the context menu at the position of the given event (uses clientX/clientY).
openAt(x: number, y: number): voidOpen the context menu at the specified x and y coordinates.
open(options, x, y, adjust): voidOpen the context menu with the given options at position (x, y). When adjust is true, repositions to keep the menu visible on screen.
close(level?: number): voidClose the context menu. If level is provided, closes submenus from that level onwards; otherwise closes all.
isClosed(): booleanReturns true if the context menu is closed.

Examples

Activating Actions

Actions can be incorporated into the context menu by adding an onclick method to the option.

Right-click within the blue area of the example to open the context menu.

live
<html>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/modal/dist/style.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/contextmenu/dist/style.min.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" />

<script src="https://cdn.jsdelivr.net/npm/lemonadejs/dist/lemonade.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lemonadejs/modal/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lemonadejs/contextmenu/dist/index.min.js"></script>

<div id='root' style="background-color: #ccc; width: 100px; height: 100px;"></div>

<script>
let root = document.getElementById('root');

const options = [
    {
        title: 'Console.log',
        onclick:function() {
            console.log('Hello!')
        },
    },
    {
        title: 'Show Alert',
        onclick:function() {
            alert('Hello!')
        },
    },
];

const contextmenu = Contextmenu(root, {
    options: options,
});
</script>
</html>

Configuring Submenu and Option Icons

Submenus can be integrated to display additional options within an option. Icons can also be added to options, providing users with visual cues to identify the associated actions.

Right-click within the blue area of the example to open the context menu.

live
<html>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/contextmenu/dist/style.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/modal/dist/style.min.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" />
<script src="https://cdn.jsdelivr.net/npm/lemonadejs/dist/lemonade.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@lemonadejs/modal/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lemonadejs/contextmenu/dist/index.min.js"></script>

<div id='root' style="background-color: #ccc; width: 100px; height: 100px;"></div>

<script>
let root = document.getElementById('root');

const options = [
    {
        title: 'Add',
        icon: 'add_box'
    },
    {
        title: 'Undo',
        icon: 'undo'
    },
    {
        title: 'Open submenu',
        submenu: [
            { title: 'Submenu Option 1' },
            { title: 'Submenu Option 2' },
        ]
    }
];


const contextmenu = Contextmenu(root, {
    options: options,
});

</script>
</html>