Header.js
14 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
'use strict';
'no babel-plugin-flow-react-proptypes';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var React = _interopRequireWildcard(_react);
var _reactNative = require('react-native');
var _HeaderTitle = require('./HeaderTitle');
var _HeaderTitle2 = _interopRequireDefault(_HeaderTitle);
var _HeaderBackButton = require('./HeaderBackButton');
var _HeaderBackButton2 = _interopRequireDefault(_HeaderBackButton);
var _HeaderStyleInterpolator = require('./HeaderStyleInterpolator');
var _HeaderStyleInterpolator2 = _interopRequireDefault(_HeaderStyleInterpolator);
var _SafeAreaView = require('../SafeAreaView');
var _SafeAreaView2 = _interopRequireDefault(_SafeAreaView);
var _withOrientation = require('../withOrientation');
var _withOrientation2 = _interopRequireDefault(_withOrientation);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var APPBAR_HEIGHT = _reactNative.Platform.OS === 'ios' ? 44 : 56;
var STATUSBAR_HEIGHT = _reactNative.Platform.OS === 'ios' ? 20 : 0;
var TITLE_OFFSET = _reactNative.Platform.OS === 'ios' ? 70 : 56;
var Header = function (_React$PureComponent) {
_inherits(Header, _React$PureComponent);
function Header() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Header);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Header.__proto__ || Object.getPrototypeOf(Header)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
widths: {}
}, _this._navigateBack = function () {
_this.props.navigation.goBack(null);
}, _this._renderTitleComponent = function (props) {
// $FlowFixMe
var details = _this.props.getScreenDetails(props.scene);
var headerTitle = details.options.headerTitle;
if (React.isValidElement(headerTitle)) {
return headerTitle;
}
var titleString = _this._getHeaderTitleString(props.scene);
var titleStyle = details.options.headerTitleStyle;
var color = details.options.headerTintColor;
var allowFontScaling = details.options.headerTitleAllowFontScaling;
// On iOS, width of left/right components depends on the calculated
// size of the title.
var onLayoutIOS = _reactNative.Platform.OS === 'ios' ? function (e) {
_this.setState({
widths: _extends({}, _this.state.widths, _defineProperty({}, props.scene.key, e.nativeEvent.layout.width))
});
} : undefined;
var RenderedHeaderTitle = headerTitle && typeof headerTitle !== 'string' ? headerTitle : _HeaderTitle2.default;
return React.createElement(
RenderedHeaderTitle,
{
onLayout: onLayoutIOS,
allowFontScaling: allowFontScaling == null ? true : allowFontScaling,
style: [color ? { color: color } : null, titleStyle]
},
titleString
);
}, _this._renderLeftComponent = function (props) {
// $FlowFixMe
var _this$props$getScreen = _this.props.getScreenDetails(props.scene),
options = _this$props$getScreen.options;
if (React.isValidElement(options.headerLeft) || options.headerLeft === null) {
return options.headerLeft;
}
if (props.scene.index === 0) {
return null;
}
var backButtonTitle = _this._getBackButtonTitleString(props.scene);
var truncatedBackButtonTitle = _this._getTruncatedBackButtonTitle(props.scene);
var width = _this.state.widths[props.scene.key] ? (_this.props.layout.initWidth - _this.state.widths[props.scene.key]) / 2 : undefined;
var RenderedLeftComponent = options.headerLeft || _HeaderBackButton2.default;
return React.createElement(RenderedLeftComponent, {
onPress: _this._navigateBack,
pressColorAndroid: options.headerPressColorAndroid,
tintColor: options.headerTintColor,
title: backButtonTitle,
truncatedTitle: truncatedBackButtonTitle,
titleStyle: options.headerBackTitleStyle,
width: width
});
}, _this._renderRightComponent = function (props) {
var details = _this.props.getScreenDetails(props.scene);
var headerRight = details.options.headerRight;
return headerRight || null;
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Header, [{
key: '_getHeaderTitleString',
value: function _getHeaderTitleString(scene) {
var sceneOptions = this.props.getScreenDetails(scene).options;
if (typeof sceneOptions.headerTitle === 'string') {
return sceneOptions.headerTitle;
}
return sceneOptions.title;
}
}, {
key: '_getLastScene',
value: function _getLastScene(scene) {
return this.props.scenes.find(function (s) {
return s.index === scene.index - 1;
});
}
}, {
key: '_getBackButtonTitleString',
value: function _getBackButtonTitleString(scene) {
var lastScene = this._getLastScene(scene);
if (!lastScene) {
return null;
}
var headerBackTitle = this.props.getScreenDetails(lastScene).options.headerBackTitle;
if (headerBackTitle || headerBackTitle === null) {
return headerBackTitle;
}
return this._getHeaderTitleString(lastScene);
}
}, {
key: '_getTruncatedBackButtonTitle',
value: function _getTruncatedBackButtonTitle(scene) {
var lastScene = this._getLastScene(scene);
if (!lastScene) {
return null;
}
return this.props.getScreenDetails(lastScene).options.headerTruncatedBackTitle;
}
}, {
key: '_renderLeft',
value: function _renderLeft(props) {
return this._renderSubView(props, 'left', this._renderLeftComponent, this.props.leftInterpolator);
}
}, {
key: '_renderTitle',
value: function _renderTitle(props, options) {
var style = {};
if (_reactNative.Platform.OS === 'android') {
if (!options.hasLeftComponent) {
style.left = 0;
}
if (!options.hasRightComponent) {
style.right = 0;
}
} else if (_reactNative.Platform.OS === 'ios' && !options.hasLeftComponent && !options.hasRightComponent) {
style.left = 0;
style.right = 0;
}
return this._renderSubView(_extends({}, props, { style: style }), 'title', this._renderTitleComponent, this.props.titleInterpolator);
}
}, {
key: '_renderRight',
value: function _renderRight(props) {
return this._renderSubView(props, 'right', this._renderRightComponent, this.props.rightInterpolator);
}
}, {
key: '_renderSubView',
value: function _renderSubView(props, name, renderer, styleInterpolator) {
var scene = props.scene;
var index = scene.index,
isStale = scene.isStale,
key = scene.key;
var 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;
}
var subView = renderer(props);
if (subView == null) {
return null;
}
var pointerEvents = offset !== 0 || isStale ? 'none' : 'box-none';
return React.createElement(
_reactNative.Animated.View,
{
pointerEvents: pointerEvents,
key: name + '_' + key,
style: [styles.item, styles[name], props.style, styleInterpolator(_extends({}, this.props, props))]
},
subView
);
}
}, {
key: '_renderHeader',
value: function _renderHeader(props) {
var left = this._renderLeft(props);
var right = this._renderRight(props);
var title = this._renderTitle(props, {
hasLeftComponent: !!left,
hasRightComponent: !!right
});
return React.createElement(
_reactNative.View,
{
style: [_reactNative.StyleSheet.absoluteFill, styles.header],
key: 'scene_' + props.scene.key
},
title,
left,
right
);
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var appBar = void 0;
if (this.props.mode === 'float') {
var scenesProps = this.props.scenes.map(function (scene) {
return {
position: _this2.props.position,
progress: _this2.props.progress,
scene: scene
};
});
appBar = scenesProps.map(this._renderHeader, this);
} else {
appBar = this._renderHeader({
position: new _reactNative.Animated.Value(this.props.scene.index),
progress: new _reactNative.Animated.Value(0),
scene: this.props.scene
});
}
// eslint-disable-next-line no-unused-vars
var _props = this.props,
scenes = _props.scenes,
scene = _props.scene,
position = _props.position,
screenProps = _props.screenProps,
progress = _props.progress,
isLandscape = _props.isLandscape,
rest = _objectWithoutProperties(_props, ['scenes', 'scene', 'position', 'screenProps', 'progress', 'isLandscape']);
var _props$getScreenDetai = this.props.getScreenDetails(scene),
options = _props$getScreenDetai.options;
var headerStyle = options.headerStyle;
var appBarHeight = _reactNative.Platform.OS === 'ios' ? isLandscape ? 32 : 44 : 56;
var containerStyles = [styles.container, {
height: appBarHeight
}, headerStyle];
return React.createElement(
_reactNative.Animated.View,
rest,
React.createElement(
_SafeAreaView2.default,
{
style: containerStyles,
forceInset: { top: 'always', bottom: 'never' }
},
React.createElement(
_reactNative.View,
{ style: styles.appBar },
appBar
)
)
);
}
}], [{
key: 'HEIGHT',
get: function get() {
console.warn('Header.HEIGHT is deprecated and will be removed before react-navigation comes out of beta.');
return APPBAR_HEIGHT + STATUSBAR_HEIGHT;
}
}]);
return Header;
}(React.PureComponent);
Header.defaultProps = {
leftInterpolator: _HeaderStyleInterpolator2.default.forLeft,
titleInterpolator: _HeaderStyleInterpolator2.default.forCenter,
rightInterpolator: _HeaderStyleInterpolator2.default.forRight
};
var platformContainerStyles = void 0;
if (_reactNative.Platform.OS === 'ios') {
platformContainerStyles = {
borderBottomWidth: _reactNative.StyleSheet.hairlineWidth,
borderBottomColor: 'rgba(0, 0, 0, .3)'
};
} else {
platformContainerStyles = {
shadowColor: 'black',
shadowOpacity: 0.1,
shadowRadius: _reactNative.StyleSheet.hairlineWidth,
shadowOffset: {
height: _reactNative.StyleSheet.hairlineWidth
},
elevation: 4
};
}
var styles = _reactNative.StyleSheet.create({
container: _extends({
backgroundColor: _reactNative.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: _reactNative.Platform.OS === 'ios' ? 'center' : 'flex-start'
},
left: {
left: 0,
bottom: 0,
top: 0,
position: 'absolute'
},
right: {
right: 0,
bottom: 0,
top: 0,
position: 'absolute'
}
});
exports.default = (0, _withOrientation2.default)(Header);