/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow strict-local * @format */ 'use strict'; import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; import RNTesterText from '../../components/RNTesterText'; import React from 'react'; import { Modal, StatusBar, StyleSheet, TouchableHighlight, View, } from 'react-native'; const colors = ['#ff0000', '#00ff00', '#0000ff', 'rgba(0, 0, 0, 0.4)']; const barStyles = ['default', 'light-content', 'dark-content']; const showHideTransitions = ['fade', 'slide']; function getValue(values: Array, index: number): T { return values[index % values.length]; } class StatusBarHiddenExample extends React.Component<{...}, $FlowFixMe> { state: | $FlowFixMe | {animated: boolean, hidden: boolean, showHideTransition: string} = { animated: true, hidden: false, showHideTransition: getValue(showHideTransitions, 0), }; _showHideTransitionIndex = 0; _onChangeAnimated = () => { this.setState({animated: !this.state.animated}); }; _onChangeHidden = () => { this.setState({hidden: !this.state.hidden}); }; _onChangeTransition = () => { this._showHideTransitionIndex++; this.setState({ showHideTransition: getValue( showHideTransitions, this._showHideTransitionIndex, ), }); }; render(): React.Node { return ( ); } } class StatusBarStyleExample extends React.Component<{...}, $FlowFixMe> { _barStyleIndex = 0; _onChangeBarStyle = () => { this._barStyleIndex++; this.setState({barStyle: getValue(barStyles, this._barStyleIndex)}); }; _onChangeAnimated = () => { this.setState({animated: !this.state.animated}); }; state: $FlowFixMe | {animated: boolean, barStyle: string} = { animated: true, barStyle: getValue(barStyles, this._barStyleIndex), }; render(): React.Node { return ( style: '{getValue(barStyles, this._barStyleIndex)}' (default is dark for iOS, light for Android) animated (ios only): {this.state.animated ? 'true' : 'false'} ); } } class StatusBarBackgroundColorExample extends React.Component< {...}, $FlowFixMe, > { state: $FlowFixMe | {animated: boolean, backgroundColor: string} = { animated: true, backgroundColor: getValue(colors, 0), }; _colorIndex = 0; _onChangeBackgroundColor = () => { this._colorIndex++; this.setState({backgroundColor: getValue(colors, this._colorIndex)}); }; _onChangeAnimated = () => { this.setState({animated: !this.state.animated}); }; render(): React.Node { return ( backgroundColor: '{getValue(colors, this._colorIndex)}' animated: {this.state.animated ? 'true' : 'false'} ); } } class StatusBarTranslucentExample extends React.Component<{...}, $FlowFixMe> { state: $FlowFixMe | {translucent: boolean} = { translucent: false, }; _onChangeTranslucent = () => { this.setState({ translucent: !this.state.translucent, }); }; render(): React.Node { return ( translucent: {this.state.translucent ? 'true' : 'false'} ); } } class StatusBarStaticIOSExample extends React.Component<{...}> { render(): React.Node { return ( { StatusBar.setHidden(true, 'slide'); }}> setHidden(true, 'slide') { StatusBar.setHidden(false, 'fade'); }}> setHidden(false, 'fade') { StatusBar.setBarStyle('default', true); }}> setBarStyle('default', true) (default is dark for iOS, light for Android) { StatusBar.setBarStyle('light-content', true); }}> setBarStyle('light-content', true) ); } } class StatusBarStaticAndroidExample extends React.Component<{...}> { render(): React.Node { return ( { StatusBar.setHidden(true); }}> setHidden(true) { StatusBar.setHidden(false); }}> setHidden(false) { StatusBar.setBarStyle('light-content'); }}> setBarStyle('light-content') { StatusBar.setBarStyle('dark-content'); }}> setBarStyle('dark-content') { StatusBar.setBarStyle('default'); }}> setBarStyle('default') (default is dark for iOS, light for Android) { StatusBar.setBackgroundColor('#ff00ff', true); }}> setBackgroundColor('#ff00ff', true) { StatusBar.setBackgroundColor('#00ff00', true); }}> setBackgroundColor('#00ff00', true) { StatusBar.setTranslucent(true); StatusBar.setBackgroundColor('rgba(0, 0, 0, 0.4)', true); }}> setTranslucent(true) and setBackgroundColor('rgba(0, 0, 0, 0.4)', true) { StatusBar.setTranslucent(false); StatusBar.setBackgroundColor('black', true); }}> setTranslucent(false) and setBackgroundColor('black', true) ); } } class ModalExample extends React.Component<{...}, $FlowFixMe> { state: $FlowFixMe | {modalVisible: boolean} = { modalVisible: false, }; _onChangeModalVisible = () => { this.setState({modalVisible: !this.state.modalVisible}); }; render(): React.Node { return ( modal visible: {this.state.hidden ? 'true' : 'false'} This modal was presented! Close ); } } exports.framework = 'React'; exports.title = 'StatusBar'; exports.category = 'Basic'; exports.documentationURL = 'https://reactnative.dev/docs/statusbar'; exports.description = 'Component for controlling the status bar'; exports.examples = [ { title: 'StatusBar hidden', render(): React.Node { return ; }, }, { title: 'StatusBar style', render(): React.Node { return ; }, }, { title: 'StatusBar background color', render(): React.Node { return ; }, platform: 'android', }, { title: 'StatusBar translucent', render(): React.Node { return ; }, platform: 'android', }, { title: 'StatusBar static API', render(): React.Node { return ; }, platform: 'ios', }, { title: 'StatusBar static API', render(): React.Node { return ; }, platform: 'android', }, { title: 'StatusBar dimensions', render(): React.Node { return ( Height (Android only): {StatusBar.currentHeight} pts ); }, platform: 'android', }, ] as Array; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', padding: 20, backgroundColor: '#f5fcff', }, innerContainer: { borderRadius: 10, alignItems: 'center', }, wrapper: { borderRadius: 5, marginBottom: 5, }, button: { borderRadius: 5, backgroundColor: '#eeeeee', padding: 10, }, buttonText: { color: 'black', }, modalButton: { marginTop: 10, }, modalText: { color: 'black', }, });