Skip to content

Commit

Permalink
Mobile - useKeyboardOffset - Reset timeout when willShowSubscription …
Browse files Browse the repository at this point in the history
…is called
  • Loading branch information
Gerardo committed Apr 4, 2023
1 parent 3ff3fd4 commit 0572495
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,28 @@ export default function useKeyboardOffset(
clearTimeout( timeoutRef.current );
timeoutRef.current = setTimeout( () => {
setKeyboardOffset( 0 );
}, 500 );
}, 200 );
}, [ shouldPreventAutomaticScroll ] );

const onKeyboardDidShow = useCallback( ( { endCoordinates } ) => {
clearTimeout( timeoutRef.current );
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
Expand All @@ -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 ];
}

0 comments on commit 0572495

Please sign in to comment.