refactor(ContextMenu): remove unecessary functions

This commit is contained in:
Tim 2025-03-20 16:06:25 +01:00
parent 6a2054372f
commit 68cff0362c

View file

@ -33,6 +33,10 @@ const ContextMenu = ({ children, on, autoClose }: Props) => {
setActive(false);
};
const stopPropagation = (event: React.MouseEvent | React.TouchEvent) => {
event.stopPropagation();
};
const onContextMenu = (event: MouseEvent) => {
event.preventDefault();
const { clientX, clientY } = event;
@ -41,22 +45,10 @@ const ContextMenu = ({ children, on, autoClose }: Props) => {
setActive(true);
};
const onClickOutside = () => {
close();
};
const onClick = useCallback(() => {
autoClose && close();
}, [autoClose]);
const onMouseDown = (event: React.MouseEvent) => {
event.stopPropagation();
};
const onTouchStart = (event: React.TouchEvent) => {
event.stopPropagation();
};
useEffect(() => {
on.forEach((ref) => ref.current && ref.current.addEventListener('contextmenu', onContextMenu));
return () => on.forEach((ref) => ref.current && ref.current.removeEventListener('contextmenu', onContextMenu));
@ -65,15 +57,15 @@ const ContextMenu = ({ children, on, autoClose }: Props) => {
return active && createPortal((
<div
className={styles['context-menu-container']}
onMouseDown={onClickOutside}
onTouchStart={onClickOutside}
onMouseDown={close}
onTouchStart={close}
>
<div
ref={ref}
className={styles['context-menu']}
style={style}
onMouseDown={onMouseDown}
onTouchStart={onTouchStart}
onMouseDown={stopPropagation}
onTouchStart={stopPropagation}
onClick={onClick}
>
{children}