2021-09-14 15:31:30 +00:00
|
|
|
import { useCallback, useRef, useState } from 'react';
|
|
|
|
|
2022-10-28 10:07:58 +00:00
|
|
|
import { findAncestorByClass } from '../functions.web';
|
2021-09-14 15:31:30 +00:00
|
|
|
|
2022-10-06 10:09:40 +00:00
|
|
|
|
|
|
|
type RaiseContext = {
|
2021-09-14 15:31:30 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-06 10:09:40 +00:00
|
|
|
* The entity for which the menu is context menu is raised.
|
2021-09-14 15:31:30 +00:00
|
|
|
*/
|
2022-10-06 10:09:40 +00:00
|
|
|
entity?: string | Object;
|
2021-09-14 15:31:30 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-06 10:09:40 +00:00
|
|
|
* Target elements against which positioning calculations are made.
|
2021-09-14 15:31:30 +00:00
|
|
|
*/
|
2022-10-06 10:09:40 +00:00
|
|
|
offsetTarget?: HTMLElement | null;
|
|
|
|
};
|
2021-09-14 15:31:30 +00:00
|
|
|
|
2022-01-07 10:54:42 +00:00
|
|
|
const initialState = Object.freeze({});
|
2021-09-14 15:31:30 +00:00
|
|
|
|
2022-10-06 10:09:40 +00:00
|
|
|
const useContextMenu = (): [(force?: boolean | Object) => void,
|
|
|
|
(entity: string | Object, target: HTMLElement | null) => void,
|
|
|
|
(entity: string | Object) => (e: MouseEvent) => void,
|
|
|
|
() => void,
|
|
|
|
() => void,
|
|
|
|
RaiseContext] => {
|
2021-09-14 15:31:30 +00:00
|
|
|
const [ raiseContext, setRaiseContext ] = useState < RaiseContext >(initialState);
|
|
|
|
const isMouseOverMenu = useRef(false);
|
|
|
|
|
|
|
|
const lowerMenu = useCallback((force: boolean | Object = false) => {
|
|
|
|
/**
|
|
|
|
* We are tracking mouse movement over the active participant item and
|
|
|
|
* the context menu. Due to the order of enter/leave events, we need to
|
|
|
|
* defer checking if the mouse is over the context menu with
|
|
|
|
* queueMicrotask.
|
|
|
|
*/
|
|
|
|
window.queueMicrotask(() => {
|
|
|
|
if (isMouseOverMenu.current && !(force === true)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (raiseContext !== initialState) {
|
|
|
|
setRaiseContext(initialState);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, [ raiseContext ]);
|
|
|
|
|
2022-10-06 10:09:40 +00:00
|
|
|
const raiseMenu = useCallback((entity: string | Object, target: HTMLElement | null) => {
|
2021-09-14 15:31:30 +00:00
|
|
|
setRaiseContext({
|
|
|
|
entity,
|
|
|
|
offsetTarget: findAncestorByClass(target, 'list-item-container')
|
|
|
|
});
|
|
|
|
}, [ raiseContext ]);
|
|
|
|
|
|
|
|
const toggleMenu = useCallback((entity: string | Object) => (e: MouseEvent) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
const { entity: raisedEntity } = raiseContext;
|
|
|
|
|
|
|
|
if (raisedEntity && raisedEntity === entity) {
|
|
|
|
lowerMenu();
|
|
|
|
} else {
|
2022-10-06 10:09:40 +00:00
|
|
|
raiseMenu(entity, e.target as HTMLElement);
|
2021-09-14 15:31:30 +00:00
|
|
|
}
|
|
|
|
}, [ raiseContext ]);
|
|
|
|
|
|
|
|
const menuEnter = useCallback(() => {
|
|
|
|
isMouseOverMenu.current = true;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const menuLeave = useCallback(() => {
|
|
|
|
isMouseOverMenu.current = false;
|
|
|
|
lowerMenu();
|
|
|
|
}, [ lowerMenu ]);
|
|
|
|
|
|
|
|
return [ lowerMenu, raiseMenu, toggleMenu, menuEnter, menuLeave, raiseContext ];
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useContextMenu;
|