2021-04-29 00:56:31 -07:00
# Menu
2017-07-26 13:02:40 -07:00
## Class: Menu
2016-04-21 15:39:12 -07:00
> Create native application menus and context menus.
2016-04-21 15:35:29 -07:00
2016-11-23 11:20:56 -08:00
Process: [Main ](../glossary.md#main-process )
2016-11-03 10:26:00 -07:00
2025-07-16 12:20:08 -07:00
> [!TIP]
> See also: [A detailed guide about how to implement menus in your application](../tutorial/menus.md).
2025-06-10 01:00:06 -07:00
> [!WARNING]
> Electron's built-in classes cannot be subclassed in user code.
> For more information, see [the FAQ](../faq.md#class-inheritance-does-not-work-with-electron-built-in-modules).
2016-11-14 16:41:15 -08:00
### `new Menu()`
Creates a new menu.
### Static Methods
2019-05-21 11:41:41 +05:00
The `Menu` class has the following static methods:
2016-11-14 16:41:15 -08:00
#### `Menu.setApplicationMenu(menu)`
2025-07-16 12:20:08 -07:00
- `menu` Menu | null
2016-11-14 16:41:15 -08:00
2017-04-04 18:00:20 +12:00
Sets `menu` as the application menu on macOS. On Windows and Linux, the
`menu` will be set as each window's top menu.
2018-10-29 19:08:47 +01:00
Also on Windows and Linux, you can use a `&` in the top-level item name to
indicate which letter should get a generated accelerator. For example, using
`&File` for the file menu would result in a generated `Alt-F` accelerator that
2021-02-18 22:41:16 -08:00
opens the associated menu. The indicated character in the button label then gets an
underline, and the `&` character is not displayed on the button label.
In order to escape the `&` character in an item name, add a proceeding `&` . For example, `&&File` would result in `&File` displayed on the button label.
2018-10-29 19:08:47 +01:00
2019-01-15 21:35:53 +01:00
Passing `null` will suppress the default menu. On Windows and Linux,
this has the additional effect of removing the menu bar from the window.
2016-11-14 16:41:15 -08:00
2025-05-09 14:36:42 -07:00
> [!NOTE]
> The default menu will be created automatically if the app does not set one.
> It contains standard items such as `File`, `Edit`, `View`, `Window` and `Help`.
2016-11-14 16:41:15 -08:00
#### `Menu.getApplicationMenu()`
2017-11-19 11:41:22 +02:00
Returns `Menu | null` - The application menu, if set, or `null` , if not set.
2016-11-14 16:41:15 -08:00
2025-05-09 14:36:42 -07:00
> [!NOTE]
> The returned `Menu` instance doesn't support dynamic addition or
> removal of menu items. [Instance properties](#instance-properties) can still
> be dynamically modified.
2017-03-23 11:55:46 -07:00
2016-11-14 16:41:15 -08:00
#### `Menu.sendActionToFirstResponder(action)` _macOS_
2025-07-16 12:20:08 -07:00
- `action` string
2016-11-14 16:41:15 -08:00
Sends the `action` to the first responder of application. This is used for
2018-05-08 00:16:09 -05:00
emulating default macOS menu behaviors. Usually you would use the
2025-07-16 12:20:08 -07:00
[`role` ](../tutorial/menus.md#roles ) property of a [`MenuItem` ](menu-item.md ).
2016-11-14 16:41:15 -08:00
See the [macOS Cocoa Event Handling Guide ](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html#//apple_ref/doc/uid/10000060i-CH3-SW7 )
for more information on macOS' native actions.
#### `Menu.buildFromTemplate(template)`
2025-07-16 12:20:08 -07:00
- `template` (MenuItemConstructorOptions | MenuItem)[]
2016-11-14 16:41:15 -08:00
Returns `Menu`
2018-05-07 08:46:14 -07:00
Generally, the `template` is an array of `options` for constructing a
2016-11-14 16:41:15 -08:00
[MenuItem ](menu-item.md ). The usage can be referenced above.
2019-02-06 10:04:40 -08:00
You can also attach other fields to the element of the `template` and they will become properties of the constructed menu items.
2016-11-14 16:41:15 -08:00
### Instance Methods
The `menu` object has the following instance methods:
2019-06-04 14:03:24 -07:00
#### `menu.popup([options])`
2017-02-16 08:34:39 -08:00
2025-07-16 12:20:08 -07:00
- `options` Object (optional)
- `window` [BaseWindow ](base-window.md ) (optional) - Default is the focused window.
- `frame` [WebFrameMain ](web-frame-main.md ) (optional) - Provide the relevant frame
2025-04-11 07:15:35 -07:00
if you want certain OS-level features such as Writing Tools on macOS to function correctly. Typically, this should be `params.frame` from the [`context-menu` event ](web-contents.md#event-context-menu ) on a WebContents, or the [`focusedFrame` property ](web-contents.md#contentsfocusedframe-readonly ) of a WebContents.
2025-07-16 12:20:08 -07:00
- `x` number (optional) - Default is the current mouse cursor position.
2017-06-05 08:55:42 -07:00
Must be declared if `y` is declared.
2025-07-16 12:20:08 -07:00
- `y` number (optional) - Default is the current mouse cursor position.
2017-06-05 08:55:42 -07:00
Must be declared if `x` is declared.
2025-07-16 12:20:08 -07:00
- `positioningItem` number (optional) _macOS_ - The index of the menu item to
2017-02-16 08:34:39 -08:00
be positioned under the mouse cursor at the specified coordinates. Default
is -1.
2025-07-16 12:20:08 -07:00
- `sourceType` string (optional) _Windows_ _Linux_ - This should map to the `menuSourceType`
2023-06-29 15:54:06 -07:00
provided by the `context-menu` event. It is not recommended to set this value manually,
only provide values you receive from other APIs or leave it `undefined` .
Can be `none` , `mouse` , `keyboard` , `touch` , `touchMenu` , `longPress` , `longTap` , `touchHandle` , `stylus` , `adjustSelection` , or `adjustSelectionReset` .
2025-07-16 12:20:08 -07:00
- `callback` Function (optional) - Called when menu is closed.
2016-11-14 16:41:15 -08:00
2024-12-04 22:35:28 +01:00
Pops up this menu as a context menu in the [`BaseWindow` ](base-window.md ).
2016-11-14 16:41:15 -08:00
2025-07-16 12:20:08 -07:00
> [!TIP]
> For more details, see the [Context Menu](../tutorial/context-menu.md) guide.
2024-12-04 22:35:28 +01:00
#### `menu.closePopup([window])`
2017-02-16 11:18:13 -08:00
2025-07-16 12:20:08 -07:00
- `window` [BaseWindow ](base-window.md ) (optional) - Default is the focused window.
2017-02-16 11:18:13 -08:00
2024-12-04 22:35:28 +01:00
Closes the context menu in the `window` .
2017-02-16 11:18:13 -08:00
2016-11-14 16:41:15 -08:00
#### `menu.append(menuItem)`
2025-07-16 12:20:08 -07:00
- `menuItem` [MenuItem ](menu-item.md )
2016-11-14 16:41:15 -08:00
Appends the `menuItem` to the menu.
2017-09-26 21:14:16 -04:00
#### `menu.getMenuItemById(id)`
2025-07-16 12:20:08 -07:00
- `id` string
2017-09-26 21:14:16 -04:00
2020-07-06 13:24:54 -07:00
Returns `MenuItem | null` the item with the specified `id`
2017-09-26 21:14:16 -04:00
2016-11-14 16:41:15 -08:00
#### `menu.insert(pos, menuItem)`
2025-07-16 12:20:08 -07:00
- `pos` Integer
- `menuItem` [MenuItem ](menu-item.md )
2016-11-14 16:41:15 -08:00
Inserts the `menuItem` to the `pos` position of the menu.
2018-01-27 11:28:42 -05:00
### Instance Events
2019-02-15 10:29:33 -08:00
Objects created with `new Menu` or returned by `Menu.buildFromTemplate` emit the following events:
2018-01-27 11:28:42 -05:00
2025-05-09 14:36:42 -07:00
> [!NOTE]
> Some events are only available on specific operating systems and are
> labeled as such.
2018-01-27 11:28:42 -05:00
2018-01-28 18:57:44 -05:00
#### Event: 'menu-will-show'
2018-01-27 11:28:42 -05:00
Returns:
2025-07-16 12:20:08 -07:00
- `event` Event
2018-01-27 11:28:42 -05:00
Emitted when `menu.popup()` is called.
2018-01-28 18:57:44 -05:00
#### Event: 'menu-will-close'
2018-01-27 11:28:42 -05:00
Returns:
2025-07-16 12:20:08 -07:00
- `event` Event
2018-01-27 11:28:42 -05:00
2018-01-28 13:59:53 -05:00
Emitted when a popup is closed either manually or with `menu.closePopup()` .
2018-01-27 11:28:42 -05:00
2016-11-14 16:41:15 -08:00
### Instance Properties
`menu` objects also have the following properties:
#### `menu.items`
2017-07-24 10:29:03 +02:00
A `MenuItem[]` array containing the menu's items.
2016-11-14 16:41:15 -08:00
2025-07-16 12:20:08 -07:00
Each `Menu` consists of multiple [`MenuItem` ](menu-item.md ) instances and each `MenuItem`
can nest a `Menu` into its `submenu` property.