-
Notifications
You must be signed in to change notification settings - Fork 24
/
hooks.ts
270 lines (234 loc) · 8.9 KB
/
hooks.ts
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
import React from 'react'
import { Point } from './types'
const getMousePoint = (e: MouseEvent | React.MouseEvent): Point => ({
x: Number(e.clientX),
y: Number(e.clientY),
})
const getTouchPoint = (touch: Touch | React.Touch): Point => ({
x: Number(touch.clientX),
y: Number(touch.clientY),
})
const getPointInContainer = (point: Point, containerTopLeft: Point): Point => {
return {
x: point.x - containerTopLeft.x,
y: point.y - containerTopLeft.y,
}
}
const preventDefault = (event: Event) => {
event.preventDefault()
}
const disableContextMenu = () => {
window.addEventListener('contextmenu', preventDefault, { capture: true, passive: false })
}
const enableContextMenu = () => {
window.removeEventListener('contextmenu', preventDefault)
}
export type OnStartArgs = { point: Point; pointInWindow: Point }
export type OnMoveArgs = { point: Point; pointInWindow: Point }
type UseDragProps = {
onStart?: (args: OnStartArgs) => void
onMove?: (args: OnMoveArgs) => void
onEnd?: () => void
allowDrag?: boolean
containerRef: React.MutableRefObject<HTMLElement | null>
knobs?: HTMLElement[]
}
export const useDrag = ({
onStart,
onMove,
onEnd,
allowDrag = true,
containerRef,
knobs,
}: UseDragProps) => {
// contains the top-left coordinates of the container in the window. Set on drag start and used in drag move
const containerPositionRef = React.useRef<Point>({ x: 0, y: 0 })
// on touch devices, we only start the drag gesture after pressing the item 200ms.
// this ref contains the timer id to be able to cancel it
const handleTouchStartTimerRef = React.useRef<number | undefined>(undefined)
// on non-touch device, we don't call onStart on mouse down but on the first mouse move
// we do this to let the user clicks on clickable element inside the container
// this means that the drag gesture actually starts on the fist move
const isFirstMoveRef = React.useRef(false)
// see https://twitter.com/ValentinHervieu/status/1324407814970920968
// we do this so that the parent doesn't have to use `useCallback()` for these callbacks
const callbacksRef = React.useRef({ onStart, onMove, onEnd })
// instead of relying on hacks to know if the device is a touch device or not,
// we track this using an onTouchStart listener on the document. (see https://codeburst.io/the-only-way-to-detect-touch-with-javascript-7791a3346685)
const [isTouchDevice, setTouchDevice] = React.useState(false)
React.useEffect(() => {
callbacksRef.current = { onStart, onMove, onEnd }
}, [onStart, onMove, onEnd])
const cancelTouchStart = () => {
if (handleTouchStartTimerRef.current) {
window.clearTimeout(handleTouchStartTimerRef.current)
}
}
const saveContainerPosition = React.useCallback(() => {
if (containerRef.current) {
const bounds = containerRef.current.getBoundingClientRect()
containerPositionRef.current = { x: bounds.left, y: bounds.top }
}
}, [containerRef])
const onDrag = React.useCallback((pointInWindow: Point) => {
const point = getPointInContainer(pointInWindow, containerPositionRef.current)
if (callbacksRef.current.onMove) {
callbacksRef.current.onMove({ pointInWindow, point })
}
}, [])
const onMouseMove = React.useCallback(
(e: MouseEvent) => {
// if this is the first move, we trigger the onStart logic
if (isFirstMoveRef.current) {
isFirstMoveRef.current = false
const pointInWindow = getMousePoint(e)
const point = getPointInContainer(pointInWindow, containerPositionRef.current)
if (callbacksRef.current.onStart) {
callbacksRef.current.onStart({ point, pointInWindow })
}
}
// otherwise, we do the normal move logic
else {
onDrag(getMousePoint(e))
}
},
[onDrag]
)
const onTouchMove = React.useCallback(
(e: TouchEvent) => {
if (e.cancelable) {
// Prevent the whole page from scrolling
e.preventDefault()
onDrag(getTouchPoint(e.touches[0]))
} else {
// if the event is not cancelable, it means the browser is currently scrolling
// which cannot be interrupted. Thus we cancel the drag gesture.
document.removeEventListener('touchmove', onTouchMove)
if (callbacksRef.current.onEnd) {
callbacksRef.current.onEnd()
}
}
},
[onDrag]
)
const onMouseUp = React.useCallback(() => {
isFirstMoveRef.current = false
document.removeEventListener('mousemove', onMouseMove)
document.removeEventListener('mouseup', onMouseUp)
if (callbacksRef.current.onEnd) {
callbacksRef.current.onEnd()
}
}, [onMouseMove])
const onTouchEnd = React.useCallback(() => {
document.removeEventListener('touchmove', onTouchMove)
document.removeEventListener('touchend', onTouchEnd)
enableContextMenu()
if (callbacksRef.current.onEnd) {
callbacksRef.current.onEnd()
}
}, [onTouchMove])
const onMouseDown = React.useCallback(
(e: React.MouseEvent<HTMLElement, MouseEvent>) => {
if (e.button !== 0) {
// we don't want to handle clicks other than left ones
return
}
if (knobs?.length && !knobs.find((knob) => knob.contains(e.target as Node))) {
return
}
document.addEventListener('mousemove', onMouseMove)
document.addEventListener('mouseup', onMouseUp)
saveContainerPosition()
// mark the next move as being the first one
isFirstMoveRef.current = true
},
[onMouseMove, onMouseUp, saveContainerPosition, knobs]
)
const handleTouchStart = React.useCallback(
(point: Point, pointInWindow: Point) => {
document.addEventListener('touchmove', onTouchMove, { capture: false, passive: false })
document.addEventListener('touchend', onTouchEnd)
disableContextMenu()
if (callbacksRef.current.onStart) {
callbacksRef.current.onStart({ point, pointInWindow })
}
},
[onTouchEnd, onTouchMove]
)
const onTouchStart = React.useCallback(
(e: TouchEvent) => {
if (knobs?.length && !knobs.find((knob) => knob.contains(e.target as Node))) {
return
}
saveContainerPosition()
const pointInWindow = getTouchPoint(e.touches[0])
const point = getPointInContainer(pointInWindow, containerPositionRef.current)
// we wait 120ms to start the gesture to be sure that the user
// is not trying to scroll the page
handleTouchStartTimerRef.current = window.setTimeout(
() => handleTouchStart(point, pointInWindow),
120
)
},
[handleTouchStart, saveContainerPosition, knobs]
)
const detectTouchDevice = React.useCallback(() => {
setTouchDevice(true)
document.removeEventListener('touchstart', detectTouchDevice)
}, [])
// if the user is scrolling on mobile, we cancel the drag gesture
const touchScrollListener = React.useCallback(() => {
cancelTouchStart()
}, [])
React.useLayoutEffect(() => {
if (isTouchDevice) {
const container = containerRef.current
if (allowDrag) {
container?.addEventListener('touchstart', onTouchStart, { capture: true, passive: false })
// we are adding this touchmove listener to cancel drag if user is scrolling
// however, it's also important to have a touchmove listener always set
// with non-capture and non-passive option to prevent an issue on Safari
// with e.preventDefault (https://github.com/atlassian/react-beautiful-dnd/issues/1374)
document.addEventListener('touchmove', touchScrollListener, {
capture: false,
passive: false,
})
document.addEventListener('touchend', touchScrollListener, {
capture: false,
passive: false,
})
}
return () => {
container?.removeEventListener('touchstart', onTouchStart, { capture: true })
document.removeEventListener('touchmove', touchScrollListener, { capture: false })
document.removeEventListener('touchend', touchScrollListener, { capture: false })
document.removeEventListener('touchmove', onTouchMove)
document.removeEventListener('touchend', onTouchEnd)
enableContextMenu()
cancelTouchStart()
}
}
// if non-touch device
document.addEventListener('touchstart', detectTouchDevice)
return () => {
document.removeEventListener('touchstart', detectTouchDevice)
document.removeEventListener('mousemove', onMouseMove)
document.removeEventListener('mouseup', onMouseUp)
}
}, [
isTouchDevice,
allowDrag,
detectTouchDevice,
onMouseMove,
onTouchMove,
touchScrollListener,
onTouchEnd,
onMouseUp,
containerRef,
onTouchStart,
])
// on touch devices, we cannot attach the onTouchStart directly via React:
// Touch handlers must be added with {passive: false} to be cancelable.
// https://developers.google.com/web/updates/2017/01/scrolling-intervention
return isTouchDevice ? {} : { onMouseDown }
}