Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new method getBottomTabHeight added to BottomNavigationBar #4408

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Santhosh-Umapathi
Copy link

Motivation

Our app requires a feature "Scroll to show/hide BottomTabs" same as LinkedIn Bottom Tabs feature. Implementing this feature was not a problem, however to achieve this animation, we need the Height of the BottomTabs.

This height is retrieved from useBottomTabBarHeight();. But it does not calculate the height correctly on Android when we use Custom tabBar with createBottomTabNavigator from '@react-navigation/bottom-tabs'

Goal

To get the correct height of the rendered Bottom tabs when its using react-native-paper

Related issue

Currently, the BottomNavigationBar does not provide a way to get the height of the rendered BottomNavigationBar.

When using the useBottomTabBarHeight() it gives the height of the default bottom tabs from @react-navigation/bottom-tabs

Test plan

Please note that the zustand global store was used to store this bottom tab height, but can easily replicate with local state as well.

Example usage

const AndroidCustomTabBarWrapper = (props: BottomTabBarProps) => (
  <AndroidCustomTabBar {...props} />
);

const AndroidCustomTabBar = ({
  navigation,
  state,
  descriptors,
  insets,
}: BottomTabBarProps) => {
  const {actions} = useStore();

  const descriptor = descriptors[state.routes[state.index].key];

  // Get the options from the descriptor
  const {tabBarStyle} = descriptor.options;

  return (
    <Animated.View style={[tabBarStyle as AnimatedStyle<ViewStyle>]}>
      <BottomNavigation.Bar
        activeIndicatorStyle={styles.activeIndicatorStyle}
        inactiveColor={colors.grey[800]}
        style={styles.barStyle}
        navigationState={state}
        safeAreaInsets={insets}
        getBottomTabHeight={actions.setBottomTabHeight} // Patch - Set the height of the bottom tab bar
        onTabPress={({route, preventDefault}) => {
          const event = navigation.emit({
            type: 'tabPress',
            target: route.key,
            canPreventDefault: true,
          });

          if (event.defaultPrevented) {
            preventDefault();
          } else {
            navigation.dispatch({
              ...CommonActions.navigate(route.name, route.params),
              target: state.key,
            });
          }
        }}
        renderIcon={({route, focused, color}) => {
          const {options} = descriptors[route.key];
          if (options.tabBarIcon) {
            return options.tabBarIcon({focused, color, size: spacings[24]});
          }

          return null;
        }}
        getLabelText={({route}) => {
          const {options} = descriptors[route.key];
          const label =
            options.tabBarLabel !== undefined
              ? options.tabBarLabel
              : options.title !== undefined
                ? options.title
                : undefined;

          return typeof label === 'string' ? label : undefined;
        }}
      />
    </Animated.View>
  );
};

export const BottomTabs = () => {
  return (
    <Tab.Navigator tabBar={AndroidCustomTabBarWrapper}>
      <Tab.Screen
        name={'Home'}
        component={()=><></>}
      />

    </Tab.Navigator>
  );
};

@callstack-bot
Copy link

Hey @Santhosh-Umapathi, thank you for your pull request 🤗. The documentation from this branch can be viewed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants