From 3ad4eab9e86d2cd636ece1d1898aee2dbca99386 Mon Sep 17 00:00:00 2001 From: sumo_slonik Date: Thu, 14 Nov 2024 16:08:33 +0100 Subject: [PATCH] fix problem with no handling gestures --- src/components/Modal/ReactNativeModal/Modal.tsx | 11 ++++++++--- .../Modal/ReactNativeModal/panResponders.ts | 14 ++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/components/Modal/ReactNativeModal/Modal.tsx b/src/components/Modal/ReactNativeModal/Modal.tsx index 88f650b8cfdc..8187b6f93a54 100644 --- a/src/components/Modal/ReactNativeModal/Modal.tsx +++ b/src/components/Modal/ReactNativeModal/Modal.tsx @@ -47,10 +47,15 @@ function ReactNativeModal(incomingProps: ModalProps) { const [deviceHeight, setDeviceHeight] = useState(Dimensions.get('window').height); const [pan, setPan] = useState(new Animated.ValueXY()); const [panResponder, setPanResponder] = useState(null); - const [currentSwipingDirection, setCurrentSwipingDirection] = useState(null); const [inSwipeClosingState, setInSwipeClosingState] = useState(false); const isSwipeable = !!props.swipeDirection; + const currentSwipingDirectionRef = useRef(null); + + const setCurrentSwipingDirection = (direction: Direction | null) => { + currentSwipingDirectionRef.current = direction; + }; + const animEvt = useRef(null); const setAnimEvt = (currentAnim: AnimationEvent | null): void => { @@ -67,8 +72,8 @@ function ReactNativeModal(incomingProps: ModalProps) { PanResponder.create({ onMoveShouldSetPanResponder: onMoveShouldSetPanResponder(props, setAnimEvt, setCurrentSwipingDirection, pan), onStartShouldSetPanResponder: (a, b) => onStartShouldSetPanResponder(props, setCurrentSwipingDirection)(a as EnhancedGestureEvent, b), - onPanResponderMove: onPanResponderMove(props, currentSwipingDirection, setCurrentSwipingDirection, setAnimEvt, animEvt, pan, deviceHeight, deviceWidth), - onPanResponderRelease: onPanResponderRelease(props, currentSwipingDirection, setInSwipeClosingState, pan), + onPanResponderMove: onPanResponderMove(props, currentSwipingDirectionRef, setCurrentSwipingDirection, setAnimEvt, animEvt, pan, deviceHeight, deviceWidth), + onPanResponderRelease: onPanResponderRelease(props, currentSwipingDirectionRef, setInSwipeClosingState, pan), }), ); // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps diff --git a/src/components/Modal/ReactNativeModal/panResponders.ts b/src/components/Modal/ReactNativeModal/panResponders.ts index 7f6a4e4a6595..f5c7b7ac51d1 100644 --- a/src/components/Modal/ReactNativeModal/panResponders.ts +++ b/src/components/Modal/ReactNativeModal/panResponders.ts @@ -75,7 +75,7 @@ const onStartShouldSetPanResponder = (props: RemainingModalProps, setCurrentSwip const onPanResponderMove = ( props: RemainingModalProps, - currentSwipingDirection: Direction | null, + currentSwipingDirectionRef: MutableRefObject, setCurrentSwipingDirection: (direction: Direction | null) => void, setCurrentAnimEvt: (currentAnim: AnimationEvent | null) => void, animEvt: MutableRefObject, @@ -84,15 +84,14 @@ const onPanResponderMove = ( deviceWidth: number, ) => { return (evt: GestureResponderEvent, gestureState: PanResponderGestureState) => { + const currentSwipingDirection = currentSwipingDirectionRef.current; if (!currentSwipingDirection) { if (gestureState.dx === 0 && gestureState.dy === 0) { return; } - setCurrentSwipingDirection(getSwipingDirection(gestureState)); setCurrentAnimEvt(createAnimationEventForSwipe(currentSwipingDirection, pan)); } - if (isSwipeDirectionAllowed(gestureState, currentSwipingDirection)) { const newOpacityFactor = 1 - calcDistancePercentage(gestureState, currentSwipingDirection, props.deviceHeight ?? deviceHeight, props.deviceWidth ?? deviceWidth); @@ -123,8 +122,15 @@ const onPanResponderMove = ( } }; }; -const onPanResponderRelease = (props: RemainingModalProps, currentSwipingDirection: Direction | null, setInSwipeClosingState: (val: boolean) => void, pan: Animated.ValueXY) => { + +const onPanResponderRelease = ( + props: RemainingModalProps, + currentSwipingDirectionRef: MutableRefObject, + setInSwipeClosingState: (val: boolean) => void, + pan: Animated.ValueXY, +) => { return (evt: GestureResponderEvent, gestureState: PanResponderGestureState) => { + const currentSwipingDirection = currentSwipingDirectionRef.current; const accDistance = getAccDistancePerDirection(gestureState, currentSwipingDirection); if (accDistance > props.swipeThreshold && isSwipeDirectionAllowed(gestureState, currentSwipingDirection, props.swipeDirection)) { if (props.onSwipeComplete) {