leabum
leabum

Reputation: 395

How to create gradient border color for React Navigation TabBar

I want to place a thin gradient border ontop of my TabBar. I tried to simply implement it with the BorderTopColor style but React Native does not support gradients per default. So I installed www.npmjs.com/package/react-native-linear-gradient. However, I do not know hot to place the gradient container between my TabBar and the Screens.

Here is what I have so far:

const Tab = createBottomTabNavigator();

const App: () => Node = () => {
  const customTabBarStyle = {
    activeTintColor: '#ffffff',
    inactiveTintColor: '#b9b9b9',
    showLabel: false,
    style: {
      backgroundColor: '#1e1e1e',
      height: 200
    }
  };

  return (
    <NavigationContainer>
      <LinearGradient start={{x: 0, y: 0}} end={{x: 1, y: 0}} colors={['red', 'yellow']} style={{height: 2}} />
      <Tab.Navigator
        tabBarOptions = {customTabBarStyle}
        screenOptions={{ tabBarStyle: { backgroundColor: '#1e1e1e', borderTopWidth: 0, elevation: 0}, headerStyle: { backgroundColor: '#1e1e1e'}, headerTitleStyle: {color: 'white'}}}>
        <Tab.Screen
          name="PrimaryMarket"
          component={PrimaryMarketScreen}
          options={{
            tabBarIcon: ({color}) => (
              <Icon name="shopping-cart" size={40} color={color} />
            ),
          }}
        />
      </Tab.Navigator>
    </NavigationContainer>
  );
};

Here is what I want:

enter image description here

Upvotes: 2

Views: 2463

Answers (1)

ridvanaltun
ridvanaltun

Reputation: 3020

I had the same issue, so I looked to the source code of react-navigation v6 to solve the problem. In source code, I found this for tabBarBackground:

Function which returns a React Element to use as background for the tab bar. You could render an image, a gradient, blur view etc.

You can use tabBarBackground inside screenOptions like below:

const Tab = createBottomTabNavigator();

const App: () => Node = () => {
  const customTabBarStyle = {
    activeTintColor: "#ffffff",
    inactiveTintColor: "#b9b9b9",
    showLabel: false,
    style: {
      backgroundColor: "#1e1e1e",
      height: 200,
    },
  };

  return (
    <NavigationContainer>
      <Tab.Navigator
        tabBarOptions={customTabBarStyle}
        screenOptions={{
          tabBarStyle: {
            borderTopWidth: 0,
            elevation: 0,
          },
          headerStyle: { backgroundColor: "#1e1e1e" },
          headerTitleStyle: { color: "white" },
          tabBarBackground: () => (
            <View style={{ flex: 1 }}>
              <LinearGradient
                start={{ x: 0, y: 0 }}
                end={{ x: 1, y: 0 }}
                colors={["red", "yellow"]}
                style={{ height: 2 }}
              />
            </View>
          ),
        }}
      >
        <Tab.Screen
          name="PrimaryMarket"
          component={PrimaryMarketScreen}
          options={{
            tabBarIcon: ({ color }) => (
              <Icon name="shopping-cart" size={40} color={color} />
            ),
          }}
        />
      </Tab.Navigator>
    </NavigationContainer>
  );
};

Not same code but I used same technique:

showcase

Upvotes: 5

Related Questions