|
| 1 | +import { useEffect, useRef } from 'react'; |
| 2 | +import { Position2D } from './interaction-modifier'; |
| 3 | + |
| 4 | +interface UseLongPressOptions { |
| 5 | + delay?: number; |
| 6 | + movementThreshold?: number; |
| 7 | +} |
| 8 | + |
| 9 | +const useLongPress = ( |
| 10 | + onLongPress: (position: Position2D) => void, |
| 11 | + options: UseLongPressOptions = {} |
| 12 | +) => { |
| 13 | + const { delay: LONG_PRESS_DELAY = 500, movementThreshold = 35 } = options; |
| 14 | + |
| 15 | + const touchMoved = useRef<boolean>(false); |
| 16 | + const longPressTimer = useRef<NodeJS.Timeout | null>(null); |
| 17 | + const touchStartPosition = useRef<Position2D | null>(null); |
| 18 | + const longPressTriggered = useRef<boolean>(false); |
| 19 | + |
| 20 | + const onTouchStart = (event: React.TouchEvent) => { |
| 21 | + touchMoved.current = false; |
| 22 | + longPressTriggered.current = false; |
| 23 | + const touch = event.touches[0]; |
| 24 | + touchStartPosition.current = { x: touch.pageX, y: touch.pageY }; |
| 25 | + |
| 26 | + // Clear any existing timer |
| 27 | + if (longPressTimer.current) { |
| 28 | + clearTimeout(longPressTimer.current); |
| 29 | + } |
| 30 | + |
| 31 | + longPressTimer.current = setTimeout(() => { |
| 32 | + if (!touchMoved.current && touchStartPosition.current) { |
| 33 | + longPressTriggered.current = true; |
| 34 | + onLongPress({ |
| 35 | + x: touchStartPosition.current.x, |
| 36 | + y: touchStartPosition.current.y, |
| 37 | + }); |
| 38 | + } |
| 39 | + }, LONG_PRESS_DELAY); |
| 40 | + }; |
| 41 | + |
| 42 | + const onTouchMove = (event: React.TouchEvent) => { |
| 43 | + if (touchStartPosition.current && event.touches[0]) { |
| 44 | + const touch = event.touches[0]; |
| 45 | + const deltaX = Math.abs(touch.pageX - touchStartPosition.current.x); |
| 46 | + const deltaY = Math.abs(touch.pageY - touchStartPosition.current.y); |
| 47 | + |
| 48 | + // If moved more than threshold, cancel long press |
| 49 | + if (deltaX > movementThreshold || deltaY > movementThreshold) { |
| 50 | + touchMoved.current = true; |
| 51 | + longPressTriggered.current = false; |
| 52 | + if (longPressTimer.current) { |
| 53 | + clearTimeout(longPressTimer.current); |
| 54 | + longPressTimer.current = null; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + const onTouchEnd = (event: React.TouchEvent) => { |
| 61 | + // Cancel long press if touch ends before timer completes |
| 62 | + if (longPressTimer.current) { |
| 63 | + clearTimeout(longPressTimer.current); |
| 64 | + longPressTimer.current = null; |
| 65 | + } |
| 66 | + |
| 67 | + // Prevent default context menu if long press was successfully triggered |
| 68 | + if (longPressTriggered.current) { |
| 69 | + event.preventDefault(); |
| 70 | + } |
| 71 | + |
| 72 | + touchStartPosition.current = null; |
| 73 | + longPressTriggered.current = false; |
| 74 | + }; |
| 75 | + |
| 76 | + useEffect(() => { |
| 77 | + return () => { |
| 78 | + // Cleanup timer on unmount |
| 79 | + if (longPressTimer.current) { |
| 80 | + clearTimeout(longPressTimer.current); |
| 81 | + longPressTimer.current = null; |
| 82 | + } |
| 83 | + }; |
| 84 | + }, []); |
| 85 | + |
| 86 | + return { |
| 87 | + onTouchStart, |
| 88 | + onTouchMove, |
| 89 | + onTouchEnd, |
| 90 | + }; |
| 91 | +}; |
| 92 | + |
| 93 | +export default useLongPress; |
0 commit comments