Skip to content

Commit

Permalink
RTL support
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbabenko committed Mar 21, 2022
1 parent 37653d7 commit c8e87eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Zoom to scale
- Double tap to scale
- Native iOS feeling (rubber effect, decay animation on pan gesture)
- RTL support
- Fully customizable
- Both orientations (portrait + landscape)
- Infinite list
Expand Down
26 changes: 19 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, {
useState,
} from 'react';
import {
I18nManager,
Image,
StyleSheet,
useWindowDimensions,
Expand All @@ -27,6 +28,8 @@ import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import { useVector } from 'react-native-redash';
import { clamp, withDecaySpring, withRubberBandClamp } from './utils';

const rtl = I18nManager.isRTL;

const DOUBLE_TAP_SCALE = 3;
const MAX_SCALE = 6;
const SPACE_BETWEEN_IMAGES = 40;
Expand Down Expand Up @@ -560,6 +563,10 @@ const ResizableImage = React.memo(
x[1] - offset.x.value
);

const transX = rtl
? initialTranslateX.value - translationX + clampedX
: initialTranslateX.value + translationX - clampedX;

if (
hideAdjacentImagesOnScaledImage &&
disableTransitionOnScaledImage
Expand All @@ -568,7 +575,7 @@ const ResizableImage = React.memo(
disableTransitionOnScaledImage && scale.value > 1;

const moveX = withRubberBandClamp(
initialTranslateX.value + translationX - clampedX,
transX,
0.55,
width,
disabledTransition
Expand All @@ -580,17 +587,18 @@ const ResizableImage = React.memo(
translateX.value = moveX;
}
if (disabledTransition) {
translation.x.value = clampedX + moveX - translateX.value;
translation.x.value = rtl
? clampedX - moveX + translateX.value
: clampedX + moveX - translateX.value;
} else {
translation.x.value = clampedX;
}
} else {
if (loop) {
translateX.value =
initialTranslateX.value + translationX - clampedX;
translateX.value = transX;
} else {
translateX.value = withRubberBandClamp(
initialTranslateX.value + translationX - clampedX,
transX,
0.55,
width,
disableTransitionOnScaledImage && scale.value > 1
Expand Down Expand Up @@ -657,7 +665,11 @@ const ResizableImage = React.memo(
snapPoints = [getPosition(index)];
}

let snapTo = snapPoint(translateX.value, velocityX, snapPoints);
let snapTo = snapPoint(
translateX.value,
rtl ? -velocityX : velocityX,
snapPoints
);

const nextIndex = getIndexFromPosition(snapTo);

Expand Down Expand Up @@ -914,7 +926,7 @@ const GalleryComponent = <T extends any>(
const currentIndex = useSharedValue(initialIndex);

const animatedStyle = useAnimatedStyle(() => ({
transform: [{ translateX: translateX.value }],
transform: [{ translateX: rtl ? -translateX.value : translateX.value }],
}));

const changeIndex = useCallback(
Expand Down

0 comments on commit c8e87eb

Please sign in to comment.