/** * 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 {useState, useEffect} from 'react'; import {Appearance, Text, useColorScheme, View, Button} from 'react-native'; import type { AppearancePreferences, ColorSchemeName, } from 'react-native/Libraries/Utilities/NativeAppearance'; import {RNTesterThemeContext, themes} from '../../components/RNTesterTheme'; function ColorSchemeSubscription() { const [colorScheme, setScheme] = useState( Appearance.getColorScheme(), ); useEffect(() => { const subscription = Appearance.addChangeListener( (preferences: AppearancePreferences) => { const {colorScheme: scheme} = preferences; setScheme(scheme); }, ); return () => subscription?.remove(); }, [setScheme]); return ( {theme => { return ( {colorScheme} ); }} ); } const ThemedContainer = (props: {children: React.Node}) => ( {theme => { return ( {props.children} ); }} ); const ThemedText = (props: {children: React.Node | string}) => ( {theme => { return {props.children}; }} ); const AppearanceViaHook = () => { const colorScheme = useColorScheme(); return ( useColorScheme(): {colorScheme} ); }; const ColorShowcase = (props: {themeName: string}) => ( {theme => { return ( {props.themeName} {Object.keys(theme).map(key => ( {key} {typeof theme[key] === 'string' ? theme[key] : JSON.stringify(theme[key])} ))} ); }} ); const ToggleNativeAppearance = () => { const [nativeColorScheme, setNativeColorScheme] = useState(null); const colorScheme = useColorScheme(); useEffect(() => { Appearance.setColorScheme(nativeColorScheme); }, [nativeColorScheme]); return ( Native colorScheme: {nativeColorScheme} Current colorScheme: {colorScheme}