@@ -227,9 +227,13 @@ Example:
227
227
<samp id =" material-top-tab-custom-tab-bar " />
228
228
229
229
``` js
230
- import { Animated , View , TouchableOpacity } from ' react-native' ;
230
+ import { Animated , View , TouchableOpacity , Platform } from ' react-native' ;
231
+ import { useLinkBuilder , useTheme } from ' @react-navigation/native' ;
231
232
232
233
function MyTabBar ({ state, descriptors, navigation, position }) {
234
+ const { colors } = useTheme ();
235
+ const { buildHref } = useLinkBuilder ();
236
+
233
237
return (
234
238
< View style= {{ flexDirection: ' row' }}>
235
239
{state .routes .map ((route , index ) => {
@@ -238,8 +242,8 @@ function MyTabBar({ state, descriptors, navigation, position }) {
238
242
options .tabBarLabel !== undefined
239
243
? options .tabBarLabel
240
244
: options .title !== undefined
241
- ? options .title
242
- : route .name ;
245
+ ? options .title
246
+ : route .name ;
243
247
244
248
const isFocused = state .index === index;
245
249
@@ -251,8 +255,7 @@ function MyTabBar({ state, descriptors, navigation, position }) {
251
255
});
252
256
253
257
if (! isFocused && ! event .defaultPrevented ) {
254
- // The `merge: true` option makes sure that the params inside the tab screen are preserved
255
- navigation .navigate ({ name: route .name , merge: true });
258
+ navigation .navigate (route .name , route .params );
256
259
}
257
260
};
258
261
@@ -266,20 +269,21 @@ function MyTabBar({ state, descriptors, navigation, position }) {
266
269
const inputRange = state .routes .map ((_ , i ) => i);
267
270
const opacity = position .interpolate ({
268
271
inputRange,
269
- outputRange: inputRange .map (i => (i === index ? 1 : 0 )),
272
+ outputRange: inputRange .map (( i ) => (i === index ? 1 : 0 )),
270
273
});
271
274
272
275
return (
273
276
< TouchableOpacity
274
- accessibilityRole= " button"
277
+ href= {buildHref (route .name , route .params )}
278
+ accessibilityRole= {Platform .OS === ' web' ? ' link' : ' button' }
275
279
accessibilityState= {isFocused ? { selected: true } : {}}
276
280
accessibilityLabel= {options .tabBarAccessibilityLabel }
277
281
testID= {options .tabBarButtonTestID }
278
282
onPress= {onPress}
279
283
onLongPress= {onLongPress}
280
284
style= {{ flex: 1 }}
281
285
>
282
- < Animated .Text style= {{ opacity }}>
286
+ < Animated .Text style= {{ opacity, color : colors . text }}>
283
287
{label}
284
288
< / Animated .Text >
285
289
< / TouchableOpacity>
@@ -291,9 +295,9 @@ function MyTabBar({ state, descriptors, navigation, position }) {
291
295
292
296
// ...
293
297
294
- < Tab .Navigator tabBar= {props => < MyTabBar {... props} / > }>
295
- {... }
296
- < / Tab .Navigator >
298
+ < Tab .Navigator tabBar= {( props ) => < MyTabBar {... props} / > }>
299
+ {/* ... */ }
300
+ < / Tab .Navigator > ;
297
301
```
298
302
299
303
This example will render a basic tab bar with labels.
0 commit comments