@@ -15,59 +15,84 @@ interface TouchOrigin {
1515 timestamp : number ;
1616}
1717
18- export function useSwipeGesture ( options : SwipeGestureOptions ) {
19- let origin : TouchOrigin | null = null ;
18+ interface SwipeGestureState {
19+ options : SwipeGestureOptions ;
20+ origin : TouchOrigin | null ;
21+ }
22+
23+ export function useSwipeGesture (
24+ options : SwipeGestureOptions | SwipeGestureOptions [ ] ,
25+ ) {
26+ const states : SwipeGestureState [ ] = (
27+ Array . isArray ( options ) ? options : [ options ]
28+ ) . map ( ( gestureOptions ) => ( {
29+ options : gestureOptions ,
30+ origin : null ,
31+ } ) ) ;
2032
2133 function onTouchStart ( event : TouchEvent ) {
22- if ( event . touches . length !== 1 || options . canStart ?.( event ) === false ) {
23- origin = null ;
24- return ;
25- }
34+ for ( const state of states ) {
35+ if (
36+ event . touches . length !== 1 ||
37+ state . options . canStart ?.( event ) === false
38+ ) {
39+ state . origin = null ;
40+ continue ;
41+ }
2642
27- const touch = event . touches [ 0 ] ;
28- origin = {
29- x : touch . clientX ,
30- y : touch . clientY ,
31- timestamp : Date . now ( ) ,
32- } ;
43+ const touch = event . touches [ 0 ] ;
44+ state . origin = {
45+ x : touch . clientX ,
46+ y : touch . clientY ,
47+ timestamp : Date . now ( ) ,
48+ } ;
49+ }
3350 }
3451
3552 function onTouchEnd ( event : TouchEvent ) {
36- if ( ! origin || event . changedTouches . length !== 1 ) {
37- origin = null ;
38- return ;
39- }
53+ for ( const state of states ) {
54+ if ( ! state . origin || event . changedTouches . length !== 1 ) {
55+ state . origin = null ;
56+ continue ;
57+ }
4058
41- const start = origin ;
42- const touch = event . changedTouches [ 0 ] ;
43- origin = null ;
59+ const start = state . origin ;
60+ const touch = event . changedTouches [ 0 ] ;
61+ state . origin = null ;
4462
45- if ( Date . now ( ) - start . timestamp > ( options . maxDuration ?? 700 ) ) return ;
63+ if (
64+ Date . now ( ) - start . timestamp >
65+ ( state . options . maxDuration ?? 700 )
66+ )
67+ continue ;
4668
47- const deltaX = touch . clientX - start . x ;
48- const deltaY = touch . clientY - start . y ;
49- const minDistance = options . minDistance ?? 64 ;
50- const axisRatio = options . axisRatio ?? 1.25 ;
51- const horizontal = Math . abs ( deltaX ) > Math . abs ( deltaY ) * axisRatio ;
52- const vertical = Math . abs ( deltaY ) > Math . abs ( deltaX ) * axisRatio ;
69+ const deltaX = touch . clientX - start . x ;
70+ const deltaY = touch . clientY - start . y ;
71+ const minDistance = state . options . minDistance ?? 64 ;
72+ const axisRatio = state . options . axisRatio ?? 1.25 ;
73+ const horizontal = Math . abs ( deltaX ) > Math . abs ( deltaY ) * axisRatio ;
74+ const vertical = Math . abs ( deltaY ) > Math . abs ( deltaX ) * axisRatio ;
5375
54- const matched =
55- ( options . direction === 'left' &&
56- horizontal &&
57- deltaX <= - minDistance ) ||
58- ( options . direction === 'right' &&
59- horizontal &&
60- deltaX >= minDistance ) ||
61- ( options . direction === 'up' &&
62- vertical &&
63- deltaY <= - minDistance ) ||
64- ( options . direction === 'down' && vertical && deltaY >= minDistance ) ;
76+ const matched =
77+ ( state . options . direction === 'left' &&
78+ horizontal &&
79+ deltaX <= - minDistance ) ||
80+ ( state . options . direction === 'right' &&
81+ horizontal &&
82+ deltaX >= minDistance ) ||
83+ ( state . options . direction === 'up' &&
84+ vertical &&
85+ deltaY <= - minDistance ) ||
86+ ( state . options . direction === 'down' &&
87+ vertical &&
88+ deltaY >= minDistance ) ;
6589
66- if ( matched ) options . onSwipe ( ) ;
90+ if ( matched ) state . options . onSwipe ( ) ;
91+ }
6792 }
6893
6994 function onTouchCancel ( ) {
70- origin = null ;
95+ for ( const state of states ) state . origin = null ;
7196 }
7297
7398 return { onTouchStart, onTouchEnd, onTouchCancel } ;
0 commit comments