Header.js.flow
10.1 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* @flow */
'no babel-plugin-flow-react-proptypes';
import * as React from 'react';
import {
Animated,
Dimensions,
Platform,
StyleSheet,
View,
ViewPropTypes,
} from 'react-native';
import HeaderTitle from './HeaderTitle';
import HeaderBackButton from './HeaderBackButton';
import HeaderStyleInterpolator from './HeaderStyleInterpolator';
import SafeAreaView from '../SafeAreaView';
import withOrientation from '../withOrientation';
import type {
NavigationScene,
NavigationStyleInterpolator,
LayoutEvent,
HeaderProps,
} from '../../TypeDefinition';
type SceneProps = {
scene: NavigationScene,
position: Animated.Value,
progress: Animated.Value,
style?: ViewPropTypes.style,
};
type SubViewRenderer<T> = (props: SceneProps) => ?React.Node;
type SubViewName = 'left' | 'title' | 'right';
type State = {
widths: {
[key: string]: number,
},
};
const APPBAR_HEIGHT = Platform.OS === 'ios' ? 44 : 56;
const STATUSBAR_HEIGHT = Platform.OS === 'ios' ? 20 : 0;
const TITLE_OFFSET = Platform.OS === 'ios' ? 70 : 56;
type Props = HeaderProps & { isLandscape: boolean };
class Header extends React.PureComponent<Props, State> {
static defaultProps = {
leftInterpolator: HeaderStyleInterpolator.forLeft,
titleInterpolator: HeaderStyleInterpolator.forCenter,
rightInterpolator: HeaderStyleInterpolator.forRight,
};
static get HEIGHT() {
console.warn(
'Header.HEIGHT is deprecated and will be removed before react-navigation comes out of beta.'
);
return APPBAR_HEIGHT + STATUSBAR_HEIGHT;
}
state = {
widths: {},
};
_getHeaderTitleString(scene: NavigationScene): ?string {
const sceneOptions = this.props.getScreenDetails(scene).options;
if (typeof sceneOptions.headerTitle === 'string') {
return sceneOptions.headerTitle;
}
return sceneOptions.title;
}
_getLastScene(scene: NavigationScene): ?NavigationScene {
return this.props.scenes.find((s: *) => s.index === scene.index - 1);
}
_getBackButtonTitleString(scene: NavigationScene): ?string {
const lastScene = this._getLastScene(scene);
if (!lastScene) {
return null;
}
const { headerBackTitle } = this.props.getScreenDetails(lastScene).options;
if (headerBackTitle || headerBackTitle === null) {
return headerBackTitle;
}
return this._getHeaderTitleString(lastScene);
}
_getTruncatedBackButtonTitle(scene: NavigationScene): ?string {
const lastScene = this._getLastScene(scene);
if (!lastScene) {
return null;
}
return this.props.getScreenDetails(lastScene).options
.headerTruncatedBackTitle;
}
_navigateBack = () => {
this.props.navigation.goBack(null);
};
_renderTitleComponent = (props: SceneProps): ?React.Node => {
// $FlowFixMe
const details = this.props.getScreenDetails(props.scene);
const headerTitle = details.options.headerTitle;
if (React.isValidElement(headerTitle)) {
return headerTitle;
}
const titleString = this._getHeaderTitleString(props.scene);
const titleStyle = details.options.headerTitleStyle;
const color = details.options.headerTintColor;
const allowFontScaling = details.options.headerTitleAllowFontScaling;
// On iOS, width of left/right components depends on the calculated
// size of the title.
const onLayoutIOS =
Platform.OS === 'ios'
? (e: LayoutEvent) => {
this.setState({
widths: {
...this.state.widths,
[props.scene.key]: e.nativeEvent.layout.width,
},
});
}
: undefined;
const RenderedHeaderTitle =
headerTitle && typeof headerTitle !== 'string'
? headerTitle
: HeaderTitle;
return (
<RenderedHeaderTitle
onLayout={onLayoutIOS}
allowFontScaling={allowFontScaling == null ? true : allowFontScaling}
style={[color ? { color } : null, titleStyle]}
>
{titleString}
</RenderedHeaderTitle>
);
};
_renderLeftComponent = (props: SceneProps): ?React.Node => {
// $FlowFixMe
const { options } = this.props.getScreenDetails(props.scene);
if (
React.isValidElement(options.headerLeft) ||
options.headerLeft === null
) {
return options.headerLeft;
}
if (props.scene.index === 0) {
return null;
}
const backButtonTitle = this._getBackButtonTitleString(props.scene);
const truncatedBackButtonTitle = this._getTruncatedBackButtonTitle(
props.scene
);
const width = this.state.widths[props.scene.key]
? (this.props.layout.initWidth - this.state.widths[props.scene.key]) / 2
: undefined;
const RenderedLeftComponent = options.headerLeft || HeaderBackButton;
return (
<RenderedLeftComponent
onPress={this._navigateBack}
pressColorAndroid={options.headerPressColorAndroid}
tintColor={options.headerTintColor}
title={backButtonTitle}
truncatedTitle={truncatedBackButtonTitle}
titleStyle={options.headerBackTitleStyle}
width={width}
/>
);
};
_renderRightComponent = (props: SceneProps): ?React.Node => {
const details = this.props.getScreenDetails(props.scene);
const { headerRight } = details.options;
return headerRight || null;
};
_renderLeft(props: SceneProps): ?React.Node {
return this._renderSubView(
props,
'left',
this._renderLeftComponent,
this.props.leftInterpolator
);
}
_renderTitle(props: SceneProps, options: *): ?React.Node {
const style = {};
if (Platform.OS === 'android') {
if (!options.hasLeftComponent) {
style.left = 0;
}
if (!options.hasRightComponent) {
style.right = 0;
}
} else if (
Platform.OS === 'ios' &&
!options.hasLeftComponent &&
!options.hasRightComponent
) {
style.left = 0;
style.right = 0;
}
return this._renderSubView(
{ ...props, style },
'title',
this._renderTitleComponent,
this.props.titleInterpolator
);
}
_renderRight(props: SceneProps): ?React.Node {
return this._renderSubView(
props,
'right',
this._renderRightComponent,
this.props.rightInterpolator
);
}
_renderSubView<T>(
props: SceneProps,
name: SubViewName,
renderer: SubViewRenderer<T>,
styleInterpolator: NavigationStyleInterpolator
): ?React.Node {
const { scene } = props;
const { index, isStale, key } = scene;
const offset = this.props.navigation.state.index - index;
if (Math.abs(offset) > 2) {
// Scene is far away from the active scene. Hides it to avoid unnecessary
// rendering.
return null;
}
const subView = renderer(props);
if (subView == null) {
return null;
}
const pointerEvents = offset !== 0 || isStale ? 'none' : 'box-none';
return (
<Animated.View
pointerEvents={pointerEvents}
key={`${name}_${key}`}
style={[
styles.item,
styles[name],
props.style,
styleInterpolator({
// todo: determine if we really need to splat all this.props
...this.props,
...props,
}),
]}
>
{subView}
</Animated.View>
);
}
_renderHeader(props: SceneProps): React.Node {
const left = this._renderLeft(props);
const right = this._renderRight(props);
const title = this._renderTitle(props, {
hasLeftComponent: !!left,
hasRightComponent: !!right,
});
return (
<View
style={[StyleSheet.absoluteFill, styles.header]}
key={`scene_${props.scene.key}`}
>
{title}
{left}
{right}
</View>
);
}
render() {
let appBar;
if (this.props.mode === 'float') {
const scenesProps: Array<
SceneProps
> = this.props.scenes.map((scene: NavigationScene) => ({
position: this.props.position,
progress: this.props.progress,
scene,
}));
appBar = scenesProps.map(this._renderHeader, this);
} else {
appBar = this._renderHeader({
position: new Animated.Value(this.props.scene.index),
progress: new Animated.Value(0),
scene: this.props.scene,
});
}
// eslint-disable-next-line no-unused-vars
const {
scenes,
scene,
position,
screenProps,
progress,
isLandscape,
...rest
} = this.props;
const { options } = this.props.getScreenDetails(scene);
const { headerStyle } = options;
const appBarHeight = Platform.OS === 'ios' ? (isLandscape ? 32 : 44) : 56;
const containerStyles = [
styles.container,
{
height: appBarHeight,
},
headerStyle,
];
return (
<Animated.View {...rest}>
<SafeAreaView
style={containerStyles}
forceInset={{ top: 'always', bottom: 'never' }}
>
<View style={styles.appBar}>{appBar}</View>
</SafeAreaView>
</Animated.View>
);
}
}
let platformContainerStyles;
if (Platform.OS === 'ios') {
platformContainerStyles = {
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: 'rgba(0, 0, 0, .3)',
};
} else {
platformContainerStyles = {
shadowColor: 'black',
shadowOpacity: 0.1,
shadowRadius: StyleSheet.hairlineWidth,
shadowOffset: {
height: StyleSheet.hairlineWidth,
},
elevation: 4,
};
}
const styles = StyleSheet.create({
container: {
backgroundColor: Platform.OS === 'ios' ? '#F7F7F7' : '#FFF',
...platformContainerStyles,
},
appBar: {
flex: 1,
},
header: {
flexDirection: 'row',
},
item: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'transparent',
},
title: {
bottom: 0,
left: TITLE_OFFSET,
right: TITLE_OFFSET,
top: 0,
position: 'absolute',
alignItems: Platform.OS === 'ios' ? 'center' : 'flex-start',
},
left: {
left: 0,
bottom: 0,
top: 0,
position: 'absolute',
},
right: {
right: 0,
bottom: 0,
top: 0,
position: 'absolute',
},
});
export default withOrientation(Header);