forked from react-navigation/experimental-transitioner
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCardTransition.js
More file actions
256 lines (247 loc) · 6.41 KB
/
Copy pathCardTransition.js
File metadata and controls
256 lines (247 loc) · 6.41 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React from "react";
import {
StyleSheet,
Text,
Button,
View,
TextInput,
TouchableWithoutFeedback,
Image,
TouchableHighlight,
SafeAreaView,
Dimensions,
ScrollView,
} from "react-native";
import Animated, { Easing } from "react-native-reanimated";
import { PanGestureHandler, State } from "react-native-gesture-handler";
import { Consumer } from "./LayoutContext";
const {
multiply,
add,
sub,
eq,
neq,
cond,
spring,
timing,
lessOrEq,
not,
block,
call,
event,
Value,
Clock,
debug,
defined,
clockRunning,
stopClock,
startClock,
set,
lessThan,
divide,
greaterThan,
and,
interpolate,
} = Animated;
const callWhenTrue = (val, callback) => cond(val, call([val], callback));
const TOSS_VELOCITY_MULTIPLIER = 0.2;
export default class CardTransition extends React.Component {
static navigationOptions = {
getBehindTransitionAnimatedStyle: transition => {
return {
opacity: interpolate(transition.progress, {
inputRange: [0, 1],
outputRange: [1, 0.85],
}),
transform: [
{
translateX: multiply(
-0.3,
multiply(transition.screenWidth, transition.progress),
),
},
],
};
},
createTransition: transition => {
const clock = new Clock();
const gestureState = new Value(State.END);
const gestureTranslateX = new Value(0);
const prevGestureTranslateX = new Value(0);
const gestureVelocityX = new Value(0);
const targetProgress = new Value(0);
const screenWidth = new Value(Dimensions.get("window").width);
const targetProgressDistance = multiply(targetProgress, screenWidth);
const progressDistance = new Value(0);
const isAtRest = and(
not(clockRunning(clock)),
neq(gestureState, State.ACTIVE),
);
const uprightGestureTranslateX = multiply(gestureTranslateX, -1);
const uprightGestureVelocityX = multiply(gestureVelocityX, -1);
const lastGestureTranslateX = new Value(0);
const lastGestureVelocityX = new Value(0);
const state = {
finished: new Value(0),
velocity: new Value(0),
position: progressDistance,
time: new Value(0),
};
const config = {
stiffness: 1000,
damping: 1000,
mass: 3,
overshootClamping: true,
restSpeedThreshold: 0.01,
restDisplacementThreshold: 0.01,
toValue: targetProgressDistance,
};
const goSpring = [
cond(clockRunning(clock), 0, [
set(state.finished, 0),
set(state.velocity, uprightGestureVelocityX),
startClock(clock),
]),
spring(clock, state, config),
cond(state.finished, stopClock(clock)),
];
const springProgressDistance = block([
cond(
eq(gestureState, State.ACTIVE),
[
stopClock(clock),
set(
progressDistance,
add(
progressDistance,
sub(uprightGestureTranslateX, prevGestureTranslateX),
),
),
set(prevGestureTranslateX, uprightGestureTranslateX),
set(lastGestureTranslateX, uprightGestureTranslateX),
set(lastGestureVelocityX, uprightGestureVelocityX),
],
[set(prevGestureTranslateX, 0), goSpring],
),
progressDistance,
]);
let callbacksWaitingForRest = [];
const whenDoneCallback = () => {
callbacksWaitingForRest.forEach(cb => cb());
callbacksWaitingForRest = [];
};
const waitForDone = () =>
new Promise(resolve => {
callbacksWaitingForRest.push(resolve);
});
const closingCallback = () => {
// transition.navigation.goBack(transition.transitionRouteKey);
targetProgress.setValue(0);
};
const isClosing = and(
neq(gestureState, State.ACTIVE),
lessThan(
add(
lastGestureTranslateX,
multiply(TOSS_VELOCITY_MULTIPLIER, lastGestureVelocityX),
),
-100,
),
);
const finalDistanceProgress = block([
springProgressDistance,
callWhenTrue(isAtRest, whenDoneCallback),
callWhenTrue(isClosing, closingCallback),
springProgressDistance,
]);
return {
...transition,
screenWidth,
gestureState,
gestureTranslateX,
gestureVelocityX,
targetProgress,
progress: divide(finalDistanceProgress, screenWidth),
waitForDone,
};
},
runTransition: async transition => {
transition.targetProgress.setValue(1);
await transition.waitForDone();
},
};
_renderCard = styles => {
return (
<Animated.View
style={{
...styles,
shadowColor: "#000",
shadowOffset: { width: 0, height: 0 },
shadowRadius: 5,
backgroundColor: "#E9E9EF",
flex: 1,
opacity: 1,
}}
>
{this.props.children}
</Animated.View>
);
};
render() {
return <Consumer>{this._renderWithLayout}</Consumer>;
}
_renderWithLayout = layout => {
const { transition, navigation } = this.props;
const myKey = navigation.state.key;
if (!transition || transition.transitionRouteKey !== myKey) {
return this._renderCard({});
}
const {
screenWidth,
fromState,
toState,
progress,
gestureState,
gestureTranslateX,
gestureVelocityX,
} = transition;
screenWidth.setValue(layout.width);
const styles = {
transform: [
{
translateX: interpolate(progress, {
inputRange: [0, 1],
outputRange: [layout.width, 0],
}),
},
],
shadowOpacity: interpolate(progress, {
inputRange: [0, 1],
outputRange: [0, 0.2],
extrapolate: "clamp",
}),
};
return (
<PanGestureHandler
minOffsetX={10}
onHandlerStateChange={event([
{
nativeEvent: {
state: gestureState,
},
},
])}
onGestureEvent={event([
{
nativeEvent: {
translationX: gestureTranslateX,
velocityX: gestureVelocityX,
},
},
])}
>
{this._renderCard(styles)}
</PanGestureHandler>
);
};
}