/** * 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 * @format */ import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; import type {ColorSchemeName} from 'react-native'; import RNTesterText from '../../components/RNTesterText'; import {RNTesterThemeContext, themes} from '../../components/RNTesterTheme'; import * as React from 'react'; import {useEffect, useState} from 'react'; import {Appearance, Button, Text, View, useColorScheme} from 'react-native'; function ColorSchemeSubscription() { const [colorScheme, setColorScheme] = useState( Appearance.getColorScheme(), ); useEffect(() => { const subscription = Appearance.addChangeListener( ({colorScheme: newColorScheme}: {colorScheme: ?ColorSchemeName}) => { setColorScheme(newColorScheme); }, ); return () => subscription.remove(); }, [setColorScheme]); 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 => typeof theme[key] === 'string' && ( {key} {theme[key]} ), )} ); }} ); const ToggleNativeAppearance = () => { const [nativeColorScheme, setNativeColorScheme] = useState('unspecified'); const colorScheme = useColorScheme(); useEffect(() => { Appearance.setColorScheme(nativeColorScheme); }, [nativeColorScheme]); return ( Native colorScheme: {nativeColorScheme} Current colorScheme: {colorScheme}