|
| 1 | +import React from 'react'; |
| 2 | +import { Button, Modal, SafeAreaView, Text, View } from 'react-native'; |
| 3 | + |
| 4 | +import { createNativeStackNavigator } from '@react-navigation/native-stack'; |
| 5 | +import { NavigationContainer } from '@react-navigation/native'; |
| 6 | +import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; |
| 7 | + |
| 8 | +const Stack = createNativeStackNavigator(); |
| 9 | +const Tabs = createBottomTabNavigator(); |
| 10 | + |
| 11 | + |
| 12 | +function TabScreens({ navigation }): React.JSX.Element { |
| 13 | + return ( |
| 14 | + <Tabs.Navigator> |
| 15 | + <Tabs.Screen name='A' component={HomeScreen} /> |
| 16 | + <Tabs.Screen name='B' component={TabHomeScreen} /> |
| 17 | + </Tabs.Navigator> |
| 18 | + ); |
| 19 | +} |
| 20 | + |
| 21 | +function TabHomeScreen({ navigation }): React.JSX.Element { |
| 22 | + return ( |
| 23 | + <View style={{ flex: 1, backgroundColor: 'lightcoral', justifyContent: 'center', }}> |
| 24 | + <Text>Where do you where do you go, my lovely, oh oh oh oh</Text> |
| 25 | + <Button title='Show owned transparentModal (outer navigator)' onPress={() => { navigation.navigate('TransparentModal') }} /> |
| 26 | + <Button title='Show owned modal (outer navigator)' onPress={() => { navigation.navigate('Modal') }} /> |
| 27 | + </View> |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +function HomeScreen({ navigation }): React.JSX.Element { |
| 33 | + const [toggle, setToggle] = React.useState(false); |
| 34 | + return ( |
| 35 | + <View style={{ flex: 1, backgroundColor: 'lightseagreen', justifyContent: 'center', }}> |
| 36 | + <Text>Where do you where do you go, my lovely, oh oh oh oh</Text> |
| 37 | + <Button title='Show owned transparentModal' onPress={() => { navigation.navigate('TransparentModal') }} /> |
| 38 | + <Button title='Show owned modal' onPress={() => { navigation.navigate('Modal') }} /> |
| 39 | + <Button title='Show tabs' onPress={() => { navigation.navigate('Tabs') }} /> |
| 40 | + <Button title='Show foreign modal' onPress={() => { setToggle(old => !old) }} /> |
| 41 | + <Modal |
| 42 | + visible={toggle} |
| 43 | + onRequestClose={() => setToggle(false)} |
| 44 | + presentationStyle='formSheet' |
| 45 | + animationType='slide' |
| 46 | + > |
| 47 | + <View style={{ flex: 1, backgroundColor: 'lightblue' }}> |
| 48 | + <Text>Hello I'm a foreign modal</Text> |
| 49 | + <Button title='Close foreign modal' onPress={() => { setToggle(false) }} /> |
| 50 | + </View> |
| 51 | + </Modal> |
| 52 | + </View> |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +function ModalScreen({ navigation }): React.JSX.Element { |
| 57 | + const [toggle2, setToggle2] = React.useState(false); |
| 58 | + |
| 59 | + return ( |
| 60 | + <View style={{ flex: 1, backgroundColor: 'lightcoral', opacity: 0.4 }}> |
| 61 | + <Text>Where do you where do you go, my lovely, oh oh oh oh</Text> |
| 62 | + <Button title='Go back' onPress={() => { navigation.goBack() }} /> |
| 63 | + <View style={{ width: '100%', height: 50, backgroundColor: 'red' }} /> |
| 64 | + <Button title='Push another Modal' onPress={() => { navigation.push('Modal') }} /> |
| 65 | + <Button title='Push foreign modal(inside Screen Component)' onPress={() => { navigation.push('ForeignModal')}} /> |
| 66 | + <Button title='Push foreign modal' onPress={() => { setToggle2(old => !old )}} /> |
| 67 | + <Modal |
| 68 | + visible={toggle2} |
| 69 | + onRequestClose={() => setToggle2(false)} |
| 70 | + presentationStyle='formSheet' |
| 71 | + animationType='slide' |
| 72 | + > |
| 73 | + <View style={{ flex: 1, backgroundColor: 'lightblue' }}> |
| 74 | + <Text>Hello I'm a foreign modal</Text> |
| 75 | + <Button title='Close foreign modal' onPress={() => { setToggle2(false) }} /> |
| 76 | + </View> |
| 77 | + </Modal> |
| 78 | + </View> |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +function ForeignModal({ navigation }): React.JSX.Element | null { |
| 83 | + const [toggle, setToggle] = React.useState(false); |
| 84 | + return ( |
| 85 | + <Modal |
| 86 | + visible={toggle} |
| 87 | + onRequestClose={() => setToggle(false)} |
| 88 | + presentationStyle='formSheet' |
| 89 | + animationType='slide' |
| 90 | + > |
| 91 | + <View style={{ flex: 1, backgroundColor: 'lightblue' }}> |
| 92 | + <Text>Hello I'm a foreign modal</Text> |
| 93 | + <Button title='Close foreign modal' onPress={() => { setToggle(false) }} /> |
| 94 | + </View> |
| 95 | + </Modal> |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +const TestScreen = ({ navigation }): React.JSX.Element => { |
| 101 | + return ( |
| 102 | + <SafeAreaView> |
| 103 | + <Button |
| 104 | + title={ |
| 105 | + 'Click me and drag around a bit and I should log something still' |
| 106 | + } |
| 107 | + onPress={() => { |
| 108 | + console.log(Date.now()); |
| 109 | + }} |
| 110 | + /> |
| 111 | + <Button |
| 112 | + title={'Navigate to modal'} |
| 113 | + onPress={() => { |
| 114 | + navigation.navigate('Test2'); |
| 115 | + }} |
| 116 | + /> |
| 117 | + </SafeAreaView> |
| 118 | + ); |
| 119 | +}; |
| 120 | + |
| 121 | +function App(): React.JSX.Element { |
| 122 | + return ( |
| 123 | + <NavigationContainer> |
| 124 | + <Stack.Navigator initialRouteName='Tabs'> |
| 125 | + <Stack.Screen name='Tabs' component={TabScreens} options={{ headerShown: false }} /> |
| 126 | + <Stack.Screen |
| 127 | + name="Home" |
| 128 | + component={HomeScreen} |
| 129 | + /> |
| 130 | + <Stack.Screen |
| 131 | + name="TransparentModal" |
| 132 | + component={ModalScreen} |
| 133 | + options={{ |
| 134 | + presentation: 'transparentModal', |
| 135 | + }} |
| 136 | + /> |
| 137 | + <Stack.Screen |
| 138 | + name="Modal" |
| 139 | + component={ModalScreen} |
| 140 | + options={{ |
| 141 | + presentation: 'modal', |
| 142 | + headerShown: true, |
| 143 | + }} |
| 144 | + /> |
| 145 | + <Stack.Screen name={"ForeignModal"} component={ForeignModal} /> |
| 146 | + </Stack.Navigator> |
| 147 | + </NavigationContainer> |
| 148 | + ); |
| 149 | +} |
| 150 | + |
| 151 | +export default App; |
0 commit comments