Files
dsp-thomann-interface/src/hooks/useLongPress.ts
T
2026-10-26 15:27:04 +01:00

22 lines
764 B
TypeScript

import { useRef } from 'react';
export function useLongPress(
onLong: (e: React.PointerEvent, el: HTMLElement) => void,
ms = 500
) {
const timer = useRef<number>(0);
const clear = () => window.clearTimeout(timer.current);
return {
onPointerDown: (e: React.PointerEvent<HTMLElement>) => {
if (e.pointerType !== 'touch') return; // mouse keeps double-click
const el = e.currentTarget; // currentTarget is null inside a timeout
clear();
timer.current = window.setTimeout(() => onLong(e, el), ms);
},
onPointerUp: clear,
onPointerLeave: clear,
onPointerCancel: clear, // fires when the user scrolls
onContextMenu: (e: React.MouseEvent) => e.preventDefault(),
};
}