2016-11-15 00:24:48 +00:00
## Class: MenuItem
2015-08-28 23:19:28 +00:00
2016-04-22 18:42:54 +00:00
> Add items to native application menus and context menus.
2015-08-28 23:19:28 +00:00
2016-11-23 19:20:56 +00:00
Process: [Main ](../glossary.md#main-process )
2016-11-03 17:26:00 +00:00
2016-06-22 04:23:07 +00:00
See [`Menu` ](menu.md ) for examples.
2013-09-09 07:35:57 +00:00
2016-08-22 18:29:15 +00:00
### `new MenuItem(options)`
2013-08-14 22:43:35 +00:00
* `options` Object
2016-12-29 22:11:26 +00:00
* `click` Function (optional) - Will be called with
2016-06-22 04:23:07 +00:00
`click(menuItem, browserWindow, event)` when the menu item is clicked.
2016-10-13 06:30:57 +00:00
* `menuItem` MenuItem
2020-06-17 17:34:25 +00:00
* `browserWindow` [BrowserWindow ](browser-window.md ) | undefined - This will not be defined if no window is open.
2019-02-19 09:24:19 +00:00
* `event` [KeyboardEvent ](structures/keyboard-event.md )
2022-01-06 18:41:28 +00:00
* `role` string (optional) - Can be `undo` , `redo` , `cut` , `copy` , `paste` , `pasteAndMatchStyle` , `delete` , `selectAll` , `reload` , `forceReload` , `toggleDevTools` , `resetZoom` , `zoomIn` , `zoomOut` , `toggleSpellChecker` , `togglefullscreen` , `window` , `minimize` , `close` , `help` , `about` , `services` , `hide` , `hideOthers` , `unhide` , `quit` , 'showSubstitutions', 'toggleSmartQuotes', 'toggleSmartDashes', 'toggleTextReplacement', `startSpeaking` , `stopSpeaking` , `zoom` , `front` , `appMenu` , `fileMenu` , `editMenu` , `viewMenu` , `shareMenu` , `recentDocuments` , `toggleTabBar` , `selectNextTab` , `selectPreviousTab` , `mergeAllWindows` , `clearRecentDocuments` , `moveTabToNewWindow` or `windowMenu` - Define the action of the menu item, when specified the
2017-03-30 18:07:25 +00:00
`click` property will be ignored. See [roles ](#roles ).
2021-11-16 04:13:18 +00:00
* `type` string (optional) - Can be `normal` , `separator` , `submenu` , `checkbox` or
2016-06-22 04:23:07 +00:00
`radio` .
2021-11-16 04:13:18 +00:00
* `label` string (optional)
* `sublabel` string (optional)
* `toolTip` string (optional) _macOS_ - Hover text for this menu item.
2016-12-29 22:11:26 +00:00
* `accelerator` [Accelerator ](accelerator.md ) (optional)
2021-11-16 04:13:18 +00:00
* `icon` ([NativeImage](native-image.md) | string) (optional)
* `enabled` boolean (optional) - If false, the menu item will be greyed out and
2016-04-22 13:53:26 +00:00
unclickable.
2021-11-16 04:13:18 +00:00
* `acceleratorWorksWhenHidden` boolean (optional) _macOS_ - default is `true` , and when `false` will prevent the accelerator from triggering the item if the item is not visible`.
* `visible` boolean (optional) - If false, the menu item will be entirely hidden.
* `checked` boolean (optional) - Should only be specified for `checkbox` or `radio` type
2016-06-22 04:23:07 +00:00
menu items.
2021-11-16 04:13:18 +00:00
* `registerAccelerator` boolean (optional) _Linux_ _Windows_ - If false, the accelerator won't be registered
2018-11-26 18:43:55 +00:00
with the system, but it will still be displayed. Defaults to true.
2020-10-20 01:33:06 +00:00
* `sharingItem` SharingItem (optional) _macOS_ - The item to share when the `role` is `shareMenu` .
2018-09-10 03:03:58 +00:00
* `submenu` (MenuItemConstructorOptions[] | [Menu ](menu.md )) (optional) - Should be specified
for `submenu` type menu items. If `submenu` is specified, the `type: 'submenu'` can be omitted.
If the value is not a [`Menu` ](menu.md ) then it will be automatically converted to one using
2016-06-22 04:23:07 +00:00
`Menu.buildFromTemplate` .
2021-11-16 04:13:18 +00:00
* `id` string (optional) - Unique within a single menu. If defined then it can be used
2016-06-22 04:23:07 +00:00
as a reference to this item by the position attribute.
2021-11-16 04:13:18 +00:00
* `before` string[] (optional) - Inserts this item before the item with the specified label. If
2018-09-10 03:03:58 +00:00
the referenced item doesn't exist the item will be inserted at the end of the menu. Also implies
that the menu item in question should be placed in the same “group” as the item.
2021-11-16 04:13:18 +00:00
* `after` string[] (optional) - Inserts this item after the item with the specified label. If the
2018-09-10 03:03:58 +00:00
referenced item doesn't exist the item will be inserted at the end of
the menu.
2021-11-16 04:13:18 +00:00
* `beforeGroupContaining` string[] (optional) - Provides a means for a single context menu to declare
2018-09-10 03:03:58 +00:00
the placement of their containing group before the containing group of the item
with the specified label.
2021-11-16 04:13:18 +00:00
* `afterGroupContaining` string[] (optional) - Provides a means for a single context menu to declare
2018-09-10 03:03:58 +00:00
the placement of their containing group after the containing group of the item
with the specified label.
2015-09-02 01:19:18 +00:00
2019-02-28 17:00:54 +00:00
**Note:** `acceleratorWorksWhenHidden` is specified as being macOS-only because accelerators always work when items are hidden on Windows and Linux. The option is exposed to users to give them the option to turn it off, as this is possible in native macOS development. This property is only usable on macOS High Sierra 10.13 or newer.
2017-03-30 18:07:25 +00:00
### Roles
Roles allow menu items to have predefined behaviors.
2016-03-31 03:57:28 +00:00
It is best to specify `role` for any menu item that matches a standard role,
rather than trying to manually implement the behavior in a `click` function.
The built-in `role` behavior will give the best native experience.
2015-09-02 01:19:18 +00:00
2017-03-30 18:07:25 +00:00
The `label` and `accelerator` values are optional when using a `role` and will
default to appropriate values for each platform.
2016-06-22 21:10:38 +00:00
2018-08-17 20:10:14 +00:00
Every menu item must have either a `role` , `label` , or in the case of a separator
a `type` .
2015-09-02 01:19:18 +00:00
The `role` property can have following values:
* `undo`
2020-05-21 22:38:26 +00:00
* `about` - Trigger a native about panel (custom message box on Window, which does not provide its own).
2015-09-02 01:19:18 +00:00
* `redo`
* `cut`
* `copy`
* `paste`
2017-12-28 05:22:39 +00:00
* `pasteAndMatchStyle`
* `selectAll`
2016-06-04 15:23:35 +00:00
* `delete`
2017-11-29 10:38:35 +00:00
* `minimize` - Minimize current window.
* `close` - Close current window.
2018-07-20 17:58:19 +00:00
* `quit` - Quit the application.
2017-11-29 10:38:35 +00:00
* `reload` - Reload the current window.
2017-12-28 05:22:39 +00:00
* `forceReload` - Reload the current window ignoring the cache.
* `toggleDevTools` - Toggle developer tools in the current window.
2019-07-31 21:13:56 +00:00
* `togglefullscreen` - Toggle full screen mode on the current window.
2017-12-28 05:22:39 +00:00
* `resetZoom` - Reset the focused page's zoom level to the original size.
* `zoomIn` - Zoom in the focused page by 10%.
* `zoomOut` - Zoom out the focused page by 10%.
2020-11-11 15:29:18 +00:00
* `toggleSpellChecker` - Enable/disable builtin spell checker.
2019-01-10 13:32:03 +00:00
* `fileMenu` - Whole default "File" menu (Close / Quit)
2017-11-29 10:38:35 +00:00
* `editMenu` - Whole default "Edit" menu (Undo, Copy, etc.).
2019-01-10 13:32:03 +00:00
* `viewMenu` - Whole default "View" menu (Reload, Toggle Developer Tools, etc.)
2019-02-25 13:34:32 +00:00
* `windowMenu` - Whole default "Window" menu (Minimize, Zoom, etc.).
2017-11-29 10:38:35 +00:00
The following additional roles are available on _macOS_ :
2019-01-10 13:32:03 +00:00
* `appMenu` - Whole default "App" menu (About, Services, etc.)
2017-11-29 10:38:35 +00:00
* `hide` - Map to the `hide` action.
2017-12-28 05:22:39 +00:00
* `hideOthers` - Map to the `hideOtherApplications` action.
2017-11-29 10:38:35 +00:00
* `unhide` - Map to the `unhideAllApplications` action.
2022-01-06 18:41:28 +00:00
* `showSubstitutions` - Map to the `orderFrontSubstitutionsPanel` action.
* `toggleSmartQuotes` - Map to the `toggleAutomaticQuoteSubstitution` action.
* `toggleSmartDashes` - Map to the `toggleAutomaticDashSubstitution` action.
* `toggleTextReplacement` - Map to the `toggleAutomaticTextReplacement` action.
2017-12-28 05:22:39 +00:00
* `startSpeaking` - Map to the `startSpeaking` action.
* `stopSpeaking` - Map to the `stopSpeaking` action.
2017-11-29 10:38:35 +00:00
* `front` - Map to the `arrangeInFront` action.
* `zoom` - Map to the `performZoom` action.
2017-12-28 05:22:39 +00:00
* `toggleTabBar` - Map to the `toggleTabBar` action.
* `selectNextTab` - Map to the `selectNextTab` action.
* `selectPreviousTab` - Map to the `selectPreviousTab` action.
* `mergeAllWindows` - Map to the `mergeAllWindows` action.
* `moveTabToNewWindow` - Map to the `moveTabToNewWindow` action.
2017-11-29 10:38:35 +00:00
* `window` - The submenu is a "Window" menu.
* `help` - The submenu is a "Help" menu.
2019-05-30 21:33:48 +00:00
* `services` - The submenu is a ["Services" ](https://developer.apple.com/documentation/appkit/nsapplication/1428608-servicesmenu?language=objc ) menu. This is only intended for use in the Application Menu and is *not* the same as the "Services" submenu used in context menus in macOS apps, which is not implemented in Electron.
2017-12-28 05:22:39 +00:00
* `recentDocuments` - The submenu is an "Open Recent" menu.
* `clearRecentDocuments` - Map to the `clearRecentDocuments` action.
2020-10-20 01:33:06 +00:00
* `shareMenu` - The submenu is [share menu][ShareMenu]. The `sharingItem` property must also be set to indicate the item to share.
2016-03-31 03:57:28 +00:00
2017-03-30 18:07:25 +00:00
When specifying a `role` on macOS, `label` and `accelerator` are the only
options that will affect the menu item. All other options will be ignored.
2017-12-28 05:22:39 +00:00
Lowercase `role` , e.g. `toggledevtools` , is still supported.
2016-04-20 01:12:37 +00:00
2020-03-31 04:06:25 +00:00
**Nota Bene:** The `enabled` and `visibility` properties are not available for top-level menu items in the tray on macOS.
2018-10-29 20:04:25 +00:00
2016-06-22 04:23:07 +00:00
### Instance Properties
2016-03-31 03:57:28 +00:00
2016-06-22 04:23:07 +00:00
The following properties are available on instances of `MenuItem` :
2016-03-31 03:57:28 +00:00
2019-04-24 16:53:15 +00:00
#### `menuItem.id`
2021-11-16 04:13:18 +00:00
A `string` indicating the item's unique id, this property can be
2019-04-24 16:53:15 +00:00
dynamically changed.
#### `menuItem.label`
2021-11-16 04:13:18 +00:00
A `string` indicating the item's visible label.
2019-04-24 16:53:15 +00:00
#### `menuItem.click`
A `Function` that is fired when the MenuItem receives a click event.
It can be called with `menuItem.click(event, focusedWindow, focusedWebContents)` .
2020-11-05 22:12:43 +00:00
2019-04-24 16:53:15 +00:00
* `event` [KeyboardEvent ](structures/keyboard-event.md )
* `focusedWindow` [BrowserWindow ](browser-window.md )
* `focusedWebContents` [WebContents ](web-contents.md )
#### `menuItem.submenu`
A `Menu` (optional) containing the menu
item's submenu, if present.
#### `menuItem.type`
2021-11-16 04:13:18 +00:00
A `string` indicating the type of the item. Can be `normal` , `separator` , `submenu` , `checkbox` or `radio` .
2019-04-24 16:53:15 +00:00
#### `menuItem.role`
2021-11-16 04:13:18 +00:00
A `string` (optional) indicating the item's role, if set. Can be `undo` , `redo` , `cut` , `copy` , `paste` , `pasteAndMatchStyle` , `delete` , `selectAll` , `reload` , `forceReload` , `toggleDevTools` , `resetZoom` , `zoomIn` , `zoomOut` , `toggleSpellChecker` , `togglefullscreen` , `window` , `minimize` , `close` , `help` , `about` , `services` , `hide` , `hideOthers` , `unhide` , `quit` , `startSpeaking` , `stopSpeaking` , `zoom` , `front` , `appMenu` , `fileMenu` , `editMenu` , `viewMenu` , `shareMenu` , `recentDocuments` , `toggleTabBar` , `selectNextTab` , `selectPreviousTab` , `mergeAllWindows` , `clearRecentDocuments` , `moveTabToNewWindow` or `windowMenu`
2019-04-24 16:53:15 +00:00
#### `menuItem.accelerator`
2021-06-29 23:28:16 +00:00
An `Accelerator` (optional) indicating the item's accelerator, if set.
#### `menuItem.userAccelerator` _Readonly_ _macOS_
An `Accelerator | null` indicating the item's [user-assigned accelerator ](https://developer.apple.com/documentation/appkit/nsmenuitem/1514850-userkeyequivalent?language=objc ) for the menu item.
**Note:** This property is only initialized after the `MenuItem` has been added to a `Menu` . Either via `Menu.buildFromTemplate` or via `Menu.append()/insert()` . Accessing before initialization will just return `null` .
2019-04-24 16:53:15 +00:00
#### `menuItem.icon`
2021-11-16 04:13:18 +00:00
A `NativeImage | string` (optional) indicating the
2019-04-24 16:53:15 +00:00
item's icon, if set.
#### `menuItem.sublabel`
2021-11-16 04:13:18 +00:00
A `string` indicating the item's sublabel.
2019-04-24 16:53:15 +00:00
2019-07-11 08:56:22 +00:00
#### `menuItem.toolTip` _macOS_
2021-11-16 04:13:18 +00:00
A `string` indicating the item's hover text.
2019-07-11 08:56:22 +00:00
2016-06-22 04:23:07 +00:00
#### `menuItem.enabled`
2016-03-31 03:57:28 +00:00
2021-11-16 04:13:18 +00:00
A `boolean` indicating whether the item is enabled, this property can be
2016-06-22 04:23:07 +00:00
dynamically changed.
2016-03-31 03:57:28 +00:00
2016-06-22 04:23:07 +00:00
#### `menuItem.visible`
2021-11-16 04:13:18 +00:00
A `boolean` indicating whether the item is visible, this property can be
2016-06-22 04:23:07 +00:00
dynamically changed.
#### `menuItem.checked`
2021-11-16 04:13:18 +00:00
A `boolean` indicating whether the item is checked, this property can be
2016-06-22 04:23:07 +00:00
dynamically changed.
A `checkbox` menu item will toggle the `checked` property on and off when
selected.
2016-03-31 03:57:28 +00:00
A `radio` menu item will turn on its `checked` property when clicked, and
2016-06-22 04:23:07 +00:00
will turn off that property for all adjacent items in the same menu.
You can add a `click` function for additional behavior.
2016-11-25 12:17:31 +00:00
2019-04-24 16:53:15 +00:00
#### `menuItem.registerAccelerator`
2016-11-25 12:17:31 +00:00
2021-11-16 04:13:18 +00:00
A `boolean` indicating if the accelerator should be registered with the
2020-01-20 06:44:22 +00:00
system or just displayed.
This property can be dynamically changed.
2016-11-25 12:17:31 +00:00
2020-10-20 01:33:06 +00:00
#### `menuItem.sharingItem` _macOS_
A `SharingItem` indicating the item to share when the `role` is `shareMenu` .
This property can be dynamically changed.
2019-04-24 16:53:15 +00:00
#### `menuItem.commandId`
2016-11-25 12:17:31 +00:00
2021-11-16 04:13:18 +00:00
A `number` indicating an item's sequential unique id.
2019-04-24 16:53:15 +00:00
#### `menuItem.menu`
A `Menu` that the item is a part of.
2020-10-20 01:33:06 +00:00
[ShareMenu]: https://developer.apple.com/design/human-interface-guidelines/macos/extensions/share-extensions/