@@ -3,6 +3,7 @@ import React, {useEffect, useRef} from 'react';
33import type { LayoutChangeEvent } from 'react-native' ;
44import { useReanimatedKeyboardAnimation } from 'react-native-keyboard-controller' ;
55import Reanimated , { Easing , useAnimatedReaction , useAnimatedStyle , useSharedValue , withTiming } from 'react-native-reanimated' ;
6+ import usePrevious from '@hooks/usePrevious' ;
67import useWindowDimensions from '@hooks/useWindowDimensions' ;
78import isInLandscapeModeUtil from '@libs/isInLandscapeMode' ;
89import type { CollapsibleHeaderOnKeyboardProps } from './types' ;
@@ -29,6 +30,7 @@ function isKeyboardOpeningAtGivenProgress(keyboardProgress: number, prevKeyboard
2930 */
3031function CollapsibleHeaderOnKeyboard ( { children, collapsibleHeaderOffset = 0 } : CollapsibleHeaderOnKeyboardProps ) {
3132 const isFocused = useIsFocused ( ) ;
33+ const prevIsFocused = usePrevious ( isFocused ) ;
3234 // JS ref guards against re-measurement when the Reanimated.View fires onLayout with height=0
3335 const naturalHeightRef = useRef ( - 1 ) ;
3436 // Worklet-accessible mirror of naturalHeightRef. -1 signals "not yet measured".
@@ -81,6 +83,20 @@ function CollapsibleHeaderOnKeyboard({children, collapsibleHeaderOffset = 0}: Co
8183 }
8284 } , [ isInLandscapeMode , isFocused , animatedHeight ] ) ;
8385
86+ // Restores the header when the screen loses focus
87+ useEffect ( ( ) => {
88+ if ( ! prevIsFocused || isFocused ) {
89+ return ;
90+ }
91+
92+ const naturalHeightValue = naturalHeightRef . current ;
93+ if ( naturalHeightValue === - 1 ) {
94+ return ;
95+ }
96+ animatedHeight . set ( withTiming ( naturalHeightValue , { duration : RESTORE_DURATION } ) ) ;
97+ // eslint-disable-next-line react-hooks/exhaustive-deps -- we only want to run this effect when the screen loses focus
98+ } , [ isFocused ] ) ;
99+
84100 // Runs on the UI thread whenever keyboard state changes.
85101 // Fires at two key moments:
86102 // 1. When keyboard just starts opening: on iOS keyboardHeight is already at its final value
0 commit comments