/** * 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. * * @format * @flow */ import * as React from 'react'; import {Appearance, Text, useColorScheme, View} from 'react-native'; import type {AppearancePreferences} from 'react-native/Libraries/Utilities/NativeAppearance'; import type {EventSubscription} from 'react-native/Libraries/vendor/emitter/EventEmitter'; import {RNTesterThemeContext, themes} from '../../components/RNTesterTheme'; class ColorSchemeSubscription extends React.Component< {...}, {colorScheme: ?string, ...}, > { _subscription: ?EventSubscription; state = { colorScheme: Appearance.getColorScheme(), }; componentDidMount() { this._subscription = Appearance.addChangeListener( (preferences: AppearancePreferences) => { const {colorScheme} = preferences; this.setState({colorScheme}); }, ); } componentWillUnmount() { this._subscription?.remove(); } render() { return ( {theme => { return ( {this.state.colorScheme} ); }} ); } } const ThemedContainer = props => ( {theme => { return ( {props.children} ); }} ); const ThemedText = props => ( {theme => { return {props.children}; }} ); exports.title = 'Appearance'; exports.category = 'UI'; exports.documentationURL = 'https://reactnative.dev/docs/appearance'; exports.description = 'Light and dark user interface examples.'; exports.examples = [ { title: 'useColorScheme hook', render(): React.Node { const AppearanceViaHook = () => { const colorScheme = useColorScheme(); return ( useColorScheme(): {colorScheme} ); }; return ; }, }, { title: 'Non-component `getColorScheme` API', render(): React.Element { return ; }, }, { title: 'Consuming Context', render(): React.Element { return ( {theme => { return ( This block of text inherits its theme via Context. ); }} ); }, }, { title: 'Context forced to light theme', render(): React.Element { return ( This block of text will always render with a light theme. ); }, }, { title: 'Context forced to dark theme', render(): React.Element { return ( This block of text will always render with a dark theme. ); }, }, { title: 'RNTester App Colors', description: 'A light and a dark theme based on standard iOS 13 colors.', render(): React.Element { const ColorShowcase = props => ( {theme => { return ( {props.themeName} {Object.keys(theme).map(key => ( {key} {typeof theme[key] === 'string' ? theme[key] : JSON.stringify(theme[key])} ))} ); }} ); return ( ); }, }, ];