0 dependencies
npm install use-electron-context-menuNote: If you prefer yarn instead of npm, just use yarn add use-electron-context-menu.
import React, { useRef } from "react"
import useContextMenu from "use-electron-context-menu"
export default function App() {
const ref = useRef(null)
useContextMenu(ref, [
{
label: "Copy",
click: () => console.log("copied")
}
])
return (
<div ref={ref} style={{ width: 200, height: 100 }}>
{/* right clicking this area will spawn the context menu */}
</div>
)
}const { closeMenu } = useContextMenu(ref, menuItems, options)ref: React element you want to add the context menu tomenuItems: An array of Electron menu itemsoptions: (optional) An object of options for this hook
closeMenu(): Force the context menu to close
x: Display context menu at fixed x coordinatey: Display context menu at fixed y coordinateonContext(event): Called when context menu opensonClose(): Called when context menu closes