| 1 |
import { onMounted, onWillUnmount } from "@odoo/owl"; |
| 2 |
|
| 3 |
/** |
| 4 |
* `useTimedPress` — A hook to detect and respond to different press durations on a DOM element. |
| 5 |
* |
| 6 |
* It supports two types of interactions: |
| 7 |
* - `"release"` (default): Triggers the callback **after the pointer is released**, if the duration falls within the defined delay range. |
| 8 |
* - `"hold"`: Triggers the callback after a given delay **while the pointer is held down**. |
| 9 |
* |
| 10 |
* This hook is compatible with mouse, touch, and stylus inputs via `pointer` events. |
| 11 |
* |
| 12 |
* @param {Ref} ref - An OWL `useRef` pointing to the target DOM element. |
| 13 |
* @param {Array<Object>} ranges - An array of press range objects defining when and how to trigger callbacks. |
| 14 |
* Each object supports the following properties: |
| 15 |
* @param {number} [ranges[].delay=0] - Minimum duration in milliseconds before the callback can be triggered. |
| 16 |
* @param {number} [ranges[].maxDelay] - Optional maximum duration; if specified, the callback is only triggered if the press duration is less than this value. |
| 17 |
* @param {Function} ranges[].callback - The function to execute. It receives the original pointer event and the press duration in milliseconds (for `"release"`). |
| 18 |
* Signature: `(event: PointerEvent, duration: number) => void` |
| 19 |
* @param {string} [ranges[].type="release"] - Determines when to trigger the callback: |
| 20 |
* - `"hold"`: triggers while holding the press after `delay` |
| 21 |
* - `"release"`: triggers after release if the press duration is within `[delay, maxDelay)` |
| 22 |
* |
| 23 |
* @example |
| 24 |
* useTimedPress(myRef, [ |
| 25 |
* { |
| 26 |
* delay: 600, |
| 27 |
* callback: (e, duration) => console.log("Long press while holding"), |
| 28 |
* type: "hold", |
| 29 |
* }, |
| 30 |
* { |
| 31 |
* delay: 0, |
| 32 |
* maxDelay: 200, |
| 33 |
* callback: (e, duration) => console.log("Tap released", duration), |
| 34 |
* type: "release", |
| 35 |
* }, |
| 36 |
* { |
| 37 |
* delay: 600, |
| 38 |
* callback: (e, duration) => console.log("Long press released", duration), |
| 39 |
* type: "release", |
| 40 |
* }, |
| 41 |
* ]); |
| 42 |
*/ |
| 43 |
export function useTimedPress(ref, ranges = []) { |
| 44 |
let timerStart = null; |
| 45 |
let holdTimers = []; |
| 46 |
|
| 47 |
const handlePointerDown = (event) => { |
| 48 |
if (event.button !== 0) { |
| 49 |
return; |
| 50 |
} |
| 51 |
timerStart = performance.now(); |
| 52 |
|
| 53 |
for (const { delay = 0, type = "release", callback } of ranges) { |
| 54 |
if (type === "hold" && typeof callback === "function") { |
| 55 |
const timer = setTimeout(() => { |
| 56 |
callback(event, delay); |
| 57 |
}, delay); |
| 58 |
holdTimers.push(timer); |
| 59 |
} |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
const handlePointerUp = (event) => { |
| 64 |
if (timerStart === null) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
const elapsed = performance.now() - timerStart; |
| 69 |
timerStart = null; |
| 70 |
clearAllHoldTimers(); |
| 71 |
|
| 72 |
for (const { delay = 0, maxDelay, type = "release", callback } of ranges) { |
| 73 |
if (type === "release" && typeof callback === "function") { |
| 74 |
if (elapsed >= delay && (maxDelay === undefined || elapsed < maxDelay)) { |
| 75 |
callback(event, elapsed); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
const cancel = () => { |
| 82 |
timerStart = null; |
| 83 |
clearAllHoldTimers(); |
| 84 |
}; |
| 85 |
|
| 86 |
const clearAllHoldTimers = () => { |
| 87 |
for (const timer of holdTimers) { |
| 88 |
clearTimeout(timer); |
| 89 |
} |
| 90 |
holdTimers = []; |
| 91 |
}; |
| 92 |
|
| 93 |
onMounted(() => { |
| 94 |
const el = ref.el; |
| 95 |
el?.addEventListener("pointerdown", handlePointerDown); |
| 96 |
el?.addEventListener("pointerup", handlePointerUp); |
| 97 |
el?.addEventListener("pointerleave", cancel); |
| 98 |
el?.addEventListener("pointercancel", cancel); |
| 99 |
}); |
| 100 |
|
| 101 |
onWillUnmount(() => { |
| 102 |
const el = ref.el; |
| 103 |
el?.removeEventListener("pointerdown", handlePointerDown); |
| 104 |
el?.removeEventListener("pointerup", handlePointerUp); |
| 105 |
el?.removeEventListener("pointerleave", cancel); |
| 106 |
el?.removeEventListener("pointercancel", cancel); |
| 107 |
}); |
| 108 |
} |