Skip to content

Commit 81c62c5

Browse files
Hedger WangFacebook Github Bot 1
Hedger Wang
authored and
Facebook Github Bot 1
committed
Fix initial scenes rendering.
Summary:The initial layout used to render scenes does not contain the actual width and height measured and causes the issue as described at ericvicenti/navigation-rfc#61 The fix is to update the layout and re-render scenes once layout is modified. Also scenes renderer should also consider the case that when the layout is not measured yet. Reviewed By: ericvicenti Differential Revision: D3162143 fb-gh-sync-id: 197574329d3849cad2a21e07e1bd5e800f74c3ea fbshipit-source-id: 197574329d3849cad2a21e07e1bd5e800f74c3ea
1 parent ecae44a commit 81c62c5

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

Libraries/CustomComponents/NavigationExperimental/NavigationCardStackStyleInterpolator.js

+30
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,39 @@ import type {
5151
* +------------+
5252
*/
5353

54+
/**
55+
* Render the initial style when the initial layout isn't measured yet.
56+
*/
57+
function forInitial(props: NavigationSceneRendererProps): Object {
58+
const {
59+
navigationState,
60+
scene,
61+
} = props;
62+
63+
const focused = navigationState.index === scene.index;
64+
const opacity = focused ? 1 : 0;
65+
// If not focused, move the scene to the far away.
66+
const translate = focused ? 0 : 1000000;
67+
return {
68+
opacity,
69+
transform: [
70+
{ translateX: translate },
71+
{ translateY: translate },
72+
],
73+
};
74+
}
75+
5476
function forHorizontal(props: NavigationSceneRendererProps): Object {
5577
const {
5678
layout,
5779
position,
5880
scene,
5981
} = props;
6082

83+
if (!layout.isMeasured) {
84+
return forInitial(props);
85+
}
86+
6187
const index = scene.index;
6288
const inputRange = [index - 1, index, index + 1];
6389
const width = layout.initWidth;
@@ -95,6 +121,10 @@ function forVertical(props: NavigationSceneRendererProps): Object {
95121
scene,
96122
} = props;
97123

124+
if (!layout.isMeasured) {
125+
return forInitial(props);
126+
}
127+
98128
const index = scene.index;
99129
const inputRange = [index - 1, index, index + 1];
100130
const height = layout.initHeight;

Libraries/NavigationExperimental/NavigationAnimatedView.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Props = {
3939
};
4040

4141
type State = {
42+
layout: NavigationLayout,
4243
position: NavigationAnimatedValue,
4344
scenes: Array<NavigationScene>,
4445
};
@@ -61,7 +62,6 @@ function applyDefaultAnimation(
6162
class NavigationAnimatedView
6263
extends React.Component<any, Props, State> {
6364

64-
_layout: NavigationLayout;
6565
_onLayout: (event: any) => void;
6666
_onProgressChange: (data: {value: number}) => void;
6767
_positionListener: any;
@@ -84,14 +84,18 @@ class NavigationAnimatedView
8484
constructor(props: Props, context: any) {
8585
super(props, context);
8686

87-
this._layout = {
88-
initWidth: 0,
87+
// The initial layout isn't measured. Measured layout will be only available
88+
// when the component is mounted.
89+
const layout = {
90+
height: new Animated.Value(0),
8991
initHeight: 0,
92+
initWidth: 0,
93+
isMeasured: false,
9094
width: new Animated.Value(0),
91-
height: new Animated.Value(0),
9295
};
9396

9497
this.state = {
98+
layout,
9599
position: new Animated.Value(this.props.navigationState.index),
96100
scenes: NavigationScenesReducer([], this.props.navigationState),
97101
};
@@ -180,7 +184,7 @@ class NavigationAnimatedView
180184
} = this.state;
181185

182186
return renderScene({
183-
layout: this._layout,
187+
layout: this.state.layout,
184188
navigationState,
185189
onNavigate,
186190
position,
@@ -203,7 +207,7 @@ class NavigationAnimatedView
203207
} = this.state;
204208

205209
return renderOverlay({
206-
layout: this._layout,
210+
layout: this.state.layout,
207211
navigationState,
208212
onNavigate,
209213
position,
@@ -218,15 +222,16 @@ class NavigationAnimatedView
218222
const {height, width} = event.nativeEvent.layout;
219223

220224
const layout = {
221-
...this._layout,
225+
...this.state.layout,
222226
initHeight: height,
223227
initWidth: width,
228+
isMeasured: true,
224229
};
225230

226-
this._layout = layout;
227-
228231
layout.height.setValue(height);
229232
layout.width.setValue(width);
233+
234+
this.setState({ layout });
230235
}
231236
}
232237

Libraries/NavigationExperimental/NavigationPropTypes.js

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const layout = PropTypes.shape({
5252
height: animatedValue,
5353
initHeight: PropTypes.number.isRequired,
5454
initWidth: PropTypes.number.isRequired,
55+
isMeasured: PropTypes.bool.isRequired,
5556
width: animatedValue,
5657
});
5758

Libraries/NavigationExperimental/NavigationTypeDefinition.js

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type NavigationLayout = {
3737
height: NavigationAnimatedValue,
3838
initHeight: number,
3939
initWidth: number,
40+
isMeasured: boolean,
4041
width: NavigationAnimatedValue,
4142
};
4243

0 commit comments

Comments
 (0)