import { useRef } from 'react'; export function useLongPress( onLong: (e: React.PointerEvent, el: HTMLElement) => void, ms = 500 ) { const timer = useRef(0); const clear = () => window.clearTimeout(timer.current); return { onPointerDown: (e: React.PointerEvent) => { 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(), }; }