Skip to content

feat: measure custom tab bar #355

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

Merged
merged 1 commit into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-adults-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-native-bottom-tabs': patch
---

feat: measure custom tab bar for useBottomTabBarHeight
16 changes: 14 additions & 2 deletions packages/react-native-bottom-tabs/src/TabView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useLayoutEffect, useRef } from 'react';
import type {
OnNativeLayout,
OnPageSelectedEventData,
Expand Down Expand Up @@ -195,6 +195,7 @@
}: Props<Route>) => {
// @ts-ignore
const focusedKey = navigationState.routes[navigationState.index].key;
const customTabBarWrapperRef = useRef<View>(null);
const [tabBarHeight, setTabBarHeight] = React.useState<number | undefined>(0);
const [measuredDimensions, setMeasuredDimensions] = React.useState<
{ width: number; height: number } | undefined
Expand All @@ -220,7 +221,7 @@

if (!loaded.includes(focusedKey)) {
// Set the current tab to be loaded if it was not loaded before
setLoaded((loaded) => [...loaded, focusedKey]);

Check warning on line 224 in packages/react-native-bottom-tabs/src/TabView.tsx

View workflow job for this annotation

GitHub Actions / lint

'loaded' is already declared in the upper scope on line 220 column 10
}

const icons = React.useMemo(
Expand Down Expand Up @@ -313,6 +314,15 @@
[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 (
<BottomTabBarHeightContext.Provider value={tabBarHeight}>
<NativeTabView
Expand Down Expand Up @@ -374,7 +384,9 @@
);
})}
</NativeTabView>
{renderCustomTabBar?.() ?? null}
{renderCustomTabBar ? (
<View ref={customTabBarWrapperRef}>{renderCustomTabBar()}</View>
) : null}
</BottomTabBarHeightContext.Provider>
);
};
Expand Down
Loading