From 13f8f052f954a20f30cb66132c933778c75e4367 Mon Sep 17 00:00:00 2001 From: tm-ruxandra Date: Wed, 10 Jul 2024 17:17:14 -0400 Subject: [PATCH] fix: pull header height context out to app level so all overlay sheets can access it (#1742) --- dev-client/src/App.tsx | 43 +++++++++++-------- .../src/components/sheets/OverlaySheet.tsx | 3 +- .../src/context/HeaderHeightContext.tsx | 5 ++- .../components/LandPKSInfoModal.tsx | 2 +- dev-client/src/screens/ScreenScaffold.tsx | 21 +++++---- 5 files changed, 43 insertions(+), 31 deletions(-) diff --git a/dev-client/src/App.tsx b/dev-client/src/App.tsx index 3b59c2d4b..b4d11b347 100644 --- a/dev-client/src/App.tsx +++ b/dev-client/src/App.tsx @@ -25,7 +25,7 @@ // react-native-get-random-values needed for uuid - https://github.com/uuidjs/uuid#react-native--expo import 'react-native-get-random-values'; -import {useEffect} from 'react'; +import {useEffect, useState} from 'react'; import {LogBox, PermissionsAndroid} from 'react-native'; import {GestureHandlerRootView} from 'react-native-gesture-handler'; import {Provider} from 'react-redux'; @@ -46,6 +46,7 @@ import * as Sentry from '@sentry/react-native'; import {APP_CONFIG} from 'terraso-mobile-client/config'; import {GeospatialProvider} from 'terraso-mobile-client/context/GeospatialContext'; +import {HeaderHeightContext} from 'terraso-mobile-client/context/HeaderHeightContext'; import {HomeScreenContextProvider} from 'terraso-mobile-client/context/HomeScreenContext'; import {checkAndroidPermissions} from 'terraso-mobile-client/native/checkAndroidPermissions'; import {RootNavigator} from 'terraso-mobile-client/navigation/navigators/RootNavigator'; @@ -80,27 +81,31 @@ function App(): React.JSX.Element { ), ); + const [headerHeight, setHeaderHeight] = useState(0); + return ( - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + ); diff --git a/dev-client/src/components/sheets/OverlaySheet.tsx b/dev-client/src/components/sheets/OverlaySheet.tsx index 66ccf29e8..9fe820e83 100644 --- a/dev-client/src/components/sheets/OverlaySheet.tsx +++ b/dev-client/src/components/sheets/OverlaySheet.tsx @@ -60,7 +60,8 @@ export const OverlaySheet = forwardRef< }, forwardedRef, ) => { - const headerHeight = useHeaderHeight(); + const {headerHeight} = useHeaderHeight(); + const ref = useRef(null); const methods = useMemo( () => ({ diff --git a/dev-client/src/context/HeaderHeightContext.tsx b/dev-client/src/context/HeaderHeightContext.tsx index c08da7092..863aedf41 100644 --- a/dev-client/src/context/HeaderHeightContext.tsx +++ b/dev-client/src/context/HeaderHeightContext.tsx @@ -17,4 +17,7 @@ import {createContext} from 'react'; -export const HeaderHeightContext = createContext(undefined); +export const HeaderHeightContext = createContext<{ + headerHeight: number | undefined; + setHeaderHeight: (height: number) => void; +}>({headerHeight: undefined, setHeaderHeight: () => {}}); diff --git a/dev-client/src/screens/HomeScreen/components/LandPKSInfoModal.tsx b/dev-client/src/screens/HomeScreen/components/LandPKSInfoModal.tsx index d716f877f..e6c32d06f 100644 --- a/dev-client/src/screens/HomeScreen/components/LandPKSInfoModal.tsx +++ b/dev-client/src/screens/HomeScreen/components/LandPKSInfoModal.tsx @@ -31,7 +31,7 @@ type Props = { export const LandPKSInfoModal = forwardRef( ({onClose}, ref) => { - const headerHeight = useHeaderHeight(); + const {headerHeight} = useHeaderHeight(); return ( , }: Props) => { - const [headerHeight, setHeaderHeight] = useState( + const [appBarHeight, setAppBarHeight] = useState( undefined, ); + const onLayout = useCallback( + (e: LayoutChangeEvent) => setAppBarHeight(e.nativeEvent.layout.height), + [setAppBarHeight], + ); const safeAreaTop = useSafeAreaInsets().top; - const onLayout = useCallback( - (e: LayoutChangeEvent) => setHeaderHeight(e.nativeEvent.layout.height), - [setHeaderHeight], + const {setHeaderHeight} = useHeaderHeight(); + useEffect( + () => setHeaderHeight(safeAreaTop + (appBarHeight ?? 0)), + [setHeaderHeight, safeAreaTop, appBarHeight], ); return ( @@ -58,9 +63,7 @@ export const ScreenScaffold = ({ /> {PropsAppBar} - - {children} - + {children} );