) => {
const { pageX, pageY } = e;
// TODO: We could do an initial render to see if the menu fits here
// and do a second render if it does not.
setOpenPosition({ x: pageX, y: pageY });
e.preventDefault();
e.stopPropagation();
},
[]
);
// Add click-away listeners when window is open
useEffect(() => {
if (openPosition == null) {
return;
}
document.addEventListener("click", handleGlobalClick);
document.body.addEventListener("contextmenu", closeMenu);
return () => {
document.removeEventListener("click", handleGlobalClick);
document.body.removeEventListener("contextmenu", closeMenu);
};
}, [openPosition, closeMenu, handleGlobalClick]);
return (
{renderContents()}
{children}
);
}