Skip to content

Commit ea6b8a4

Browse files
committed
feat: support to RTL
re #69
1 parent f24ee1a commit ea6b8a4

File tree

6 files changed

+81
-64
lines changed

6 files changed

+81
-64
lines changed

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"babel-plugin-module-resolver": "^4.0.0",
3636
"babel-preset-expo": "8.3.0",
3737
"expo-cli": "^4.0.13",
38+
"fiction-expo-restart": "^1.1.0",
3839
"react-native-gesture-handler": "~1.10.2",
3940
"react-native-reanimated": "2.2.3"
4041
}

example/src/home/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default Index;
5555

5656
const styles = StyleSheet.create({
5757
listItem: {
58-
alignItems: 'center',
58+
alignItems: 'flex-start',
5959
borderColor: Colors.grey60,
6060
borderBottomWidth: 0.5,
6161
padding: 16,

example/src/index.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as React from 'react';
22
import { NavigationContainer } from '@react-navigation/native';
33
import { createNativeStackNavigator } from '@react-navigation/native-stack';
4+
// @ts-ignore
5+
import { Restart } from 'fiction-expo-restart';
46

57
import Home from './home';
68
import NormalComponent from './normal';
@@ -9,6 +11,9 @@ import StackComponent from './stack';
911
import ComplexComponent from './complex';
1012
import SnapCarouselComplexComponent from './snap-carousel-complex';
1113
import SnapCarouselLoopComponent from './snap-carousel-loop';
14+
import { I18nManager } from 'react-native';
15+
import { Text } from 'react-native-ui-lib';
16+
import { TouchableWithoutFeedback } from 'react-native-gesture-handler';
1217

1318
const Stack = createNativeStackNavigator<RootStackParamList>();
1419

@@ -23,6 +28,7 @@ export type RootStackParamList = {
2328
};
2429

2530
function App() {
31+
const [isRTL, setIsRTL] = React.useState(I18nManager.isRTL);
2632
return (
2733
<NavigationContainer>
2834
<Stack.Navigator
@@ -32,6 +38,19 @@ function App() {
3238
flex: 1,
3339
backgroundColor: 'white',
3440
},
41+
headerRight: ({ tintColor }) => (
42+
<TouchableWithoutFeedback
43+
onPress={() => {
44+
I18nManager.forceRTL(!isRTL);
45+
setIsRTL(!isRTL);
46+
Restart();
47+
}}
48+
>
49+
<Text color={tintColor}>
50+
{isRTL ? 'LTR' : 'RTL'}
51+
</Text>
52+
</TouchableWithoutFeedback>
53+
),
3554
}}
3655
>
3756
<Stack.Screen name="Home" component={Home} />

example/yarn.lock

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@
15171517
json5 "^1.0.1"
15181518
write-file-atomic "^2.3.0"
15191519

1520-
"@expo/[email protected]", "@expo/metro-config@^0.1.63":
1520+
"@expo/[email protected]", "@expo/metro-config@^0.1.16", "@expo/metro-config@^0.1.63":
15211521
version "0.1.84"
15221522
resolved "https://registry.nlark.com/@expo/metro-config/download/@expo/metro-config-0.1.84.tgz#ddcc7b4f1087c29f86bc9d916933d29bacd2c726"
15231523
integrity sha1-3cx7TxCHwp+GvJ2RaTPSm6zSxyY=
@@ -5855,6 +5855,15 @@ expo-splash-screen@~0.10.2:
58555855
dependencies:
58565856
"@expo/configure-splash-screen" "0.3.4"
58575857

5858+
expo-updates@^0.4.1:
5859+
version "0.4.2"
5860+
resolved "https://registry.npmmirror.com/expo-updates/download/expo-updates-0.4.2.tgz#15862bc209fa3fb98d3fa16b7aac4edfcc33f182"
5861+
integrity sha1-FYYrwgn6P7mNP6FreqxO38wz8YI=
5862+
dependencies:
5863+
"@expo/metro-config" "^0.1.16"
5864+
fbemitter "^2.1.1"
5865+
uuid "^3.4.0"
5866+
58585867
expo@^41.0.0:
58595868
version "41.0.1"
58605869
resolved "https://registry.npmjs.org/expo/-/expo-41.0.1.tgz#2689003212dcc948d010f86dadf055721a6314b4"
@@ -6163,6 +6172,13 @@ fecha@^4.2.0:
61636172
resolved "https://registry.npm.taobao.org/fecha/download/fecha-4.2.1.tgz?cache=0&sync_timestamp=1617642874416&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ffecha%2Fdownload%2Ffecha-4.2.1.tgz#0a83ad8f86ef62a091e22bb5a039cd03d23eecce"
61646173
integrity sha1-CoOtj4bvYqCR4iu1oDnNA9I+7M4=
61656174

6175+
fiction-expo-restart@^1.1.0:
6176+
version "1.1.0"
6177+
resolved "https://registry.npmmirror.com/fiction-expo-restart/download/fiction-expo-restart-1.1.0.tgz#c8be9a5aa523c733071abd73ffd86ba506656662"
6178+
integrity sha1-yL6aWqUjxzMHGr1z/9hrpQZlZmI=
6179+
dependencies:
6180+
expo-updates "^0.4.1"
6181+
61666182
figgy-pudding@^3.5.1:
61676183
version "3.5.2"
61686184
resolved "https://registry.npm.taobao.org/figgy-pudding/download/figgy-pudding-3.5.2.tgz#b4eee8148abb01dcf1d1ac34367d59e12fa61d6e"

src/layouts/ParallaxLayout.tsx

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import { StyleSheet } from 'react-native';
32
import Animated, {
43
Extrapolate,
54
interpolate,
@@ -44,17 +43,6 @@ export const ParallaxLayout: React.FC<{
4443
[vertical, width, height]
4544
);
4645

47-
const layoutStyle = React.useMemo(() => {
48-
return {
49-
width: !vertical
50-
? width * parallaxScrollingScale
51-
: `${100 * parallaxScrollingScale}%`,
52-
height: vertical
53-
? height * parallaxScrollingScale
54-
: `${100 * parallaxScrollingScale}%`,
55-
};
56-
}, [vertical, parallaxScrollingScale, width, height]);
57-
5846
const x = useOffsetX(
5947
{
6048
handlerOffsetX,
@@ -67,49 +55,49 @@ export const ParallaxLayout: React.FC<{
6755
);
6856

6957
const offsetXStyle = useAnimatedStyle(() => {
70-
const baseTranslateX = x.value - index * size;
71-
const padding = (1 - parallaxScrollingScale) * size;
72-
const extraOffset = index * padding + padding / 2;
58+
const value = x.value / size;
59+
60+
const translate = interpolate(
61+
value,
62+
[-1, 0, 1],
63+
[
64+
-size + parallaxScrollingOffset,
65+
0,
66+
size - parallaxScrollingOffset,
67+
],
68+
Extrapolate.CLAMP
69+
);
7370

7471
const zIndex = interpolate(
75-
x.value,
76-
[-size, 0, size],
72+
value,
73+
[-1, 0, 1],
7774
[0, size, 0],
7875
Extrapolate.CLAMP
7976
);
77+
8078
const scale = interpolate(
81-
x.value,
82-
[-size, 0, size],
83-
[parallaxScrollingScale, 1, parallaxScrollingScale],
84-
Extrapolate.CLAMP
85-
);
86-
const relatedTranslateX = interpolate(
87-
x.value,
88-
[-size, 0, size],
89-
[parallaxScrollingOffset, 0, -parallaxScrollingOffset],
79+
value,
80+
[-1, 0, 1],
81+
[
82+
Math.pow(parallaxScrollingScale, 2),
83+
parallaxScrollingScale,
84+
Math.pow(parallaxScrollingScale, 2),
85+
],
9086
Extrapolate.CLAMP
9187
);
9288

93-
if (vertical) {
94-
return {
95-
transform: [
96-
{
97-
translateY:
98-
baseTranslateX + extraOffset + relatedTranslateX,
99-
},
100-
{ scale },
101-
],
102-
zIndex,
103-
};
104-
}
105-
10689
return {
10790
transform: [
91+
vertical
92+
? {
93+
translateY: translate,
94+
}
95+
: {
96+
translateX: translate,
97+
},
10898
{
109-
translateX:
110-
baseTranslateX + extraOffset + relatedTranslateX,
99+
scale,
111100
},
112-
{ scale },
113101
],
114102
zIndex,
115103
};
@@ -137,14 +125,18 @@ export const ParallaxLayout: React.FC<{
137125
);
138126

139127
return (
140-
<Animated.View style={[layoutStyle, styles.container, offsetXStyle]}>
128+
<Animated.View
129+
style={[
130+
{
131+
width,
132+
height,
133+
position: 'absolute',
134+
alignSelf: 'center',
135+
},
136+
offsetXStyle,
137+
]}
138+
>
141139
<LazyView shouldUpdate={shouldUpdate}>{children}</LazyView>
142140
</Animated.View>
143141
);
144142
};
145-
146-
const styles = StyleSheet.create({
147-
container: {
148-
alignSelf: 'center',
149-
},
150-
});

src/layouts/StackLayout.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import React from 'react';
2-
import {
3-
Dimensions,
4-
StyleSheet,
5-
TransformsStyle,
6-
ViewStyle,
7-
} from 'react-native';
2+
import { Dimensions, TransformsStyle, ViewStyle } from 'react-native';
83
import Animated, {
94
Extrapolate,
105
interpolate,
@@ -355,18 +350,12 @@ export const StackLayout: React.FC<{
355350
width,
356351
height,
357352
position: 'absolute',
353+
alignSelf: 'center',
358354
},
359355
offsetXStyle,
360-
styles.container,
361356
]}
362357
>
363358
<LazyView shouldUpdate={shouldUpdate}>{children}</LazyView>
364359
</Animated.View>
365360
);
366361
};
367-
368-
const styles = StyleSheet.create({
369-
container: {
370-
alignSelf: 'center',
371-
},
372-
});

0 commit comments

Comments
 (0)