Skip to content

Commit ee3679c

Browse files
committed
Add href to tab bar examples
1 parent 3e50b6a commit ee3679c

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

versioned_docs/version-7.x/bottom-tab-navigator.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,13 @@ Example:
189189
<samp id="custom-tab-bar" />
190190

191191
```js
192-
import { View, Text, TouchableOpacity } from 'react-native';
192+
import { View, Text, TouchableOpacity, Platform } from 'react-native';
193+
import { useLinkBuilder, useTheme } from '@react-navigation/native';
193194

194195
function MyTabBar({ state, descriptors, navigation }) {
196+
const { colors } = useTheme();
197+
const { buildHref } = useLinkBuilder();
198+
195199
return (
196200
<View style={{ flexDirection: 'row' }}>
197201
{state.routes.map((route, index) => {
@@ -200,8 +204,8 @@ function MyTabBar({ state, descriptors, navigation }) {
200204
options.tabBarLabel !== undefined
201205
? options.tabBarLabel
202206
: options.title !== undefined
203-
? options.title
204-
: route.name;
207+
? options.title
208+
: route.name;
205209

206210
const isFocused = state.index === index;
207211

@@ -213,8 +217,7 @@ function MyTabBar({ state, descriptors, navigation }) {
213217
});
214218

215219
if (!isFocused && !event.defaultPrevented) {
216-
// The `merge: true` option makes sure that the params inside the tab screen are preserved
217-
navigation.navigate({ name: route.name, merge: true });
220+
navigation.navigate(route.name, route.params);
218221
}
219222
};
220223

@@ -227,15 +230,16 @@ function MyTabBar({ state, descriptors, navigation }) {
227230

228231
return (
229232
<TouchableOpacity
230-
accessibilityRole="button"
233+
href={buildHref(route.name, route.params)}
234+
accessibilityRole={Platform.OS === 'web' ? 'link' : 'button'}
231235
accessibilityState={isFocused ? { selected: true } : {}}
232236
accessibilityLabel={options.tabBarAccessibilityLabel}
233237
testID={options.tabBarButtonTestID}
234238
onPress={onPress}
235239
onLongPress={onLongPress}
236240
style={{ flex: 1 }}
237241
>
238-
<Text style={{ color: isFocused ? '#673ab7' : '#222' }}>
242+
<Text style={{ color: isFocused ? colors.primary : colors.text }}>
239243
{label}
240244
</Text>
241245
</TouchableOpacity>
@@ -247,9 +251,9 @@ function MyTabBar({ state, descriptors, navigation }) {
247251

248252
// ...
249253

250-
<Tab.Navigator tabBar={props => <MyTabBar {...props} />}>
251-
{...}
252-
</Tab.Navigator>
254+
<Tab.Navigator tabBar={(props) => <MyTabBar {...props} />}>
255+
{/* ... */}
256+
</Tab.Navigator>;
253257
```
254258

255259
This example will render a basic tab bar with labels.

versioned_docs/version-7.x/material-top-tab-navigator.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,13 @@ Example:
227227
<samp id="material-top-tab-custom-tab-bar" />
228228

229229
```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';
231232

232233
function MyTabBar({ state, descriptors, navigation, position }) {
234+
const { colors } = useTheme();
235+
const { buildHref } = useLinkBuilder();
236+
233237
return (
234238
<View style={{ flexDirection: 'row' }}>
235239
{state.routes.map((route, index) => {
@@ -238,8 +242,8 @@ function MyTabBar({ state, descriptors, navigation, position }) {
238242
options.tabBarLabel !== undefined
239243
? options.tabBarLabel
240244
: options.title !== undefined
241-
? options.title
242-
: route.name;
245+
? options.title
246+
: route.name;
243247

244248
const isFocused = state.index === index;
245249

@@ -251,8 +255,7 @@ function MyTabBar({ state, descriptors, navigation, position }) {
251255
});
252256

253257
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);
256259
}
257260
};
258261

@@ -266,20 +269,21 @@ function MyTabBar({ state, descriptors, navigation, position }) {
266269
const inputRange = state.routes.map((_, i) => i);
267270
const opacity = position.interpolate({
268271
inputRange,
269-
outputRange: inputRange.map(i => (i === index ? 1 : 0)),
272+
outputRange: inputRange.map((i) => (i === index ? 1 : 0)),
270273
});
271274

272275
return (
273276
<TouchableOpacity
274-
accessibilityRole="button"
277+
href={buildHref(route.name, route.params)}
278+
accessibilityRole={Platform.OS === 'web' ? 'link' : 'button'}
275279
accessibilityState={isFocused ? { selected: true } : {}}
276280
accessibilityLabel={options.tabBarAccessibilityLabel}
277281
testID={options.tabBarButtonTestID}
278282
onPress={onPress}
279283
onLongPress={onLongPress}
280284
style={{ flex: 1 }}
281285
>
282-
<Animated.Text style={{ opacity }}>
286+
<Animated.Text style={{ opacity, color: colors.text }}>
283287
{label}
284288
</Animated.Text>
285289
</TouchableOpacity>
@@ -291,9 +295,9 @@ function MyTabBar({ state, descriptors, navigation, position }) {
291295

292296
// ...
293297

294-
<Tab.Navigator tabBar={props => <MyTabBar {...props} />}>
295-
{...}
296-
</Tab.Navigator>
298+
<Tab.Navigator tabBar={(props) => <MyTabBar {...props} />}>
299+
{/* ... */}
300+
</Tab.Navigator>;
297301
```
298302

299303
This example will render a basic tab bar with labels.

0 commit comments

Comments
 (0)