# How to Create Custom Context Menus in Electron: A Complete Guide

> Learn to build custom context menus in Electron using Menu.buildFromTemplate and menu.popup. This guide provides a complete walkthrough for enhancing your Electron application's user experience.

- Repository: [Electron/electron](https://github.com/electron/electron)
- Tags: how-to-guide
- Published: 2026-02-25

---

**To create custom context menus in Electron, construct a `Menu` instance using `Menu.buildFromTemplate()` in the main process, then display it with `menu.popup()` when handling the `webContents` `'context-menu'` event or renderer IPC messages.**

Unlike web browsers, Electron does not provide a default context menu—you must build custom context menus in Electron yourself and trigger them on user actions such as right-click, Control-click, or Shift+F10. The implementation relies on the `Menu` API defined in the `electron/electron` repository, which bridges JavaScript templates to native Chromium menu systems.

## Understanding the Menu API Architecture

### Core Implementation in menu.ts

The `Menu` class is implemented in [`lib/browser/api/menu.ts`](https://github.com/electron/electron/blob/main/lib/browser/api/menu.ts), where the constructor initializes internal maps including `commandsMap`, `groupsMap`, and `items` to manage menu state. When you call `Menu.buildFromTemplate()`, the method populates these structures by appending `MenuItem` instances defined in [`lib/browser/api/menu-item.ts`](https://github.com/electron/electron/blob/main/lib/browser/api/menu-item.ts).

### Native Bridge to Chromium

The actual display logic crosses into native code via `shell/browser/api/electron_api_menu.cc`. When you invoke `menu.popup()`, the TypeScript layer validates options (including `window`, `x`, `y`, `positioningItem`, `sourceType`, and `frame`) and forwards the request to the native `popupAt` binding, which renders the menu using Chromium's platform-specific UI toolkit.

## Building the Context Menu

### Creating Menu Templates

A menu template is an array of plain objects that Electron converts into `MenuItem` instances. Each object can specify `label`, `role`, `type`, `click` handlers, and nested `submenu` arrays.

```javascript
const { Menu } = require('electron');

const template = [
  {
    label: 'File',
    submenu: [
      { role: 'quit' }
    ]
  },
  {
    label: 'Edit',
    submenu: [
      { role: 'undo' },
      { role: 'redo' },
      { type: 'separator' },
      { role: 'cut' },
      { role: 'copy' },
      { role: 'paste' }
    ]
  }
];

const menu = Menu.buildFromTemplate(template);

```

### Menu Item Configuration

For context-specific actions, define `click` handlers that receive `menuItem`, `browserWindow`, and `event` parameters. You can also use the `accelerator` property to define keyboard shortcuts, though these are more common for application menus than context menus.

## Displaying Custom Context Menus in Electron

### Main Process Approach (context-menu event)

The most efficient method listens to the `webContents` `'context-menu'` event, which fires before the default OS menu appears. The event provides a `params` object containing metadata about the clicked element, including `linkURL`, `srcURL`, `isEditable`, and `frame`.

```javascript
const { BrowserWindow, Menu } = require('electron');

const win = new BrowserWindow({ width: 800, height: 600 });

const contextMenu = Menu.buildFromTemplate([
  {
    label: 'Open Link in Browser',
    visible: false,
    click: (menuItem, browserWindow, event) => {
      const { shell } = require('electron');
      shell.openExternal(event.linkURL);
    }
  },
  { type: 'separator' },
  { role: 'copy' },
  { role: 'paste' }
]);

win.webContents.on('context-menu', (event, params) => {
  // Show link-specific items only when clicking a hyperlink
  contextMenu.items[0].visible = !!params.linkURL;
  
  contextMenu.popup({
    window: win,
    x: params.x,
    y: params.y
  });
});

```

### Renderer Process Approach (IPC)

When `contextIsolation` is enabled or you prefer to keep menu logic out of the main process, capture the DOM `contextmenu` event in the renderer and forward coordinates via IPC.

**preload.js:**

```javascript
const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
  showContextMenu: (x, y) => ipcRenderer.send('show-context-menu', { x, y })
});

```

**renderer.js:**

```javascript
window.addEventListener('contextmenu', (e) => {
  e.preventDefault();
  window.electronAPI.showContextMenu(e.x, e.y);
});

```

**main.js:**

```javascript
const { ipcMain, BrowserWindow, Menu } = require('electron');

const menu = Menu.buildFromTemplate([
  { label: 'Inspect', click: () => { /* ... */ } },
  { type: 'separator' },
  { role: 'reload' }
]);

ipcMain.on('show-context-menu', (event, { x, y }) => {
  const win = BrowserWindow.fromWebContents(event.sender);
  menu.popup({ window: win, x, y });
});

```

### macOS-Specific Considerations (frame option)

By default, macOS disables Writing Tools, AutoFill, and Services in Electron context menus. To re-enable these platform features, pass the `frame` option (a `WebFrameMain` instance) obtained from the `context-menu` event parameters.

```javascript
win.webContents.on('context-menu', (event, params) => {
  if (params.isEditable) {
    const editMenu = Menu.buildFromTemplate([{ role: 'editMenu' }]);
    editMenu.popup({
      window: win,
      frame: params.frame  // Enables Writing Tools, AutoFill, etc.
    });
  }
});

```

## Complete Code Examples

### Example 1: Link Context Menu (Main Process)

```javascript
const { app, BrowserWindow, Menu, shell } = require('electron');
const path = require('path');

function createWindow() {
  const win = new BrowserWindow({
    width: 1000,
    height: 800,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true
    }
  });

  win.loadFile('index.html');

  const linkMenu = Menu.buildFromTemplate([
    {
      label: 'Open Link in Default Browser',
      click: (menuItem, browserWindow, event) => {
        if (event.linkURL) shell.openExternal(event.linkURL);
      }
    },
    {
      label: 'Copy Link Address',
      click: (menuItem, browserWindow, event) => {
        if (event.linkURL) {
          const { clipboard } = require('electron');
          clipboard.writeText(event.linkURL);
        }
      }
    }
  ]);

  win.webContents.on('context-menu', (event, params) => {
    if (params.linkURL && params.linkURL !== '') {
      linkMenu.popup({ window: win });
    }
  });
}

app.whenReady().then(createWindow);

```

### Example 2: Editable Field Menu with macOS Writing Tools

```javascript
const { BrowserWindow, Menu } = require('electron');

const win = new BrowserWindow({ width: 800, height: 600 });

const contextMenu = Menu.buildFromTemplate([
  { role: 'undo' },
  { role: 'redo' },
  { type: 'separator' },
  { role: 'cut' },
  { role: 'copy' },
  { role: 'paste' },
  { role: 'selectAll' }
]);

win.webContents.on('context-menu', (event, params) => {
  if (params.isEditable) {
    contextMenu.popup({
      window: win,
      x: params.x,
      y: params.y,
      frame: params.frame  // Critical for macOS Writing Tools
    });
  }
});

```

## Summary

- **Electron requires manual context menu creation** using the `Menu` class; no default menu is provided.
- **Build menus** with `Menu.buildFromTemplate()` in the main process, defining items with labels, roles, and click handlers.
- **Display menus** by calling `menu.popup()` with options for `window`, `x`, `y`, and optionally `frame` for macOS features.
- **Detect user requests** via the `webContents` `'context-menu'` event (main process) or DOM `'contextmenu'` with IPC (renderer process).
- **Enable macOS Writing Tools** by passing `params.frame` from the context-menu event to `menu.popup()`.

## Frequently Asked Questions

### How do I create a custom context menu in Electron?

To create a custom context menu in Electron, import the `Menu` module in your main process and call `Menu.buildFromTemplate()` with an array of menu item objects. Each object can specify a `label`, `role`, `type`, or `click` function. Store the resulting `Menu` instance and call `menu.popup({ window: browserWindow })` to display it when the user right-clicks.

### What is the difference between the main process and renderer process approaches?

The main process approach listens to the `webContents` `'context-menu'` event, which fires automatically on right-click and provides detailed metadata about the clicked element via `params`. This is the most efficient method. The renderer process approach attaches a DOM `'contextmenu'` event listener in the preload or renderer script, prevents the default, and sends coordinates to the main process via IPC to trigger `menu.popup()`. Use the renderer approach when you need custom logic in the frontend or when `contextIsolation` requires explicit IPC communication.

### How do I enable macOS Writing Tools in Electron context menus?

By default, macOS disables Writing Tools, AutoFill, and Services in Electron context menus. To enable these features, pass the `frame` option to `menu.popup()`. When handling the `'context-menu'` event, the `params` object includes a `frame` property (a `WebFrameMain` instance). Include this in your popup options: `menu.popup({ window: win, frame: params.frame })`. This tells the native menu layer that the popup belongs to that specific frame, lifting macOS restrictions.

### Can I show a context menu at specific coordinates?

Yes, the `menu.popup()` method accepts `x` and `y` coordinates to position the menu precisely. When using the main process `'context-menu'` event, the `params` object provides `params.x` and `params.y` representing the click location relative to the web page. Pass these directly to `menu.popup({ window: win, x: params.x, y: params.y })`. If you omit coordinates, Electron displays the menu at the current mouse cursor position.