waveterm/frontend/app/hook/useLongClick.tsx
Mike Sawka 4be8a1e37e
move codeedit to directory. new useLongClick hook. show quick navig… (#105)
move codeedit to directory. new useLongClick hook. show quick navigation for directoryview on longclick of folder icon. lots more generic
stuff for header
new fitaddon for xtermjs
more fixes for xtermjs scrollbars
2024-07-08 23:13:12 -07:00

57 lines
1.7 KiB
TypeScript

import { useCallback, useEffect, useRef, useState } from "react";
export const useLongClick = (ref, onClick, onLongClick, ms = 300) => {
const timerRef = useRef(null);
const [longClickTriggered, setLongClickTriggered] = useState(false);
const startPress = useCallback(
(e: React.MouseEvent<any>) => {
if (onLongClick == null) {
return;
}
setLongClickTriggered(false);
timerRef.current = setTimeout(() => {
setLongClickTriggered(true);
onLongClick?.(e);
}, ms);
},
[onLongClick, ms]
);
const stopPress = useCallback(() => {
clearTimeout(timerRef.current);
}, []);
const handleClick = useCallback(
(e: React.MouseEvent<any>) => {
if (longClickTriggered) {
event.preventDefault();
event.stopPropagation();
return;
}
onClick?.(e);
},
[longClickTriggered, onClick]
);
useEffect(() => {
const element = ref.current;
if (!element) return;
element.addEventListener("mousedown", startPress);
element.addEventListener("mouseup", stopPress);
element.addEventListener("mouseleave", stopPress);
element.addEventListener("click", handleClick);
return () => {
element.removeEventListener("mousedown", startPress);
element.removeEventListener("mouseup", stopPress);
element.removeEventListener("mouseleave", stopPress);
element.removeEventListener("click", handleClick);
};
}, [ref.current, startPress, stopPress, handleClick]);
return ref;
};