Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 50x 8x 8x 24x 21x 19x 19x 19x 19x 21x 21x 11x 11x 11x 24x 10x 9x 9x 9x 9x 9x 9x 2x 7x 24x 6x 5x 5x 4x 4x 4x 3x 2x 24x 16x 24x 16x 15x 24x 4x 24x 4x 24x 1x 24x 6x 6x 24x 2x 2x 24x 3x 3x 24x 24x 22x | import { useCallback, useEffect, useMemo, useRef } from "react";
import { useLatest } from "@usefy/use-latest";
import type {
LongPressCallback,
LongPressCancelReason,
LongPressEvent,
UseLongPressOptions,
UseLongPressReturn,
} from "./types";
import {
DEFAULT_MOVE_THRESHOLD,
DEFAULT_THRESHOLD,
TOUCH_MOUSE_GUARD_MS,
getDistance,
getPositionFromEvent,
now,
} from "./utils";
/**
* Recognise a **long-press** ("press and hold") gesture on any element, for both
* mouse and touch, and get back a `bind` object of DOM handler props to spread
* onto the target.
*
* The `callback` fires exactly once, when the pointer has been held down on the
* element for at least `threshold` milliseconds without being released or moved
* too far. Releasing early, or dragging farther than `moveThreshold` pixels from
* the down point before the threshold elapses, cancels the gesture.
*
* ### Usage
* ```tsx
* const bind = useLongPress(() => console.log("held!"));
* return <button {...bind}>Press and hold</button>;
* ```
*
* ### Behaviour & guarantees
* - **Mouse + touch** — the same gesture works with either input. Browsers emit
* synthetic `mouse*` events after a touch sequence; those are detected by
* timestamp ({@link TOUCH_MOUSE_GUARD_MS}) and ignored, so a single touch
* long-press never double-fires through the mouse handlers.
* - **Movement cancellation** — the down position is captured from the down
* event and compared on every move; drifting past `moveThreshold` cancels with
* reason `"moved"`. Pass `moveThreshold: false` to disable this.
* - **Lifecycle callbacks** — optional `onStart` (press began), `onFinish` (a
* completed long press was then released) and `onCancel` (ended early, with a
* `{ reason }`). All are kept in latest-refs, so passing new inline functions
* every render never destabilises the returned handlers or re-runs effects.
* - **Stable handlers** — every handler is `useCallback`-wrapped and the `bind`
* object is `useMemo`-ised, so its identity never changes; spreading it does
* not churn the element's listeners.
* - **Cleanup** — the pending threshold timer is always cleared on release,
* cancel, and unmount, so nothing fires after the element is gone.
* - **SSR-safe** — the hook only returns handlers and touches no
* `window`/`document` at module or render time.
* - **StrictMode / concurrent-safe** — user callbacks are dispatched from the
* event handlers (never from inside a `setState` updater), and there is no
* render-time side effect to double-run.
*
* ### `preventDefault` caveat
* React attaches `onTouchStart`/`onTouchMove` as **passive** listeners, so you
* cannot call `event.preventDefault()` from these handlers to stop scrolling or
* the long-press context menu — the browser ignores it (and may warn). Prevent
* those natively with CSS on the target instead: `touch-action: none` (stop
* scroll/pan) and `user-select: none` / `-webkit-touch-callout: none` (stop text
* selection and the iOS callout). This hook deliberately does not call
* `preventDefault`.
*
* @typeParam T - The element type you spread the `bind` object onto.
* @param callback - Fires once when the press reaches `threshold`. Receives the
* originating pointer-down event.
* @param options - {@link UseLongPressOptions}.
* @returns A stable `bind` object of DOM handler props — see {@link
* UseLongPressReturn}.
*
* @example
* ```tsx
* // Basic press-and-hold.
* function DeleteButton() {
* const bind = useLongPress(() => deleteItem(), { threshold: 600 });
* return <button {...bind}>Hold to delete</button>;
* }
* ```
*
* @example
* ```tsx
* // Full lifecycle with movement cancellation disabled.
* const bind = useLongPress(
* (event) => console.log("long press", event.type),
* {
* threshold: 500,
* moveThreshold: 20,
* onStart: () => setState("pressing"),
* onFinish: () => setState("done"),
* onCancel: (_event, { reason }) => setState(`cancelled: ${reason}`),
* }
* );
* ```
*/
export function useLongPress<T extends Element = Element>(
callback: LongPressCallback<T>,
options: UseLongPressOptions<T> = {}
): UseLongPressReturn<T> {
const {
threshold = DEFAULT_THRESHOLD,
moveThreshold = DEFAULT_MOVE_THRESHOLD,
disabled = false,
onStart,
onFinish,
onCancel,
} = options;
// Latest-refs: read fresh values inside the stable handlers without
// re-creating them (and without callers needing to memoize).
const callbackRef = useLatest(callback);
const thresholdRef = useLatest(threshold);
const moveThresholdRef = useLatest(moveThreshold);
const disabledRef = useLatest(disabled);
const onStartRef = useLatest(onStart);
const onFinishRef = useLatest(onFinish);
const onCancelRef = useLatest(onCancel);
// Mutable per-gesture state (no re-renders).
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const startPosRef = useRef<{ x: number; y: number } | null>(null);
const pressingRef = useRef(false); // a press is currently in progress
const triggeredRef = useRef(false); // the threshold fired for this press
const lastTouchRef = useRef(0); // timestamp of the most recent touch event
const clearTimer = useCallback(() => {
if (timerRef.current !== null) {
clearTimeout(timerRef.current);
timerRef.current = null;
}
}, []);
const start = useCallback(
(event: LongPressEvent<T>) => {
// Disabled, or a press is already tracking (ignore duplicate downs).
if (disabledRef.current || pressingRef.current) return;
pressingRef.current = true;
triggeredRef.current = false;
startPosRef.current = getPositionFromEvent(event);
onStartRef.current?.(event);
clearTimer();
timerRef.current = setTimeout(() => {
timerRef.current = null;
triggeredRef.current = true;
callbackRef.current(event);
}, thresholdRef.current);
},
[clearTimer, callbackRef, disabledRef, onStartRef, thresholdRef]
);
const end = useCallback(
(event: LongPressEvent<T>, reason: LongPressCancelReason) => {
if (!pressingRef.current) return;
const wasTriggered = triggeredRef.current;
pressingRef.current = false;
triggeredRef.current = false;
startPosRef.current = null;
clearTimer();
if (wasTriggered) {
// A completed long press that is now being released/ended.
onFinishRef.current?.(event);
} else {
// Ended before the threshold — a cancellation.
onCancelRef.current?.(event, { reason });
}
},
[clearTimer, onFinishRef, onCancelRef]
);
const move = useCallback(
(event: LongPressEvent<T>) => {
// Only relevant while pressing and before the threshold has fired.
if (!pressingRef.current || triggeredRef.current) return;
const limit = moveThresholdRef.current;
if (limit === false) return; // movement cancellation disabled
const startPos = startPosRef.current;
const pos = getPositionFromEvent(event);
if (!startPos || !pos) return;
if (getDistance(startPos, pos) > limit) {
end(event, "moved");
}
},
[end, moveThresholdRef]
);
// Ignore browser-synthesised mouse events that follow a touch sequence.
const isSyntheticMouse = useCallback(
() => now() - lastTouchRef.current < TOUCH_MOUSE_GUARD_MS,
[]
);
const onMouseDown = useCallback(
(event: LongPressEvent<T>) => {
if (isSyntheticMouse()) return;
start(event);
},
[start, isSyntheticMouse]
);
const onMouseMove = useCallback(
(event: LongPressEvent<T>) => {
move(event);
},
[move]
);
const onMouseUp = useCallback(
(event: LongPressEvent<T>) => {
end(event, "released");
},
[end]
);
const onMouseLeave = useCallback(
(event: LongPressEvent<T>) => {
end(event, "released");
},
[end]
);
const onTouchStart = useCallback(
(event: LongPressEvent<T>) => {
lastTouchRef.current = now();
start(event);
},
[start]
);
const onTouchMove = useCallback(
(event: LongPressEvent<T>) => {
lastTouchRef.current = now();
move(event);
},
[move]
);
const onTouchEnd = useCallback(
(event: LongPressEvent<T>) => {
lastTouchRef.current = now();
end(event, "released");
},
[end]
);
// Clear any pending timer on unmount (covers StrictMode's double mount too).
useEffect(() => clearTimer, [clearTimer]);
return useMemo<UseLongPressReturn<T>>(
() => ({
onMouseDown,
onMouseUp,
onMouseLeave,
onMouseMove,
onTouchStart,
onTouchEnd,
onTouchMove,
}),
[
onMouseDown,
onMouseUp,
onMouseLeave,
onMouseMove,
onTouchStart,
onTouchEnd,
onTouchMove,
]
) as UseLongPressReturn<T>;
}
|