Skip to content

Commit e137b62

Browse files
committed
Add cross platform status bar and handle default backgroundColor
1 parent 6f49242 commit e137b62

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

index.js

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import React, {
22
StyleSheet,
33
Navigator,
4-
StatusBarIOS,
54
View,
65
Platform,
76
PropTypes,
87
Text,
8+
StatusBar,
99
} from 'react-native';
1010

1111
import EventEmitter from 'react-native/Libraries/vendor/emitter/EventEmitter';
1212

1313
import NavBarContainer from './components/NavBarContainer';
1414
import * as Styles from './styles';
1515
import aspect from 'aspect-js';
16+
import _ from 'underscore';
1617

1718
const propTypes = {
1819
backButtonComponent: PropTypes.func,
@@ -27,6 +28,7 @@ const propTypes = {
2728
noStatusBar: PropTypes.bool,
2829
rightCorner: PropTypes.func,
2930
statusBarColor: PropTypes.string,
31+
statusBarProps: PropTypes.object,
3032
titleStyle: Text.propTypes.style,
3133
};
3234

@@ -287,16 +289,8 @@ class Router extends React.Component {
287289

288290
render() {
289291
let navigationBar;
290-
// Status bar color
291-
if (Platform.OS === 'ios') {
292-
if (this.props.statusBarColor === 'black') {
293-
StatusBarIOS.setStyle(0); // "Default" style according to StatusBarIOS.js
294-
} else {
295-
StatusBarIOS.setStyle(1); // "light-content" style according to StatusBarIOS.js
296-
}
297-
} else if (Platform.OS === 'android') {
298-
// no android version yet
299-
}
292+
let statusBar;
293+
let statusBarProps = {};
300294

301295
if (!this.props.hideNavigationBar) {
302296
navigationBar = (
@@ -320,14 +314,73 @@ class Router extends React.Component {
320314
);
321315
}
322316

317+
// Check if StatusBar is available (React-Native >= 0.20)
318+
if (StatusBar) {
319+
// Check for default values provided to Router
320+
if (this.props.statusBarProps) {
321+
// statusBarProps = _.defaults(this.props.statusBarProps, statusBarProps);
322+
statusBarProps = this.props.statusBarProps;
323+
}
324+
325+
// Check for values provided to current route
326+
if (this.state.route.statusBarProps) {
327+
statusBarProps = _.defaults(this.state.route.statusBarProps, statusBarProps);
328+
}
329+
330+
// Android specific code
331+
if (Platform.OS === 'android') {
332+
if (!_.has(statusBarProps, 'backgroundColor') && !_.has(statusBarProps, 'translucent')) {
333+
let backgroundColor;
334+
335+
if (this.state.route.headerStyle && this.state.router.headerStyle.backgroundColor) {
336+
// If current route has specific header style
337+
const stateHeaderStyle = StyleSheet.flatten(this.props.headerStyle);
338+
339+
if (stateHeaderStyle && stateHeaderStyle.backgroundColor) {
340+
backgroundColor = stateHeaderStyle.backgroundColor;
341+
}
342+
} else if (this.props.headerStyle) {
343+
// Otherwise, get backgroundColor as specified to Router
344+
const propsHeaderStyle = StyleSheet.flatten(this.props.headerStyle);
345+
346+
if (propsHeaderStyle && propsHeaderStyle.backgroundColor) {
347+
backgroundColor = propsHeaderStyle.backgroundColor;
348+
}
349+
}
350+
351+
if (backgroundColor) {
352+
statusBarProps.backgroundColor = backgroundColor;
353+
}
354+
}
355+
} else if (Platform.OS === 'ios') {
356+
if (!_.has(statusBarProps, 'barStyle')) {
357+
// NOTE Deprecated prop, shouldn't be used.
358+
if (this.props.statusBarColor === 'black') {
359+
statusBarProps.barStyle = 'default';
360+
} else {
361+
statusBarProps.barStyle = 'light-content';
362+
}
363+
}
364+
}
365+
366+
statusBar = (
367+
<StatusBar
368+
{...statusBarProps}
369+
/>
370+
);
371+
}
372+
323373
return (
324-
<Navigator
325-
ref="navigator"
326-
initialRoute={this.props.firstRoute}
327-
navigationBar={navigationBar}
328-
renderScene={this.renderScene}
329-
configureScene={this.configureScene}
330-
/>
374+
<View style={{ flex: 1 }}>
375+
{statusBar}
376+
<Navigator
377+
ref="navigator"
378+
initialRoute={this.props.firstRoute}
379+
navigationBar={navigationBar}
380+
renderScene={this.renderScene}
381+
configureScene={this.configureScene}
382+
/>
383+
</View>
331384
);
332385
}
333386
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
},
3434
"homepage": "https://github.com/react-native-simple-router-community/react-native-simple-router",
3535
"dependencies": {
36-
"aspect-js": "^1.0.3"
36+
"aspect-js": "^1.0.3",
37+
"underscore": "^1.8.3"
3738
},
3839
"peerDependencies": {
3940
"react-native": ">=0.12.0 || 0.5.0-rc1 || 0.6.0-rc || 0.7.0-rc || 0.7.0-rc.2 || 0.8.0-rc || 0.8.0-rc.2 || 0.9.0-rc || 0.10.0-rc || 0.11.0-rc || 0.12.0-rc || 0.13.0-rc || 0.14.0-rc || 0.15.0-rc || 0.16.0-rc || 0.17.0-rc || 0.18.0-rc || 0.19.0-rc || 0.20.0-rc || 0.20.0-rc1"

0 commit comments

Comments
 (0)