Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
fix: RTL support for web (#1192)
Browse files Browse the repository at this point in the history
  • Loading branch information
volfpe authored Jul 30, 2021
1 parent 4b30aed commit e5d5cf6
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Pager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ export default function Pager<T extends Route>({
return false;
}

const isRtl = I18nManager.isRTL;
const diffX = isRtl ? -gestureState.dx : gestureState.dx;

return (
isMovingHorizontally(event, gestureState) &&
((gestureState.dx >= DEAD_ZONE && currentIndexRef.current > 0) ||
(gestureState.dx <= -DEAD_ZONE &&
currentIndexRef.current < routes.length - 1))
((diffX >= DEAD_ZONE && currentIndexRef.current > 0) ||
(diffX <= -DEAD_ZONE && currentIndexRef.current < routes.length - 1))
);
};

Expand All @@ -163,18 +165,20 @@ export default function Pager<T extends Route>({
_: GestureResponderEvent,
gestureState: PanResponderGestureState
) => {
const isRtl = I18nManager.isRTL;
const diffX = isRtl ? -gestureState.dx : gestureState.dx;
if (
// swiping left
(gestureState.dx > 0 && index <= 0) ||
(diffX > 0 && index <= 0) ||
// swiping right
(gestureState.dx < 0 && index >= routes.length - 1)
(diffX < 0 && index >= routes.length - 1)
) {
return;
}

if (layout.width) {
// @ts-expect-error: _offset is private, but docs use it as well
const position = (panX._offset + gestureState.dx) / -layout.width;
const position = (panX._offset + diffX) / -layout.width;
const next =
position > index ? Math.ceil(position) : Math.floor(position);

Expand All @@ -183,13 +187,15 @@ export default function Pager<T extends Route>({
}
}

panX.setValue(gestureState.dx);
panX.setValue(diffX);
};

const finishGesture = (
_: GestureResponderEvent,
gestureState: PanResponderGestureState
) => {
const isRtl = I18nManager.isRTL;

panX.flattenOffset();

onSwipeEnd?.();
Expand All @@ -211,7 +217,9 @@ export default function Pager<T extends Route>({
Math.min(
Math.max(
0,
currentIndex - gestureState.dx / Math.abs(gestureState.dx)
isRtl
? currentIndex + gestureState.dx / Math.abs(gestureState.dx)
: currentIndex - gestureState.dx / Math.abs(gestureState.dx)
),
routes.length - 1
)
Expand Down

0 comments on commit e5d5cf6

Please sign in to comment.