mirror of
https://github.com/status-im/react-native.git
synced 2025-02-23 22:58:19 +00:00
Summary: Related to #22100 `Libraries/Components/StatusBar/StatusBar.js` Enhance StatusBar with mergePropsStack and _defaultProps. - [x] npm run prettier - [x] npm run flow - [x] npm run flow-check-ios - [x] npm run flow-check-android - [x] npm run lint - [x] npm run test - [x] ./scripts/run-android-local-unit-tests.sh [GENERAL][ENHANCEMENT][StatusBar.js] - apply flow strict-local Pull Request resolved: https://github.com/facebook/react-native/pull/22282 Reviewed By: TheSavior Differential Revision: D13103971 Pulled By: RSNara fbshipit-source-id: 27f69c6df3a8a7792fcd595c0ff362943ccab8ca
442 lines
12 KiB
JavaScript
442 lines
12 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow strict-local
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const React = require('React');
|
|
const Platform = require('Platform');
|
|
|
|
const processColor = require('processColor');
|
|
|
|
const StatusBarManager = require('NativeModules').StatusBarManager;
|
|
|
|
/**
|
|
* Status bar style
|
|
*/
|
|
export type StatusBarStyle = $Enum<{
|
|
/**
|
|
* Default status bar style (dark for iOS, light for Android)
|
|
*/
|
|
default: string,
|
|
/**
|
|
* Dark background, white texts and icons
|
|
*/
|
|
'light-content': string,
|
|
/**
|
|
* Light background, dark texts and icons
|
|
*/
|
|
'dark-content': string,
|
|
}>;
|
|
|
|
/**
|
|
* Status bar animation
|
|
*/
|
|
export type StatusBarAnimation = $Enum<{
|
|
/**
|
|
* No animation
|
|
*/
|
|
none: string,
|
|
/**
|
|
* Fade animation
|
|
*/
|
|
fade: string,
|
|
/**
|
|
* Slide animation
|
|
*/
|
|
slide: string,
|
|
}>;
|
|
|
|
type AndroidProps = $ReadOnly<{|
|
|
/**
|
|
* The background color of the status bar.
|
|
* @platform android
|
|
*/
|
|
backgroundColor?: ?string,
|
|
/**
|
|
* 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
|
|
*/
|
|
translucent?: ?boolean,
|
|
|}>;
|
|
|
|
type IOSProps = $ReadOnly<{|
|
|
/**
|
|
* If the network activity indicator should be visible.
|
|
*
|
|
* @platform ios
|
|
*/
|
|
networkActivityIndicatorVisible?: ?boolean,
|
|
/**
|
|
* The transition effect when showing and hiding the status bar using the `hidden`
|
|
* prop. Defaults to 'fade'.
|
|
*
|
|
* @platform ios
|
|
*/
|
|
showHideTransition?: ?('fade' | 'slide'),
|
|
|}>;
|
|
|
|
type Props = $ReadOnly<{|
|
|
...AndroidProps,
|
|
...IOSProps,
|
|
/**
|
|
* If the status bar is hidden.
|
|
*/
|
|
hidden?: ?boolean,
|
|
/**
|
|
* If the transition between status bar property changes should be animated.
|
|
* Supported for backgroundColor, barStyle and hidden.
|
|
*/
|
|
animated?: ?boolean,
|
|
/**
|
|
* Sets the color of the status bar text.
|
|
*/
|
|
barStyle?: ?('default' | 'light-content' | 'dark-content'),
|
|
|}>;
|
|
|
|
type StackEntryProps = {|
|
|
/**
|
|
* The background color of the status bar.
|
|
*
|
|
* @platform android
|
|
*/
|
|
backgroundColor: {|
|
|
value: ?string,
|
|
animated: ?boolean,
|
|
|},
|
|
/**
|
|
* Sets the color of the status bar text.
|
|
*/
|
|
barStyle: {|
|
|
value: ?string,
|
|
animated: ?boolean,
|
|
|},
|
|
/**
|
|
* 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.
|
|
*/
|
|
translucent: ?boolean,
|
|
/**
|
|
*
|
|
*/
|
|
hidden: {|
|
|
value: ?boolean,
|
|
animated: boolean,
|
|
transition: ?('slide' | 'fade'),
|
|
|},
|
|
/**
|
|
* If the network activity indicator should be visible.
|
|
*
|
|
* @platform ios
|
|
*/
|
|
networkActivityIndicatorVisible: ?boolean,
|
|
|};
|
|
|
|
/**
|
|
* Merges the prop stack with the default values.
|
|
*/
|
|
function mergePropsStack(
|
|
propsStack: $ReadOnlyArray<StackEntryProps>,
|
|
defaultValues: StackEntryProps,
|
|
): StackEntryProps {
|
|
const init: StackEntryProps = {
|
|
...defaultValues,
|
|
};
|
|
|
|
return propsStack.reduce((prev, cur) => {
|
|
for (const prop in cur) {
|
|
if (cur[prop] != null) {
|
|
prev[prop] = cur[prop];
|
|
}
|
|
}
|
|
return prev;
|
|
}, init);
|
|
}
|
|
|
|
/**
|
|
* Returns an object to insert in the props stack from the props
|
|
* and the transition/animation info.
|
|
*/
|
|
function createStackEntry(props: Props): StackEntryProps {
|
|
return {
|
|
backgroundColor: {
|
|
value: props.backgroundColor,
|
|
animated: props.animated,
|
|
},
|
|
barStyle: {
|
|
value: props.barStyle,
|
|
animated: props.animated,
|
|
},
|
|
translucent: props.translucent || false,
|
|
hidden: {
|
|
value: props.hidden,
|
|
animated: props.animated || false,
|
|
transition: props.showHideTransition,
|
|
},
|
|
networkActivityIndicatorVisible:
|
|
props.networkActivityIndicatorVisible || false,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 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>
|
|
* ```
|
|
*
|
|
* ### 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
|
|
* to use the static API and the component for the same prop because any value
|
|
* set by the static API will get overriden by the one set by the component in
|
|
* the next render.
|
|
*
|
|
* ### Constants
|
|
*
|
|
* `currentHeight` (Android only) The height of the status bar.
|
|
*/
|
|
class StatusBar extends React.Component<Props> {
|
|
static _propsStack: Array<StackEntryProps> = [];
|
|
|
|
static _defaultProps: StackEntryProps = createStackEntry({
|
|
animated: false,
|
|
showHideTransition: 'fade',
|
|
backgroundColor: 'black',
|
|
barStyle: 'default',
|
|
translucent: false,
|
|
hidden: false,
|
|
networkActivityIndicatorVisible: false,
|
|
});
|
|
|
|
// Timer for updating the native module values at the end of the frame.
|
|
static _updateImmediate = null;
|
|
|
|
// The current merged values from the props stack.
|
|
static _currentValues = null;
|
|
|
|
// 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.
|
|
*
|
|
* @platform android
|
|
*/
|
|
static currentHeight = StatusBarManager.HEIGHT;
|
|
|
|
// Provide an imperative API as static functions of the component.
|
|
// See the corresponding prop for more detail.
|
|
|
|
/**
|
|
* Show or hide the status bar
|
|
* @param hidden Hide the status bar.
|
|
* @param animation Optional animation when
|
|
* changing the status bar hidden property.
|
|
*/
|
|
static setHidden(hidden: boolean, animation?: StatusBarAnimation) {
|
|
StatusBar._defaultProps.hidden.value = hidden;
|
|
if (Platform.OS === 'ios') {
|
|
StatusBarManager.setHidden(hidden, animation || 'none');
|
|
} 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) {
|
|
StatusBar._defaultProps.barStyle.value = style;
|
|
if (Platform.OS === 'ios') {
|
|
StatusBarManager.setStyle(style, animated || false);
|
|
} else if (Platform.OS === 'android') {
|
|
StatusBarManager.setStyle(style);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
StatusBar._defaultProps.backgroundColor.value = color;
|
|
StatusBarManager.setColor(processColor(color), animated || false);
|
|
}
|
|
|
|
/**
|
|
* 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 defaultProps = {
|
|
animated: false,
|
|
showHideTransition: 'fade',
|
|
};
|
|
|
|
_stackEntry = null;
|
|
|
|
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 hierarchy will have priority.
|
|
this._stackEntry = createStackEntry(this.props);
|
|
StatusBar._propsStack.push(this._stackEntry);
|
|
this._updatePropsStack();
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
// When a StatusBar is unmounted, remove itself from the stack and update
|
|
// the native bar with the next props.
|
|
const index = StatusBar._propsStack.indexOf(this._stackEntry);
|
|
StatusBar._propsStack.splice(index, 1);
|
|
|
|
this._updatePropsStack();
|
|
}
|
|
|
|
componentDidUpdate() {
|
|
const index = StatusBar._propsStack.indexOf(this._stackEntry);
|
|
this._stackEntry = createStackEntry(this.props);
|
|
StatusBar._propsStack[index] = this._stackEntry;
|
|
|
|
this._updatePropsStack();
|
|
}
|
|
|
|
/**
|
|
* Updates the native status bar with the props from the stack.
|
|
*/
|
|
_updatePropsStack = () => {
|
|
// 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,
|
|
);
|
|
|
|
// 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') {
|
|
if (
|
|
!oldProps ||
|
|
oldProps.barStyle.value !== mergedProps.barStyle.value
|
|
) {
|
|
StatusBarManager.setStyle(mergedProps.barStyle.value);
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
// Update the current prop values.
|
|
StatusBar._currentValues = mergedProps;
|
|
});
|
|
};
|
|
|
|
render(): React.Node {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
module.exports = StatusBar;
|