react-native-curved-bottom-bar

repository·master·Indexed 20 days ago

https://github.com/hoaphantn7604/react-native-curved-bottom-bar

A high-performance, customizable curved bottom navigation bar for React Native applications. Built on top of react-native-svg and @react-navigation/bottom-tabs, it features a central action button (circle) with support for both 'UP' and 'DOWN' curve types. The library provides specific implementations for both React Native CLI and Expo environments, allowing developers to control visibility via a ref and customize tab rendering through the tabBar and renderCircle props.

Tokens
6.5K
Snippets
18
Records
24
Agent score
67%

What's inside react-native-curved-bottom-bar

  1. Use CurvedBottomBar in Expo

    master

    To use the library in an Expo project, import CurvedBottomBarExpo from react-native-curved-bottom-bar and wrap your application in a NavigationContainer from @react-navigation/native.

    import React from 'react';
    import { Alert, Animated, StyleSheet, TouchableOpacity, View } from 'react-native';
    import { CurvedBottomBarExpo } from 'react-native-curved-bottom-bar';
    import Ionicons from '@expo/vector-icons/Ionicons';
    import { NavigationContainer } from '@react-navigation/native';
    
    // ... Screen components ...
    
    export default function App() {
      const renderTabBar = ({ routeName, selectedTab, navigate }) => {
        return (
          <TouchableOpacity onPress={() => navigate(routeName)} style={styles.tabbarItem}>
            {/* Icon logic here */}
          </TouchableOpacity>
        );
      };
    
      return (
        <NavigationContainer>
          <CurvedBottomBarExpo.Navigator
            type="DOWN"
            initialRouteName="title1"
            renderCircle={({ selectedTab, navigate }) => (
              <Animated.View style={styles.btnCircleUp}>
                <TouchableOpacity onPress={() => Alert.alert('Click Action')}>
                  <Ionicons name={'apps-sharp'} color="gray" size={25} />
                </TouchableOpacity>
              </Animated.View>
            )}
            tabBar={renderTabBar}
          >
            <CurvedBottomBarExpo.Screen
              name="title1"
              position="LEFT"
              component={() => <Screen1 />}
            />
            <CurvedBottomBarExpo.Screen
              name="title2"
              position="RIGHT"
              component={() => <Screen2 />}
            />
          </CurvedBottomBarExpo.Navigator
        </NavigationContainer>
      );
    }
  2. Use CurvedBottomBar in React Native CLI

    master

    For React Native CLI projects, import CurvedBottomBar from react-native-curved-bottom-bar. Ensure you have installed vector icons (e.g., react-native-vector-icons) as required by your implementation.

    import React from 'react';
    import { Alert, Animated, StyleSheet, TouchableOpacity, View } from 'react-native';
    import { CurvedBottomBar } from 'react-native-curved-bottom-bar';
    import Ionicons from 'react-native-vector-icons/Ionicons';
    import { NavigationContainer } from '@react-navigation/native';
    
    // ... Screen components ...
    
    export default function App() {
      const renderTabBar = ({ routeName, selectedTab, navigate }) => {
        return (
          <TouchableOpacity onPress={() => navigate(routeName)} style={styles.tabbarItem}>
            {/* Icon logic here */}
          </TouchableOpacity>
        );
      };
    
      return (
        <NavigationContainer>
          <CurvedBottomBar.Navigator
            type="UP"
            initialRouteName="title1"
            renderCircle={({ selectedTab, navigate }) => (
              <Animated.View style={styles.btnCircleUp}>
                <TouchableOpacity onPress={() => Alert.alert('Click Action')}>
                  <Ionicons name={'apps-sharp'} color="gray" size={25} />
                </TouchableOpacity>
              </Animated.View>
            )}
            tabBar={renderTabBar}
          >
            <CurvedBottomBar.Screen
              name="title1"
              position="LEFT"
              component={() => <Screen1 />}
            />
            <CurvedBottomBar.Screen
              name="title2"
              position="RIGHT"
              component={() => <Screen2 />}
            />
          </CurvedBottomBar.Navigator
        </NavigationContainer>
      );
    }
  3. Configure CurvedBottomBar.Navigator props

    master

    The CurvedBottomBar.Navigator is the main component used to manage the curved bottom navigation. It accepts several props to control the visual style and behavior of the bar.

    Required Props

    • type: 'DOWN' or 'UP'. Determines if the center tab item has a downward or upward curve.
    • initialRouteName: String. The name of the route to render on first load.
    • tabBar: ({ routeName, selectedTab, navigate }) => JSX.Element. A function that returns the React element to display as the tab bar.
    • renderCircle: ({ routeName, selectedTab, navigate }) => JSX.Element. A function that returns the React element for the center tab item.

    Optional Props

    • circlePosition: 'CENTER', 'LEFT', or 'RIGHT'. Position of the circle button.
    • circleWidth: Number. Width of the center tab item (Min: 50px, Max: 60px).
    • height: Number. Height of the container (Min: 50px, Max: 90px).
    • width: Number. Width of the container.
    • bgColor: String. Background color of the container.
    • style: ViewStyle. Styling for the container view.
    • shadowStyle: ViewStyle. Styling for the shadow view.
    • borderTopLeftRight: Boolean. Applies border radius to the top left and top right of the container.
    • borderColor: String. Border color.
    • borderWidth: Number. Border width.
  4. Configure CurvedBottomBar.Screen props

    master

    Use CurvedBottomBar.Screen to define the individual routes within the navigator.

    Required Props

    • name: String. The unique name of the route.
    • position: 'LEFT', 'RIGHT', or 'CIRCLE'. Sets the position of the tab bar icon relative to the circle button. Use 'CIRCLE' only if you want the circle button to act as a tab view.
    • component: (props) => JSX.Element. The component to render for this screen. Screen params are merged into the destination route.
  5. Configure autolinking for react-native-curved-bottom-bar in a monorepo or local environment

    master

    When using react-native-curved-bottom-bar in a local development environment or a monorepo where the package is not installed via a standard registry, you may need to configure react-native.config.js to point to the correct source directory. This ensures that the React Native CLI can correctly locate the native code for autolinking.

    In the example provided, the dependencies object maps the package name to its root directory using path.join(__dirname, '..').

    const path = require('path');
    const pak = require('../package.json');
    
    module.exports = {
      dependencies: {
        [pak.name]: {
          root: path.join(__dirname, '..'),
        },
      },
    };
  6. Customizing Tab Item Rendering

    master

    By default, the component renders simple text labels for tabs. You can override this behavior by providing a tabBar function prop. This function is called for every tab item (excluding the central circle).

    The tabBar function receives:

    • routeName: The name of the route.
    • selectedTab: The name of the currently focused route.
    • navigate: A function to trigger navigation: (selectTab: string) => void.
    <BottomBarComponent
      renderCircle={({ routeName, selectedTab, navigate }) => (
        <MyCircleButton />
      )}
      tabBar={({ routeName, selectedTab, navigate }) => (
        <TouchableOpacity onPress={() => navigate(routeName)}>
          <Icon name={routeName === selectedTab ? 'home-active' : 'home-inactive'} />
        </TouchableOpacity>
      )}
    >
      {/* Screens */}
    </BottomBarComponent>
  7. Configure the CurvedBottomBar component

    master

    The BottomBarComponent (exported as default) is a custom navigator built on top of @react-navigation/bottom-tabs. It allows you to create a navigation bar with a curved shape and a central action button (circle).

    Key Props

    PropTypeDefaultDescription
    type'DOWN' | 'UP''DOWN'Determines if the curve goes down or up.
    circlePosition'LEFT' | 'CENTER' | 'RIGHT''CENTER'The position of the central action button.
    bgColorstring'gray'Background color of the bar.
    heightnumber65Height of the tab bar.
    circleWidthnumber50Width of the central circle button.
    renderCirclefunctionRequiredA function to render the central button. Receives { routeName, selectedTab, navigate }.
    tabBarfunctionOptionalA function to render individual tab items. Receives { routeName, selectedTab, navigate }.
    renderLeft / renderRightfunctionN/A(Implicitly handled via children position)
    borderTopLeftRightbooleanfalseWhether to apply borders to the top left/right edges.
    borderColorstring'gray'Color of the border.
    borderWidthnumber0Width of the border.
    shadowStyleViewStyleundefinedStyle applied to the CurvedViewComponent for shadows.
    styleViewStyleundefinedStyle applied to the main container.
    initialRouteNamestringundefinedThe name of the initial route.

    Child Configuration

    Children are passed as Tab.Screen equivalents. To control the layout, use the position prop on the children:

    • position='LEFT': Renders to the left of the circle.
    • position='RIGHT': Renders to the right of the circle.
    • position='CIRCLE' or 'CENTER': Defines the route used for the central action button.
    <BottomBarComponent
      type="DOWN"
      circlePosition="CENTER"
      bgColor="white"
      renderCircle={({ routeName, selectedTab, navigate }) => (
        <TouchableOpacity onPress={() => navigate('SpecialRoute')}>
          <View style={{ width: 50, height: 50, borderRadius: 25, backgroundColor: 'blue' }} />
        </TouchableOpacity>
      )}
    >
      <Tab.Screen name="Home" component={HomeScreen} position="LEFT" />
      <Tab.Screen name="Settings" component={SettingsScreen} position="RIGHT" />
      <Tab.Screen name="SpecialRoute" component={SpecialScreen} position="CIRCLE" />
    </BottomBarComponent>
  8. Customize Tab Items with the tabBar prop

    master

    If you want full control over how individual tab buttons are rendered, use the tabBar prop. This function is called for every tab item (excluding the center circle button).

    It receives an object with:

    • routeName: The name of the route.
    • selectedTab: The name of the currently focused route.
    • navigate: A function to trigger navigation to that route.

    If tabBar is not provided, the component defaults to a simple TouchableOpacity containing the route name text.

    <BottomBarComponent
      tabBar={({ routeName, selectedTab, navigate }) => (
        <TouchableOpacity onPress={() => navigate(routeName)}>
          <MyCustomIcon 
            name={routeName} 
            active={selectedTab === routeName} 
          />
        </TouchableOpacity>
      )}
    >
      {/* ... screens ... */}
    </BottomBarComponent>
  9. Configure MenuItem position in CurvedBottomBar.Screen

    master

    When defining a screen within the CurvedBottomBar.Navigator, you can specify the position property to control where the menu item appears relative to the curved bar.

    Available position values are:

    • 'LEFT': Positioned on the left side.
    • 'RIGHT': Positioned on the right side.
    • 'CIRCLE': Positioned in the center, typically used for the prominent floating action button that interacts with the curve.
    • 'CENTER': Positioned in the center of the bar.
    // Example usage of the position prop in a screen configuration
    <CurvedBottomBar.Screen 
      position="CIRCLE" 
      component={MyComponent} 
      options={{...}} 
    />