2016-02-03 14:40:39 +00:00
|
|
|
|
/**
|
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
*
|
|
|
|
|
* @providesModule StatusBar
|
|
|
|
|
* @flow
|
|
|
|
|
*/
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const React = require('React');
|
2017-04-12 23:09:48 +00:00
|
|
|
|
const PropTypes = require('prop-types');
|
2016-02-03 14:40:39 +00:00
|
|
|
|
const ColorPropType = require('ColorPropType');
|
|
|
|
|
const Platform = require('Platform');
|
|
|
|
|
|
|
|
|
|
const processColor = require('processColor');
|
|
|
|
|
|
|
|
|
|
const StatusBarManager = require('NativeModules').StatusBarManager;
|
|
|
|
|
|
2016-06-21 21:22:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* Status bar style
|
|
|
|
|
*/
|
2016-02-16 11:04:16 +00:00
|
|
|
|
export type StatusBarStyle = $Enum<{
|
2016-06-21 21:22:44 +00:00
|
|
|
|
/**
|
2016-09-30 19:49:05 +00:00
|
|
|
|
* Default status bar style (dark for iOS, light for Android)
|
2016-06-21 21:22:44 +00:00
|
|
|
|
*/
|
2016-02-16 11:04:16 +00:00
|
|
|
|
'default': string,
|
2016-06-21 21:22:44 +00:00
|
|
|
|
/**
|
2016-09-30 19:49:05 +00:00
|
|
|
|
* Dark background, white texts and icons
|
2016-06-21 21:22:44 +00:00
|
|
|
|
*/
|
2016-02-16 11:04:16 +00:00
|
|
|
|
'light-content': string,
|
2016-09-30 19:49:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Light background, dark texts and icons
|
|
|
|
|
*/
|
|
|
|
|
'dark-content': string,
|
2016-02-16 11:04:16 +00:00
|
|
|
|
}>;
|
|
|
|
|
|
2016-06-21 21:22:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* Status bar animation
|
|
|
|
|
*/
|
2016-02-16 11:04:16 +00:00
|
|
|
|
export type StatusBarAnimation = $Enum<{
|
2016-06-21 21:22:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* No animation
|
|
|
|
|
*/
|
2016-02-16 11:04:16 +00:00
|
|
|
|
'none': string,
|
2016-06-21 21:22:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* Fade animation
|
|
|
|
|
*/
|
2016-02-16 11:04:16 +00:00
|
|
|
|
'fade': string,
|
2016-06-21 21:22:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* Slide animation
|
|
|
|
|
*/
|
2016-02-16 11:04:16 +00:00
|
|
|
|
'slide': string,
|
|
|
|
|
}>;
|
|
|
|
|
|
2016-02-03 14:40:39 +00:00
|
|
|
|
type DefaultProps = {
|
2016-08-09 13:32:41 +00:00
|
|
|
|
animated: boolean,
|
2016-02-03 14:40:39 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Merges the prop stack with the default values.
|
|
|
|
|
*/
|
2016-02-16 11:04:16 +00:00
|
|
|
|
function mergePropsStack(propsStack: Array<Object>, defaultValues: Object): Object {
|
2016-02-03 14:40:39 +00:00
|
|
|
|
return propsStack.reduce((prev, cur) => {
|
2016-08-09 13:32:41 +00:00
|
|
|
|
for (const prop in cur) {
|
2016-03-17 23:53:46 +00:00
|
|
|
|
if (cur[prop] != null) {
|
|
|
|
|
prev[prop] = cur[prop];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return prev;
|
|
|
|
|
}, Object.assign({}, defaultValues));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an object to insert in the props stack from the props
|
|
|
|
|
* and the transition/animation info.
|
|
|
|
|
*/
|
|
|
|
|
function createStackEntry(props: any): any {
|
|
|
|
|
return {
|
|
|
|
|
backgroundColor: props.backgroundColor != null ? {
|
|
|
|
|
value: props.backgroundColor,
|
|
|
|
|
animated: props.animated,
|
|
|
|
|
} : null,
|
|
|
|
|
barStyle: props.barStyle != null ? {
|
|
|
|
|
value: props.barStyle,
|
|
|
|
|
animated: props.animated,
|
|
|
|
|
} : null,
|
|
|
|
|
translucent: props.translucent,
|
|
|
|
|
hidden: props.hidden != null ? {
|
|
|
|
|
value: props.hidden,
|
|
|
|
|
animated: props.animated,
|
|
|
|
|
transition: props.showHideTransition,
|
|
|
|
|
} : null,
|
|
|
|
|
networkActivityIndicatorVisible: props.networkActivityIndicatorVisible,
|
|
|
|
|
};
|
2016-02-03 14:40:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Component to control the app status bar.
|
|
|
|
|
*
|
|
|
|
|
* ### Usage with Navigator
|
|
|
|
|
*
|
|
|
|
|
* It is possible to have multiple `StatusBar` components mounted at the same
|
|
|
|
|
* time. The props will be merged in the order the `StatusBar` components were
|
|
|
|
|
* mounted. One use case is to specify status bar styles per route using `Navigator`.
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* <View>
|
|
|
|
|
* <StatusBar
|
|
|
|
|
* backgroundColor="blue"
|
|
|
|
|
* barStyle="light-content"
|
|
|
|
|
* />
|
|
|
|
|
* <Navigator
|
|
|
|
|
* initialRoute={{statusBarHidden: true}}
|
|
|
|
|
* renderScene={(route, navigator) =>
|
|
|
|
|
* <View>
|
|
|
|
|
* <StatusBar hidden={route.statusBarHidden} />
|
|
|
|
|
* ...
|
|
|
|
|
* </View>
|
|
|
|
|
* }
|
|
|
|
|
* />
|
|
|
|
|
* </View>
|
|
|
|
|
* ```
|
2016-02-16 11:04:16 +00:00
|
|
|
|
*
|
|
|
|
|
* ### Imperative API
|
|
|
|
|
*
|
|
|
|
|
* For cases where using a component is not ideal, there is also an imperative
|
|
|
|
|
* API exposed as static functions on the component. It is however not recommended
|
2016-03-29 16:17:27 +00:00
|
|
|
|
* to use the static API and the component for the same prop because any value
|
2016-02-16 11:04:16 +00:00
|
|
|
|
* set by the static API will get overriden by the one set by the component in
|
|
|
|
|
* the next render.
|
2016-11-06 00:03:02 +00:00
|
|
|
|
*
|
|
|
|
|
* ### Constants
|
|
|
|
|
*
|
2017-01-26 01:55:20 +00:00
|
|
|
|
* `currentHeight` (Android only) The height of the status bar.
|
2016-02-03 14:40:39 +00:00
|
|
|
|
*/
|
2016-07-26 08:00:02 +00:00
|
|
|
|
class StatusBar extends React.Component {
|
|
|
|
|
props: {
|
|
|
|
|
hidden?: boolean,
|
|
|
|
|
animated?: boolean,
|
|
|
|
|
backgroundColor?: $FlowFixMe,
|
|
|
|
|
translucent?: boolean,
|
2016-09-30 19:49:05 +00:00
|
|
|
|
barStyle?: 'default' | 'light-content' | 'dark-content',
|
2016-07-26 08:00:02 +00:00
|
|
|
|
networkActivityIndicatorVisible?: boolean,
|
|
|
|
|
showHideTransition?: 'fade' | 'slide',
|
|
|
|
|
};
|
2016-02-16 11:04:16 +00:00
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
|
static _propsStack = [];
|
2016-06-21 21:22:44 +00:00
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
|
static _defaultProps = createStackEntry({
|
|
|
|
|
animated: false,
|
|
|
|
|
showHideTransition: 'fade',
|
|
|
|
|
backgroundColor: 'black',
|
|
|
|
|
barStyle: 'default',
|
|
|
|
|
translucent: false,
|
|
|
|
|
hidden: false,
|
|
|
|
|
networkActivityIndicatorVisible: false,
|
|
|
|
|
});
|
2016-02-16 11:04:16 +00:00
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
|
// Timer for updating the native module values at the end of the frame.
|
|
|
|
|
static _updateImmediate = null;
|
2016-02-16 11:04:16 +00:00
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
|
// The current merged values from the props stack.
|
|
|
|
|
static _currentValues = null;
|
2016-02-16 11:04:16 +00:00
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
|
// TODO(janic): Provide a real API to deal with status bar height. See the
|
|
|
|
|
// discussion in #6195.
|
|
|
|
|
/**
|
|
|
|
|
* The current height of the status bar on the device.
|
2017-01-26 01:55:20 +00:00
|
|
|
|
*
|
|
|
|
|
* @platform android
|
2016-07-26 08:00:02 +00:00
|
|
|
|
*/
|
|
|
|
|
static currentHeight = StatusBarManager.HEIGHT;
|
2016-02-16 11:04:16 +00:00
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
|
// Provide an imperative API as static functions of the component.
|
|
|
|
|
// See the corresponding prop for more detail.
|
2016-02-03 14:40:39 +00:00
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Show or hide the status bar
|
2016-11-04 16:42:04 +00:00
|
|
|
|
* @param hidden Hide the status bar.
|
2016-07-26 08:00:02 +00:00
|
|
|
|
* @param animation Optional animation when
|
|
|
|
|
* changing the status bar hidden property.
|
|
|
|
|
*/
|
|
|
|
|
static setHidden(hidden: boolean, animation?: StatusBarAnimation) {
|
|
|
|
|
animation = animation || 'none';
|
|
|
|
|
StatusBar._defaultProps.hidden.value = hidden;
|
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
|
StatusBarManager.setHidden(hidden, animation);
|
|
|
|
|
} else if (Platform.OS === 'android') {
|
|
|
|
|
StatusBarManager.setHidden(hidden);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the status bar style
|
|
|
|
|
* @param style Status bar style to set
|
|
|
|
|
* @param animated Animate the style change.
|
|
|
|
|
*/
|
|
|
|
|
static setBarStyle(style: StatusBarStyle, animated?: boolean) {
|
|
|
|
|
animated = animated || false;
|
|
|
|
|
StatusBar._defaultProps.barStyle.value = style;
|
2016-09-30 19:49:05 +00:00
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
|
StatusBarManager.setStyle(style, animated);
|
|
|
|
|
} else if (Platform.OS === 'android') {
|
|
|
|
|
StatusBarManager.setStyle(style);
|
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Control the visibility of the network activity indicator
|
|
|
|
|
* @param visible Show the indicator.
|
|
|
|
|
*/
|
|
|
|
|
static setNetworkActivityIndicatorVisible(visible: boolean) {
|
|
|
|
|
if (Platform.OS !== 'ios') {
|
|
|
|
|
console.warn('`setNetworkActivityIndicatorVisible` is only available on iOS');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
StatusBar._defaultProps.networkActivityIndicatorVisible = visible;
|
|
|
|
|
StatusBarManager.setNetworkActivityIndicatorVisible(visible);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the background color for the status bar
|
|
|
|
|
* @param color Background color.
|
|
|
|
|
* @param animated Animate the style change.
|
|
|
|
|
*/
|
|
|
|
|
static setBackgroundColor(color: string, animated?: boolean) {
|
|
|
|
|
if (Platform.OS !== 'android') {
|
|
|
|
|
console.warn('`setBackgroundColor` is only available on Android');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
animated = animated || false;
|
|
|
|
|
StatusBar._defaultProps.backgroundColor.value = color;
|
|
|
|
|
StatusBarManager.setColor(processColor(color), animated);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Control the translucency of the status bar
|
|
|
|
|
* @param translucent Set as translucent.
|
|
|
|
|
*/
|
|
|
|
|
static setTranslucent(translucent: boolean) {
|
|
|
|
|
if (Platform.OS !== 'android') {
|
|
|
|
|
console.warn('`setTranslucent` is only available on Android');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
StatusBar._defaultProps.translucent = translucent;
|
|
|
|
|
StatusBarManager.setTranslucent(translucent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static propTypes = {
|
2016-02-03 14:40:39 +00:00
|
|
|
|
/**
|
|
|
|
|
* If the status bar is hidden.
|
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
|
hidden: PropTypes.bool,
|
2016-02-03 14:40:39 +00:00
|
|
|
|
/**
|
|
|
|
|
* If the transition between status bar property changes should be animated.
|
|
|
|
|
* Supported for backgroundColor, barStyle and hidden.
|
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
|
animated: PropTypes.bool,
|
2016-02-03 14:40:39 +00:00
|
|
|
|
/**
|
|
|
|
|
* The background color of the status bar.
|
|
|
|
|
* @platform android
|
|
|
|
|
*/
|
|
|
|
|
backgroundColor: ColorPropType,
|
|
|
|
|
/**
|
|
|
|
|
* If the status bar is translucent.
|
|
|
|
|
* When translucent is set to true, the app will draw under the status bar.
|
|
|
|
|
* This is useful when using a semi transparent status bar color.
|
|
|
|
|
*
|
|
|
|
|
* @platform android
|
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
|
translucent: PropTypes.bool,
|
2016-02-03 14:40:39 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets the color of the status bar text.
|
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
|
barStyle: PropTypes.oneOf([
|
2016-02-03 14:40:39 +00:00
|
|
|
|
'default',
|
|
|
|
|
'light-content',
|
2016-09-30 19:49:05 +00:00
|
|
|
|
'dark-content',
|
2016-02-03 14:40:39 +00:00
|
|
|
|
]),
|
|
|
|
|
/**
|
|
|
|
|
* If the network activity indicator should be visible.
|
|
|
|
|
*
|
|
|
|
|
* @platform ios
|
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
|
networkActivityIndicatorVisible: PropTypes.bool,
|
2016-02-03 14:40:39 +00:00
|
|
|
|
/**
|
|
|
|
|
* The transition effect when showing and hiding the status bar using the `hidden`
|
|
|
|
|
* prop. Defaults to 'fade'.
|
|
|
|
|
*
|
|
|
|
|
* @platform ios
|
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
|
showHideTransition: PropTypes.oneOf([
|
2016-02-03 14:40:39 +00:00
|
|
|
|
'fade',
|
|
|
|
|
'slide',
|
|
|
|
|
]),
|
2016-07-26 08:00:02 +00:00
|
|
|
|
};
|
2016-02-03 14:40:39 +00:00
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
|
static defaultProps = {
|
|
|
|
|
animated: false,
|
|
|
|
|
showHideTransition: 'fade',
|
|
|
|
|
};
|
2016-02-03 14:40:39 +00:00
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
|
_stackEntry = null;
|
2016-03-17 23:53:46 +00:00
|
|
|
|
|
2016-02-03 14:40:39 +00:00
|
|
|
|
componentDidMount() {
|
|
|
|
|
// Every time a StatusBar component is mounted, we push it's prop to a stack
|
|
|
|
|
// and always update the native status bar with the props from the top of then
|
|
|
|
|
// stack. This allows having multiple StatusBar components and the one that is
|
|
|
|
|
// added last or is deeper in the view hierachy will have priority.
|
2016-03-17 23:53:46 +00:00
|
|
|
|
this._stackEntry = createStackEntry(this.props);
|
|
|
|
|
StatusBar._propsStack.push(this._stackEntry);
|
2016-02-03 14:40:39 +00:00
|
|
|
|
this._updatePropsStack();
|
2016-07-26 08:00:02 +00:00
|
|
|
|
}
|
2016-02-03 14:40:39 +00:00
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
// When a StatusBar is unmounted, remove itself from the stack and update
|
|
|
|
|
// the native bar with the next props.
|
2016-07-26 08:00:02 +00:00
|
|
|
|
// $FlowFixMe found when converting React.createClass to ES6
|
2016-03-17 23:53:46 +00:00
|
|
|
|
const index = StatusBar._propsStack.indexOf(this._stackEntry);
|
2016-02-03 14:40:39 +00:00
|
|
|
|
StatusBar._propsStack.splice(index, 1);
|
|
|
|
|
|
|
|
|
|
this._updatePropsStack();
|
2016-07-26 08:00:02 +00:00
|
|
|
|
}
|
2016-02-03 14:40:39 +00:00
|
|
|
|
|
2016-03-17 23:53:46 +00:00
|
|
|
|
componentDidUpdate() {
|
2016-07-26 08:00:02 +00:00
|
|
|
|
// $FlowFixMe found when converting React.createClass to ES6
|
2016-03-17 23:53:46 +00:00
|
|
|
|
const index = StatusBar._propsStack.indexOf(this._stackEntry);
|
|
|
|
|
this._stackEntry = createStackEntry(this.props);
|
|
|
|
|
StatusBar._propsStack[index] = this._stackEntry;
|
2016-02-03 14:40:39 +00:00
|
|
|
|
|
|
|
|
|
this._updatePropsStack();
|
2016-07-26 08:00:02 +00:00
|
|
|
|
}
|
2016-02-03 14:40:39 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the native status bar with the props from the stack.
|
|
|
|
|
*/
|
2016-07-26 08:00:02 +00:00
|
|
|
|
_updatePropsStack = () => {
|
2016-03-17 23:53:46 +00:00
|
|
|
|
// Send the update to the native module only once at the end of the frame.
|
|
|
|
|
clearImmediate(StatusBar._updateImmediate);
|
|
|
|
|
StatusBar._updateImmediate = setImmediate(() => {
|
|
|
|
|
const oldProps = StatusBar._currentValues;
|
|
|
|
|
const mergedProps = mergePropsStack(StatusBar._propsStack, StatusBar._defaultProps);
|
2016-02-03 14:40:39 +00:00
|
|
|
|
|
2016-03-17 23:53:46 +00:00
|
|
|
|
// Update the props that have changed using the merged values from the props stack.
|
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
|
if (!oldProps || oldProps.barStyle.value !== mergedProps.barStyle.value) {
|
|
|
|
|
StatusBarManager.setStyle(
|
|
|
|
|
mergedProps.barStyle.value,
|
|
|
|
|
mergedProps.barStyle.animated,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (!oldProps || oldProps.hidden.value !== mergedProps.hidden.value) {
|
|
|
|
|
StatusBarManager.setHidden(
|
|
|
|
|
mergedProps.hidden.value,
|
|
|
|
|
mergedProps.hidden.animated ?
|
|
|
|
|
mergedProps.hidden.transition :
|
|
|
|
|
'none',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!oldProps || oldProps.networkActivityIndicatorVisible !== mergedProps.networkActivityIndicatorVisible) {
|
|
|
|
|
StatusBarManager.setNetworkActivityIndicatorVisible(
|
|
|
|
|
mergedProps.networkActivityIndicatorVisible
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else if (Platform.OS === 'android') {
|
2016-09-30 19:49:05 +00:00
|
|
|
|
if (!oldProps || oldProps.barStyle.value !== mergedProps.barStyle.value) {
|
|
|
|
|
StatusBarManager.setStyle(mergedProps.barStyle.value);
|
|
|
|
|
}
|
2016-03-17 23:53:46 +00:00
|
|
|
|
if (!oldProps || oldProps.backgroundColor.value !== mergedProps.backgroundColor.value) {
|
|
|
|
|
StatusBarManager.setColor(
|
|
|
|
|
processColor(mergedProps.backgroundColor.value),
|
|
|
|
|
mergedProps.backgroundColor.animated,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (!oldProps || oldProps.hidden.value !== mergedProps.hidden.value) {
|
|
|
|
|
StatusBarManager.setHidden(mergedProps.hidden.value);
|
|
|
|
|
}
|
|
|
|
|
if (!oldProps || oldProps.translucent !== mergedProps.translucent) {
|
|
|
|
|
StatusBarManager.setTranslucent(mergedProps.translucent);
|
|
|
|
|
}
|
2016-02-03 14:40:39 +00:00
|
|
|
|
}
|
2016-03-17 23:53:46 +00:00
|
|
|
|
// Update the current prop values.
|
|
|
|
|
StatusBar._currentValues = mergedProps;
|
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
|
};
|
2016-02-03 14:40:39 +00:00
|
|
|
|
|
2016-10-16 11:11:59 +00:00
|
|
|
|
render(): ?React.Element<any> {
|
2016-02-03 14:40:39 +00:00
|
|
|
|
return null;
|
2016-07-26 08:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-03 14:40:39 +00:00
|
|
|
|
|
|
|
|
|
module.exports = StatusBar;
|