forked from gorhom/react-native-bottom-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavigatorExample.tsx
More file actions
150 lines (138 loc) · 3.62 KB
/
NavigatorExample.tsx
File metadata and controls
150 lines (138 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import React, { useCallback, useMemo, useRef } from 'react';
import { View, StyleSheet } from 'react-native';
import {
createStackNavigator,
HeaderBackButton,
StackNavigationOptions,
TransitionPresets,
} from '@react-navigation/stack';
import BottomSheet, { TouchableOpacity } from '@gorhom/bottom-sheet';
import Button from '../../components/button';
import createDummyScreen from '../DummyScreen';
const Stack = createStackNavigator();
const ScreenA = createDummyScreen({
title: 'FlatList Screen',
nextScreen: 'ScreenB',
type: 'FlatList',
});
const ScreenB = createDummyScreen({
title: 'ScrollView Screen',
nextScreen: 'ScreenC',
type: 'ScrollView',
count: 25,
});
const ScreenC = createDummyScreen({
title: 'SectionList Screen',
nextScreen: 'ScreenD',
type: 'SectionList',
count: 20,
});
const ScreenD = createDummyScreen({
title: 'View Screen',
nextScreen: 'ScreenA',
type: 'View',
count: 5,
});
const Navigator = () => {
const screenOptions = useMemo<StackNavigationOptions>(
() => ({
...TransitionPresets.SlideFromRightIOS,
headerShown: true,
safeAreaInsets: { top: 0 },
headerLeft: ({ onPress, ...props }) => (
<TouchableOpacity onPress={onPress}>
<HeaderBackButton {...props} />
</TouchableOpacity>
),
}),
[]
);
const screenAOptions = useMemo(() => ({ headerLeft: () => null }), []);
return (
<Stack.Navigator screenOptions={screenOptions} headerMode="screen">
<Stack.Screen
name="ScreenA"
options={screenAOptions}
component={ScreenA}
/>
<Stack.Screen name="ScreenB" component={ScreenB} />
<Stack.Screen name="ScreenC" component={ScreenC} />
<Stack.Screen name="ScreenD" component={ScreenD} />
</Stack.Navigator>
);
};
const NavigatorExample = () => {
// hooks
const bottomSheetRef = useRef<BottomSheet>(null);
// variables
const snapPoints = useMemo(() => ['25%', '50%', '90%'], []);
// callbacks
const handleSheetChange = useCallback(index => {
console.log('handleSheetChange', index);
}, []);
const handleSnapPress = useCallback(index => {
bottomSheetRef.current?.snapTo(index);
}, []);
const handleExpandPress = useCallback(() => {
bottomSheetRef.current?.expand();
}, []);
const handleCollapsePress = useCallback(() => {
bottomSheetRef.current?.collapse();
}, []);
const handleClosePress = useCallback(() => {
bottomSheetRef.current?.close();
}, []);
// renders
return (
<View style={styles.container}>
<Button
label="Snap To 90%"
style={styles.buttonContainer}
onPress={() => handleSnapPress(2)}
/>
<Button
label="Snap To 50%"
style={styles.buttonContainer}
onPress={() => handleSnapPress(1)}
/>
<Button
label="Snap To 25%"
style={styles.buttonContainer}
onPress={() => handleSnapPress(0)}
/>
<Button
label="Expand"
style={styles.buttonContainer}
onPress={handleExpandPress}
/>
<Button
label="Collapse"
style={styles.buttonContainer}
onPress={handleCollapsePress}
/>
<Button
label="Close"
style={styles.buttonContainer}
onPress={handleClosePress}
/>
<BottomSheet
ref={bottomSheetRef}
index={1}
snapPoints={snapPoints}
onChange={handleSheetChange}
>
<Navigator />
</BottomSheet>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 24,
},
buttonContainer: {
marginBottom: 6,
},
});
export default NavigatorExample;