/**
* 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}
);
};
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 {
return ;
},
},
{
title: 'Non-component `getColorScheme` API',
render(): React.MixedElement {
return ;
},
},
{
title: 'Consuming Context',
render(): React.MixedElement {
return (
{theme => {
return (
This block of text inherits its theme via Context.
);
}}
);
},
},
{
title: 'Context forced to light theme',
render(): React.MixedElement {
return (
This block of text will always render with a light theme.
);
},
},
{
title: 'Context forced to dark theme',
render(): React.MixedElement {
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.MixedElement {
return (
);
},
},
{
title: 'Toggle native appearance',
description: 'Overwrite application-level appearance mode',
render(): React.MixedElement {
return ;
},
},
] as Array;