{"id":13432605,"url":"https://github.com/sindresorhus/electron-context-menu","last_synced_at":"2025-05-12T09:23:54.415Z","repository":{"id":37961547,"uuid":"60405378","full_name":"sindresorhus/electron-context-menu","owner":"sindresorhus","description":"Context menu for your Electron app","archived":false,"fork":false,"pushed_at":"2025-02-06T15:33:44.000Z","size":308,"stargazers_count":1428,"open_issues_count":25,"forks_count":157,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-05-11T08:37:03.464Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","buy_me_a_coffee":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2016-06-04T10:36:38.000Z","updated_at":"2025-05-06T15:22:45.000Z","dependencies_parsed_at":"2022-08-09T01:00:08.637Z","dependency_job_id":"576df80c-5e28-4941-8e9c-636556b5e1fb","html_url":"https://github.com/sindresorhus/electron-context-menu","commit_stats":{"total_commits":161,"total_committers":53,"mean_commits":"3.0377358490566038","dds":"0.36024844720496896","last_synced_commit":"72da237f7bbc378a8e1918dd06f8b0438ea1aa86"},"previous_names":[],"tags_count":54,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Felectron-context-menu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Felectron-context-menu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Felectron-context-menu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Felectron-context-menu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/electron-context-menu/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253540671,"owners_count":21924523,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T02:01:14.067Z","updated_at":"2025-05-11T08:37:53.778Z","avatar_url":"https://github.com/sindresorhus.png","language":"JavaScript","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://buymeacoffee.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["JavaScript","Library","Tools"],"sub_categories":["UI","For Electron"],"readme":"# electron-context-menu\n\n\u003e Context menu for your [Electron](https://electronjs.org) app\n\n\u003cimg src=\"screenshot.png\" width=\"125\" align=\"right\"\u003e\n\nElectron doesn't have a built-in context menu. You're supposed to handle that yourself. But it's both tedious and hard to get right. This module gives you a nice extensible context menu with spellchecking and items like `Cut`/`Copy`/`Paste` for text, `Save Image` for images, and `Copy Link` for links. It also adds an `Inspect Element` menu item when in development to quickly view items in the inspector like in Chrome.\n\nThis package can only be used in the main process.\n\n## Install\n\n```sh\nnpm install electron-context-menu\n```\n\n*Requires Electron 30 or later.*\n\n## Usage\n\n```js\nimport {app, BrowserWindow} from 'electron';\nimport contextMenu from 'electron-context-menu';\n\ncontextMenu({\n\tshowSaveImageAs: true\n});\n\nlet mainWindow;\n(async () =\u003e {\n\tawait app.whenReady();\n\n\tmainWindow = new BrowserWindow();\n})();\n```\n\nAdvanced example:\n\n```js\nimport {app, BrowserWindow, shell} from 'electron';\nimport contextMenu from 'electron-context-menu';\n\ncontextMenu({\n\tprepend: (defaultActions, parameters, browserWindow) =\u003e [\n\t\t{\n\t\t\tlabel: 'Rainbow',\n\t\t\t// Only show it when right-clicking images\n\t\t\tvisible: parameters.mediaType === 'image'\n\t\t},\n\t\t{\n\t\t\tlabel: 'Search Google for “{selection}”',\n\t\t\t// Only show it when right-clicking text\n\t\t\tvisible: parameters.selectionText.trim().length \u003e 0,\n\t\t\tclick: () =\u003e {\n\t\t\t\tshell.openExternal(`https://google.com/search?q=${encodeURIComponent(parameters.selectionText)}`);\n\t\t\t}\n\t\t}\n\t]\n});\n\nlet mainWindow;\n(async () =\u003e {\n\tawait app.whenReady();\n\n\tmainWindow = new BrowserWindow();\n})();\n```\n\nThe return value of `contextMenu()` is a function that disposes of the created event listeners:\n\n```js\nconst dispose = contextMenu();\n\ndispose();\n```\n\n## API\n\n### contextMenu(options?)\n\nCreates a context menu and returns a dispose function.\n\n### options\n\nType: `object`\n\n#### window\n\nType: `BrowserWindow | BrowserView | WebViewTag | WebContents`\n\nWindow or WebView to add the context menu to.\n\nWhen not specified, the context menu will be added to all existing and new windows.\n\n#### prepend\n\nType: `Function`\n\nShould return an array of [`MenuItem`](https://electronjs.org/docs/api/menu-item/)'s to be prepended to the context menu.\n\nThe first argument is an array of default actions that can be used. The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu). The third argument is the [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for. The fourth argument is the context menu event.\n\n`MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in [`options.labels`](#labels).\n\n#### append\n\nType: `Function`\n\nShould return an array of [`MenuItem`](https://electronjs.org/docs/api/menu-item/)'s to be appended to the context menu.\n\nThe first argument is an array of default actions that can be used. The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu). The third argument is the [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for. The fourth argument is the context menu event.\n\n`MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in [`options.labels`](#labels).\n\n#### showLearnSpelling\n\nType: `boolean`\\\nDefault: `true`\n\nShow the `Learn Spelling {selection}` menu item when right-clicking text.\n\nThe spellcheck will only show when right-clicking misspelled words.\n\n#### showLookUpSelection\n\nType: `boolean`\\\nDefault: `true`\n\nShow the `Look Up {selection}` menu item when right-clicking text.\n\n#### showSearchWithGoogle\n\nType: `boolean`\\\nDefault: `true`\n\nShow the `Search with Google` menu item when right-clicking text.\n\n#### showSelectAll\n\nType: `boolean`\\\nDefault: `false` on macOS, `true` on Windows and Linux\n\nShow the `Select All` menu item when right-clicking in a window.\n\n#### showCopyImage\n\nType: `boolean`\\\nDefault: `true`\n\nShow the `Copy Image` menu item when right-clicking on an image.\n\n#### showCopyImageAddress\n\nType: `boolean`\\\nDefault: `false`\n\nShow the `Copy Image Address` menu item when right-clicking on an image.\n\n#### showSaveImage\n\nType: `boolean`\\\nDefault: `false`\n\nShow the `Save Image` menu item when right-clicking on an image.\n\n#### showSaveImageAs\n\nType: `boolean`\\\nDefault: `false`\n\nShow the `Save Image As…` menu item when right-clicking on an image.\n\n#### showCopyVideoAddress\n\nType: `boolean`\\\nDefault: `false`\n\nShow the `Copy Video Address` menu item when right-clicking on a video.\n\n#### showSaveVideo\n\nType: `boolean`\\\nDefault: `false`\n\nShow the `Save Video` menu item when right-clicking on a video.\n\n#### showSaveVideoAs\n\nType: `boolean`\\\nDefault: `false`\n\nShow the `Save Video As…` menu item when right-clicking on a video.\n\n#### showCopyLink\n\nType: `boolean`\\\nDefault: `true`\n\nShow the `Copy Link` menu item when right-clicking on a link.\n\n#### showSaveLinkAs\n\nType: `boolean`\\\nDefault: `false`\n\nShow the `Save Link As…` menu item when right-clicking on a link.\n\n#### showInspectElement\n\nType: `boolean`\\\nDefault: [Only in development](https://github.com/sindresorhus/electron-is-dev)\n\nForce enable or disable the `Inspect Element` menu item.\n\n#### showServices\n\nType: `boolean`\\\nDefault: `false`\n\nShow the system `Services` submenu when right-clicking text on macOS.\n\nNote: Due to [a bug in the Electron implementation](https://github.com/electron/electron/issues/18476), this menu is not identical to the \"Services\" submenu in the context menus of native apps. Instead, it looks the same as the \"Services\" menu in the main App Menu. For this reason, it is currently disabled by default.\n\n#### labels\n\nType: `object`\\\nDefault: `{}`\n\nOverride labels for the default menu items. Useful for i18n.\n\nThe placeholder `{selection}` may be used in any label, and will be replaced by the currently selected text, trimmed to a maximum of 25 characters length. This is useful when localizing the `Look Up “{selection}”` menu item, but can also be used in custom menu items, for example, to implement a `Search Google for “{selection}”` menu item. If there is no selection, the `{selection}` placeholder will be replaced by an empty string. Normally this placeholder is only useful for menu items which will only be shown when there is text selected. This can be checked using `visible: parameters.selectionText.trim().length \u003e 0` when implementing a custom menu item, as shown in the usage example above.\n\nFormat:\n\n```js\n{\n\tlabels: {\n\t\tcopy: 'Copiar',\n\t\tsaveImageAs: 'Guardar imagen como…',\n\t\tlookUpSelection: 'Consultar “{selection}”'\n\t}\n}\n```\n\n#### shouldShowMenu\n\nType: `Function`\n\nDetermines whether or not to show the menu. Can be useful if you for example have other code presenting a context menu in some contexts.\n\nThe second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents#event-context-menu).\n\nExample:\n\n```js\n{\n\t// Doesn't show the menu if the element is editable\n\tshouldShowMenu: (event, parameters) =\u003e !parameters.isEditable\n}\n```\n\n#### menu\n\nType: `Function`\n\nThis option lets you manually pick what menu items to include. It's meant for advanced needs. The default menu with the other options should be enough for most use-cases, and it ensures correct behavior, for example, correct order of menu items. So prefer the `append` and `prepend` option instead of `menu` whenever possible.\n\nThe function passed to this option is expected to return [`MenuItem[]`](https://electronjs.org/docs/api/menu-item/). The first argument the function receives is an array of default actions that can be used. These actions are functions that can take an object with a transform property (except for `separator` and `inspect`). The transform function will be passed the content of the action and can modify it if needed. If you use `transform` on `cut`, `copy`, or `paste`, they will convert rich text to plain text. The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu). The third argument is the [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for. The fourth argument is an Array of menu items for dictionary suggestions. This should be used if you wish to implement spellcheck in your custom menu. The last argument is the context menu event.\n\nEven though you include an action, it will still only be shown/enabled when appropriate. For example, the `saveImage` action is only shown when right-clicking an image.\n\n`MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in [`options.labels`](#labels).\n\nTo get spellchecking, “Correct Automatically”, and “Learn Spelling” in the menu, make sure you have not disabled the `spellcheck` option (it's `true` by default) in `BrowserWindow`.\n\nThe following options are ignored when `menu` is used:\n\n- `showLookUpSelection`\n- `showSearchWithGoogle`\n- `showSelectAll`\n- `showCopyImage`\n- `showCopyImageAddress`\n- `showSaveImageAs`\n- `showCopyVideoAddress`\n- `showSaveVideo`\n- `showSaveVideoAs`\n- `showCopyLink`\n- `showSaveLinkAs`\n- `showInspectElement`\n- `showServices`\n\nDefault actions:\n\n- `spellCheck`\n- `learnSpelling`\n- `separator`\n- `lookUpSelection`\n- `searchWithGoogle`\n- `cut`\n- `copy`\n- `paste`\n- `selectAll`\n- `saveImage`\n- `saveImageAs`\n- `saveVideo`\n- `saveVideoAs`\n- `copyImage`\n- `copyImageAddress`\n- `copyVideoAddress`\n- `copyLink`\n- `saveLinkAs`\n- `inspect`\n- `services`\n\nExample for actions:\n\n```js\n{\n\tmenu: (actions, props, browserWindow, dictionarySuggestions) =\u003e [\n\t\t...dictionarySuggestions,\n\t\tactions.separator(),\n\t\tactions.copyLink({\n\t\t\ttransform: content =\u003e `modified_link_${content}`\n\t\t}),\n\t\tactions.separator(),\n\t\t{\n\t\t\tlabel: 'Unicorn'\n\t\t},\n\t\tactions.separator(),\n\t\tactions.copy({\n\t\t\ttransform: content =\u003e `modified_copy_${content}`\n\t\t}),\n\t\t{\n\t\t\tlabel: 'Invisible',\n\t\t\tvisible: false\n\t\t},\n\t\tactions.paste({\n\t\t\ttransform: content =\u003e `modified_paste_${content}`\n\t\t})\n\t]\n}\n```\n\n#### onShow\n\nType: `Function`\n\nCalled when the context menu is shown.\n\nThe function receives an [`Event` object](https://developer.mozilla.org/en-US/docs/Web/API/Event).\n\n#### onClose\n\nType: `Function`\n\nCalled when the context menu is closed.\n\nThe function receives an [`Event` object](https://developer.mozilla.org/en-US/docs/Web/API/Event).\n\n## Related\n\n- [electron-util](https://github.com/sindresorhus/electron-util) - Useful utilities for developing Electron apps and modules\n- [electron-debug](https://github.com/sindresorhus/electron-debug) - Adds useful debug features to your Electron app\n- [electron-store](https://github.com/sindresorhus/electron-store) - Save and load data like user settings, app state, cache, etc\n- [electron-reloader](https://github.com/sindresorhus/electron-reloader) - Simple auto-reloading for Electron apps during development\n- [electron-serve](https://github.com/sindresorhus/electron-serve) - Static file serving for Electron apps\n- [electron-unhandled](https://github.com/sindresorhus/electron-unhandled) - Catch unhandled errors and promise rejections in your Electron app\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Felectron-context-menu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Felectron-context-menu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Felectron-context-menu/lists"}