Skip to content

Commit

Permalink
feat: added RTL support (#32) by @aleppos
Browse files Browse the repository at this point in the history
  • Loading branch information
emadhajjar committed Jul 27, 2021
1 parent 6e155a5 commit 43a61e4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
24 changes: 18 additions & 6 deletions src/PaperOnboarding.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import React, { useMemo, useRef, useCallback, memo, useState } from 'react';
import { Dimensions, Insets, LayoutChangeEvent } from 'react-native';
import {
Dimensions,
Insets,
LayoutChangeEvent,
I18nManager,
} from 'react-native';
import { usePanGestureHandler, useValue } from 'react-native-redash';
import { PanGestureHandler } from 'react-native-gesture-handler';
import Animated, {
Expand Down Expand Up @@ -87,10 +92,12 @@ const PaperOnboardingComponent = ({
const { gestureHandler, state, translation, velocity } =
usePanGestureHandler();

const indicatorsContainerLeftPadding = useMemo(
() => dimensions.width / 2 - indicatorSize / 2,
[dimensions.width, indicatorSize]
);
const indicatorsContainerLeftPadding = useMemo(() => {
const containerLeftPadding = dimensions.width / 2 - indicatorSize / 2;
return I18nManager.isRTL
? -containerLeftPadding + indicatorSize * (data.length - 1)
: containerLeftPadding;
}, [dimensions.width, indicatorSize, data.length]);

// animations
const animatedStaticIndex = useValue(0);
Expand All @@ -103,10 +110,15 @@ const PaperOnboardingComponent = ({
screenWidth: dimensions.width,
});

const indicatorsContainerPosition = data.map(
(_, index) => index * indicatorSize * -1
);
const animatedIndicatorsContainerPosition = add(
interpolate(animatedIndex, {
inputRange: data.map((_, index) => index),
outputRange: data.map((_, index) => index * indicatorSize * -1),
outputRange: I18nManager.isRTL
? indicatorsContainerPosition.reverse()
: indicatorsContainerPosition,
extrapolate: Animated.Extrapolate.CLAMP,
}),
indicatorsContainerLeftPadding
Expand Down
1 change: 1 addition & 0 deletions src/components/background/Background.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const BackgroundComponent = ({
color={item.backgroundColor}
extendedSize={extendedSize}
bottomPosition={bottomPosition}
screenDimensions={screenDimensions}
indicatorSize={indicatorSize}
animatedIndicatorsContainerPosition={
animatedIndicatorsContainerPosition
Expand Down
6 changes: 4 additions & 2 deletions src/components/backgroundCircle/BackgroundCircle.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { memo } from 'react';
import { ViewStyle } from 'react-native';
import { ViewStyle, I18nManager } from 'react-native';
import { Circle, CircleProps } from 'react-native-svg';
import Animated, { add, Extrapolate } from 'react-native-reanimated';
import type { BackgroundCircleProps } from '../../types';
Expand All @@ -21,6 +21,7 @@ const BackgroundCircleComponent = ({
color,
extendedSize,
bottomPosition,
screenDimensions,
indicatorSize,
animatedIndicatorsContainerPosition,
}: BackgroundCircleProps) => {
Expand All @@ -41,7 +42,8 @@ const BackgroundCircleComponent = ({
const animatedLeftPosition = add(
animatedIndicatorsContainerPosition,
indicatorSize / 2,
index * indicatorSize
I18nManager.isRTL ? -((index + 1) * indicatorSize) : index * indicatorSize,
I18nManager.isRTL ? screenDimensions.width : 0
);
//#endregion

Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,5 @@ export interface BackgroundCircleProps
color: string;
extendedSize: number;
bottomPosition: number;
screenDimensions: PaperOnboardingScreenDimensions;
}
5 changes: 3 additions & 2 deletions src/useTiming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Animated, {
abs,
timing,
} from 'react-native-reanimated';
import { I18nManager } from 'react-native';
import { State } from 'react-native-gesture-handler';
import { useClock, useValue } from 'react-native-redash';

Expand Down Expand Up @@ -67,13 +68,13 @@ export const useTiming = ({

const valueClamp = interpolate(value, {
inputRange: [screenWidth * -1, 0, screenWidth],
outputRange: [1, 0, -1],
outputRange: I18nManager.isRTL ? [-1, 0, 1] : [1, 0, -1],
extrapolate: Animated.Extrapolate.CLAMP,
});

const velocityClamp = interpolate(velocity, {
inputRange: [screenWidth * -2, 0, screenWidth * 2],
outputRange: [0.5, 0, -0.5],
outputRange: I18nManager.isRTL ? [-0.5, 0, 0.5] : [0.5, 0, -0.5],
extrapolate: Animated.Extrapolate.CLAMP,
});

Expand Down

0 comments on commit 43a61e4

Please sign in to comment.