This repository has been archived by the owner on Feb 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Transitioner.tsx
382 lines (323 loc) · 10.3 KB
/
Transitioner.tsx
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import * as React from 'react';
import {
Animated,
Easing,
StyleSheet,
View,
LayoutChangeEvent,
} from 'react-native';
import NavigationScenesReducer from './ScenesReducer';
import {
NavigationStackProp,
Scene,
SceneDescriptorMap,
TransitionerLayout,
TransitionProps,
} from '../types';
type TransitionSpec = {};
type Props = {
render: (
current: TransitionProps,
previous?: TransitionProps
) => React.ReactNode;
configureTransition?: (
current: TransitionProps,
previous?: TransitionProps
) => TransitionSpec;
onTransitionStart?: (
current: TransitionProps,
previous?: TransitionProps
) => void | Promise<any>;
onTransitionEnd?: (
current: TransitionProps,
previous?: TransitionProps
) => void | Promise<any>;
navigation: NavigationStackProp;
descriptors: SceneDescriptorMap;
screenProps?: unknown;
};
type State = {
layout: TransitionerLayout;
position: Animated.Value;
scenes: Scene[];
nextScenes?: Scene[];
};
// Used for all animations unless overriden
const DefaultTransitionSpec = {
duration: 250,
easing: Easing.inOut(Easing.ease),
timing: Animated.timing,
};
class Transitioner extends React.Component<Props, State> {
private positionListener: string;
private prevTransitionProps: TransitionProps | undefined;
private transitionProps: TransitionProps;
private isComponentMounted: boolean;
private isTransitionRunning: boolean;
private queuedTransition: { prevProps: Props } | null;
constructor(props: Props) {
super(props);
// The initial layout isn't measured. Measured layout will be only available
// when the component is mounted.
const layout: TransitionerLayout = {
height: new Animated.Value(0),
initHeight: 0,
initWidth: 0,
isMeasured: false,
width: new Animated.Value(0),
};
const position = new Animated.Value(this.props.navigation.state.index);
this.positionListener = position.addListener((/* { value } */) => {
// This should work until we detach position from a view! so we have to be
// careful to not ever detach it, thus the gymnastics in _getPosition in
// StackViewLayout
// This should log each frame when releasing the gesture or when pressing
// the back button! If not, something has gone wrong with the animated
// value subscription
// console.log(value);
});
this.state = {
layout,
position,
scenes: NavigationScenesReducer(
[],
this.props.navigation.state,
null,
this.props.descriptors
),
};
this.prevTransitionProps = undefined;
this.transitionProps = buildTransitionProps(props, this.state);
this.isComponentMounted = false;
this.isTransitionRunning = false;
this.queuedTransition = null;
}
componentDidMount() {
this.isComponentMounted = true;
}
componentWillUnmount() {
this.isComponentMounted = false;
this.positionListener &&
this.state.position.removeListener(this.positionListener);
}
UNSAFE_componentWillReceiveProps(nextProps: Props) {
if (this.isTransitionRunning) {
if (!this.queuedTransition) {
this.queuedTransition = { prevProps: this.props };
}
return;
}
this.startTransition(this.props, nextProps);
}
private computeScenes = (props: Props, nextProps: Props) => {
let nextScenes = NavigationScenesReducer(
this.state.scenes,
nextProps.navigation.state,
props.navigation.state,
nextProps.descriptors
);
if (!nextProps.navigation.state.isTransitioning) {
nextScenes = filterStale(nextScenes);
}
// Update nextScenes when we change screenProps
// This is a workaround for https://github.com/react-navigation/react-navigation/issues/4271
if (nextProps.screenProps !== this.props.screenProps) {
this.setState({ nextScenes });
}
if (nextScenes === this.state.scenes) {
return;
}
return nextScenes;
};
private startTransition(props: Props, nextProps: Props) {
const indexHasChanged =
props.navigation.state.index !== nextProps.navigation.state.index;
let nextScenes = this.computeScenes(props, nextProps);
if (!nextScenes) {
// prevTransitionProps are the same as transitionProps in this case
// because nothing changed
this.prevTransitionProps = this.transitionProps;
// Unsure if this is actually a good idea... Also related to
// https://github.com/react-navigation/react-navigation/issues/5247
// - the animation is interrupted before completion so this ensures
// that it is properly set to the final position before firing
// onTransitionEnd
this.state.position.setValue(props.navigation.state.index);
this.handleTransitionEnd();
return;
}
const nextState = {
...this.state,
scenes: nextScenes,
};
// grab the position animated value
const { position } = nextState;
// determine where we are meant to transition to
const toValue = nextProps.navigation.state.index;
// compute transitionProps
this.prevTransitionProps = this.transitionProps;
this.transitionProps = buildTransitionProps(nextProps, nextState);
let { isTransitioning } = this.transitionProps.navigation.state;
// if the state isn't transitioning that is meant to signal that we should
// transition immediately to the new index. if the index hasn't changed, do
// the same thing here. it's not clear to me why we ever start a transition
// when the index hasn't changed, this requires further investigation.
if (!isTransitioning || !indexHasChanged) {
this.setState(nextState, async () => {
if (nextProps.onTransitionStart) {
const result = nextProps.onTransitionStart(
this.transitionProps,
this.prevTransitionProps
);
if (result instanceof Promise) {
// why do we bother awaiting the result here?
await result;
}
}
// jump immediately to the new value
indexHasChanged && position.setValue(toValue);
// end the transition
this.handleTransitionEnd();
});
} else if (isTransitioning) {
this.isTransitionRunning = true;
this.setState(nextState, async () => {
if (nextProps.onTransitionStart) {
const result = nextProps.onTransitionStart(
this.transitionProps,
this.prevTransitionProps
);
// Wait for the onTransitionStart to resolve if needed.
if (result instanceof Promise) {
await result;
}
}
// get the transition spec.
const transitionUserSpec = nextProps.configureTransition
? nextProps.configureTransition(
this.transitionProps,
this.prevTransitionProps
)
: null;
const transitionSpec = {
...DefaultTransitionSpec,
...transitionUserSpec,
};
const { timing } = transitionSpec;
delete transitionSpec.timing;
// if swiped back, indexHasChanged == true && positionHasChanged == false
// @ts-ignore
const positionHasChanged = position.__getValue() !== toValue;
if (indexHasChanged && positionHasChanged) {
timing(position, {
...transitionSpec,
toValue: nextProps.navigation.state.index,
}).start(() => {
// In case the animation is immediately interrupted for some reason,
// we move this to the next frame so that onTransitionStart can fire
// first (https://github.com/react-navigation/react-navigation/issues/5247)
requestAnimationFrame(this.handleTransitionEnd);
});
} else {
this.handleTransitionEnd();
}
});
}
}
render() {
return (
<View onLayout={this.handleLayout} style={styles.main}>
{this.props.render(this.transitionProps, this.prevTransitionProps)}
</View>
);
}
private handleLayout = (event: LayoutChangeEvent) => {
const { height, width } = event.nativeEvent.layout;
if (
this.state.layout.initWidth === width &&
this.state.layout.initHeight === height
) {
return;
}
const layout: TransitionerLayout = {
...this.state.layout,
initHeight: height,
initWidth: width,
isMeasured: true,
};
layout.height.setValue(height);
layout.width.setValue(width);
const nextState = {
...this.state,
layout,
};
this.transitionProps = buildTransitionProps(this.props, nextState);
this.setState(nextState);
};
private handleTransitionEnd = () => {
if (!this.isComponentMounted) {
return;
}
const prevTransitionProps = this.prevTransitionProps;
this.prevTransitionProps = undefined;
const scenes = filterStale(this.state.scenes);
const nextState = {
...this.state,
scenes,
};
this.transitionProps = buildTransitionProps(this.props, nextState);
this.setState(nextState, async () => {
if (this.props.onTransitionEnd) {
const result = this.props.onTransitionEnd(
this.transitionProps,
prevTransitionProps
);
if (result instanceof Promise) {
await result;
}
}
if (this.queuedTransition) {
let { prevProps } = this.queuedTransition;
this.queuedTransition = null;
this.startTransition(prevProps, this.props);
} else {
this.isTransitionRunning = false;
}
});
};
}
function buildTransitionProps(props: Props, state: State): TransitionProps {
const { navigation } = props;
const { layout, position, scenes } = state;
const scene = scenes.find(isSceneActive);
if (!scene) {
throw new Error('Could not find active scene');
}
return {
layout,
navigation,
position,
scenes,
scene,
index: scene.index,
};
}
function isSceneNotStale(scene: Scene) {
return !scene.isStale;
}
function filterStale(scenes: Scene[]) {
const filtered = scenes.filter(isSceneNotStale);
if (filtered.length === scenes.length) {
return scenes;
}
return filtered;
}
function isSceneActive(scene: Scene) {
return scene.isActive;
}
const styles = StyleSheet.create({
main: {
flex: 1,
},
});
export default Transitioner;