Skip to content
This repository was archived by the owner on Apr 15, 2020. It is now read-only.

Commit d92558b

Browse files
committed
Progress on shared elements to new API
1 parent e7a2aa8 commit d92558b

2 files changed

Lines changed: 36 additions & 48 deletions

File tree

Shared.js

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,29 @@ const getLayout = (layoutsObj, id) => {
4444
return layout;
4545
};
4646

47-
const createSharedTransition = getScreenStyle => transition => {
48-
let key = transition.fromState.routes[transition.fromState.index].key;
49-
if (
50-
transition.toState &&
51-
transition.toState.index >= transition.fromState.index
52-
) {
53-
key = transition.toState.routes[transition.toState.index].key;
54-
}
47+
const createSharedTransition = transition => {
5548
const progress = new Animated.Value(0);
5649

5750
return {
5851
...transition,
59-
getScreenStyle,
6052
progress,
61-
key,
6253
fromLayouts: {},
6354
toLayouts: {},
6455
toScreenLayout: {},
6556
fromScreenLayout: {},
6657
};
6758
};
6859

69-
const runSharedTransition = async (transition, transitionScreenRefs) => {
60+
const runSharedTransition = async (
61+
transition,
62+
transitionScreenRefs,
63+
fromState,
64+
toState,
65+
) => {
7066
// By now, everything is already rendered. This is our opportunity to measure shared
7167
// elements and set those measurements into Animated values so that the pre-rendered
7268
// transition looks correct
7369

74-
const { toState, fromState } = transition;
7570
const toRouteKey = toState.routes[toState.index].key;
7671
const fromRouteKey = fromState.routes[fromState.index].key;
7772
const fromScreen = transitionScreenRefs[fromRouteKey].current;
@@ -117,7 +112,7 @@ const SharedScreenContext = React.createContext(null);
117112

118113
export class SharedFadeTransition extends React.Component {
119114
static navigationOptions = {
120-
createTransition: createSharedTransition(),
115+
createTransition: createSharedTransition,
121116
runTransition: runSharedTransition,
122117
};
123118

@@ -134,23 +129,18 @@ export class SharedFadeTransition extends React.Component {
134129
_sharedScreenContext = {
135130
setSharedElement: this._setSharedElement,
136131
getNavigation: () => this.props.navigation,
132+
getTransitioningFromState: () => this.props.transitioningFromState,
133+
getTransitioningToState: () => this.props.transitioningToState,
137134
};
138135
render() {
139-
const { transition, navigation, children } = this.props;
140-
const myKey = navigation.state.key;
136+
const { transition, children } = this.props;
141137
let opacity = 1;
142138
if (transition) {
143-
const { fromState, toState, key } = transition;
144-
if (key === myKey) {
145-
const fromKey = fromState.routes[fromState.index].key;
146-
const toKey = toState.routes[toState.index].key;
147-
const fromOpacity = myKey === fromKey ? 1 : 0;
148-
const toOpacity = myKey === toKey ? 1 : 0;
149-
opacity = interpolate(transition.progress, {
150-
inputRange: [0, 1],
151-
outputRange: [fromOpacity, toOpacity],
152-
});
153-
}
139+
const { progress } = transition;
140+
opacity = interpolate(progress, {
141+
inputRange: [0, 1],
142+
outputRange: [0, 1],
143+
});
154144
}
155145
return (
156146
<SharedScreenContext.Provider value={this._sharedScreenContext}>
@@ -169,7 +159,11 @@ export class SharedFadeTransition extends React.Component {
169159
}
170160
}
171161

172-
const getTransitionElementStyle = (transition, thisScreenKey, id) => {
162+
const getTransitionElementStyle = (transitionContext, screenContext, id) => {
163+
const transition = transitionContext.getTransition();
164+
const fromState = screenContext.getTransitioningFromState();
165+
const toState = screenContext.getTransitioningToState();
166+
const thisScreenKey = screenContext.getNavigation().state.key;
173167
if (!transition) {
174168
return [{ transform: [] }];
175169
}
@@ -179,9 +173,8 @@ const getTransitionElementStyle = (transition, thisScreenKey, id) => {
179173
if (!toLayout || !fromLayout) {
180174
return [{ transform: [] }];
181175
}
182-
const toRouteKey = transition.toState.routes[transition.toState.index].key;
183-
const fromRouteKey =
184-
transition.fromState.routes[transition.fromState.index].key;
176+
const toRouteKey = toState.routes[toState.index].key;
177+
const fromRouteKey = fromState.routes[fromState.index].key;
185178
const isToScreen = toRouteKey === thisScreenKey;
186179
const isFromScreen = fromRouteKey === thisScreenKey;
187180

@@ -278,21 +271,21 @@ class SharedViewWithContext extends React.Component {
278271
if (!sharedScreenContext) {
279272
throw new Error("Cannot render shared element outside of shared screen!");
280273
}
281-
const { setSharedElement, getNavigation } = sharedScreenContext;
274+
const { setSharedElement } = sharedScreenContext;
282275

283276
if (!transitionContext) {
284277
throw new Error("Cannot render shared element outside of transitioner!");
285278
}
286279

287-
const transition = transitionContext.getTransition();
288-
289-
const thisScreenKey = getNavigation().state.key;
290-
291280
return (
292281
<Animated.View
293282
style={[
294283
style,
295-
getTransitionElementStyle(transition, thisScreenKey, sharedElId),
284+
getTransitionElementStyle(
285+
transitionContext,
286+
sharedScreenContext,
287+
sharedElId,
288+
),
296289
]}
297290
ref={r => setSharedElement(sharedElId, r)}
298291
>
@@ -335,21 +328,21 @@ class SharedTextWithContext extends React.Component {
335328
if (!sharedScreenContext) {
336329
throw new Error("Cannot render shared element outside of shared screen!");
337330
}
338-
const { setSharedElement, getNavigation } = sharedScreenContext;
331+
const { setSharedElement } = sharedScreenContext;
339332

340333
if (!transitionContext) {
341334
throw new Error("Cannot render shared element outside of transitioner!");
342335
}
343336

344-
const transition = transitionContext.getTransition();
345-
346-
const thisScreenKey = getNavigation().state.key;
347-
348337
return (
349338
<Animated.View
350339
style={[
351340
style,
352-
getTransitionElementStyle(transition, thisScreenKey, sharedElId),
341+
getTransitionElementStyle(
342+
transitionContext,
343+
sharedScreenContext,
344+
sharedElId,
345+
),
353346
{ alignSelf: "center" },
354347
]}
355348
ref={r => setSharedElement(sharedElId, r)}

Transitioner.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,10 @@ export class Transitioner extends React.Component {
206206
descriptors,
207207
} = this.state;
208208

209-
// const { transition, navState, isTransitioning } = this.state;
210209
const mainRouteKeys = navState.routes.map(r => r.key);
211210
let routeKeys = mainRouteKeys;
212-
// let toDescriptors = {};
213211

214212
if (transitionRouteKey) {
215-
// if (transition.toDescriptors) {
216-
// toDescriptors = transition.toDescriptors;
217-
// }
218213
if (transitioningFromState) {
219214
const prevRouteKeys = transitioningFromState.routes.map(r => r.key);
220215
// While transitioning, our main nav state is navState. But we also need to render screens from the last state, preserving the order
@@ -232,7 +227,7 @@ export class Transitioner extends React.Component {
232227
descriptors[key] || transitioningFromDescriptors[key];
233228
const C = descriptor.getComponent();
234229

235-
const backScreenStyles = {}; //fixme
230+
const backScreenStyles = {}; // FIX THIS:
236231
// const backScreenRouteKeys = routeKeys.slice(index + 1);
237232
// const backScreenStyles = backScreenRouteKeys.map(
238233
// backScreenRouteKey => {

0 commit comments

Comments
 (0)