6.0.1 • Published 2 years ago

@swanstack/react-navigation-bottom-tabs v6.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Custom React Navigation Bottom Tabs

npm runs with expo

This extension to @react-navigation/bottom-tabs introduces a seamless solution for managing a large number of tabs. When the tabs exceed a specified limit, the last tab automatically transforms into a 'More' tab, revealing a Bottom Sheet with the remaining tabs.

Inspired by:

Screenshots

Setup

yarn add @swanstack/react-navigation-bottom-tabs

Dependencies

This library needs these dependencies to be installed in your project before you can use it:

yarn add react-native-reanimated react-native-gesture-handler

Using Expo?

npx expo install react-native-reanimated react-native-gesture-handler

React Native Gesture Handler needs extra steps to finalize its installation, please follow their installation instructions. Please make sure to wrap your App with GestureHandlerRootView.

React Native Reanimated v2 needs extra steps to finalize its installation, please follow their installation instructions.

Usage

import { createBottomTabNavigator } from '@swanstack/react-navigation-bottom-tabs';

const Tab = createBottomTabNavigator();

const MyTabs = () => {
  return (
    <Tab.Navigator
      initialRouteName="List"
      screenOptions={{
        tabBarMaxVisibleTabs: 5, // add this line
      }}
    >
      <Tab.Screen name="List" component={ListScreen} />
      <Tab.Screen name="Recipe" component={RecipeScreen} />
      <Tab.Screen name="Pantry" component={PantryScreen} />
      <Tab.Screen name="MealPlan" component={MealPlanScreen} />
      <Tab.Screen name="Profile" component={ProfileScreen} />
      <Tab.Screen name="Settings" component={SettingsScreen} />
    </Tab.Navigator>
  );
};

Properties

PropertyDescriptionTypeDefault Value
... (the original properties)... (directly related to @react-navigation/bottom-tabs)--
tabBarMaxVisibleTabsMax visible tabs including "More" tab.number4
tabBarPulseOnPressEnable pulse animation on tab press.booleantrue
tabBarIconSizeSize of the tab bar icons.number28
tabBarMoreAccessibilityLabelAccessibility label for the "More" tab.string-
tabBarMoreTestIDTestID for the "More" tab.string-
tabBarMoreShowLabelShow label for the "More" tab.booleantabBarShowLabel | true
tabBarMoreAllowFontScalingAllow font scaling for the "More" tab label.booleantabBarAllowFontScaling | -
tabBarMoreActiveTintColorActive tint color for the "More" tab.stringtabBarActiveTintColor | Primary color from theme
tabBarMoreInactiveTintColorInactive tint color for the "More" tab.stringtabBarInactiveTintColor | Text color from theme
tabBarMoreActiveBackgroundColorActive background color for the "More" tab.stringtabBarActiveBackgroundColor | 'transparent'
tabBarMoreInactiveBackgroundColorInactive background color for the "More" tab.stringtabBarInactiveBackgroundColor | 'transparent'
tabBarMoreButtonCustom component for the "More" tab button.(props: BottomTabBarButtonProps) => ReactNode-
tabBarMoreItemStyleStyle for the "More" tab item.styletabBarItemStyle | -
tabBarMoreIconCustom icon for the "More" tab.(props: BottomTabBarIconProps) => ReactNode-
tabBarMoreIconStyleStyle for the "More" tab icon.styletabBarIconStyle | -
tabBarMoreIconSizeSize of the "More" tab icon.numbertabBarIconSize | 28
tabBarMoreLabelCustom label for the "More" tab.string'More'
tabBarMoreLabelStyleStyle for the "More" tab label.styletabBarLabelStyle | -
tabBarMoreBadgeBadge for the "More" tab.(props: BottomTabBarBadgeProps) => ReactNode-
tabBarMoreBadgeStyleStyle for the badge on the "More" tab.styletabBarBadgeStyle | -
tabBarMoreBottomSheetBackdropStyleStyle for the backdrop of the Bottom Sheet in "More" tab.style-
tabBarMoreBottomSheetModalStyleStyle for the modal of the Bottom Sheet in "More" tab.style-
tabBarMoreBottomSheetActiveTintColorActive tint color for Bottom Sheet items in "More" tab.stringtabBarActiveTintColor | Primary color from theme
tabBarMoreBottomSheetInactiveTintColorInactive tint color for Bottom Sheet items in "More" tab.stringtabBarInactiveTintColor | Text color from theme
tabBarMoreBottomSheetActiveBackgroundColorActive background color for Bottom Sheet items in "More" tab.stringtabBarActiveBackgroundColor | 'transparent'
tabBarMoreBottomSheetInactiveBackgroundColorInactive background color for Bottom Sheet items in "More" tab.stringtabBarInactiveBackgroundColor | 'transparent'
tabBarMoreBottomSheetButtonCustom component for Bottom Sheet items in "More" tab.(props: BottomTabBarButtonProps) => ReactNode-
tabBarMoreBottomSheetItemStyleStyle for Bottom Sheet items in "More" tab.styletabBarItemStyle | -
tabBarMoreBottomSheetIconCustom icon for Bottom Sheet item in "More" tab.(props: BottomTabBarIconProps) => ReactNode-
tabBarMoreBottomSheetIconStyleStyle for Bottom Sheet icons in "More" tab.styletabBarIconStyle | -
tabBarMoreBottomSheetIconSizeSize of Bottom Sheet icons in "More" tab.numbertabBarIconSize | 28
tabBarMoreBottomSheetLabelCustom label for Bottom Sheet item in "More" tab.stringtabBarLabel | -
tabBarMoreBottomSheetLabelStyleStyle for Bottom Sheet labels in "More" tab.styletabBarLabelStyle | -
tabBarMoreBottomSheetBadgeStyleStyle for badges on Bottom Sheet items in "More" tab.styletabBarBadgeStyle | -

Not supported original properties from @react-navigation/bottom-tabs

PropertySupported
tabBarHideOnKeyboard❌ (Not applicable)
tabBarVisibilityAnimationConfig❌ (Not applicable)

Full Example

import * as React from 'react';
import { StyleSheet, View } from 'react-native';
import {
  NavigationContainer,
  type CompositeScreenProps,
  type NavigatorScreenParams,
} from '@react-navigation/native';
import {
  createNativeStackNavigator,
  type NativeStackScreenProps,
} from '@react-navigation/native-stack';
import {
  createBottomTabNavigator,
  type BottomTabScreenProps,
} from '@swanstack/react-navigation-bottom-tabs';

/* Your Param List */

type MyTabsParamList = {
  List: undefined;
  Recipe: undefined;
  Pantry: undefined;
  MealPlan: undefined;
  Profile: undefined;
  Settings: undefined;
};

type RootStackParamList = {
  MyTabs: NavigatorScreenParams<MyTabsParamList>;
};

export type RootStackScreenProps<R extends keyof RootStackParamList> =
  NativeStackScreenProps<RootStackParamList, R>;

/* MyTabs is a nested navigator within the RootStack navigator */

export type MyTabsScreenProps<R extends keyof MyTabsParamList> =
  CompositeScreenProps<
    BottomTabScreenProps<MyTabsParamList, R>,
    NativeStackScreenProps<RootStackParamList>
  >;

/* Your Screens */

const ListScreen = (props: MyTabsScreenProps<'List'>) => {
  return <View />;
};

const RecipeScreen = (props: MyTabsScreenProps<'Recipe'>) => {
  return <View />;
};

const PantryScreen = (props: MyTabsScreenProps<'Pantry'>) => {
  return <View />;
};

const MealPlanScreen = (props: MyTabsScreenProps<'MealPlan'>) => {
  return <View />;
};

const ProfileScreen = (props: MyTabsScreenProps<'Profile'>) => {
  return <View />;
};

const SettingsScreen = (props: MyTabsScreenProps<'Settings'>) => {
  return <View />;
};

const Tab = createBottomTabNavigator<MyTabsParamList>();

const MyTabs = () => {
  return (
    <Tab.Navigator
      initialRouteName="List"
      screenOptions={{
        tabBarMaxVisibleTabs: 5,
      }}
    >
      <Tab.Screen name="List" component={ListScreen} />
      <Tab.Screen name="Recipe" component={RecipeScreen} />
      <Tab.Screen name="Pantry" component={PantryScreen} />
      <Tab.Screen name="MealPlan" component={MealPlanScreen} />
      <Tab.Screen name="Profile" component={ProfileScreen} />
      <Tab.Screen name="Settings" component={SettingsScreen} />
    </Tab.Navigator>
  );
};

const RootStack = createNativeStackNavigator<RootStackParamList>();

export default function App() {
  return (
    <View style={styles.container}>
      <NavigationContainer>
        <RootStack.Navigator>
          <RootStack.Screen
            name="MyTabs"
            component={MyTabs}
            options={{ headerShown: false }}
          />
        </RootStack.Navigator>
      </NavigationContainer>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

6.0.1

2 years ago

6.0.0

2 years ago