@@ -24,44 +24,51 @@ const Interactive = React.forwardRef<HTMLDivElement, InteractiveProps>((props, r
24
24
hasTouched . current = isTouch ( event ) ;
25
25
return true ;
26
26
} ;
27
-
28
27
const handleMove = useCallback (
29
28
( event : MouseEvent | TouchEvent ) => {
30
29
preventDefaultMove ( event ) ;
31
- // If user moves the pointer outside of the window or iframe bounds and release it there,
32
- // `mouseup`/`touchend` won't be fired. In order to stop the picker from following the cursor
33
- // after the user has moved the mouse/finger back to the document, we check `event.buttons`
34
- // and `event.touches`. It allows us to detect that the user is just moving his pointer
35
- // without pressing it down
30
+ if ( ! container . current ) return ;
31
+
36
32
const isDown = isTouch ( event ) ? event . touches . length > 0 : event . buttons > 0 ;
37
- if ( isDown && container . current ) {
38
- onMoveCallback && onMoveCallback ( getRelativePosition ( container . current ! , event ) , event ) ;
39
- } else {
33
+ if ( ! isDown ) {
40
34
setDragging ( false ) ;
35
+ return ;
41
36
}
37
+
38
+ onMoveCallback ?.( getRelativePosition ( container . current , event ) , event ) ;
42
39
} ,
43
40
[ onMoveCallback ] ,
44
41
) ;
45
42
46
43
const handleMoveEnd = useCallback ( ( ) => setDragging ( false ) , [ ] ) ;
47
- const toggleDocumentEvents = useCallback ( ( state : boolean ) => {
48
- const toggleEvent = state ? window . addEventListener : window . removeEventListener ;
49
- toggleEvent ( hasTouched . current ? 'touchmove' : 'mousemove' , handleMove ) ;
50
- toggleEvent ( hasTouched . current ? 'touchend' : 'mouseup' , handleMoveEnd ) ;
51
- } , [ ] ) ;
44
+ const toggleDocumentEvents = useCallback (
45
+ ( state : boolean ) => {
46
+ if ( state ) {
47
+ window . addEventListener ( hasTouched . current ? 'touchmove' : 'mousemove' , handleMove ) ;
48
+ window . addEventListener ( hasTouched . current ? 'touchend' : 'mouseup' , handleMoveEnd ) ;
49
+ } else {
50
+ window . removeEventListener ( 'mousemove' , handleMove ) ;
51
+ window . removeEventListener ( 'mouseup' , handleMoveEnd ) ;
52
+ window . removeEventListener ( 'touchmove' , handleMove ) ;
53
+ window . removeEventListener ( 'touchend' , handleMoveEnd ) ;
54
+ }
55
+ } ,
56
+ [ handleMove , handleMoveEnd ] ,
57
+ ) ;
52
58
53
59
useEffect ( ( ) => {
54
60
toggleDocumentEvents ( isDragging ) ;
55
61
return ( ) => {
56
- isDragging && toggleDocumentEvents ( false ) ;
62
+ toggleDocumentEvents ( false ) ;
57
63
} ;
58
- } , [ isDragging , toggleDocumentEvents ] ) ;
64
+ } , [ isDragging , handleMove , handleMoveEnd , toggleDocumentEvents ] ) ;
59
65
60
66
const handleMoveStart = useCallback (
61
67
( event : React . MouseEvent | React . TouchEvent ) => {
62
68
preventDefaultMove ( event . nativeEvent ) ;
63
69
if ( ! isValid ( event . nativeEvent ) ) return ;
64
- onKeyCallback && onKeyCallback ( getRelativePosition ( container . current ! , event . nativeEvent ) , event . nativeEvent ) ;
70
+ if ( ! container . current ) return ;
71
+ onKeyCallback ?.( getRelativePosition ( container . current , event . nativeEvent ) , event . nativeEvent ) ;
65
72
setDragging ( true ) ;
66
73
} ,
67
74
[ onKeyCallback ] ,
0 commit comments