Menu | Electron
https://www.electronjs.org/docs/latest/api/menu • 50 KB fetched Open original page
Menu | Electron
Skip to main content
Electron Docs API Blog Tools
* Electron Forge
* Electron Fiddle
Community
* Governance
* Showcase
* Resources
Releases English
* English
* Deutsch
* Español
* Français
* 日本語
* Português
* Русский
* 中文
Search
* Main Process Modules
* app
* autoUpdater
* BaseWindow
* BrowserView
* Deprecated
* BrowserWindow
* clipboard
* contentTracing
* crashReporter
* desktopCapturer
* dialog
* globalShortcut
* ImageView
* inAppPurchase
* ipcMain
* Menu
* MenuItem
* MessageChannelMain
* MessagePortMain
* nativeImage
* nativeTheme
* net
* netLog
* Notification
* powerMonitor
* powerSaveBlocker
* process
* protocol
* pushNotifications
* safeStorage
* screen
* session
* sharedTexture
* ShareMenu
* shell
* systemPreferences
* TouchBar
* Tray
* utilityProcess
* webContents
* WebContentsView
* webFrameMain
* View
* Renderer Process Modules
* Utility Process Modules
* Custom DOM Elements
* Chromium and Node.js
* Classes
* API Structures
*
* Main Process Modules
* Menu On this page
Menu
Class: Menu
Create application menus and context menus.
Process: Main
The presentation of menus varies depending on the operating system:
* Under Windows and Linux, menus are visually similar to Chromium.
* Under macOS, these will be native menus.
tip
See also: A detailed guide about how to implement menus in your application .
warning
Electron's built-in classes cannot be subclassed in user code.
For more information, see the FAQ .
new Menu()
Creates a new menu.
Static Methods
The Menu class has the following static methods:
Menu.setApplicationMenu(menu)
* menu Menu | null
Sets menu as the application menu on macOS. On Windows and Linux, the
menu will be set as each window's top menu.
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
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 preceding & . For example, &&File would result in &File displayed on the button label.
Passing null will suppress the default menu. On Windows and Linux,
this has the additional effect of removing the menu bar from the window.
note
The default menu will be created automatically if the app does not set one.
It contains standard items such as File , Edit , View , and Window .
Menu.getApplicationMenu()
Returns Menu | null - The application menu, if set, or null , if not set.
note
The returned Menu instance doesn't support dynamic addition or
removal of menu items. Instance properties can still
be dynamically modified.
Menu.sendActionToFirstResponder(action) macOS
* action string
Sends the action to the first responder of application. This is used for
emulating default macOS menu behaviors. Usually you would use the
role property of a MenuItem .
See the macOS Cocoa Event Handling Guide
for more information on macOS' native actions.
Menu.buildFromTemplate(template)
* template ( MenuItemConstructorOptions | MenuItem )[]
Returns Menu
Generally, the template is an array of options for constructing a
MenuItem . The usage can be referenced above.
You can also attach other fields to the element of the template and they will become properties of the constructed menu items.
Instance Methods
The menu object has the following instance methods:
menu.popup([options])
* options Object (optional)
* window BaseWindow (optional) - Default is the focused window.
* frame WebFrameMain (optional) - Provide the relevant frame
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 on a WebContents, or the focusedFrame property of a WebContents.
* x number (optional) - Default is the current mouse cursor position.
Must be declared if y is declared.
* y number (optional) - Default is the current mouse cursor position.
Must be declared if x is declared.
* positioningItem number (optional) macOS - The index of the menu item to
be positioned under the mouse cursor at the specified coordinates. Default
is -1.
* sourceType string (optional) Windows Linux - This should map to the menuSourceType
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 .
* callback Function (optional) - Called when menu is closed.
Pops up this menu as a context menu in the BaseWindow .
tip
For more details, see the Context Menu guide.
menu.closePopup([window])
* window BaseWindow (optional) - Default is the focused window.
Closes the context menu in the window .
menu.append(menuItem)
* menuItem MenuItem
Appends the menuItem to the menu.
menu.getMenuItemById(id)
* id string
Returns MenuItem | null - the item with the specified id
menu.insert(pos, menuItem)
* pos Integer
* menuItem MenuItem
Inserts the menuItem to the pos position of the menu.
Instance Events
Objects created with new Menu or returned by Menu.buildFromTemplate emit the following events:
note
Some events are only available on specific operating systems and are
labeled as such.
Event: 'menu-will-show'
Returns:
* event Event
Emitted when menu.popup() is called.
Event: 'menu-will-close'
Returns:
* event Event
Emitted when a popup is closed either manually or with menu.closePopup() .
Instance Properties
menu objects also have the following properties:
menu.items
A MenuItem[] array containing the menu's items.
Each Menu consists of multiple MenuItem instances and each MenuItem
can nest a Menu into its submenu property.
Edit this page
Previous
ipcMain
Next
MenuItem
* Class: Menu
* new Menu()
* Static Methods
* setApplicationMenu
* getApplicationMenu
* sendActionToFirstResponder
* buildFromTemplate
* Instance Methods
* popup
* closePopup
* append
* getMenuItemById
* insert
* Instance Events
* 'menu-will-show'
* 'menu-will-close'
* Instance Properties
* items
Docs
* Getting Started
* API Reference
Checklists
* Performance
* Security
Tools
* Electron Forge
* Electron Fiddle
Community
* Governance
* Resources
* Discord
* Bluesky
* X
* Mastodon
* Stack Overflow
More
* GitHub
* Open Collective
* Infrastructure Dashboard
Copyright OpenJS Foundation and Electron contributors. All rights reserved. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation , please see our Trademark Policy and Trademark List . Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. The OpenJS Foundation | Terms of Use | Privacy Policy | Bylaws | Code of Conduct | Trademark Policy | Trademark List | Cookie Policy
Hosting and infrastructure graciously provided by
Links found on this page
- Skip to main content [direct]
- Electron [direct]
- Docs [direct]
- API [direct]
- Blog [direct]
- Electron Forge [direct]
- Electron Fiddle [direct]
- Governance [direct]
- Showcase [direct]
- Resources [direct]
- Releases [direct]
- Deutsch [direct]
- Español [direct]
- Français [direct]
- 日本語 [direct]
- Português [direct]
- Русский [direct]
- 中文 [direct]
- autoUpdater [direct]
- BaseWindow [direct]
- BrowserView Deprecated [direct]
- BrowserWindow [direct]
- clipboard [direct]
- contentTracing [direct]
- crashReporter [direct]
- desktopCapturer [direct]
- dialog [direct]
- globalShortcut [direct]
- ImageView [direct]
- inAppPurchase [direct]
- ipcMain [direct]
- MenuItem [direct]
- MessageChannelMain [direct]
- MessagePortMain [direct]
- nativeImage [direct]
- nativeTheme [direct]
- net [direct]
- netLog [direct]
- Notification [direct]
- powerMonitor [direct]
- powerSaveBlocker [direct]
- process [direct]
- protocol [direct]
- pushNotifications [direct]
- safeStorage [direct]
- screen [direct]
- session [direct]
- sharedTexture [direct]
- ShareMenu [direct]
- shell [direct]
- systemPreferences [direct]
- TouchBar [direct]
- Tray [direct]
- utilityProcess [direct]
- webContents [direct]
- WebContentsView [direct]
- webFrameMain [direct]
- View [direct]
- Custom DOM Elements [direct]
- Chromium and Node.js [direct]
- Classes [direct]
- API Structures [direct]
- Main [direct]
- A detailed guide about how to implement menus in your application [direct]
- the FAQ [direct]
- macOS Cocoa Event Handling Guide [direct]
- Context Menu [direct]
- Edit this page [direct]
- Performance [direct]
- Security [direct]
- Discord [direct]
- Bluesky [direct]
- X [direct]
- Mastodon [direct]
- Stack Overflow [direct]
- GitHub [direct]
- Open Collective [direct]
- Infrastructure Dashboard [direct]
- OpenJS Foundation [direct]
- Trademark Policy [direct]
|
|