1
1
import React from 'react'
2
-
3
2
const noop = ( ) => { }
4
3
5
4
const stateKeys = { onDrag : 'drag' , onHover : 'move' , onMove : 'move' , onPinch : 'pinch' , onScroll : 'scroll' , onWheel : 'wheel' }
@@ -135,15 +134,16 @@ export function useGesture(props, config) {
135
134
136
135
const getKinematics = ( mov_x , mov_y , event , stateKey , isDelta = false ) => {
137
136
const lastLocal = state . current [ stateKey ] . lastLocal || initialState [ stateKey ] . lastLocal
138
- const { xy, initial, delta , time, transform } = state . current [ stateKey ]
137
+ const { xy, initial, time, transform } = state . current [ stateKey ]
139
138
const t = transform || event . transform || configRef . current . transform
140
139
141
140
const delta_t = event . timeStamp - time
142
141
143
142
const x = isDelta ? mov_x + xy [ 0 ] : mov_x
144
143
const y = isDelta ? mov_y + xy [ 1 ] : mov_y
145
- const delta_x = t . x ( isDelta ? mov_x + delta [ 0 ] : x - initial [ 0 ] )
146
- const delta_y = t . y ( isDelta ? mov_y + delta [ 1 ] : y - initial [ 1 ] )
144
+
145
+ const delta_x = t . x ( x - initial [ 0 ] )
146
+ const delta_y = t . y ( y - initial [ 1 ] )
147
147
148
148
const x_dist = t . x ( x - xy [ 0 ] )
149
149
const y_dist = t . y ( y - xy [ 1 ] )
@@ -393,6 +393,7 @@ export function useGesture(props, config) {
393
393
394
394
const onWheel = event => {
395
395
if ( ! configRef . current . enabled || ! configRef . current . wheel ) return
396
+
396
397
clearTimeout ( timeouts . current . wheel )
397
398
timeouts . current . wheel = setTimeout ( onWheelEnd , 100 )
398
399
const { mov_x, mov_y, ...rest } = getWheelEventData ( event )
@@ -435,36 +436,36 @@ export function useGesture(props, config) {
435
436
const listeners = { }
436
437
437
438
if ( actions . has ( 'onMove' ) ) {
438
- pushEventProp ( listeners , pointerEvents ? 'onPointerMove' : 'onMouseMove' , onMove )
439
+ pushInKeys ( listeners , pointerEvents ? 'onPointerMove' : 'onMouseMove' , onMove )
439
440
}
440
441
441
442
if ( actions . has ( 'onDrag' ) ) {
442
443
if ( pointerEvents ) {
443
- pushEventProp ( listeners , 'onPointerDown' , onDragStart )
444
- pushEventProp ( listeners , 'onPointerMove' , onDragMove )
445
- pushEventProp ( listeners , [ 'onPointerUp' , 'onPointerCancel' ] , onDragEnd )
444
+ pushInKeys ( listeners , 'onPointerDown' , onDragStart )
445
+ pushInKeys ( listeners , 'onPointerMove' , onDragMove )
446
+ pushInKeys ( listeners , [ 'onPointerUp' , 'onPointerCancel' ] , onDragEnd )
446
447
} else {
447
- pushEventProp ( listeners , [ 'onMouseDown' , 'onTouchStart' ] , onDragStart )
448
+ pushInKeys ( listeners , [ 'onMouseDown' , 'onTouchStart' ] , onDragStart )
448
449
}
449
450
}
450
451
451
452
if ( actions . has ( 'onPinch' ) ) {
452
- pushEventProp ( listeners , 'onTouchStart' , onPinchStart )
453
- pushEventProp ( listeners , 'onTouchMove' , onPinchMove )
454
- pushEventProp ( listeners , [ 'onTouchEnd' , 'onTouchCancel' ] , onPinchEnd )
453
+ pushInKeys ( listeners , 'onTouchStart' , onPinchStart )
454
+ pushInKeys ( listeners , 'onTouchMove' , onPinchMove )
455
+ pushInKeys ( listeners , [ 'onTouchEnd' , 'onTouchCancel' ] , onPinchEnd )
455
456
}
456
457
457
458
if ( actions . has ( 'onHover' ) ) {
458
- pushEventProp ( listeners , pointerEvents ? 'onPointerEnter' : 'onMouseEnter' , onEnter )
459
- pushEventProp ( listeners , pointerEvents ? 'onPointerLeave' : 'onMouseLeave' , onLeave )
459
+ pushInKeys ( listeners , pointerEvents ? 'onPointerEnter' : 'onMouseEnter' , onEnter )
460
+ pushInKeys ( listeners , pointerEvents ? 'onPointerLeave' : 'onMouseLeave' , onLeave )
460
461
}
461
462
462
463
if ( actions . has ( 'onScroll' ) ) {
463
- pushEventProp ( listeners , 'onScroll' , onScroll )
464
+ pushInKeys ( listeners , 'onScroll' , onScroll )
464
465
}
465
466
466
467
if ( actions . has ( 'onWheel' ) ) {
467
- pushEventProp ( listeners , 'onWheel' , onWheel )
468
+ pushInKeys ( listeners , 'onWheel' , onWheel )
468
469
}
469
470
470
471
if ( domTarget ) {
@@ -482,13 +483,17 @@ export function useGesture(props, config) {
482
483
483
484
/*** UTILS ***/
484
485
485
- const chain = ( ...fns ) => ( ...args ) => fns . forEach ( a => a ( ...args ) )
486
- const pushEventProp = ( l , keys , fn ) => {
486
+ // returns a function that chains all functions given as parameters
487
+ const chain = ( ...fns ) => ( ...args ) => fns . forEach ( fn => fn ( ...args ) )
488
+
489
+ // utility function that pushes values in object keys which are in fact arrays
490
+ const pushInKeys = ( obj , keys , value ) => {
487
491
if ( ! Array . isArray ( keys ) ) keys = [ keys ]
488
- keys . forEach ( key => ( l [ key ] = l [ key ] ? [ ...l [ key ] , fn ] : [ fn ] ) )
492
+ keys . forEach ( key => ( obj [ key ] = obj [ key ] ? [ ...obj [ key ] , value ] : [ value ] ) )
489
493
}
490
494
491
- const clearTimeouts = timeouts => Object . values ( timeouts ) . forEach ( clearTimeout )
495
+ // clears timeouts in keys
496
+ const clearTimeouts = timeoutsObj => Object . values ( timeoutsObj ) . forEach ( clearTimeout )
492
497
493
498
const setListeners = add => ( el , listeners , options ) => {
494
499
const action = add ? 'addEventListener' : 'removeEventListener'
0 commit comments