Navbar Logic - abhiram-shaji/Langroove GitHub Wiki

Developer Documentation: BottomNavBar Component

This documentation explains the BottomNavBar component's functionality, focusing on its dynamic animation and navigation behavior within a React Native application.


Overview

The BottomNavBar component:

  1. Provides a customizable bottom navigation bar for an app using React Navigation.
  2. Dynamically animates the position of the navigation bar and an active indicator.
  3. Handles navigation between screens (Feed, ChatListScreen, and Settings) based on the active state.

Features and Animation

1. Dynamic Navigation Bar Position

Purpose

  • To adjust the position of the navigation bar based on the currently active screen.

Implementation

  1. Animation Logic:

    • Uses React Native's Animated API to smoothly transition the navigation bar's left position.
    • Adjusts position dynamically when the active screen changes, particularly for the Feed screen.
  2. Code Snippet:

    const animatedLeft = useRef(
      new Animated.Value((Dimensions.get('window').width - Dimensions.get('window').width * 0.7) / 2)
    ).current;
    
    useEffect(() => {
      const isFeedScreen = state.routes[currentScreenIndex].name === 'Feed';
      Animated.timing(animatedLeft, {
        toValue: isFeedScreen ? 20 : (screenWidth - navBarWidth) / 2,
        duration: 300,
        useNativeDriver: false,
      }).start();
    }, [currentScreenIndex]);
  3. Process:

    • The animatedLeft value is calculated based on the screen width and the navigation bar width.
    • The position transitions are triggered whenever the currentScreenIndex changes.

2. Active Indicator Animation

Purpose

  • To highlight the currently active screen by animating a background indicator under the active icon.

Implementation

  1. Animation Logic:

    • Uses React Native's Animated API to animate the translateX property of the active indicator.
    • Ensures smooth transitions between icons when navigating screens.
  2. Code Snippet:

    const animatedPosition = useRef(new Animated.Value(0)).current;
    
    useEffect(() => {
      Animated.timing(animatedPosition, {
        toValue: iconWidth * currentScreenIndex,
        duration: 300,
        useNativeDriver: true,
      }).start(() => {
        setActiveScreen(state.routes[currentScreenIndex].name);
      });
    }, [currentScreenIndex]);
  3. Process:

    • The animatedPosition value is updated based on the currentScreenIndex.
    • The animation ensures the active indicator smoothly aligns with the selected icon.

3. Screen Navigation

Purpose

  • To enable navigation between different screens in the app using the bottom navigation bar.

Implementation

  1. Functionality:

    • Each navigation item uses React Navigation's navigate method to switch screens.
    • Icons and labels change color dynamically based on the active screen.
  2. Code Snippet:

    <TouchableOpacity
      style={bottomNavBarStyles.navItem}
      onPress={() => navigation.navigate('Feed')}
    >
      <Ionicons name="home-outline" size={24} color={getIconColor('Feed')} />
      <Text style={[bottomNavBarStyles.label, getLabelColor('Feed')]}>Feed</Text>
    </TouchableOpacity>
  3. Process:

    • Each TouchableOpacity triggers the navigate method for its respective screen.
    • The getIconColor and getLabelColor functions determine the active screen's styles.

Key Properties and Methods

Property/Method Description
animatedLeft Controls the navigation bar's horizontal position for dynamic alignment.
animatedPosition Controls the position of the active indicator under the icons.
getIconColor(screenName) Returns the color of the icon based on whether the screen is active or inactive.
getLabelColor(screenName) Returns the label's text color based on whether the screen is active or inactive.
navigation.navigate() Handles navigation between screens.

Error Handling

  • Missing Screen Navigation:

    • Ensure all screens referenced in navigation.navigate exist in the navigation stack.
  • Animation Jitter:

    • Verify that Dimensions and state.routes.length are correctly calculated to avoid unexpected animation behavior.

Notes

Styling

  • Styles for the navigation bar and its elements are defined in the bottomNavBarStyles object imported from BottomNavBarStyles.

Flexibility

  • The navigation bar dynamically adjusts based on the number of screens in the navigation state (state.routes.length).

For further customization or troubleshooting, consult the React Navigation Documentation.

⚠️ **GitHub.com Fallback** ⚠️