/** * 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 */ 'use strict'; import * as React from 'react'; import {Appearance, Text, useColorScheme, View} from 'react-native'; import type {AppearancePreferences} from '../../../../Libraries/Utilities/NativeAppearance'; import {RNTesterThemeContext, themes} from '../../components/RNTesterTheme'; class ColorSchemeSubscription extends React.Component< {...}, {colorScheme: ?string, ...}, > { state = { colorScheme: Appearance.getColorScheme(), }; componentDidMount() { Appearance.addChangeListener(this._handleAppearanceChange); } componentWillUnmount() { Appearance.removeChangeListener(this._handleAppearanceChange); } _handleAppearanceChange = (preferences: AppearancePreferences) => { const {colorScheme} = preferences; this.setState({colorScheme}); }; 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.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 ( ); }, }, ];