From 42c43ff7617112afd223ecb323be622666c79096 Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Thu, 12 Aug 2021 01:06:31 +0200 Subject: [PATCH] fix: fix headerTransparent not working outside stack navigator --- example/src/Screens/BottomTabs.tsx | 39 +++++++++++++++---- .../bottom-tabs/src/views/BottomTabBar.tsx | 14 ++++--- .../bottom-tabs/src/views/BottomTabView.tsx | 1 + packages/drawer/src/views/DrawerView.tsx | 1 + packages/elements/src/Screen.tsx | 9 +++++ .../src/views/NativeStackView.tsx | 1 + 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/example/src/Screens/BottomTabs.tsx b/example/src/Screens/BottomTabs.tsx index 991a4628..598094ef 100644 --- a/example/src/Screens/BottomTabs.tsx +++ b/example/src/Screens/BottomTabs.tsx @@ -2,16 +2,17 @@ import { createBottomTabNavigator, useBottomTabBarHeight, } from '@react-navigation/bottom-tabs'; -import { HeaderBackButton } from '@react-navigation/elements'; +import { HeaderBackButton, useHeaderHeight } from '@react-navigation/elements'; import { getFocusedRouteNameFromRoute, NavigatorScreenParams, ParamListBase, + useIsFocused, } from '@react-navigation/native'; import type { StackScreenProps } from '@react-navigation/stack'; import { BlurView } from 'expo-blur'; import * as React from 'react'; -import { ScrollView, StyleSheet } from 'react-native'; +import { ScrollView, StatusBar, StyleSheet } from 'react-native'; import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons'; import Albums from '../Shared/Albums'; @@ -32,12 +33,22 @@ type BottomTabParams = { }; const AlbumsScreen = () => { + const headerHeight = useHeaderHeight(); const tabBarHeight = useBottomTabBarHeight(); + const isFocused = useIsFocused(); return ( - - - + <> + {isFocused && } + + + + ); }; @@ -94,11 +105,25 @@ export default function BottomTabsScreen({ component={AlbumsScreen} options={{ title: 'Albums', + headerTintColor: '#fff', + headerTransparent: true, + headerBackground: () => ( + + ), tabBarIcon: getTabBarIcon('image-album'), - tabBarStyle: { position: 'absolute' }, + tabBarInactiveTintColor: 'rgba(255, 255, 255, 0.5)', + tabBarActiveTintColor: '#fff', + tabBarStyle: { + position: 'absolute', + borderTopColor: 'rgba(0, 0, 0, .2)', + }, tabBarBackground: () => ( diff --git a/packages/bottom-tabs/src/views/BottomTabBar.tsx b/packages/bottom-tabs/src/views/BottomTabBar.tsx index a0644253..57c8a694 100644 --- a/packages/bottom-tabs/src/views/BottomTabBar.tsx +++ b/packages/bottom-tabs/src/views/BottomTabBar.tsx @@ -145,6 +145,10 @@ export default function BottomTabBar({ tabBarVisibilityAnimationConfig, tabBarStyle, tabBarBackground, + tabBarActiveTintColor, + tabBarInactiveTintColor, + tabBarActiveBackgroundColor, + tabBarInactiveBackgroundColor, } = focusedOptions; const dimensions = useSafeAreaFrame(); @@ -342,12 +346,10 @@ export default function BottomTabBar({ to={buildLink(route.name, route.params)} testID={options.tabBarTestID} allowFontScaling={options.tabBarAllowFontScaling} - activeTintColor={options.tabBarActiveTintColor} - inactiveTintColor={options.tabBarInactiveTintColor} - activeBackgroundColor={options.tabBarActiveBackgroundColor} - inactiveBackgroundColor={ - options.tabBarInactiveBackgroundColor - } + activeTintColor={tabBarActiveTintColor} + inactiveTintColor={tabBarInactiveTintColor} + activeBackgroundColor={tabBarActiveBackgroundColor} + inactiveBackgroundColor={tabBarInactiveBackgroundColor} button={options.tabBarButton} icon={ options.tabBarIcon ?? diff --git a/packages/bottom-tabs/src/views/BottomTabView.tsx b/packages/bottom-tabs/src/views/BottomTabView.tsx index b2213d75..63a08956 100644 --- a/packages/bottom-tabs/src/views/BottomTabView.tsx +++ b/packages/bottom-tabs/src/views/BottomTabView.tsx @@ -131,6 +131,7 @@ export default function BottomTabView(props: Props) { route={descriptor.route} navigation={descriptor.navigation} headerShown={descriptor.options.headerShown} + headerTransparent={descriptor.options.headerTransparent} headerStatusBarHeight={ descriptor.options.headerStatusBarHeight } diff --git a/packages/drawer/src/views/DrawerView.tsx b/packages/drawer/src/views/DrawerView.tsx index 85679267..44e69511 100644 --- a/packages/drawer/src/views/DrawerView.tsx +++ b/packages/drawer/src/views/DrawerView.tsx @@ -234,6 +234,7 @@ function DrawerViewBase({ route={descriptor.route} navigation={descriptor.navigation} headerShown={descriptor.options.headerShown} + headerTransparent={descriptor.options.headerTransparent} headerStatusBarHeight={descriptor.options.headerStatusBarHeight} header={header({ layout: dimensions, diff --git a/packages/elements/src/Screen.tsx b/packages/elements/src/Screen.tsx index f08e329e..6ea25974 100644 --- a/packages/elements/src/Screen.tsx +++ b/packages/elements/src/Screen.tsx @@ -25,6 +25,7 @@ type Props = { header: React.ReactNode; headerShown?: boolean; headerStatusBarHeight?: number; + headerTransparent?: boolean; style?: StyleProp; children: React.ReactNode; }; @@ -41,6 +42,7 @@ export default function Screen(props: Props) { modal = false, header, headerShown = true, + headerTransparent, headerStatusBarHeight = isParentHeaderShown ? 0 : insets.top, navigation, route, @@ -78,6 +80,7 @@ export default function Screen(props: Props) { setHeaderHeight(height); }} + style={headerTransparent ? styles.absolute : null} > {header} @@ -97,4 +100,10 @@ const styles = StyleSheet.create({ content: { flex: 1, }, + absolute: { + position: 'absolute', + top: 0, + left: 0, + right: 0, + }, }); diff --git a/packages/native-stack/src/views/NativeStackView.tsx b/packages/native-stack/src/views/NativeStackView.tsx index 149adeb6..a0d26fcb 100644 --- a/packages/native-stack/src/views/NativeStackView.tsx +++ b/packages/native-stack/src/views/NativeStackView.tsx @@ -60,6 +60,7 @@ export default function NativeStackView({ state, descriptors }: Props) { route={route} navigation={navigation} headerShown={headerShown} + headerTransparent={headerTransparent} header={ header !== undefined ? ( header({