import * as React from 'react';
import {useState, useEffect, useCallback} from 'react';
import type {ContextMenuPosition} from './types';
type Payload = {
shouldShow: boolean,
position: ContextMenuPosition | null,
hide: () => void,
};
export default function useContextMenu(anchorElementRef: {
current: React.ElementRef<any> | null,
}): Payload {
const [shouldShow, setShouldShow] = useState(false);
const [position, setPosition] = React.useState<ContextMenuPosition | null>(
null,
);
const hide = useCallback(() => {
setShouldShow(false);
setPosition(null);
}, []);
useEffect(() => {
const anchor = anchorElementRef.current;
if (anchor == null) return;
function handleAnchorContextMenu(e: MouseEvent) {
e.preventDefault();
e.stopPropagation();
const {pageX, pageY} = e;
const ownerDocument = anchor?.ownerDocument;
const portalContainer = ownerDocument?.querySelector(
'[data-react-devtools-portal-root]',
);
if (portalContainer == null) {
throw new Error(
"DevTools tooltip root node not found: can't display the context menu",
);
}
const {top: containerTop, left: containerLeft} =
portalContainer.getBoundingClientRect();
setShouldShow(true);
setPosition({x: pageX - containerLeft, y: pageY - containerTop});
}
anchor.addEventListener('contextmenu', handleAnchorContextMenu);
return () => {
anchor.removeEventListener('contextmenu', handleAnchorContextMenu);
};
}, [anchorElementRef]);
return {shouldShow, position, hide};
}