@@ -4,6 +4,8 @@ import { ControllerSettings } from "../../types.js";
4
4
import { UseSensors } from "../../hooks/useSensors.js" ;
5
5
import { useEventCallback } from "../../hooks/useEventCallback.js" ;
6
6
import { usePointerEvents } from "../../hooks/usePointerEvents.js" ;
7
+ import { cssClass } from "../../utils.js" ;
8
+ import { CLASS_SLIDE , CLASS_SLIDE_WRAPPER } from "../../consts.js" ;
7
9
8
10
enum Gesture {
9
11
NONE ,
@@ -14,7 +16,7 @@ enum Gesture {
14
16
const SWIPE_THRESHOLD = 30 ;
15
17
16
18
export function usePointerSwipe < T extends Element = Element > (
17
- { disableSwipeNavigation } : ControllerSettings ,
19
+ { disableSwipeNavigation, closeOnBackdropClick } : ControllerSettings ,
18
20
subscribeSensors : UseSensors < T > [ "subscribeSensors" ] ,
19
21
isSwipeValid : ( offset : number ) => boolean ,
20
22
containerWidth : number ,
@@ -29,6 +31,7 @@ export function usePointerSwipe<T extends Element = Element>(
29
31
onPullProgress : ( offset : number ) => void ,
30
32
onPullFinish : ( offset : number , duration : number ) => void ,
31
33
onPullCancel : ( offset : number ) => void ,
34
+ onClose : ( ) => void ,
32
35
) {
33
36
const offset = React . useRef < number > ( 0 ) ;
34
37
const pointers = React . useRef < React . PointerEvent [ ] > ( [ ] ) ;
@@ -59,6 +62,11 @@ export function usePointerSwipe<T extends Element = Element>(
59
62
[ clearPointer ] ,
60
63
) ;
61
64
65
+ const lookupPointer = React . useCallback (
66
+ ( event : React . PointerEvent ) => pointers . current . find ( ( { pointerId } ) => event . pointerId === pointerId ) ,
67
+ [ ] ,
68
+ ) ;
69
+
62
70
const onPointerDown = useEventCallback ( ( event : React . PointerEvent ) => {
63
71
addPointer ( event ) ;
64
72
} ) ;
@@ -67,36 +75,50 @@ export function usePointerSwipe<T extends Element = Element>(
67
75
( pullDownEnabled && value > threshold ) || ( pullUpEnabled && value < - threshold ) ;
68
76
69
77
const onPointerUp = useEventCallback ( ( event : React . PointerEvent ) => {
70
- if ( pointers . current . find ( ( x ) => x . pointerId === event . pointerId ) && activePointer . current === event . pointerId ) {
71
- const duration = Date . now ( ) - startTime . current ;
72
- const currentOffset = offset . current ;
78
+ const pointer = lookupPointer ( event ) ;
79
+ if ( pointer ) {
80
+ if ( activePointer . current === event . pointerId ) {
81
+ const duration = Date . now ( ) - startTime . current ;
82
+ const currentOffset = offset . current ;
83
+
84
+ if ( gesture . current === Gesture . SWIPE ) {
85
+ if (
86
+ Math . abs ( currentOffset ) > 0.3 * containerWidth ||
87
+ ( Math . abs ( currentOffset ) > 5 && duration < swipeAnimationDuration )
88
+ ) {
89
+ onSwipeFinish ( currentOffset , duration ) ;
90
+ } else {
91
+ onSwipeCancel ( currentOffset ) ;
92
+ }
93
+ } else if ( gesture . current === Gesture . PULL ) {
94
+ if ( exceedsPullThreshold ( currentOffset , 2 * SWIPE_THRESHOLD ) ) {
95
+ onPullFinish ( currentOffset , duration ) ;
96
+ } else {
97
+ onPullCancel ( currentOffset ) ;
98
+ }
99
+ }
73
100
74
- if ( gesture . current === Gesture . SWIPE ) {
101
+ offset . current = 0 ;
102
+ gesture . current = Gesture . NONE ;
103
+ } else {
104
+ // Handle click events
105
+ const { target } = event ;
75
106
if (
76
- Math . abs ( currentOffset ) > 0.3 * containerWidth ||
77
- ( Math . abs ( currentOffset ) > 5 && duration < swipeAnimationDuration )
107
+ closeOnBackdropClick &&
108
+ target instanceof HTMLElement &&
109
+ target === pointer . target &&
110
+ ( target . classList . contains ( cssClass ( CLASS_SLIDE ) ) || target . classList . contains ( cssClass ( CLASS_SLIDE_WRAPPER ) ) )
78
111
) {
79
- onSwipeFinish ( currentOffset , duration ) ;
80
- } else {
81
- onSwipeCancel ( currentOffset ) ;
82
- }
83
- } else if ( gesture . current === Gesture . PULL ) {
84
- if ( exceedsPullThreshold ( currentOffset , 2 * SWIPE_THRESHOLD ) ) {
85
- onPullFinish ( currentOffset , duration ) ;
86
- } else {
87
- onPullCancel ( currentOffset ) ;
112
+ onClose ( ) ;
88
113
}
89
114
}
90
-
91
- offset . current = 0 ;
92
- gesture . current = Gesture . NONE ;
93
115
}
94
116
95
117
clearPointer ( event ) ;
96
118
} ) ;
97
119
98
120
const onPointerMove = useEventCallback ( ( event : React . PointerEvent ) => {
99
- const pointer = pointers . current . find ( ( p ) => p . pointerId === event . pointerId ) ;
121
+ const pointer = lookupPointer ( event ) ;
100
122
if ( pointer ) {
101
123
const isCurrentPointer = activePointer . current === event . pointerId ;
102
124
0 commit comments