|
| 1 | +import { |
| 2 | + Appearance, |
| 3 | + ColorSchemeName, |
| 4 | + ScrollView, |
| 5 | + StyleSheet, |
| 6 | + Text, |
| 7 | + TextInput, |
| 8 | + View, |
| 9 | +} from 'react-native'; |
| 10 | +import { Scenario } from '../../shared/helpers'; |
| 11 | +import { createAutoConfiguredTabs } from '../../shared/tabs'; |
| 12 | +import React, { useEffect, useState } from 'react'; |
| 13 | +import { SettingsPicker } from '../../../shared'; |
| 14 | +import { TabsHostProps } from 'react-native-screens'; |
| 15 | +import useTabsConfigState from '../../shared/hooks/tabs-config'; |
| 16 | + |
| 17 | +const SCENARIO: Scenario = { |
| 18 | + name: 'Color Scheme', |
| 19 | + key: 'test-tabs-color-scheme', |
| 20 | + details: 'Tests how tabs handle system, React Native and prop color scheme.', |
| 21 | + platforms: ['ios'], |
| 22 | + AppComponent: App, |
| 23 | +}; |
| 24 | + |
| 25 | +export default SCENARIO; |
| 26 | + |
| 27 | +type TabsParamList = { |
| 28 | + Config: undefined; |
| 29 | + Keyboard: undefined; |
| 30 | +}; |
| 31 | + |
| 32 | +function ConfigScreen() { |
| 33 | + const [config, dispatch] = useTabsConfigState<TabsParamList>(); |
| 34 | + const [reactColorScheme, setReactColorScheme] = |
| 35 | + useState<ColorSchemeName>('unspecified'); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + Appearance.setColorScheme(reactColorScheme); |
| 39 | + }, [reactColorScheme]); |
| 40 | + |
| 41 | + return ( |
| 42 | + <ScrollView style={styles.container} contentContainerStyle={styles.content}> |
| 43 | + <View style={styles.section}> |
| 44 | + <Text> |
| 45 | + There are 3 sources of color scheme, in ascending order of precedence: |
| 46 | + system, React Native and our property on TabsHost. |
| 47 | + </Text> |
| 48 | + </View> |
| 49 | + |
| 50 | + <View style={styles.section}> |
| 51 | + <Text style={styles.heading}>System color scheme</Text> |
| 52 | + <Text> |
| 53 | + Switch system color scheme via quick settings in notification drawer |
| 54 | + (Android/iOS) or Cmd+Shift+A (iOS simulator). |
| 55 | + </Text> |
| 56 | + </View> |
| 57 | + |
| 58 | + <View style={styles.section}> |
| 59 | + <Text style={styles.heading}>React Native's color scheme</Text> |
| 60 | + <SettingsPicker<ColorSchemeName> |
| 61 | + label={'colorScheme'} |
| 62 | + value={reactColorScheme} |
| 63 | + onValueChange={function (value: ColorSchemeName): void { |
| 64 | + setReactColorScheme(value); |
| 65 | + }} |
| 66 | + items={['unspecified', 'light', 'dark']} |
| 67 | + /> |
| 68 | + </View> |
| 69 | + |
| 70 | + <View style={styles.section}> |
| 71 | + <Text style={styles.heading}>TabsHost color scheme</Text> |
| 72 | + <SettingsPicker<NonNullable<TabsHostProps['colorScheme']>> |
| 73 | + label={'colorScheme'} |
| 74 | + value={config.colorScheme ?? 'inherit'} |
| 75 | + onValueChange={function (value: TabsHostProps['colorScheme']): void { |
| 76 | + dispatch({ |
| 77 | + type: 'tabBar', |
| 78 | + config: { |
| 79 | + colorScheme: value, |
| 80 | + }, |
| 81 | + }); |
| 82 | + }} |
| 83 | + items={['inherit', 'light', 'dark']} |
| 84 | + /> |
| 85 | + </View> |
| 86 | + </ScrollView> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +function TestScreen() { |
| 91 | + return ( |
| 92 | + <View style={styles.containerCenter}> |
| 93 | + <TextInput placeholder="Type something..." /> |
| 94 | + </View> |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +const Tabs = createAutoConfiguredTabs<TabsParamList>({ |
| 99 | + Config: ConfigScreen, |
| 100 | + Keyboard: TestScreen, |
| 101 | +}); |
| 102 | + |
| 103 | +export function App() { |
| 104 | + return ( |
| 105 | + <Tabs.Provider> |
| 106 | + <Tabs.Autoconfig /> |
| 107 | + </Tabs.Provider> |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +const styles = StyleSheet.create({ |
| 112 | + container: { |
| 113 | + flex: 1, |
| 114 | + }, |
| 115 | + containerCenter: { |
| 116 | + flex: 1, |
| 117 | + alignItems: 'center', |
| 118 | + justifyContent: 'center', |
| 119 | + }, |
| 120 | + content: { |
| 121 | + padding: 20, |
| 122 | + }, |
| 123 | + heading: { |
| 124 | + fontSize: 24, |
| 125 | + fontWeight: 'bold', |
| 126 | + marginBottom: 5, |
| 127 | + }, |
| 128 | + section: { |
| 129 | + marginBottom: 10, |
| 130 | + }, |
| 131 | +}); |
0 commit comments