Skip to content

Commit

Permalink
fix problem with no handling gestures
Browse files Browse the repository at this point in the history
  • Loading branch information
sumo-slonik committed Nov 14, 2024
1 parent ed4e2e9 commit 3ad4eab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/components/Modal/ReactNativeModal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ function ReactNativeModal(incomingProps: ModalProps) {
const [deviceHeight, setDeviceHeight] = useState(Dimensions.get('window').height);
const [pan, setPan] = useState<Animated.ValueXY>(new Animated.ValueXY());
const [panResponder, setPanResponder] = useState<PanResponderInstance | null>(null);
const [currentSwipingDirection, setCurrentSwipingDirection] = useState<Direction | null>(null);
const [inSwipeClosingState, setInSwipeClosingState] = useState(false);
const isSwipeable = !!props.swipeDirection;

const currentSwipingDirectionRef = useRef<Direction | null>(null);

const setCurrentSwipingDirection = (direction: Direction | null) => {
currentSwipingDirectionRef.current = direction;
};

const animEvt = useRef<AnimationEvent | null>(null);

const setAnimEvt = (currentAnim: AnimationEvent | null): void => {
Expand All @@ -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
Expand Down
14 changes: 10 additions & 4 deletions src/components/Modal/ReactNativeModal/panResponders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const onStartShouldSetPanResponder = (props: RemainingModalProps, setCurrentSwip

const onPanResponderMove = (
props: RemainingModalProps,
currentSwipingDirection: Direction | null,
currentSwipingDirectionRef: MutableRefObject<Direction | null>,
setCurrentSwipingDirection: (direction: Direction | null) => void,
setCurrentAnimEvt: (currentAnim: AnimationEvent | null) => void,
animEvt: MutableRefObject<AnimationEvent | null>,
Expand All @@ -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);

Expand Down Expand Up @@ -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<Direction | null>,
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) {
Expand Down

0 comments on commit 3ad4eab

Please sign in to comment.