-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathInterceptingGestureDetector.tsx
More file actions
283 lines (259 loc) · 9.23 KB
/
Copy pathInterceptingGestureDetector.tsx
File metadata and controls
283 lines (259 loc) · 9.23 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import HostGestureDetector from '../HostGestureDetector';
import {
VirtualChild,
GestureHandlerEventWithHandlerData,
DetectorCallbacks,
} from '../../types';
import {
InterceptingDetectorContext,
InterceptingDetectorContextValue,
InterceptingDetectorMode,
} from './useInterceptingDetectorContext';
import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper';
import { configureRelations, ensureNativeDetectorComponent } from '../utils';
import { isComposedGesture } from '../../hooks/utils/relationUtils';
import {
AnimatedNativeDetector,
InterceptingGestureDetectorProps,
nativeDetectorStyles,
} from '../common';
import { tagMessage } from '../../../utils';
import { useEnsureGestureHandlerRootView } from '../useEnsureGestureHandlerRootView';
import { ReanimatedNativeDetector } from '../ReanimatedNativeDetector';
import { Platform } from 'react-native';
import { VirtualChildrenWeb } from '../HostGestureDetector.web';
interface VirtualChildrenForNative {
viewTag: number;
handlerTags: number[];
viewRef: unknown;
}
export function InterceptingGestureDetector<THandlerData, TConfig>({
gesture,
children,
touchAction,
userSelect,
enableContextMenu,
}: InterceptingGestureDetectorProps<THandlerData, TConfig>) {
useEnsureGestureHandlerRootView();
const [virtualChildren, setVirtualChildren] = useState<Set<VirtualChild>>(
() => new Set()
);
const strippedVirtualChildren: (
| VirtualChildrenForNative
| VirtualChildrenWeb
)[] = useMemo(
() =>
Platform.OS === 'web'
? Array.from(virtualChildren).map((child) => ({
viewTag: child.viewTag,
handlerTags: child.handlerTags,
viewRef: child.viewRef,
userSelect: child.userSelect,
touchAction: child.touchAction,
enableContextMenu: child.enableContextMenu,
}))
: Array.from(virtualChildren).map((child) => ({
viewTag: child.viewTag,
handlerTags: child.handlerTags,
viewRef: child.viewRef,
})),
[virtualChildren]
);
const [mode, setMode] = useState<InterceptingDetectorMode>(
gesture?.config.shouldUseReanimatedDetector
? InterceptingDetectorMode.REANIMATED
: gesture?.config.dispatchesAnimatedEvents
? InterceptingDetectorMode.ANIMATED
: InterceptingDetectorMode.DEFAULT
);
const shouldUseReanimatedDetector =
mode === InterceptingDetectorMode.REANIMATED;
const dispatchesAnimatedEvents = mode === InterceptingDetectorMode.ANIMATED;
const NativeDetectorComponent = dispatchesAnimatedEvents
? AnimatedNativeDetector
: shouldUseReanimatedDetector
? ReanimatedNativeDetector
: HostGestureDetector;
const register = useCallback((child: VirtualChild) => {
setVirtualChildren((prev) => {
const newSet = new Set(prev);
newSet.add(child);
return newSet;
});
}, []);
const unregister = useCallback((child: VirtualChild) => {
setVirtualChildren((prev) => {
const newSet = new Set(prev);
newSet.delete(child);
return newSet;
});
}, []);
const contextValue: InterceptingDetectorContextValue = useMemo(
() => ({
mode,
setMode: (newMode: InterceptingDetectorMode) => {
if (
(newMode === InterceptingDetectorMode.REANIMATED &&
mode === InterceptingDetectorMode.ANIMATED) ||
(newMode === InterceptingDetectorMode.ANIMATED &&
mode === InterceptingDetectorMode.REANIMATED)
) {
throw new Error(
tagMessage(
'InterceptingGestureDetector can only handle either Reanimated or Animated events.'
)
);
}
setMode(newMode);
},
register,
unregister,
}),
[mode, register, unregister]
);
useEffect(() => {
if (gesture?.config?.dispatchesAnimatedEvents) {
contextValue.setMode(InterceptingDetectorMode.ANIMATED);
} else if (gesture?.config?.shouldUseReanimatedDetector) {
contextValue.setMode(InterceptingDetectorMode.REANIMATED);
}
}, [
contextValue,
gesture?.config?.dispatchesAnimatedEvents,
gesture?.config?.shouldUseReanimatedDetector,
]);
// It might happen only with ReanimatedNativeDetector
if (!NativeDetectorComponent) {
throw new Error(
tagMessage(
'Gesture expects to run on the UI thread, but failed to create the Reanimated NativeDetector.'
)
);
}
const createGestureEventHandler = useCallback(
(key: keyof DetectorCallbacks<THandlerData>) => {
return (e: GestureHandlerEventWithHandlerData<THandlerData>) => {
if (typeof gesture?.detectorCallbacks[key] === 'function') {
// @ts-expect-error passing event to a union of functions where only one is typed as such
gesture.detectorCallbacks[key](e);
}
virtualChildren.forEach((child) => {
const method = child.methods[key];
if (typeof method === 'function') {
// @ts-expect-error passing event to a union of functions where only one is typed as such
method(e);
}
});
};
},
[gesture, virtualChildren]
);
const getHandlers = useCallback(
(key: keyof DetectorCallbacks<unknown>) => {
const handlers: ((
e: GestureHandlerEventWithHandlerData<THandlerData>
) => void)[] = [];
if (gesture?.detectorCallbacks[key]) {
handlers.push(
gesture.detectorCallbacks[key] as (
e: GestureHandlerEventWithHandlerData<unknown>
) => void
);
}
virtualChildren.forEach((child) => {
const handler = child.methods[key];
if (handler) {
handlers.push(
handler as (
e: GestureHandlerEventWithHandlerData<THandlerData>
) => void
);
}
});
return handlers;
},
[virtualChildren, gesture?.detectorCallbacks]
);
const reanimatedEvents = useMemo(
() => getHandlers('reanimatedEventHandler'),
[getHandlers]
);
const reanimatedEventHandler =
Reanimated?.useComposedEventHandler(reanimatedEvents);
ensureNativeDetectorComponent(NativeDetectorComponent);
if (gesture) {
configureRelations(gesture);
}
const handlerTags = useMemo(() => {
if (gesture) {
return isComposedGesture(gesture)
? gesture.handlerTags
: [gesture.handlerTag];
}
return [];
}, [gesture]);
// On web, we're triggering Reanimated callbacks ourselves, based on the type.
// To handle this properly, we need to provide all three callbacks, so we set
// all three to the Reanimated event handler.
// On native, Reanimated handles routing internally based on the event names
// passed to the useEvent hook. We only need to pass it once, so that Reanimated
// can setup its internal listeners.
const reanimatedHandlers =
Platform.OS === 'web'
? {
onGestureHandlerReanimatedEvent: reanimatedEventHandler,
onGestureHandlerReanimatedStateChange: reanimatedEventHandler,
onGestureHandlerReanimatedTouchEvent: reanimatedEventHandler,
}
: {
onGestureHandlerReanimatedEvent: reanimatedEventHandler,
};
const jsEventHandler = useMemo(
() => createGestureEventHandler('jsEventHandler'),
[createGestureEventHandler]
);
return (
<InterceptingDetectorContext value={contextValue}>
<NativeDetectorComponent
touchAction={touchAction}
userSelect={userSelect}
enableContextMenu={enableContextMenu}
pointerEvents={'box-none'}
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
onGestureHandlerStateChange={jsEventHandler}
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
onGestureHandlerEvent={jsEventHandler}
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
onGestureHandlerTouchEvent={jsEventHandler}
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
onGestureHandlerAnimatedEvent={
gesture?.detectorCallbacks.animatedEventHandler
}
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
onGestureHandlerReanimatedStateChange={
shouldUseReanimatedDetector
? reanimatedHandlers.onGestureHandlerReanimatedStateChange
: undefined
}
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
onGestureHandlerReanimatedEvent={
shouldUseReanimatedDetector
? reanimatedHandlers.onGestureHandlerReanimatedEvent
: undefined
}
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
onGestureHandlerReanimatedTouchEvent={
shouldUseReanimatedDetector
? reanimatedHandlers.onGestureHandlerReanimatedTouchEvent
: undefined
}
handlerTags={handlerTags}
style={nativeDetectorStyles.detector}
virtualChildren={strippedVirtualChildren}
moduleId={globalThis._RNGH_MODULE_ID}>
{children}
</NativeDetectorComponent>
</InterceptingDetectorContext>
);
}