forked from react-navigation/react-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardStackStyleInterpolator.js
More file actions
167 lines (141 loc) · 3.83 KB
/
Copy pathCardStackStyleInterpolator.js
File metadata and controls
167 lines (141 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* @flow */
import { I18nManager, type AnimatedViewStylePropTypes } from 'react-native';
import type { NavigationSceneRendererProps } from '../../TypeDefinition';
/**
* Utility that builds the style for the card in the cards stack.
*
* +------------+
* +-+ |
* +-+ | |
* | | | |
* | | | Focused |
* | | | Card |
* | | | |
* +-+ | |
* +-+ |
* +------------+
*/
/**
* Render the initial style when the initial layout isn't measured yet.
*/
function forInitial(
props: NavigationSceneRendererProps
): AnimatedViewStylePropTypes {
const { navigation, scene } = props;
const focused = navigation.state.index === scene.index;
const opacity = focused ? 1 : 0;
// If not focused, move the scene far away.
const translate = focused ? 0 : 1000000;
return {
opacity,
transform: [{ translateX: translate }, { translateY: translate }],
};
}
/**
* Standard iOS-style slide in from the right.
*/
function forHorizontal(
props: NavigationSceneRendererProps
): AnimatedViewStylePropTypes {
const { layout, position, scene } = props;
if (!layout.isMeasured) {
return forInitial(props);
}
const index = scene.index;
const inputRange = [index - 1, index, index + 1];
const width = layout.initWidth;
const outputRange = I18nManager.isRTL
? ([-width, 0, width * 0.3]: Array<number>)
: ([width, 0, width * -0.3]: Array<number>);
// Add [index - 1, index - 0.99] to the interpolated opacity for screen transition.
// This makes the screen's shadow to disappear smoothly.
const opacity = position.interpolate({
inputRange: ([
index - 1,
index - 0.99,
index,
index + 0.99,
index + 1,
]: Array<number>),
outputRange: ([0, 1, 1, 0.85, 0]: Array<number>),
});
const translateY = 0;
const translateX = position.interpolate({
inputRange,
outputRange,
});
return {
opacity,
transform: [{ translateX }, { translateY }],
};
}
/**
* Standard iOS-style slide in from the bottom (used for modals).
*/
function forVertical(
props: NavigationSceneRendererProps
): AnimatedViewStylePropTypes {
const { layout, position, scene } = props;
if (!layout.isMeasured) {
return forInitial(props);
}
const index = scene.index;
const height = layout.initHeight;
const opacity = position.interpolate({
inputRange: ([
index - 1,
index - 0.99,
index,
index + 0.99,
index + 1,
]: Array<number>),
outputRange: ([0, 1, 1, 0.85, 0]: Array<number>),
});
const translateX = 0;
const translateY = position.interpolate({
inputRange: ([index - 1, index, index + 1]: Array<number>),
outputRange: ([height, 0, 0]: Array<number>),
});
return {
opacity,
transform: [{ translateX }, { translateY }],
};
}
/**
* Standard Android-style fade in from the bottom.
*/
function forFadeFromBottomAndroid(
props: NavigationSceneRendererProps
): AnimatedViewStylePropTypes {
const { layout, position, scene } = props;
if (!layout.isMeasured) {
return forInitial(props);
}
const index = scene.index;
const inputRange = [index - 1, index, index + 0.99, index + 1];
const opacity = position.interpolate({
inputRange,
outputRange: ([0, 1, 1, 0]: Array<number>),
});
const translateX = 0;
const translateY = position.interpolate({
inputRange,
outputRange: ([50, 0, 0, 0]: Array<number>),
});
return {
opacity,
transform: [{ translateX }, { translateY }],
};
}
function canUseNativeDriver(): boolean {
// The native driver can be enabled for this interpolator animating
// opacity, translateX, and translateY is supported by the native animation
// driver on iOS and Android.
return true;
}
export default {
forHorizontal,
forVertical,
forFadeFromBottomAndroid,
canUseNativeDriver,
};