|
| 1 | +import React from 'react'; |
| 2 | +import { NavigationContainer, useNavigation } from '@react-navigation/native'; |
| 3 | +import { Text, View, StyleSheet, Button } from 'react-native'; |
| 4 | +import { createNativeStackNavigator } from '@react-navigation/native-stack'; |
| 5 | +import PressableWithFeedback from '../shared/PressableWithFeedback'; |
| 6 | + |
| 7 | +const Stack = createNativeStackNavigator(); |
| 8 | + |
| 9 | +function RootStack() { |
| 10 | + return ( |
| 11 | + <Stack.Navigator |
| 12 | + screenOptions={{ |
| 13 | + headerShown: true, |
| 14 | + headerBackButtonDisplayMode: 'minimal', |
| 15 | + }}> |
| 16 | + <Stack.Screen name="Home" component={HomeScreen} /> |
| 17 | + <Stack.Screen name="Profile" component={HomeScreen} /> |
| 18 | + </Stack.Navigator> |
| 19 | + ); |
| 20 | +} |
| 21 | + |
| 22 | +const HomeScreen = ({ navigation }: any) => { |
| 23 | + const [secondButtonShown, setSecondButtonShown] = React.useState(true); |
| 24 | + const [thirdButtonShown, setThirdButtonShown] = React.useState(true); |
| 25 | + const [showAllButtons, setShowAllButtons] = React.useState(true); |
| 26 | + const [isLargeSize, setLargeSize] = React.useState(true); |
| 27 | + |
| 28 | + const headerRight = React.useCallback(() => { |
| 29 | + return ( |
| 30 | + <View style={[styles.buttonsView, !showAllButtons && { display: 'none' }]}> |
| 31 | + <PressableWithFeedback style={isLargeSize ? styles.largeButton : styles.button} onPress={() => console.log(1)}> |
| 32 | + <Text>1</Text> |
| 33 | + </PressableWithFeedback> |
| 34 | + {secondButtonShown && ( |
| 35 | + <PressableWithFeedback style={styles.button} onPress={() => console.log(2)}> |
| 36 | + <Text>2</Text> |
| 37 | + </PressableWithFeedback> |
| 38 | + )} |
| 39 | + {thirdButtonShown && ( |
| 40 | + <PressableWithFeedback style={styles.button} onPress={() => console.log(3)}> |
| 41 | + <Text>3</Text> |
| 42 | + </PressableWithFeedback> |
| 43 | + )} |
| 44 | + <PressableWithFeedback style={styles.button} onPress={() => console.log('D')}> |
| 45 | + <Text>[D]</Text> |
| 46 | + </PressableWithFeedback> |
| 47 | + </View> |
| 48 | + ); |
| 49 | + }, [secondButtonShown, thirdButtonShown, showAllButtons, isLargeSize]); |
| 50 | + |
| 51 | + React.useLayoutEffect(() => { |
| 52 | + navigation.setOptions({ |
| 53 | + headerStyle: { backgroundColor: 'pink' }, |
| 54 | + headerRight: headerRight, |
| 55 | + }); |
| 56 | + }, [navigation, headerRight, showAllButtons]); |
| 57 | + |
| 58 | + return ( |
| 59 | + <View> |
| 60 | + <Text>Home Screen</Text> |
| 61 | + <Button |
| 62 | + title="Toggle size" |
| 63 | + onPress={() => setLargeSize(p => !p)} |
| 64 | + /> |
| 65 | + <Button |
| 66 | + title="Toggle 2nd button" |
| 67 | + onPress={() => setSecondButtonShown(p => !p)} |
| 68 | + /> |
| 69 | + <Button |
| 70 | + title="Toggle 3rd button" |
| 71 | + onPress={() => setThirdButtonShown(p => !p)} |
| 72 | + /> |
| 73 | + <Button |
| 74 | + title="Toggle All Right Buttons" |
| 75 | + onPress={() => setShowAllButtons(p => !p)} |
| 76 | + /> |
| 77 | + </View> |
| 78 | + ); |
| 79 | +}; |
| 80 | + |
| 81 | +function SimpleHome() { |
| 82 | + const navigation = useNavigation(); |
| 83 | + const [isExpanded, setExpanded] = React.useState(false); |
| 84 | + |
| 85 | + const headerRight = React.useCallback(() => { |
| 86 | + return ( |
| 87 | + <PressableWithFeedback> |
| 88 | + <View style={[{ width: 128, height: 42 }, isExpanded ? { width: 192 } : null]} /> |
| 89 | + </PressableWithFeedback> |
| 90 | + ); |
| 91 | + }, [isExpanded]); |
| 92 | + |
| 93 | + React.useLayoutEffect(() => { |
| 94 | + navigation.setOptions({ |
| 95 | + headerRight, |
| 96 | + }); |
| 97 | + }, [headerRight, navigation]); |
| 98 | + |
| 99 | + return ( |
| 100 | + <View style={{ flex: 1, backgroundColor: 'lightsalmon' }}> |
| 101 | + <Button title="Toggle subview size" onPress={() => setExpanded(val => !val)} /> |
| 102 | + <Button title="Go to HomeScreen" onPress={() => navigation.navigate('HomeScreen')} /> |
| 103 | + </View> |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +function SimpleStack() { |
| 108 | + return ( |
| 109 | + <Stack.Navigator> |
| 110 | + <Stack.Screen name="SimpleHome" component={SimpleHome} /> |
| 111 | + <Stack.Screen name="HomeScreen" component={HomeScreen} /> |
| 112 | + </Stack.Navigator> |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +// function App() { |
| 117 | +// return ( |
| 118 | +// <NavigationContainer> |
| 119 | +// <RootStack /> |
| 120 | +// </NavigationContainer> |
| 121 | +// ); |
| 122 | +// } |
| 123 | + |
| 124 | +function AppSimplified() { |
| 125 | + return ( |
| 126 | + <NavigationContainer> |
| 127 | + <SimpleStack /> |
| 128 | + </NavigationContainer> |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +// export default App; |
| 133 | +export default AppSimplified; |
| 134 | + |
| 135 | + |
| 136 | +const styles = StyleSheet.create({ |
| 137 | + button: { |
| 138 | + width: 42, |
| 139 | + height: 42, |
| 140 | + marginHorizontal: 5, |
| 141 | + padding: 5, |
| 142 | + borderRadius: 20, |
| 143 | + justifyContent: 'center', |
| 144 | + alignItems: 'center', |
| 145 | + backgroundColor: 'blue', |
| 146 | + }, |
| 147 | + largeButton: { |
| 148 | + width: 64, |
| 149 | + height: 42, |
| 150 | + marginHorizontal: 5, |
| 151 | + padding: 5, |
| 152 | + borderRadius: 20, |
| 153 | + justifyContent: 'center', |
| 154 | + alignItems: 'center', |
| 155 | + backgroundColor: 'blue', |
| 156 | + }, |
| 157 | + buttonsView: { |
| 158 | + flexDirection: 'row', |
| 159 | + borderWidth: 1, |
| 160 | + }, |
| 161 | +}); |
0 commit comments