| 1 |
import { LONG_PRESS_DURATION } from "@point_of_sale/utils"; |
| 2 |
|
| 3 |
export function useLongPress(callback, delay = LONG_PRESS_DURATION) { |
| 4 |
let timer = null; |
| 5 |
|
| 6 |
function startLongPress(params) { |
| 7 |
timer = setTimeout(() => { |
| 8 |
callback(params); |
| 9 |
}, delay); |
| 10 |
} |
| 11 |
|
| 12 |
function cancelLongPress() { |
| 13 |
if (timer) { |
| 14 |
clearTimeout(timer); |
| 15 |
timer = null; |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
return { |
| 20 |
onMouseDown(event, params) { |
| 21 |
if (event.button === 0) { |
| 22 |
startLongPress(params); |
| 23 |
} |
| 24 |
}, |
| 25 |
onMouseUp: cancelLongPress, |
| 26 |
onTouchStart(params) { |
| 27 |
startLongPress(params); |
| 28 |
}, |
| 29 |
onTouchEnd: cancelLongPress, |
| 30 |
onScroll: cancelLongPress, |
| 31 |
}; |
| 32 |
} |