Skip to content
This repository was archived by the owner on Nov 27, 2022. It is now read-only.

Commit 18cc82a

Browse files
committed
feat: add activeColor and inactiveColor to customizethe label
1 parent 92049f8 commit 18cc82a

File tree

5 files changed

+178
-90
lines changed

5 files changed

+178
-90
lines changed

README.md

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ A cross-platform Tab View component for React Native.
2727
Open a Terminal in the project root and run:
2828

2929
```sh
30-
yarn add react-native-tab-view
30+
yarn add react-native-tab-view@alpha
3131
```
3232

3333
You also need to install and link [react-native-gesture-handler](https://github.com/kmagiera/react-native-gesture-handler) and [react-native-reanimated](https://github.com/kmagiera/react-native-reanimated). Follow the instructions on the linked repos to configure them. This step is unnecessary if you use Expo.
@@ -272,27 +272,27 @@ renderTabBar={props =>
272272

273273
##### `getLabelText`
274274

275-
Function which takes the current scene and returns the label text for the tab. Uses `route.title` by default.
275+
Function which takes an object with the current route and returns the label text for the tab. Uses `route.title` by default.
276276

277277
```js
278278
getLabelText={({ route }) => route.title}
279279
```
280280

281281
##### `getAccessible`
282282

283-
Function which takes the current scene returns a boolean to indicate whether to mark a tab as `accessible`. Defaults to `true`.
283+
Function which takes an object with the current route and returns a boolean to indicate whether to mark a tab as `accessible`. Defaults to `true`.
284284

285285
##### `getAccessibilityLabel`
286286

287-
Function which takes the current scene and returns a accessibility label for the tab button. Uses `route.accessibilityLabel` by default if specified, otherwise uses the route title.
287+
Function which takes an object with the current route and returns a accessibility label for the tab button. Uses `route.accessibilityLabel` by default if specified, otherwise uses the route title.
288288

289289
```js
290290
getAccessibilityLabel={({ route }) => route.accessibilityLabel}
291291
```
292292

293293
##### `testID`
294294

295-
Function which takes the current scene and returns a test id for the tab button to locate this tab button in tests. Uses `route.testID` by default.
295+
Function which takes an object with the current route and returns a test id for the tab button to locate this tab button in tests. Uses `route.testID` by default.
296296

297297
```js
298298
getTestID={({ route }) => route.testID}
@@ -307,19 +307,36 @@ Get the id to locate this tab button in tests, uses `route.testID` by default.
307307

308308
##### `renderIcon`
309309

310-
Function which takes the current scene and returns a custom React Element to be used as a icon.
310+
Function which takes an object with the current route, focused status and color and returns a custom React Element to be used as a icon.
311+
312+
```js
313+
renderIcon={({ route, focused, color }) => (
314+
<Icon
315+
name={focused ? 'abums' : 'albums-outlined'}
316+
color={color}
317+
/>
318+
)}
319+
```
311320

312321
##### `renderLabel`
313322

314-
Function which takes the current scene and returns a custom React Element to be used as a label.
323+
Function which takes an object with the current route, focused status and color and returns a custom React Element to be used as a label.
324+
325+
```js
326+
renderLabel={({ route, focused, color }) => (
327+
<Text style={{ color, margin: 8 }}>
328+
{route.title}
329+
</Text>
330+
)}
331+
```
315332

316333
##### `renderIndicator`
317334

318-
Function which takes the current scene and returns a custom React Element to be used as a tab indicator.
335+
Function which takes an object with the current route and returns a custom React Element to be used as a tab indicator.
319336

320337
##### `renderBadge`
321338

322-
Function which takes the current scene and returns a custom React Element to be used as a badge.
339+
Function which takes an object with the current route and returns a custom React Element to be used as a badge.
323340

324341
##### `onTabPress`
325342

@@ -329,6 +346,14 @@ Function to execute on tab press. It receives the scene for the pressed tab, use
329346

330347
Function to execute on tab long press, use for things like showing a menu with more options
331348

349+
##### `activeColor`
350+
351+
Custom color for icon and label in the active tab.
352+
353+
##### `inactiveColor`
354+
355+
Custom color for icon and label in the inactive tab.
356+
332357
##### `pressColor`
333358

334359
Color for material ripple (Android >= 5.0 only).

example/src/ScrollableTabBarExample.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ const styles = StyleSheet.create({
8080
backgroundColor: '#ffeb3b',
8181
},
8282
label: {
83-
color: '#fff',
8483
fontWeight: '400',
8584
},
8685
});

example/src/TabBarIconExample.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export default class TabBarIconExample extends React.Component<*, State> {
3737
index,
3838
});
3939

40-
_renderIcon = ({ route }) => (
41-
<Ionicons name={route.icon} size={24} color="white" />
40+
_renderIcon = ({ route, color }) => (
41+
<Ionicons name={route.icon} size={24} color={color} />
4242
);
4343

4444
_renderTabBar = props => {

src/TabBar.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import * as React from 'react';
4-
import { StyleSheet, View, ScrollView, Platform } from 'react-native';
4+
import { StyleSheet, View, ScrollView } from 'react-native';
55
import Animated from 'react-native-reanimated';
66
import TabBarItem from './TabBarItem';
77
import TabBarIndicator, {
@@ -16,14 +16,24 @@ import type { Scene, SceneRendererProps } from './types';
1616
type Props<T> = SceneRendererProps<T> & {
1717
scrollEnabled?: boolean,
1818
bounces?: boolean,
19+
activeColor?: string,
20+
inactiveColor?: string,
1921
pressColor?: string,
2022
pressOpacity?: number,
2123
getLabelText: (scene: Scene<T>) => ?string,
2224
getAccessible: (scene: Scene<T>) => ?boolean,
2325
getAccessibilityLabel: (scene: Scene<T>) => ?string,
2426
getTestID: (scene: Scene<T>) => ?string,
25-
renderLabel?: (scene: Scene<T>) => React.Node,
26-
renderIcon?: (scene: Scene<T>) => React.Node,
27+
renderLabel?: (scene: {
28+
route: T,
29+
focused: boolean,
30+
color: string,
31+
}) => React.Node,
32+
renderIcon?: (scene: {
33+
route: T,
34+
focused: boolean,
35+
color: string,
36+
}) => React.Node,
2737
renderBadge?: (scene: Scene<T>) => React.Node,
2838
renderIndicator: (props: IndicatorProps<T>) => React.Node,
2939
onTabPress?: (scene: Scene<T>) => mixed,
@@ -234,6 +244,8 @@ export default class TabBar<T: *> extends React.Component<Props<T>, State> {
234244
renderBadge,
235245
renderIcon,
236246
renderLabel,
247+
activeColor,
248+
inactiveColor,
237249
pressColor,
238250
pressOpacity,
239251
tabStyle,
@@ -305,7 +317,7 @@ export default class TabBar<T: *> extends React.Component<Props<T>, State> {
305317
<TabBarItem
306318
key={route.key}
307319
position={position}
308-
scene={{ route }}
320+
route={route}
309321
tabWidth={tabWidth}
310322
navigationState={navigationState}
311323
scrollEnabled={scrollEnabled}
@@ -318,6 +330,8 @@ export default class TabBar<T: *> extends React.Component<Props<T>, State> {
318330
renderLabel={renderLabel}
319331
tabStyle={tabStyle}
320332
labelStyle={labelStyle}
333+
activeColor={activeColor}
334+
inactiveColor={inactiveColor}
321335
pressColor={pressColor}
322336
pressOpacity={pressOpacity}
323337
onTabPress={this._handleTabPress}
@@ -336,19 +350,18 @@ const styles = StyleSheet.create({
336350
flex: 1,
337351
},
338352
scroll: {
339-
overflow: Platform.OS === 'web' ? ('auto': any) : 'scroll',
353+
overflow: 'scroll',
340354
},
341355
tabBar: {
342356
backgroundColor: '#2196f3',
343357
elevation: 4,
344-
shadowColor: 'black',
345-
shadowOpacity: 0.1,
346-
shadowRadius: StyleSheet.hairlineWidth,
347-
shadowOffset: {
348-
height: StyleSheet.hairlineWidth,
349-
},
350-
// We don't need zIndex on Android, disable it since it's buggy
351-
zIndex: Platform.OS === 'android' ? 0 : 1,
358+
// shadowColor: 'black',
359+
// shadowOpacity: 0.1,
360+
// shadowRadius: StyleSheet.hairlineWidth,
361+
// shadowOffset: {
362+
// height: StyleSheet.hairlineWidth,
363+
// },
364+
zIndex: 1,
352365
},
353366
tabContent: {
354367
flexDirection: 'row',

0 commit comments

Comments
 (0)