-
-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import React, { useState } from 'react'; | ||
import { | ||
Button, | ||
Dimensions, | ||
Image, | ||
StyleSheet, | ||
Text, | ||
View, | ||
} from 'react-native'; | ||
import { NavigationContainer, ParamListBase } from '@react-navigation/native'; | ||
import { | ||
createNativeStackNavigator, | ||
NativeStackNavigationProp, | ||
} from '@react-navigation/native-stack'; | ||
import { | ||
GestureHandlerRootView, | ||
ScrollView, | ||
State, | ||
TapGestureHandler, | ||
} from 'react-native-gesture-handler'; | ||
|
||
const Stack = createNativeStackNavigator(); | ||
|
||
function First({ | ||
navigation, | ||
}: { | ||
navigation: NativeStackNavigationProp<ParamListBase>; | ||
}) { | ||
return ( | ||
<View | ||
style={{ | ||
flex: 1, | ||
backgroundColor: 'blue', | ||
}}> | ||
<Button | ||
onPress={() => navigation.navigate('Second')} | ||
title="Go to second" | ||
/> | ||
</View> | ||
); | ||
} | ||
|
||
function Second() { | ||
return ( | ||
<View | ||
style={{ | ||
flex: 1, | ||
backgroundColor: 'red', | ||
}}> | ||
<Text style={styles.subTitle}> | ||
Use swipe back gesture to go back (iOS only) | ||
</Text> | ||
</View> | ||
); | ||
} | ||
|
||
export default function App() { | ||
return ( | ||
<GestureHandlerRootView style={{ flex: 1 }}> | ||
<NavigationContainer> | ||
<Stack.Navigator | ||
screenOptions={{ | ||
fullScreenGestureEnabled: false, | ||
animation: 'fade', | ||
animationMatchesGesture: true, | ||
}}> | ||
<Stack.Screen name="First" component={First} /> | ||
<Stack.Screen name="Second" component={Second} /> | ||
</Stack.Navigator> | ||
</NavigationContainer> | ||
</GestureHandlerRootView> | ||
); | ||
} | ||
|
||
// components | ||
|
||
function Post({ onPress }: { onPress?: () => void }) { | ||
const [width] = useState(Math.round(Dimensions.get('screen').width)); | ||
|
||
return ( | ||
<TapGestureHandler | ||
onHandlerStateChange={e => | ||
e.nativeEvent.oldState === State.ACTIVE && onPress?.() | ||
}> | ||
<View style={styles.post}> | ||
<Text style={styles.title}>Post</Text> | ||
<ScrollView horizontal>{generatePhotos(4, width, 400)}</ScrollView> | ||
<Text style={styles.caption}>Scroll right for more photos</Text> | ||
</View> | ||
</TapGestureHandler> | ||
); | ||
} | ||
|
||
// helpers | ||
function generatePhotos( | ||
amount: number, | ||
width: number, | ||
height: number, | ||
): JSX.Element[] { | ||
const startFrom = Math.floor(Math.random() * 20) + 10; | ||
return Array.from({ length: amount }, (_, index) => { | ||
const uri = `https://picsum.photos/id/${ | ||
startFrom + index | ||
}/${width}/${height}`; | ||
return <Image style={{ width, height }} key={uri} source={{ uri }} />; | ||
}); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
title: { | ||
fontWeight: 'bold', | ||
fontSize: 32, | ||
marginBottom: 8, | ||
marginLeft: 8, | ||
}, | ||
subTitle: { | ||
fontSize: 18, | ||
marginVertical: 16, | ||
textAlign: 'center', | ||
}, | ||
caption: { | ||
textAlign: 'center', | ||
marginTop: 4, | ||
}, | ||
post: { | ||
borderColor: '#ccc', | ||
borderTopWidth: 1, | ||
borderBottomWidth: 1, | ||
paddingVertical: 10, | ||
marginBottom: 16, | ||
backgroundColor: 'white', | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
import React from 'react'; | ||
import { Button, SafeAreaView, View } from 'react-native'; | ||
|
||
import { createNativeStackNavigator } from '@react-navigation/native-stack'; | ||
import { NavigationContainer } from '@react-navigation/native'; | ||
|
||
const Stack = createNativeStackNavigator(); | ||
|
||
const TestScreen = ({ navigation }): React.JSX.Element => { | ||
return ( | ||
<SafeAreaView> | ||
<Button | ||
title={ | ||
'Click me and drag around a bit and I should log something still' | ||
} | ||
onPress={() => { | ||
console.log(Date.now()); | ||
}} | ||
/> | ||
<Button | ||
title={'Navigate to modal'} | ||
onPress={() => { | ||
navigation.navigate('Test2'); | ||
}} | ||
/> | ||
</SafeAreaView> | ||
); | ||
}; | ||
function App(): React.JSX.Element { | ||
return ( | ||
<> | ||
<View style={{ width: 100, height: 200, backgroundColor: 'red' }} /> | ||
<NavigationContainer> | ||
<Stack.Navigator | ||
initialRouteName="Test" | ||
screenOptions={{ presentation: 'modal' }}> | ||
<Stack.Screen | ||
name="Test" | ||
component={TestScreen} | ||
options={{ headerShown: true }} | ||
/> | ||
<Stack.Screen | ||
name="Test2" | ||
component={TestScreen} | ||
options={{ headerShown: false }} | ||
/> | ||
</Stack.Navigator> | ||
</NavigationContainer> | ||
</> | ||
); | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React from 'react'; | ||
import { NavigationContainer } from '@react-navigation/native'; | ||
import { View, StyleSheet, Button } from 'react-native'; | ||
import { GestureHandlerRootView } from 'react-native-gesture-handler'; | ||
import { | ||
createNativeStackNavigator, | ||
NativeStackNavigationProp, | ||
} from 'react-native-screens/native-stack'; | ||
import { GestureDetectorProvider } from 'react-native-screens/gesture-handler'; | ||
|
||
type StackParamList = { | ||
ScreenA: undefined; | ||
ScreenB: undefined; | ||
ScreenC: undefined; | ||
}; | ||
|
||
interface MainScreenProps { | ||
navigation: NativeStackNavigationProp<StackParamList, 'ScreenA'>; | ||
} | ||
|
||
const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => ( | ||
<View style={{ ...styles.container, backgroundColor: 'moccasin' }}> | ||
<Button title="Go ScreenB" onPress={() => { | ||
navigation.navigate('ScreenB') | ||
}} /> | ||
<Button onPress={() => navigation.pop()} title="🔙 Back to Examples" /> | ||
</View> | ||
); | ||
|
||
interface ScreenBProps { | ||
navigation: NativeStackNavigationProp<StackParamList, 'ScreenB'>; | ||
} | ||
|
||
const ScreenB = ({ navigation }: ScreenBProps): JSX.Element => ( | ||
<View style={{ ...styles.container, backgroundColor: 'thistle' }}> | ||
<Button title="Go ScreenC" onPress={() => navigation.navigate('ScreenC')} /> | ||
<Button title="Go back" onPress={() => navigation.goBack()} /> | ||
</View> | ||
); | ||
|
||
interface ScreenCProps { | ||
navigation: NativeStackNavigationProp<StackParamList, 'ScreenC'>; | ||
} | ||
|
||
const ScreenC = ({ navigation }: ScreenCProps): JSX.Element => ( | ||
<View style={{ ...styles.container, backgroundColor: 'blue' }}> | ||
<Button title="Go back" onPress={() => navigation.goBack()} /> | ||
</View> | ||
); | ||
|
||
const Stack = createNativeStackNavigator<StackParamList>(); | ||
|
||
const App = (): JSX.Element => ( | ||
<GestureHandlerRootView style={{ flex: 1 }}> | ||
<NavigationContainer> | ||
<GestureDetectorProvider> | ||
<Stack.Navigator | ||
screenOptions={{ | ||
headerHideBackButton: true, | ||
stackAnimation: 'none', | ||
}}> | ||
<Stack.Screen name="ScreenA" component={MainScreen} /> | ||
<Stack.Screen | ||
name="ScreenB" | ||
component={ScreenB} | ||
options={{ | ||
goBackGesture: 'twoDimensionalSwipe', | ||
}} | ||
/> | ||
<Stack.Screen name="ScreenC" component={ScreenC} /> | ||
</Stack.Navigator> | ||
</GestureDetectorProvider> | ||
</NavigationContainer> | ||
</GestureHandlerRootView> | ||
); | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
paddingTop: 10, | ||
}, | ||
}); | ||
|
||
export default App; |