From 0572495ab6ca627f1537c2c0b817ac5915243145 Mon Sep 17 00:00:00 2001 From: Gerardo Date: Tue, 4 Apr 2023 12:59:37 +0200 Subject: [PATCH] Mobile - useKeyboardOffset - Reset timeout when willShowSubscription is called --- .../use-keyboard-offset.native.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/components/src/mobile/keyboard-aware-flat-list/use-keyboard-offset.native.js b/packages/components/src/mobile/keyboard-aware-flat-list/use-keyboard-offset.native.js index f3a5c386d59048..f12b254dd9469b 100644 --- a/packages/components/src/mobile/keyboard-aware-flat-list/use-keyboard-offset.native.js +++ b/packages/components/src/mobile/keyboard-aware-flat-list/use-keyboard-offset.native.js @@ -35,7 +35,7 @@ export default function useKeyboardOffset( clearTimeout( timeoutRef.current ); timeoutRef.current = setTimeout( () => { setKeyboardOffset( 0 ); - }, 500 ); + }, 200 ); }, [ shouldPreventAutomaticScroll ] ); const onKeyboardDidShow = useCallback( ( { endCoordinates } ) => { @@ -43,11 +43,20 @@ export default function useKeyboardOffset( setKeyboardOffset( endCoordinates.height ); }, [] ); + const onKeyboardWillShow = useCallback( () => { + clearTimeout( timeoutRef.current ); + }, [] ); + useEffect( () => { + let willShowSubscription; let showSubscription; let hideSubscription; if ( scrollEnabled ) { + willShowSubscription = Keyboard.addListener( + 'keyboardWillShow', + onKeyboardWillShow + ); showSubscription = Keyboard.addListener( 'keyboardDidShow', onKeyboardDidShow @@ -57,15 +66,22 @@ export default function useKeyboardOffset( onKeyboardDidHide ); } else { + willShowSubscription?.remove(); showSubscription?.remove(); hideSubscription?.remove(); } return () => { clearTimeout( timeoutRef.current ); + willShowSubscription?.remove(); showSubscription?.remove(); hideSubscription?.remove(); }; - }, [ scrollEnabled, onKeyboardDidShow, onKeyboardDidHide ] ); + }, [ + onKeyboardDidHide, + onKeyboardDidShow, + onKeyboardWillShow, + scrollEnabled, + ] ); return [ keyboardOffset ]; }