Skip to content

Commit d6f05c5

Browse files
committed
feat: measure custom tab bar
1 parent 2df3658 commit d6f05c5

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

.changeset/dry-adults-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-native-bottom-tabs': patch
3+
---
4+
5+
feat: measure custom tab bar for useBottomTabBarHeight

packages/react-native-bottom-tabs/src/TabView.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useLayoutEffect, useRef } from 'react';
22
import type {
33
OnNativeLayout,
44
OnPageSelectedEventData,
@@ -195,6 +195,7 @@ const TabView = <Route extends BaseRoute>({
195195
}: Props<Route>) => {
196196
// @ts-ignore
197197
const focusedKey = navigationState.routes[navigationState.index].key;
198+
const customTabBarWrapperRef = useRef<View>(null);
198199
const [tabBarHeight, setTabBarHeight] = React.useState<number | undefined>(0);
199200
const [measuredDimensions, setMeasuredDimensions] = React.useState<
200201
{ width: number; height: number } | undefined
@@ -313,6 +314,15 @@ const TabView = <Route extends BaseRoute>({
313314
[setMeasuredDimensions]
314315
);
315316

317+
useLayoutEffect(() => {
318+
// If we are rendering a custom tab bar, we need to measure it to set the tab bar height.
319+
if (renderCustomTabBar && customTabBarWrapperRef.current) {
320+
customTabBarWrapperRef.current.measure((_x, _y, _width, height) => {
321+
setTabBarHeight(height);
322+
});
323+
}
324+
}, [renderCustomTabBar]);
325+
316326
return (
317327
<BottomTabBarHeightContext.Provider value={tabBarHeight}>
318328
<NativeTabView
@@ -374,7 +384,9 @@ const TabView = <Route extends BaseRoute>({
374384
);
375385
})}
376386
</NativeTabView>
377-
{renderCustomTabBar?.() ?? null}
387+
{renderCustomTabBar ? (
388+
<View ref={customTabBarWrapperRef}>{renderCustomTabBar()}</View>
389+
) : null}
378390
</BottomTabBarHeightContext.Provider>
379391
);
380392
};

0 commit comments

Comments
 (0)