@react-navigation/material-top-tabs 7.0.0-alpha.9 → 7.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/lib/commonjs/index.js.map +1 -1
  2. package/lib/commonjs/navigators/createMaterialTopTabNavigator.js +20 -17
  3. package/lib/commonjs/navigators/createMaterialTopTabNavigator.js.map +1 -1
  4. package/lib/commonjs/types.js.map +1 -1
  5. package/lib/commonjs/utils/TabAnimationContext.js +1 -1
  6. package/lib/commonjs/utils/TabAnimationContext.js.map +1 -1
  7. package/lib/commonjs/utils/useTabAnimation.js +1 -1
  8. package/lib/commonjs/utils/useTabAnimation.js.map +1 -1
  9. package/lib/commonjs/views/MaterialTopTabBar.js +60 -107
  10. package/lib/commonjs/views/MaterialTopTabBar.js.map +1 -1
  11. package/lib/commonjs/views/MaterialTopTabView.js +23 -33
  12. package/lib/commonjs/views/MaterialTopTabView.js.map +1 -1
  13. package/lib/module/index.js.map +1 -1
  14. package/lib/module/navigators/createMaterialTopTabNavigator.js +18 -15
  15. package/lib/module/navigators/createMaterialTopTabNavigator.js.map +1 -1
  16. package/lib/module/types.js.map +1 -1
  17. package/lib/module/utils/TabAnimationContext.js.map +1 -1
  18. package/lib/module/utils/useTabAnimation.js.map +1 -1
  19. package/lib/module/views/MaterialTopTabBar.js +60 -107
  20. package/lib/module/views/MaterialTopTabBar.js.map +1 -1
  21. package/lib/module/views/MaterialTopTabView.js +22 -32
  22. package/lib/module/views/MaterialTopTabView.js.map +1 -1
  23. package/lib/typescript/src/index.d.ts +1 -1
  24. package/lib/typescript/src/index.d.ts.map +1 -1
  25. package/lib/typescript/src/navigators/createMaterialTopTabNavigator.d.ts +14 -9
  26. package/lib/typescript/src/navigators/createMaterialTopTabNavigator.d.ts.map +1 -1
  27. package/lib/typescript/src/types.d.ts +6 -7
  28. package/lib/typescript/src/types.d.ts.map +1 -1
  29. package/lib/typescript/src/views/MaterialTopTabBar.d.ts.map +1 -1
  30. package/package.json +13 -13
  31. package/src/index.tsx +1 -0
  32. package/src/navigators/createMaterialTopTabNavigator.tsx +33 -7
  33. package/src/types.tsx +14 -7
  34. package/src/views/MaterialTopTabBar.tsx +59 -70
package/src/types.tsx CHANGED
@@ -7,6 +7,7 @@ import type {
7
7
  RouteProp,
8
8
  TabActionHelpers,
9
9
  TabNavigationState,
10
+ Theme,
10
11
  } from '@react-navigation/native';
11
12
  import type React from 'react';
12
13
  import type {
@@ -70,6 +71,14 @@ export type MaterialTopTabScreenProps<
70
71
  route: RouteProp<ParamList, RouteName>;
71
72
  };
72
73
 
74
+ export type MaterialTopTabOptionsArgs<
75
+ ParamList extends ParamListBase,
76
+ RouteName extends keyof ParamList = keyof ParamList,
77
+ NavigatorID extends string | undefined = undefined,
78
+ > = MaterialTopTabScreenProps<ParamList, RouteName, NavigatorID> & {
79
+ theme: Theme;
80
+ };
81
+
73
82
  export type MaterialTopTabNavigationOptions = {
74
83
  /**
75
84
  * Title text for the screen.
@@ -109,7 +118,10 @@ export type MaterialTopTabNavigationOptions = {
109
118
  /**
110
119
  * A function that given { focused: boolean, color: string } returns a React.Node to display in the tab bar.
111
120
  */
112
- tabBarIcon?: (props: { focused: boolean; color: string }) => React.ReactNode;
121
+ tabBarIcon?: (props: {
122
+ focused: boolean;
123
+ color: string;
124
+ }) => React.ReactElement;
113
125
 
114
126
  /**
115
127
  * Whether the tab icon should be visible. Defaults to `false`.
@@ -119,7 +131,7 @@ export type MaterialTopTabNavigationOptions = {
119
131
  /**
120
132
  * Function that returns a React element to use as a badge for the tab.
121
133
  */
122
- tabBarBadge?: () => React.ReactNode;
134
+ tabBarBadge?: () => React.ReactElement;
123
135
 
124
136
  /**
125
137
  * Function that returns a React element as the tab bar indicator.
@@ -180,11 +192,6 @@ export type MaterialTopTabNavigationOptions = {
180
192
  */
181
193
  tabBarScrollEnabled?: boolean;
182
194
 
183
- /**
184
- * Style object for the tab icon container.
185
- */
186
- tabBarIconStyle?: StyleProp<ViewStyle>;
187
-
188
195
  /**
189
196
  * Style object for the tab label.
190
197
  */
@@ -1,18 +1,45 @@
1
1
  import { Text } from '@react-navigation/elements';
2
2
  import {
3
3
  type ParamListBase,
4
- type Route,
5
4
  type TabNavigationState,
5
+ useLinkBuilder,
6
6
  useLocale,
7
7
  useTheme,
8
8
  } from '@react-navigation/native';
9
9
  import Color from 'color';
10
10
  import * as React from 'react';
11
- import { StyleSheet, View } from 'react-native';
11
+ import { type StyleProp, StyleSheet, type ViewStyle } from 'react-native';
12
12
  import { TabBar, TabBarIndicator } from 'react-native-tab-view';
13
13
 
14
14
  import type { MaterialTopTabBarProps } from '../types';
15
15
 
16
+ type MaterialLabelType = {
17
+ color: string;
18
+ labelText?: string;
19
+ labelStyle?: StyleProp<ViewStyle>;
20
+ allowScaling?: boolean;
21
+ };
22
+
23
+ const MaterialLabel = ({
24
+ color,
25
+ labelText,
26
+ labelStyle,
27
+ allowScaling,
28
+ }: MaterialLabelType) => {
29
+ return (
30
+ <Text
31
+ style={[{ color }, styles.label, labelStyle]}
32
+ allowFontScaling={allowScaling}
33
+ >
34
+ {labelText}
35
+ </Text>
36
+ );
37
+ };
38
+
39
+ const renderLabel = (props: MaterialLabelType) => {
40
+ return <MaterialLabel {...props} />;
41
+ };
42
+
16
43
  export function MaterialTopTabBar({
17
44
  state,
18
45
  navigation,
@@ -21,6 +48,7 @@ export function MaterialTopTabBar({
21
48
  }: MaterialTopTabBarProps) {
22
49
  const { colors } = useTheme();
23
50
  const { direction } = useLocale();
51
+ const { buildHref } = useLinkBuilder();
24
52
 
25
53
  const focusedOptions = descriptors[state.routes[state.index].key].options;
26
54
 
@@ -29,10 +57,38 @@ export function MaterialTopTabBar({
29
57
  focusedOptions.tabBarInactiveTintColor ??
30
58
  Color(activeColor).alpha(0.5).rgb().string();
31
59
 
60
+ const tabBarOptions = Object.fromEntries(
61
+ state.routes.map((route) => {
62
+ const { options } = descriptors[route.key];
63
+
64
+ return [
65
+ route.key,
66
+ {
67
+ href: buildHref(route.name, route.params),
68
+ testID: options.tabBarButtonTestID,
69
+ accessibilityLabel: options.tabBarAccessibilityLabel,
70
+ badge: options.tabBarBadge,
71
+ icon:
72
+ options.tabBarShowIcon === false ? undefined : options.tabBarIcon,
73
+ label: options.tabBarShowLabel === false ? undefined : renderLabel,
74
+ labelAllowFontScaling: options.tabBarAllowFontScaling,
75
+ labelStyle: options.tabBarLabelStyle,
76
+ labelText:
77
+ options.tabBarShowLabel === false
78
+ ? undefined
79
+ : options.title !== undefined
80
+ ? options.title
81
+ : route.name,
82
+ },
83
+ ];
84
+ })
85
+ );
86
+
32
87
  return (
33
88
  <TabBar
34
89
  {...rest}
35
90
  navigationState={state}
91
+ options={tabBarOptions}
36
92
  direction={direction}
37
93
  scrollEnabled={focusedOptions.tabBarScrollEnabled}
38
94
  bounces={focusedOptions.tabBarBounces}
@@ -50,12 +106,6 @@ export function MaterialTopTabBar({
50
106
  indicatorContainerStyle={focusedOptions.tabBarIndicatorContainerStyle}
51
107
  contentContainerStyle={focusedOptions.tabBarContentContainerStyle}
52
108
  style={[{ backgroundColor: colors.card }, focusedOptions.tabBarStyle]}
53
- getAccessibilityLabel={({ route }) =>
54
- descriptors[route.key].options.tabBarAccessibilityLabel
55
- }
56
- getTestID={({ route }) =>
57
- descriptors[route.key].options.tabBarButtonTestID
58
- }
59
109
  onTabPress={({ route, preventDefault }) => {
60
110
  const event = navigation.emit({
61
111
  type: 'tabPress',
@@ -73,62 +123,6 @@ export function MaterialTopTabBar({
73
123
  target: route.key,
74
124
  })
75
125
  }
76
- renderIcon={({ route, focused, color }) => {
77
- const { options } = descriptors[route.key];
78
-
79
- if (options.tabBarShowIcon === false) {
80
- return null;
81
- }
82
-
83
- if (options.tabBarIcon !== undefined) {
84
- const icon = options.tabBarIcon({ focused, color });
85
-
86
- return (
87
- <View style={[styles.icon, options.tabBarIconStyle]}>{icon}</View>
88
- );
89
- }
90
-
91
- return null;
92
- }}
93
- renderLabel={({ route, focused, color }) => {
94
- const { options } = descriptors[route.key];
95
-
96
- if (options.tabBarShowLabel === false) {
97
- return null;
98
- }
99
-
100
- const label =
101
- options.tabBarLabel !== undefined
102
- ? options.tabBarLabel
103
- : options.title !== undefined
104
- ? options.title
105
- : (route as Route<string>).name;
106
-
107
- if (typeof label === 'string') {
108
- return (
109
- <Text
110
- style={[{ color }, styles.label, options.tabBarLabelStyle]}
111
- allowFontScaling={options.tabBarAllowFontScaling}
112
- >
113
- {label}
114
- </Text>
115
- );
116
- }
117
-
118
- const children =
119
- typeof options.tabBarLabel === 'string'
120
- ? options.tabBarLabel
121
- : options.title !== undefined
122
- ? options.title
123
- : route.name;
124
-
125
- return label({ focused, color, children });
126
- }}
127
- renderBadge={({ route }) => {
128
- const { tabBarBadge } = descriptors[route.key].options;
129
-
130
- return tabBarBadge?.() ?? null;
131
- }}
132
126
  renderIndicator={({ navigationState: state, ...rest }) => {
133
127
  return focusedOptions.tabBarIndicator ? (
134
128
  focusedOptions.tabBarIndicator({
@@ -144,14 +138,9 @@ export function MaterialTopTabBar({
144
138
  }
145
139
 
146
140
  const styles = StyleSheet.create({
147
- icon: {
148
- height: 24,
149
- width: 24,
150
- },
151
141
  label: {
152
142
  textAlign: 'center',
153
- textTransform: 'uppercase',
154
- fontSize: 13,
143
+ fontSize: 14,
155
144
  margin: 4,
156
145
  backgroundColor: 'transparent',
157
146
  },