Skip to content

Instantly share code, notes, and snippets.

@dperetti
Created February 24, 2020 23:55
Show Gist options
  • Save dperetti/4ca56bf428fe6cac0188cda4443bc088 to your computer and use it in GitHub Desktop.
Save dperetti/4ca56bf428fe6cac0188cda4443bc088 to your computer and use it in GitHub Desktop.
Improved react-electron-contextmenu
import * as React from 'react'
import { remote } from 'electron'
const { Menu } = remote
export interface Props<T> {
menuItemsFactory: (data: T) => Electron.MenuItemConstructorOptions[];
data: T;
style?: any;
}
export class ContextMenuArea<T> extends React.Component<Props<T>> {
private _rootElement: HTMLDivElement | null = null
componentDidMount() {
this._rootElement!.addEventListener(
'contextmenu',
e => {
e.preventDefault()
const menu = Menu.buildFromTemplate(this.props.menuItemsFactory(this.props.data))
menu.popup({
window: remote.getCurrentWindow(),
})
},
false
)
}
render() {
return (
<div style={{ ...this.props.style }} ref={ref => (this._rootElement = ref)}>
{this.props.children}
</div>
)
}
}
export default ContextMenuArea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment