Context Menu Feature (#410)

* create `ContextMenuContext` component
This commit is contained in:
asharonbaltazar
2023-05-22 15:48:25 -04:00
committed by GitHub
parent 8ed2aa79a7
commit f82d4821f9
5 changed files with 133 additions and 11 deletions
@@ -0,0 +1,23 @@
import { MouseEvent, useContext } from 'react';
import { ContextMenuContext, Option } from '../context/ContextMenuContext';
export const useContextMenu = <T extends HTMLElement>(options: Option[]) => {
const contextMenuContext = useContext(ContextMenuContext);
if (contextMenuContext === null) {
throw new Error('useContextMenu should be wrapped by ContextMenuProvider');
}
const { createContextMenu } = contextMenuContext;
const localCreateContextMenu = (contextMenuEvent: MouseEvent<T, globalThis.MouseEvent>) => {
// prevent browser default context menu from showing up
contextMenuEvent.preventDefault();
const { pageX, pageY } = contextMenuEvent;
return createContextMenu(options, { x: pageX, y: pageY });
};
return [localCreateContextMenu];
};