Skip to content

Commit

Permalink
Add onShow and onClose options (#158)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
hackal and sindresorhus authored Jun 21, 2022
1 parent a2027da commit d20fa56
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ declare namespace contextMenu {
dictionarySuggestions: MenuItemConstructorOptions[],
event: ElectronEvent
) => MenuItemConstructorOptions[];

/**
Called when the context menu is shown.
*/
readonly onShow?: (event: ElectronEvent) => void;

/**
Called when the context menu is closed.
*/
readonly onClose?: (event: ElectronEvent) => void;
}
}

Expand Down
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@ const create = (win, options) => {

if (menuTemplate.length > 0) {
const menu = electron.Menu.buildFromTemplate(menuTemplate);

if (typeof options.onShow === 'function') {
menu.on('menu-will-show', options.onShow);
}

if (typeof options.onClose === 'function') {
menu.on('menu-will-close', options.onClose);
}

menu.popup(win);
}
};
Expand Down
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,22 @@ Example for actions:
}
```

#### onShow

Type: `Function`

Called when the context menu is shown.

The function receives an [`Event` object](https://developer.mozilla.org/en-US/docs/Web/API/Event).

#### onClose

Type: `Function`

Called when the context menu is closed.

The function receives an [`Event` object](https://developer.mozilla.org/en-US/docs/Web/API/Event).

## Related

- [electron-util](https://github.com/sindresorhus/electron-util) - Useful utilities for developing Electron apps and modules
Expand Down

0 comments on commit d20fa56

Please sign in to comment.