-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathBottomNavigationExample.tsx
More file actions
205 lines (191 loc) · 5.19 KB
/
BottomNavigationExample.tsx
File metadata and controls
205 lines (191 loc) · 5.19 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import * as React from 'react';
import {
Dimensions,
Easing,
Image,
Platform,
StyleSheet,
View,
} from 'react-native';
import type { StackNavigationProp } from '@react-navigation/stack';
import {
Appbar,
BottomNavigation,
BottomNavigationRoute,
Menu,
} from 'react-native-paper';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useExampleTheme } from '../hooks/useExampleTheme';
import ScreenWrapper from '../ScreenWrapper';
type Route = { route: { key: string } };
type Props = {
navigation: StackNavigationProp<{}>;
};
const MORE_ICON = Platform.OS === 'ios' ? 'dots-horizontal' : 'dots-vertical';
const PhotoGallery = ({ route }: Route) => {
const PHOTOS = Array.from({ length: 24 }).map(
(_, i) => `https://unsplash.it/300/300/?random&__id=${route.key}${i}`
);
return (
<ScreenWrapper contentContainerStyle={styles.content}>
{PHOTOS.map((uri) => (
<View key={uri} style={styles.item}>
<Image
source={{ uri }}
style={styles.photo}
accessibilityIgnoresInvertColors
/>
</View>
))}
</ScreenWrapper>
);
};
const BottomNavigationExample = ({ navigation }: Props) => {
const { isV3 } = useExampleTheme();
const insets = useSafeAreaInsets();
const [index, setIndex] = React.useState(0);
const [menuVisible, setMenuVisible] = React.useState(false);
const [sceneAnimation, setSceneAnimation] =
React.useState<
React.ComponentProps<typeof BottomNavigation>['sceneAnimationType']
>();
const [routes] = React.useState<BottomNavigationRoute[]>([
{
key: 'album',
title: 'Album',
focusedIcon: 'image-album',
...(!isV3 && { color: '#6200ee' }),
},
{
key: 'library',
title: 'Library',
focusedIcon: 'inbox',
badge: true,
...(isV3
? { unfocusedIcon: 'inbox-outline' }
: {
color: '#2962ff',
}),
},
{
key: 'favorites',
title: 'Favorites',
focusedIcon: 'heart',
...(isV3
? { unfocusedIcon: 'heart-outline' }
: {
color: '#00796b',
}),
},
{
key: 'purchased',
title: 'Purchased',
focusedIcon: 'shopping',
...(isV3 ? { unfocusedIcon: 'shopping-outline' } : { color: '#c51162' }),
},
]);
React.useLayoutEffect(() => {
navigation.setOptions({
headerShown: false,
});
}, [navigation]);
return (
<View style={styles.screen}>
<Appbar.Header elevated>
<Appbar.BackAction onPress={() => navigation.goBack()} />
<Appbar.Content title="Bottom Navigation" />
<Menu
visible={menuVisible}
onDismiss={() => setMenuVisible(false)}
anchor={
<Appbar.Action
icon={MORE_ICON}
onPress={() => setMenuVisible(true)}
{...(!isV3 && { color: 'white' })}
/>
}
>
<Menu.Item
trailingIcon={sceneAnimation === undefined ? 'check' : undefined}
onPress={() => {
setSceneAnimation(undefined);
setMenuVisible(false);
}}
title="Scene animation: none"
/>
<Menu.Item
trailingIcon={sceneAnimation === 'shifting' ? 'check' : undefined}
onPress={() => {
setSceneAnimation('shifting');
setMenuVisible(false);
}}
title="Scene animation: shifting"
/>
<Menu.Item
trailingIcon={sceneAnimation === 'opacity' ? 'check' : undefined}
onPress={() => {
setSceneAnimation('opacity');
setMenuVisible(false);
}}
title="Scene animation: opacity"
/>
</Menu>
</Appbar.Header>
<BottomNavigation
safeAreaInsets={{ bottom: insets.bottom }}
navigationState={{ index, routes }}
onIndexChange={setIndex}
labelMaxFontSizeMultiplier={2}
renderScene={BottomNavigation.SceneMap({
album: PhotoGallery,
library: PhotoGallery,
favorites: PhotoGallery,
purchased: PhotoGallery,
})}
sceneAnimationEnabled={sceneAnimation !== undefined}
sceneAnimationType={sceneAnimation}
sceneAnimationEasing={Easing.ease}
getLazy={({ route }) => route.key !== 'album'}
/>
</View>
);
};
BottomNavigationExample.title = 'Bottom Navigation';
export default BottomNavigationExample;
const styles = StyleSheet.create({
...Platform.select({
web: {
content: {
// there is no 'grid' type in RN :(
display: 'grid' as 'none',
gridTemplateColumns: 'repeat(auto-fill, minmax(150px, 1fr))',
gridRowGap: '8px',
gridColumnGap: '8px',
padding: 8,
},
item: {
width: '100%',
height: 150,
},
},
default: {
content: {
flexDirection: 'row',
flexWrap: 'wrap',
padding: 4,
},
item: {
height: Dimensions.get('window').width / 2,
width: '50%',
padding: 4,
},
},
}),
photo: {
flex: 1,
resizeMode: 'cover',
},
screen: {
flex: 1,
},
});