react-native-bars
Version:
Components to control your app status and navigation bars.
80 lines (79 loc) • 2.47 kB
JavaScript
import * as React from "react";
import { Platform } from "react-native";
import NativeModule from "./NativeRNBars";
const isSupportedPlatform = Platform.OS === "android" && Platform.Version >= 27;
function createStackEntry({
barStyle = "light-content"
}) {
return {
barStyle
};
}
export class NavigationBar extends React.Component {
static propsStack = [];
static immediate = null;
static mergedProps = null;
static pushStackEntry(props) {
const entry = createStackEntry(props);
NavigationBar.propsStack.push(entry);
NavigationBar.updatePropsStack();
return entry;
}
static popStackEntry(entry) {
const index = NavigationBar.propsStack.indexOf(entry);
if (index !== -1) {
NavigationBar.propsStack.splice(index, 1);
}
NavigationBar.updatePropsStack();
}
static replaceStackEntry(entry, props) {
const newEntry = createStackEntry(props);
const index = NavigationBar.propsStack.indexOf(entry);
if (index !== -1) {
NavigationBar.propsStack[index] = newEntry;
}
NavigationBar.updatePropsStack();
return newEntry;
}
static updatePropsStack() {
// Send the update to the native module only once at the end of the frame.
if (NavigationBar.immediate !== null) {
clearImmediate(NavigationBar.immediate);
}
NavigationBar.immediate = setImmediate(() => {
const oldProps = NavigationBar.mergedProps;
const lastEntry = NavigationBar.propsStack[NavigationBar.propsStack.length - 1];
if (lastEntry != null) {
// Update only if style have changed or if current props are unavailable.
if (isSupportedPlatform && oldProps?.barStyle !== lastEntry.barStyle) {
NativeModule?.setNavigationBarStyle(lastEntry.barStyle);
}
// Update the current props values.
NavigationBar.mergedProps = {
...lastEntry
};
} else {
// Reset current props when the stack is empty.
NavigationBar.mergedProps = null;
}
});
}
stackEntry = null;
componentDidMount() {
this.stackEntry = NavigationBar.pushStackEntry(this.props);
}
componentDidUpdate() {
if (this.stackEntry) {
this.stackEntry = NavigationBar.replaceStackEntry(this.stackEntry, this.props);
}
}
componentWillUnmount() {
if (this.stackEntry) {
NavigationBar.popStackEntry(this.stackEntry);
}
}
render() {
return null;
}
}
//# sourceMappingURL=NavigationBar.js.map