-
Notifications
You must be signed in to change notification settings - Fork 25
/
RNGestureHandler.tsx
122 lines (111 loc) · 2.91 KB
/
RNGestureHandler.tsx
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
import React, { useMemo } from 'react';
import { View } from 'react-native';
import type { Gesture, GestureDetector } from 'react-native-gesture-handler';
import { styles } from './styles';
import type {
DefaultRNGestures,
DispatchEvents,
RNGestureHandlerGesture,
} from '../types';
import { throttle } from '../utils/throttle';
interface RNGHType {
Gesture: typeof Gesture;
GestureDetector: typeof GestureDetector;
}
export const getDefaultPanRNGesture = (
Gesture: RNGHType['Gesture'],
dispatchEvents: DispatchEvents
) => {
return Gesture.Pan()
.runOnJS(true)
.withTestId('RNGH-pan-handler')
.maxPointers(1)
.onBegin((e) => {
dispatchEvents(['mousedown', 'mousemove'], e);
})
.onUpdate(
throttle((e) => {
dispatchEvents(['mousemove'], e);
}, 50)
)
.onEnd((e) => {
dispatchEvents(['mouseup'], e);
});
};
export const getDefaultPinchRNGesture = (
Gesture: RNGHType['Gesture'],
dispatchEvents: DispatchEvents
) => {
return Gesture.Pinch()
.runOnJS(true)
.withTestId('RNGH-pinch-handler')
.onUpdate(
throttle((e) => {
dispatchEvents(['mousewheel'], e, {
zrX: e.focalX,
zrY: e.focalY,
zrDelta: e.velocity / 20,
});
}, 50)
);
};
export const getDefaultTapRNGesture = (
Gesture: RNGHType['Gesture'],
dispatchEvents: DispatchEvents
) => {
return Gesture.Tap()
.runOnJS(true)
.withTestId('RNGH-tap-handler')
.onStart((e) => {
dispatchEvents(['mousedown', 'mousemove'], e);
})
.onEnd((e) => {
dispatchEvents(['mouseup', 'click'], e);
});
};
export const getDefaultRNGestures = (
Gesture: RNGHType['Gesture'],
dispatchEvents: DispatchEvents
): DefaultRNGestures => {
return [
getDefaultPanRNGesture(Gesture, dispatchEvents),
getDefaultPinchRNGesture(Gesture, dispatchEvents),
getDefaultTapRNGesture(Gesture, dispatchEvents),
];
};
type RNGestureHandlerProps = {
RNGH: RNGHType;
dispatchEvents: DispatchEvents;
gesture?: RNGestureHandlerGesture;
};
export function RNGestureHandler({
RNGH,
dispatchEvents,
gesture: gestureProp,
}: RNGestureHandlerProps) {
const { Gesture, GestureDetector } = RNGH;
const defaultGestures = useMemo(
() => getDefaultRNGestures(Gesture, dispatchEvents),
[dispatchEvents, Gesture]
);
const propGesture = useMemo(() => {
if (!gestureProp) {
return defaultGestures;
}
if (typeof gestureProp === 'function') {
return gestureProp(defaultGestures, dispatchEvents);
}
return gestureProp;
}, [defaultGestures, dispatchEvents, gestureProp]);
const gesture = useMemo(() => {
if (Array.isArray(propGesture)) {
return Gesture.Race(...propGesture);
}
return propGesture;
}, [Gesture, propGesture]);
return (
<GestureDetector gesture={gesture}>
<View testID="gesture-handler" style={styles.GestureView} />
</GestureDetector>
);
}