|
| 1 | +import React, { createContext, ReactNode, useContext, useState } from 'react'; |
| 2 | +import { View, Text, Switch, StyleSheet } from 'react-native'; |
| 3 | +import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; |
| 4 | +import { createNativeStackNavigator } from '@react-navigation/native-stack'; |
| 5 | +import { NavigationContainer } from '@react-navigation/native'; |
| 6 | + |
| 7 | +interface HeaderConfigContextType { |
| 8 | + tabShown: boolean; |
| 9 | + setTabShown: React.Dispatch<React.SetStateAction<boolean>>; |
| 10 | + outerShown: boolean; |
| 11 | + setOuterShown: React.Dispatch<React.SetStateAction<boolean>>; |
| 12 | + innerShown: boolean; |
| 13 | + setInnerShown: React.Dispatch<React.SetStateAction<boolean>>; |
| 14 | + disableInset: boolean; |
| 15 | + setDisableInset: React.Dispatch<React.SetStateAction<boolean>>; |
| 16 | +} |
| 17 | + |
| 18 | +const HeaderConfigContext = createContext<HeaderConfigContextType | null>(null); |
| 19 | + |
| 20 | +const useHeaderConfig = () => { |
| 21 | + const context = useContext(HeaderConfigContext); |
| 22 | + if (!context) { |
| 23 | + throw new Error( |
| 24 | + 'useHeaderConfig must be used within a HeaderConfigProvider', |
| 25 | + ); |
| 26 | + } |
| 27 | + return context; |
| 28 | +}; |
| 29 | + |
| 30 | +interface HeaderConfigProviderProps { |
| 31 | + children: ReactNode; |
| 32 | +} |
| 33 | + |
| 34 | +const HeaderConfigProvider = ({ children }: HeaderConfigProviderProps) => { |
| 35 | + const [tabShown, setTabShown] = useState(true); |
| 36 | + const [outerShown, setOuterShown] = useState(true); |
| 37 | + const [innerShown, setInnerShown] = useState(true); |
| 38 | + const [disableInset, setDisableInset] = useState(true); |
| 39 | + |
| 40 | + return ( |
| 41 | + <HeaderConfigContext.Provider |
| 42 | + value={{ |
| 43 | + tabShown, |
| 44 | + setTabShown, |
| 45 | + outerShown, |
| 46 | + setOuterShown, |
| 47 | + innerShown, |
| 48 | + setInnerShown, |
| 49 | + disableInset, |
| 50 | + setDisableInset, |
| 51 | + }}> |
| 52 | + {children} |
| 53 | + </HeaderConfigContext.Provider> |
| 54 | + ); |
| 55 | +}; |
| 56 | + |
| 57 | +const ControlPanel = () => { |
| 58 | + const config = useHeaderConfig(); |
| 59 | + |
| 60 | + return ( |
| 61 | + <View style={styles.panel}> |
| 62 | + <Text style={styles.panelTitle}>Headers Config</Text> |
| 63 | + |
| 64 | + <View style={styles.row}> |
| 65 | + <Text style={styles.text}>Tab Navigator</Text> |
| 66 | + <Switch value={config.tabShown} onValueChange={config.setTabShown} /> |
| 67 | + </View> |
| 68 | + <View style={styles.divider} /> |
| 69 | + |
| 70 | + <View style={styles.row}> |
| 71 | + <Text style={styles.text}>Outer Stack</Text> |
| 72 | + <Switch |
| 73 | + value={config.outerShown} |
| 74 | + onValueChange={config.setOuterShown} |
| 75 | + /> |
| 76 | + </View> |
| 77 | + <View style={styles.divider} /> |
| 78 | + |
| 79 | + <View style={styles.row}> |
| 80 | + <Text style={styles.text}>Inner Stack</Text> |
| 81 | + <Switch |
| 82 | + value={config.innerShown} |
| 83 | + onValueChange={config.setInnerShown} |
| 84 | + /> |
| 85 | + </View> |
| 86 | + <View style={styles.divider} /> |
| 87 | + |
| 88 | + <View style={styles.row}> |
| 89 | + <Text style={styles.text}>Disable Top Inset</Text> |
| 90 | + <Switch |
| 91 | + value={config.disableInset} |
| 92 | + onValueChange={config.setDisableInset} |
| 93 | + /> |
| 94 | + </View> |
| 95 | + </View> |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +const Home = () => ( |
| 100 | + <View style={styles.container}> |
| 101 | + <Text style={styles.screenTitle}>Home Screen</Text> |
| 102 | + <ControlPanel /> |
| 103 | + </View> |
| 104 | +); |
| 105 | + |
| 106 | +const OuterStack = createNativeStackNavigator(); |
| 107 | +const InnerStack = createNativeStackNavigator(); |
| 108 | +const Tab = createBottomTabNavigator(); |
| 109 | + |
| 110 | +const NestedStack = () => { |
| 111 | + const config = useHeaderConfig(); |
| 112 | + |
| 113 | + return ( |
| 114 | + <InnerStack.Navigator |
| 115 | + screenOptions={{ |
| 116 | + title: 'Inner Stack', |
| 117 | + headerShown: config.innerShown, |
| 118 | + disableTopInsetApplication: config.disableInset, |
| 119 | + }}> |
| 120 | + <InnerStack.Screen name="Home" component={Home} /> |
| 121 | + </InnerStack.Navigator> |
| 122 | + ); |
| 123 | +}; |
| 124 | + |
| 125 | +const MainStack = () => { |
| 126 | + const config = useHeaderConfig(); |
| 127 | + |
| 128 | + return ( |
| 129 | + <OuterStack.Navigator |
| 130 | + screenOptions={{ |
| 131 | + title: 'Outer Stack', |
| 132 | + headerShown: config.outerShown, |
| 133 | + disableTopInsetApplication: config.disableInset, |
| 134 | + }}> |
| 135 | + <OuterStack.Screen name="NestedStack" component={NestedStack} /> |
| 136 | + </OuterStack.Navigator> |
| 137 | + ); |
| 138 | +}; |
| 139 | + |
| 140 | +const AppContent = () => { |
| 141 | + const config = useHeaderConfig(); |
| 142 | + |
| 143 | + return ( |
| 144 | + <NavigationContainer> |
| 145 | + <Tab.Navigator |
| 146 | + screenOptions={{ |
| 147 | + title: 'Tab Nav', |
| 148 | + headerShown: config.tabShown, |
| 149 | + }}> |
| 150 | + <Tab.Screen name="Tab" component={MainStack} /> |
| 151 | + </Tab.Navigator> |
| 152 | + </NavigationContainer> |
| 153 | + ); |
| 154 | +}; |
| 155 | + |
| 156 | +export default function App() { |
| 157 | + return ( |
| 158 | + <HeaderConfigProvider> |
| 159 | + <AppContent /> |
| 160 | + </HeaderConfigProvider> |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +const styles = StyleSheet.create({ |
| 165 | + container: { |
| 166 | + flex: 1, |
| 167 | + alignItems: 'center', |
| 168 | + justifyContent: 'center', |
| 169 | + }, |
| 170 | + screenTitle: { |
| 171 | + fontSize: 24, |
| 172 | + fontWeight: 'bold', |
| 173 | + marginBottom: 20, |
| 174 | + }, |
| 175 | + panel: { |
| 176 | + backgroundColor: '#FFF', |
| 177 | + padding: 20, |
| 178 | + width: '100%', |
| 179 | + maxWidth: 350, |
| 180 | + }, |
| 181 | + panelTitle: { |
| 182 | + fontSize: 18, |
| 183 | + fontWeight: 'bold', |
| 184 | + marginBottom: 15, |
| 185 | + }, |
| 186 | + row: { |
| 187 | + flexDirection: 'row', |
| 188 | + justifyContent: 'space-between', |
| 189 | + alignItems: 'center', |
| 190 | + marginVertical: 5, |
| 191 | + }, |
| 192 | + text: { |
| 193 | + fontSize: 16, |
| 194 | + }, |
| 195 | + divider: { |
| 196 | + height: 1, |
| 197 | + marginVertical: 10, |
| 198 | + }, |
| 199 | +}); |
0 commit comments