Navbar Logic - abhiram-shaji/Langroove GitHub Wiki
This documentation explains the BottomNavBar
component's functionality, focusing on its dynamic animation and navigation behavior within a React Native application.
The BottomNavBar
component:
- Provides a customizable bottom navigation bar for an app using React Navigation.
- Dynamically animates the position of the navigation bar and an active indicator.
- Handles navigation between screens (
Feed
,ChatListScreen
, andSettings
) based on the active state.
- To adjust the position of the navigation bar based on the currently active screen.
-
Animation Logic:
- Uses React Native's
Animated
API to smoothly transition the navigation bar'sleft
position. - Adjusts position dynamically when the active screen changes, particularly for the
Feed
screen.
- Uses React Native's
-
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]);
-
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.
- The
- To highlight the currently active screen by animating a background indicator under the active icon.
-
Animation Logic:
- Uses React Native's
Animated
API to animate thetranslateX
property of the active indicator. - Ensures smooth transitions between icons when navigating screens.
- Uses React Native's
-
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]);
-
Process:
- The
animatedPosition
value is updated based on thecurrentScreenIndex
. - The animation ensures the active indicator smoothly aligns with the selected icon.
- The
- To enable navigation between different screens in the app using the bottom navigation bar.
-
Functionality:
- Each navigation item uses React Navigation's
navigate
method to switch screens. - Icons and labels change color dynamically based on the active screen.
- Each navigation item uses React Navigation's
-
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>
-
Process:
- Each
TouchableOpacity
triggers thenavigate
method for its respective screen. - The
getIconColor
andgetLabelColor
functions determine the active screen's styles.
- Each
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. |
-
Missing Screen Navigation:
- Ensure all screens referenced in
navigation.navigate
exist in the navigation stack.
- Ensure all screens referenced in
-
Animation Jitter:
- Verify that
Dimensions
andstate.routes.length
are correctly calculated to avoid unexpected animation behavior.
- Verify that
- Styles for the navigation bar and its elements are defined in the
bottomNavBarStyles
object imported fromBottomNavBarStyles
.
- 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.