|
1 |
| -import GestureHandlerDetector from '../web/GestureHandlerDetector'; |
2 |
| -const HostGestureDetector = GestureHandlerDetector; |
| 1 | +import { useEffect, useRef } from 'react'; |
| 2 | +import { |
| 3 | + GestureHandlerStateChangeEvent, |
| 4 | + GestureHandlerTouchEvent, |
| 5 | + GestureHandlerEvent, |
| 6 | +} from '../web/interfaces'; |
| 7 | +import { View } from 'react-native'; |
| 8 | +import RNGestureHandlerModuleWeb from '../RNGestureHandlerModule.web'; |
| 9 | +import { ActionType } from '../ActionType'; |
| 10 | +export interface GestureHandlerDetectorProps { |
| 11 | + onGestureHandlerEvent?: (e: GestureHandlerEvent) => void; |
| 12 | + onGestureHandlerAnimatedEvent?: (e: GestureHandlerEvent) => void; |
| 13 | + onGestureHandlerStateChange?: (e: GestureHandlerStateChangeEvent) => void; |
| 14 | + onGestureHandlerTouchEvent?: (e: GestureHandlerTouchEvent) => void; |
| 15 | + |
| 16 | + handlerTags: number[]; |
| 17 | + dispatchesAnimatedEvents: boolean; |
| 18 | + moduleId: number; |
| 19 | + children?: React.ReactNode; |
| 20 | +} |
| 21 | + |
| 22 | +const HostGestureDetector = (props: GestureHandlerDetectorProps) => { |
| 23 | + const { |
| 24 | + onGestureHandlerEvent, |
| 25 | + onGestureHandlerAnimatedEvent, |
| 26 | + onGestureHandlerStateChange, |
| 27 | + onGestureHandlerTouchEvent, |
| 28 | + handlerTags, |
| 29 | + dispatchesAnimatedEvents, |
| 30 | + moduleId, |
| 31 | + children, |
| 32 | + } = props; |
| 33 | + |
| 34 | + const viewRef = useRef(null); |
| 35 | + const propsRef = useRef<GestureHandlerDetectorProps>(props); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + attachHandlers(); |
| 39 | + }, [ |
| 40 | + onGestureHandlerEvent, |
| 41 | + onGestureHandlerAnimatedEvent, |
| 42 | + onGestureHandlerStateChange, |
| 43 | + onGestureHandlerTouchEvent, |
| 44 | + handlerTags, |
| 45 | + dispatchesAnimatedEvents, |
| 46 | + moduleId, |
| 47 | + ]); |
| 48 | + |
| 49 | + const attachHandlers = (): void => { |
| 50 | + handlerTags.forEach((tag) => { |
| 51 | + RNGestureHandlerModuleWeb.attachGestureHandler( |
| 52 | + tag, |
| 53 | + viewRef.current, |
| 54 | + ActionType.NATIVE_DETECTOR, |
| 55 | + propsRef |
| 56 | + ); |
| 57 | + }); |
| 58 | + }; |
| 59 | + |
| 60 | + return <View ref={viewRef}>{children}</View>; |
| 61 | +}; |
| 62 | + |
3 | 63 | export default HostGestureDetector;
|
0 commit comments