diff --git a/.changeset/dry-adults-lie.md b/.changeset/dry-adults-lie.md new file mode 100644 index 00000000..8960c50b --- /dev/null +++ b/.changeset/dry-adults-lie.md @@ -0,0 +1,5 @@ +--- +'react-native-bottom-tabs': patch +--- + +feat: measure custom tab bar for useBottomTabBarHeight diff --git a/packages/react-native-bottom-tabs/src/TabView.tsx b/packages/react-native-bottom-tabs/src/TabView.tsx index 9332c7d7..3e490f67 100644 --- a/packages/react-native-bottom-tabs/src/TabView.tsx +++ b/packages/react-native-bottom-tabs/src/TabView.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useLayoutEffect, useRef } from 'react'; import type { OnNativeLayout, OnPageSelectedEventData, @@ -195,6 +195,7 @@ const TabView = ({ }: Props) => { // @ts-ignore const focusedKey = navigationState.routes[navigationState.index].key; + const customTabBarWrapperRef = useRef(null); const [tabBarHeight, setTabBarHeight] = React.useState(0); const [measuredDimensions, setMeasuredDimensions] = React.useState< { width: number; height: number } | undefined @@ -313,6 +314,15 @@ const TabView = ({ [setMeasuredDimensions] ); + useLayoutEffect(() => { + // If we are rendering a custom tab bar, we need to measure it to set the tab bar height. + if (renderCustomTabBar && customTabBarWrapperRef.current) { + customTabBarWrapperRef.current.measure((_x, _y, _width, height) => { + setTabBarHeight(height); + }); + } + }, [renderCustomTabBar]); + return ( ({ ); })} - {renderCustomTabBar?.() ?? null} + {renderCustomTabBar ? ( + {renderCustomTabBar()} + ) : null} ); };