forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBottomNavigationBar.tsx
More file actions
1008 lines (952 loc) · 31.5 KB
/
BottomNavigationBar.tsx
File metadata and controls
1008 lines (952 loc) · 31.5 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as React from 'react';
import {
Animated,
ColorValue,
EasingFunction,
Platform,
StyleProp,
StyleSheet,
Pressable,
View,
ViewStyle,
} from 'react-native';
import color from 'color';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import {
getActiveTintColor,
getInactiveTintColor,
getLabelColor,
} from './utils';
import { useInternalTheme } from '../../core/theming';
import overlay from '../../styles/overlay';
import { black, white } from '../../styles/themes/v2/colors';
import type { ThemeProp } from '../../types';
import useAnimatedValue from '../../utils/useAnimatedValue';
import useAnimatedValueArray from '../../utils/useAnimatedValueArray';
import useIsKeyboardShown from '../../utils/useIsKeyboardShown';
import useLayout from '../../utils/useLayout';
import Badge from '../Badge';
import Icon, { IconSource } from '../Icon';
import Surface from '../Surface';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
import { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
type BaseRoute = {
key: string;
title?: string;
focusedIcon?: IconSource;
unfocusedIcon?: IconSource;
badge?: string | number | boolean;
/**
* @deprecated In v5.x works only with theme version 2.
*/
color?: string;
accessibilityLabel?: string;
testID?: string;
lazy?: boolean;
};
type NavigationState<Route extends BaseRoute> = {
index: number;
routes: Route[];
};
type TabPressEvent = {
defaultPrevented: boolean;
preventDefault(): void;
};
type TouchableProps<Route extends BaseRoute> = TouchableRippleProps & {
key: string;
route: Route;
children: React.ReactNode;
borderless?: boolean;
centered?: boolean;
rippleColor?: ColorValue;
};
export type Props<Route extends BaseRoute> = {
/**
* Whether the shifting style is used, the active tab icon shifts up to show the label and the inactive tabs won't have a label.
*
* By default, this is `false` with theme version 3 and `true` when you have more than 3 tabs.
* Pass `shifting={false}` to explicitly disable this animation, or `shifting={true}` to always use this animation.
* Note that you need at least 2 tabs be able to run this animation.
*/
shifting?: boolean;
/**
* Whether to show labels in tabs. When `false`, only icons will be displayed.
*/
labeled?: boolean;
/**
* Whether tabs should be spread across the entire width.
*/
compact?: boolean;
/**
* State for the bottom navigation. The state should contain the following properties:
*
* - `index`: a number representing the index of the active route in the `routes` array
* - `routes`: an array containing a list of route objects used for rendering the tabs
*
* Each route object should contain the following properties:
*
* - `key`: a unique key to identify the route (required)
* - `title`: title of the route to use as the tab label
* - `focusedIcon`: icon to use as the focused tab icon, can be a string, an image source or a react component @renamed Renamed from 'icon' to 'focusedIcon' in v5.x
* - `unfocusedIcon`: icon to use as the unfocused tab icon, can be a string, an image source or a react component @supported Available in v5.x with theme version 3
* - `color`: color to use as background color for shifting bottom navigation @deprecatedProperty In v5.x works only with theme version 2.
* - `badge`: badge to show on the tab icon, can be `true` to show a dot, `string` or `number` to show text.
* - `accessibilityLabel`: accessibility label for the tab button
* - `testID`: test id for the tab button
*
* Example:
*
* ```js
* {
* index: 1,
* routes: [
* { key: 'music', title: 'Favorites', focusedIcon: 'heart', unfocusedIcon: 'heart-outline'},
* { key: 'albums', title: 'Albums', focusedIcon: 'album' },
* { key: 'recents', title: 'Recents', focusedIcon: 'history' },
* { key: 'notifications', title: 'Notifications', focusedIcon: 'bell', unfocusedIcon: 'bell-outline' },
* ]
* }
* ```
*
* `BottomNavigation.Bar` is a controlled component, which means the `index` needs to be updated via the `onTabPress` callback.
*/
navigationState: NavigationState<Route>;
/**
* Callback which returns a React Element to be used as tab icon.
*/
renderIcon?: (props: {
route: Route;
focused: boolean;
color: string;
}) => React.ReactNode;
/**
* Callback which React Element to be used as tab label.
*/
renderLabel?: (props: {
route: Route;
focused: boolean;
color: string;
}) => React.ReactNode;
/**
* Callback which returns a React element to be used as the touchable for the tab item.
* Renders a `TouchableRipple` on Android and `Pressable` on iOS.
*/
renderTouchable?: (props: TouchableProps<Route>) => React.ReactNode;
/**
* Get accessibility label for the tab button. This is read by the screen reader when the user taps the tab.
* Uses `route.accessibilityLabel` by default.
*/
getAccessibilityLabel?: (props: { route: Route }) => string | undefined;
/**
* Get badge for the tab, uses `route.badge` by default.
*/
getBadge?: (props: { route: Route }) => boolean | number | string | undefined;
/**
* Get color for the tab, uses `route.color` by default.
*/
getColor?: (props: { route: Route }) => string | undefined;
/**
* Get label text for the tab, uses `route.title` by default. Use `renderLabel` to replace label component.
*/
getLabelText?: (props: { route: Route }) => string | undefined;
/**
* Get the id to locate this tab button in tests, uses `route.testID` by default.
*/
getTestID?: (props: { route: Route }) => string | undefined;
/**
* Function to execute on tab press. It receives the route for the pressed tab. Use this to update the navigation state.
*/
onTabPress: (props: { route: Route } & TabPressEvent) => void;
/**
* Function to execute on tab long press. It receives the route for the pressed tab
*/
onTabLongPress?: (props: { route: Route } & TabPressEvent) => void;
/**
* Custom color for icon and label in the active tab.
*/
activeColor?: string;
/**
* Custom color for icon and label in the inactive tab.
*/
inactiveColor?: string;
/**
* The scene animation Easing.
*/
animationEasing?: EasingFunction | undefined;
/**
* Whether the bottom navigation bar is hidden when keyboard is shown.
* On Android, this works best when [`windowSoftInputMode`](https://developer.android.com/guide/topics/manifest/activity-element#wsoft) is set to `adjustResize`.
*/
keyboardHidesNavigationBar?: boolean;
/**
* Safe area insets for the tab bar. This can be used to avoid elements like the navigation bar on Android and bottom safe area on iOS.
* The bottom insets for iOS is added by default. You can override the behavior with this option.
*/
safeAreaInsets?: {
top?: number;
right?: number;
bottom?: number;
left?: number;
};
/**
* Specifies the largest possible scale a label font can reach.
*/
labelMaxFontSizeMultiplier?: number;
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
activeIndicatorStyle?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme?: ThemeProp;
/**
* TestID used for testing purposes
*/
testID?: string;
};
const MIN_RIPPLE_SCALE = 0.001; // Minimum scale is not 0 due to bug with animation
const MIN_TAB_WIDTH = 96;
const MAX_TAB_WIDTH = 168;
const BAR_HEIGHT = 56;
const OUTLINE_WIDTH = 64;
const Touchable = <Route extends BaseRoute>({
route: _0,
style,
children,
borderless,
centered,
rippleColor,
...rest
}: TouchableProps<Route>) =>
TouchableRipple.supported ? (
<TouchableRipple
{...rest}
disabled={rest.disabled || undefined}
borderless={borderless}
centered={centered}
rippleColor={rippleColor}
style={style}
>
{children}
</TouchableRipple>
) : (
<Pressable style={style} {...rest}>
{children}
</Pressable>
);
/**
* A navigation bar which can easily be integrated with [React Navigation's Bottom Tabs Navigator](https://reactnavigation.org/docs/bottom-tab-navigator/).
*
* ## Usage
* ### without React Navigation
* ```js
* import React from 'react';
* import { useState } from 'react';
* import { View } from 'react-native';
* import { BottomNavigation, Text, Provider } from 'react-native-paper';
* import MaterialCommunityIcons from '@expo/vector-icons/MaterialCommunityIcons';
*
* function HomeScreen() {
* return (
* <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
* <Text>Home!</Text>
* </View>
* );
* }
*
* function SettingsScreen() {
* return (
* <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
* <Text>Settings!</Text>
* </View>
* );
* }
*
* export default function MyComponent() {
* const [index, setIndex] = useState(0);
*
* const routes = [
* { key: 'home', title: 'Home', icon: 'home' },
* { key: 'settings', title: 'Settings', icon: 'cog' },
* ];
* const renderScene = ({ route }) => {
* switch (route.key) {
* case 'home':
* return <HomeScreen />;
* case 'settings':
* return <SettingsScreen />;
* default:
* return null;
* }
* };
*
* return (
* <Provider>
* {renderScene({ route: routes[index] })}
* <BottomNavigation.Bar
* navigationState={{ index, routes }}
* onTabPress={({ route }) => {
* const newIndex = routes.findIndex((r) => r.key === route.key);
* if (newIndex !== -1) {
* setIndex(newIndex);
* }
* }}
* renderIcon={({ route, color }) => (
* <Icon name={route.icon} size={24} color={color} />
* )}
* getLabelText={({ route }) => route.title}
* />
* </Provider>
* );
* }
* ```
*/
const BottomNavigationBar = <Route extends BaseRoute>({
navigationState,
renderIcon,
renderLabel,
renderTouchable = ({ key, ...props }: TouchableProps<Route>) => (
<Touchable key={key} {...props} />
),
getLabelText = ({ route }: { route: Route }) => route.title,
getBadge = ({ route }: { route: Route }) => route.badge,
getColor = ({ route }: { route: Route }) => route.color,
getAccessibilityLabel = ({ route }: { route: Route }) =>
route.accessibilityLabel,
getTestID = ({ route }: { route: Route }) => route.testID,
activeColor,
inactiveColor,
keyboardHidesNavigationBar = Platform.OS === 'android',
style,
activeIndicatorStyle,
labeled = true,
animationEasing,
onTabPress,
onTabLongPress,
shifting: shiftingProp,
safeAreaInsets,
labelMaxFontSizeMultiplier = 1,
compact: compactProp,
testID = 'bottom-navigation-bar',
theme: themeOverrides,
}: Props<Route>) => {
const theme = useInternalTheme(themeOverrides);
const { bottom, left, right } = useSafeAreaInsets();
const { scale } = theme.animation;
const compact = compactProp ?? !theme.isV3;
let shifting =
shiftingProp ?? (theme.isV3 ? false : navigationState.routes.length > 3);
if (shifting && navigationState.routes.length < 2) {
shifting = false;
console.warn(
'BottomNavigation.Bar needs at least 2 tabs to run shifting animation'
);
}
/**
* Visibility of the navigation bar, visible state is 1 and invisible is 0.
*/
const visibleAnim = useAnimatedValue(1);
/**
* Active state of individual tab items, active state is 1 and inactive state is 0.
*/
const tabsAnims = useAnimatedValueArray(
navigationState.routes.map(
// focused === 1, unfocused === 0
(_, i) => (i === navigationState.index ? 1 : 0)
)
);
/**
* Index of the currently active tab. Used for setting the background color.
* We don't use the color as an animated value directly, because `setValue` seems to be buggy with colors?.
*/
const indexAnim = useAnimatedValue(navigationState.index);
/**
* Animation for the background color ripple, used to determine it's scale and opacity.
*/
const rippleAnim = useAnimatedValue(MIN_RIPPLE_SCALE);
/**
* Layout of the navigation bar. The width is used to determine the size and position of the ripple.
*/
const [layout, onLayout] = useLayout();
/**
* Track whether the keyboard is visible to show and hide the navigation bar.
*/
const [keyboardVisible, setKeyboardVisible] = React.useState(false);
const handleKeyboardShow = React.useCallback(() => {
setKeyboardVisible(true);
Animated.timing(visibleAnim, {
toValue: 0,
duration: 150 * scale,
useNativeDriver: true,
}).start();
}, [scale, visibleAnim]);
const handleKeyboardHide = React.useCallback(() => {
Animated.timing(visibleAnim, {
toValue: 1,
duration: 100 * scale,
useNativeDriver: true,
}).start(() => {
setKeyboardVisible(false);
});
}, [scale, visibleAnim]);
const animateToIndex = React.useCallback(
(index: number) => {
// Reset the ripple to avoid glitch if it's currently animating
rippleAnim.setValue(MIN_RIPPLE_SCALE);
Animated.parallel([
Animated.timing(rippleAnim, {
toValue: 1,
duration: theme.isV3 || shifting ? 400 * scale : 0,
useNativeDriver: true,
}),
...navigationState.routes.map((_, i) =>
Animated.timing(tabsAnims[i], {
toValue: i === index ? 1 : 0,
duration: theme.isV3 || shifting ? 150 * scale : 0,
useNativeDriver: true,
easing: animationEasing,
})
),
]).start(() => {
// Workaround a bug in native animations where this is reset after first animation
tabsAnims.map((tab, i) => tab.setValue(i === index ? 1 : 0));
// Update the index to change bar's background color and then hide the ripple
indexAnim.setValue(index);
rippleAnim.setValue(MIN_RIPPLE_SCALE);
});
},
[
rippleAnim,
theme.isV3,
shifting,
scale,
navigationState.routes,
tabsAnims,
animationEasing,
indexAnim,
]
);
React.useEffect(() => {
// Workaround for native animated bug in react-native@^0.57
// Context: https://github.com/callstack/react-native-paper/pull/637
animateToIndex(navigationState.index);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useIsKeyboardShown({
onShow: handleKeyboardShow,
onHide: handleKeyboardHide,
});
React.useEffect(() => {
animateToIndex(navigationState.index);
}, [navigationState.index, animateToIndex]);
const eventForIndex = (index: number) => {
const event = {
route: navigationState.routes[index],
defaultPrevented: false,
preventDefault: () => {
event.defaultPrevented = true;
},
};
return event;
};
const { routes } = navigationState;
const { colors, dark: isDarkTheme, mode, isV3 } = theme;
const { backgroundColor: customBackground, elevation = 4 } =
(StyleSheet.flatten(style) || {}) as {
elevation?: number;
backgroundColor?: ColorValue;
};
const approxBackgroundColor = customBackground
? customBackground
: isDarkTheme && mode === 'adaptive'
? overlay(elevation, colors?.surface)
: colors?.primary;
const v2BackgroundColorInterpolation = shifting
? indexAnim.interpolate({
inputRange: routes.map((_, i) => i),
// FIXME: does outputRange support ColorValue or just strings?
// @ts-expect-error
outputRange: routes.map(
(route) => getColor({ route }) || approxBackgroundColor
),
})
: approxBackgroundColor;
const backgroundColor = isV3
? customBackground || theme.colors.elevation.level2
: shifting
? v2BackgroundColorInterpolation
: approxBackgroundColor;
const isDark =
typeof approxBackgroundColor === 'string'
? !color(approxBackgroundColor).isLight()
: true;
const textColor = isDark ? white : black;
const activeTintColor = getActiveTintColor({
activeColor,
defaultColor: textColor,
theme,
});
const inactiveTintColor = getInactiveTintColor({
inactiveColor,
defaultColor: textColor,
theme,
});
const touchColor = color(activeTintColor).alpha(0.12).rgb().string();
const maxTabWidth = routes.length > 3 ? MIN_TAB_WIDTH : MAX_TAB_WIDTH;
const maxTabBarWidth = maxTabWidth * routes.length;
const rippleSize = layout.width / 4;
const insets = {
left: safeAreaInsets?.left ?? left,
right: safeAreaInsets?.right ?? right,
bottom: safeAreaInsets?.bottom ?? bottom,
};
return (
<Surface
{...(theme.isV3 && { elevation: 0 })}
testID={testID}
style={[
!theme.isV3 && styles.elevation,
styles.bar,
keyboardHidesNavigationBar // eslint-disable-next-line react-native/no-inline-styles
? {
// When the keyboard is shown, slide down the navigation bar
transform: [
{
translateY: visibleAnim.interpolate({
inputRange: [0, 1],
outputRange: [layout.height, 0],
}),
},
],
// Absolutely position the navigation bar so that the content is below it
// This is needed to avoid gap at bottom when the navigation bar is hidden
position: keyboardVisible ? 'absolute' : undefined,
}
: null,
style,
]}
pointerEvents={
layout.measured
? keyboardHidesNavigationBar && keyboardVisible
? 'none'
: 'auto'
: 'none'
}
onLayout={onLayout}
container
>
<Animated.View
style={[styles.barContent, { backgroundColor }]}
testID={`${testID}-content`}
>
<View
style={[
styles.items,
{
marginBottom: insets.bottom,
marginHorizontal: Math.max(insets.left, insets.right),
},
compact && {
maxWidth: maxTabBarWidth,
},
]}
accessibilityRole={'tablist'}
testID={`${testID}-content-wrapper`}
>
{shifting && !isV3 ? (
<Animated.View
pointerEvents="none"
style={[
styles.ripple,
{
// Since we have a single ripple, we have to reposition it so that it appears to expand from active tab.
// We need to move it from the top to center of the navigation bar and from the left to the active tab.
top: (BAR_HEIGHT - rippleSize) / 2,
left:
(Math.min(layout.width, maxTabBarWidth) / routes.length) *
(navigationState.index + 0.5) -
rippleSize / 2,
height: rippleSize,
width: rippleSize,
borderRadius: rippleSize / 2,
backgroundColor: getColor({
route: routes[navigationState.index],
}),
transform: [
{
// Scale to twice the size to ensure it covers the whole navigation bar
scale: rippleAnim.interpolate({
inputRange: [0, 1],
outputRange: [0, 8],
}),
},
],
opacity: rippleAnim.interpolate({
inputRange: [0, MIN_RIPPLE_SCALE, 0.3, 1],
outputRange: [0, 0, 1, 1],
}),
},
]}
testID={`${testID}-content-ripple`}
/>
) : null}
{routes.map((route, index) => {
const focused = navigationState.index === index;
const active = tabsAnims[index];
// Scale the label up
const scale =
labeled && shifting
? active.interpolate({
inputRange: [0, 1],
outputRange: [0.5, 1],
})
: 1;
// Move down the icon to account for no-label in shifting and smaller label in non-shifting.
const translateY = labeled
? shifting
? active.interpolate({
inputRange: [0, 1],
outputRange: [7, 0],
})
: 0
: 7;
// We render the active icon and label on top of inactive ones and cross-fade them on change.
// This trick gives the illusion that we are animating between active and inactive colors.
// This is to ensure that we can use native driver, as colors cannot be animated with native driver.
const activeOpacity = active;
const inactiveOpacity = active.interpolate({
inputRange: [0, 1],
outputRange: [1, 0],
});
const v3ActiveOpacity = focused ? 1 : 0;
const v3InactiveOpacity = shifting
? inactiveOpacity
: focused
? 0
: 1;
// Scale horizontally the outline pill
const outlineScale = focused
? active.interpolate({
inputRange: [0, 1],
outputRange: [0.5, 1],
})
: 0;
const badge = getBadge({ route });
const activeLabelColor = getLabelColor({
tintColor: activeTintColor,
hasColor: Boolean(activeColor),
focused,
defaultColor: textColor,
theme,
});
const inactiveLabelColor = getLabelColor({
tintColor: inactiveTintColor,
hasColor: Boolean(inactiveColor),
focused,
defaultColor: textColor,
theme,
});
const badgeStyle = {
top: !isV3 ? -2 : typeof badge === 'boolean' ? 4 : 2,
right:
(badge != null && typeof badge !== 'boolean'
? String(badge).length * -2
: 0) - (!isV3 ? 2 : 0),
};
const isLegacyOrV3Shifting = !isV3 || (isV3 && shifting && labeled);
const font = isV3 ? theme.fonts.labelMedium : {};
return renderTouchable({
key: route.key,
route,
borderless: true,
centered: true,
rippleColor: isV3 ? 'transparent' : touchColor,
onPress: () => onTabPress(eventForIndex(index)),
onLongPress: () => onTabLongPress?.(eventForIndex(index)),
testID: getTestID({ route }),
accessibilityLabel: getAccessibilityLabel({ route }),
accessibilityRole: Platform.OS === 'ios' ? 'button' : 'tab',
accessibilityState: { selected: focused },
style: [styles.item, isV3 && styles.v3Item],
children: (
<View
pointerEvents="none"
style={
isV3 &&
(labeled
? styles.v3TouchableContainer
: styles.v3NoLabelContainer)
}
>
<Animated.View
style={[
styles.iconContainer,
isV3 && styles.v3IconContainer,
isLegacyOrV3Shifting && {
transform: [{ translateY }],
},
]}
>
{isV3 && focused && (
<Animated.View
style={[
styles.outline,
{
transform: [
{
scaleX: outlineScale,
},
],
backgroundColor: theme.colors.secondaryContainer,
},
activeIndicatorStyle,
]}
/>
)}
<Animated.View
style={[
styles.iconWrapper,
isV3 && styles.v3IconWrapper,
{
opacity: isLegacyOrV3Shifting
? activeOpacity
: v3ActiveOpacity,
},
]}
>
{renderIcon ? (
renderIcon({
route,
focused: true,
color: activeTintColor,
})
) : (
<Icon
source={route.focusedIcon as IconSource}
color={activeTintColor}
size={24}
/>
)}
</Animated.View>
<Animated.View
style={[
styles.iconWrapper,
isV3 && styles.v3IconWrapper,
{
opacity: isLegacyOrV3Shifting
? inactiveOpacity
: v3InactiveOpacity,
},
]}
>
{renderIcon ? (
renderIcon({
route,
focused: false,
color: inactiveTintColor,
})
) : (
<Icon
source={
theme.isV3 && route.unfocusedIcon !== undefined
? route.unfocusedIcon
: (route.focusedIcon as IconSource)
}
color={inactiveTintColor}
size={24}
/>
)}
</Animated.View>
<View style={[styles.badgeContainer, badgeStyle]}>
{typeof badge === 'boolean' ? (
<Badge visible={badge} size={isV3 ? 6 : 8} />
) : (
<Badge visible={badge != null} size={16}>
{badge}
</Badge>
)}
</View>
</Animated.View>
{labeled ? (
<Animated.View
style={[
styles.labelContainer,
!isV3 && { transform: [{ scale }] },
]}
>
<Animated.View
style={[
styles.labelWrapper,
{
opacity: isLegacyOrV3Shifting
? activeOpacity
: v3ActiveOpacity,
},
]}
>
{renderLabel ? (
renderLabel({
route,
focused: true,
color: activeLabelColor,
})
) : (
<Text
maxFontSizeMultiplier={labelMaxFontSizeMultiplier}
variant="labelMedium"
style={[
styles.label,
{
color: activeLabelColor,
...font,
},
]}
>
{getLabelText({ route })}
</Text>
)}
</Animated.View>
{shifting ? null : (
<Animated.View
style={[
styles.labelWrapper,
{
opacity: isLegacyOrV3Shifting
? inactiveOpacity
: v3InactiveOpacity,
},
]}
>
{renderLabel ? (
renderLabel({
route,
focused: false,
color: inactiveLabelColor,
})
) : (
<Text
maxFontSizeMultiplier={labelMaxFontSizeMultiplier}
variant="labelMedium"
selectable={false}
style={[
styles.label,
{
color: inactiveLabelColor,
...font,
},
]}
>
{getLabelText({ route })}
</Text>
)}
</Animated.View>
)}
</Animated.View>
) : (
!isV3 && <View style={styles.labelContainer} />
)}
</View>
),
});
})}
</View>
</Animated.View>
</Surface>
);
};
BottomNavigationBar.displayName = 'BottomNavigation.Bar';
export default BottomNavigationBar;
const styles = StyleSheet.create({
bar: {
left: 0,
right: 0,
bottom: 0,
},
barContent: {
alignItems: 'center',
overflow: 'hidden',
},
items: {
flexDirection: 'row',
...(Platform.OS === 'web'
? {
width: '100%',
}
: null),
},
item: {
flex: 1,
// Top padding is 6 and bottom padding is 10
// The extra 4dp bottom padding is offset by label's height
paddingVertical: 6,
},
v3Item: {
paddingVertical: 0,
},
ripple: {
position: 'absolute',
},
iconContainer: {
height: 24,
width: 24,
marginTop: 2,
marginHorizontal: 12,
alignSelf: 'center',
},
v3IconContainer: {
height: 32,
width: 32,
marginBottom: 4,
marginTop: 0,
justifyContent: 'center',
},
iconWrapper: {
...StyleSheet.absoluteFill,
alignItems: 'center',
},
v3IconWrapper: {
top: 4,
},
labelContainer: {
height: 16,
paddingBottom: 2,
},
labelWrapper: {
...StyleSheet.absoluteFill,
},
// eslint-disable-next-line react-native/no-color-literals
label: {
fontSize: 12,
height: BAR_HEIGHT,
textAlign: 'center',
backgroundColor: 'transparent',
...(Platform.OS === 'web'
? {
whiteSpace: 'nowrap',
alignSelf: 'center',
}
: null),
},
badgeContainer: {
position: 'absolute',
left: 0,
},
v3TouchableContainer: {
paddingTop: 12,
paddingBottom: 16,
},
v3NoLabelContainer: {
height: 80,
justifyContent: 'center',
alignItems: 'center',
},
outline: {
width: OUTLINE_WIDTH,