-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathBottomTabs.tsx
More file actions
148 lines (135 loc) · 3.45 KB
/
BottomTabs.tsx
File metadata and controls
148 lines (135 loc) · 3.45 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
import React from 'react';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useTheme } from 'stream-chat-react-native';
import { ChannelsUnreadCountBadge, ThreadsUnreadCountBadge } from './UnreadCountBadge';
import { ChatsTab } from '../icons/ChatsTab';
import { ThreadsTab } from '../icons/ThreadsTab';
import { MentionsTab } from '../icons/MentionsTab';
import type { BottomTabBarProps } from '@react-navigation/bottom-tabs';
import type { Route } from '@react-navigation/native';
import { DraftsTab } from '../icons/DraftsTab';
const styles = StyleSheet.create({
notification: {
left: 25, // size of icon
position: 'absolute',
},
tabContainer: {
alignItems: 'center',
flex: 1,
justifyContent: 'center',
paddingVertical: 8,
},
tabListContainer: {
borderTopColor: 'rgba(0, 0, 0, 0.0677)',
borderTopWidth: 1,
flexDirection: 'row',
},
tabTitle: {
fontSize: 12,
marginTop: 7,
textAlign: 'center',
},
});
const getTab = (key: string) => {
switch (key) {
case 'ChatScreen':
return {
icon: <ChatsTab />,
iconActive: <ChatsTab active />,
notification: <ChannelsUnreadCountBadge />,
title: 'Chats',
};
case 'DraftsScreen':
return {
icon: <DraftsTab />,
iconActive: <DraftsTab active />,
title: 'Drafts',
notification: <ChannelsUnreadCountBadge />,
};
case 'ThreadsScreen':
return {
icon: <ThreadsTab />,
iconActive: <ThreadsTab active />,
notification: <ThreadsUnreadCountBadge />,
title: 'Threads',
};
case 'MentionsScreen':
return {
icon: <MentionsTab />,
iconActive: <MentionsTab active />,
title: 'Mentions',
};
default:
return null;
}
};
type TabProps = Pick<BottomTabBarProps, 'navigation' | 'state'> & {
index: number;
route: Route<string>;
};
const Tab = (props: TabProps) => {
const { navigation, state, route, index } = props;
const {
theme: {
colors: { black, grey },
},
} = useTheme();
const tab = getTab(route.name);
const isFocused = state.index === index;
if (!tab) {
return null;
}
const onPress = () => {
navigation.emit({
canPreventDefault: true,
target: route.key,
type: 'tabPress',
});
if (!isFocused) {
navigation.navigate(route.name);
}
};
return (
<TouchableOpacity onPress={onPress} style={styles.tabContainer}>
<View>
{isFocused ? tab.iconActive : tab.icon}
{tab.notification && <View style={styles.notification}>{tab.notification}</View>}
</View>
<Text
style={[
styles.tabTitle,
{
color: isFocused ? black : grey,
},
]}
>
{tab.title}
</Text>
</TouchableOpacity>
);
};
export const BottomTabs: React.FC<BottomTabBarProps> = (props) => {
const { navigation, state } = props;
const {
theme: {
colors: { white },
},
} = useTheme();
const { bottom } = useSafeAreaInsets();
return (
<View
style={[
styles.tabListContainer,
{
backgroundColor: white,
paddingBottom: bottom,
},
]}
>
{state.routes.map((route, index) => (
<Tab key={index} route={route} index={index} navigation={navigation} state={state} />
))}
</View>
);
};