import { useEffect, useState } from 'react'; import { Keyboard, Platform } from 'react-native'; /** Live keyboard height in px, 0 when hidden. */ export function useKeyboardHeight(): number { const [height, setHeight] = useState(0); useEffect(() => { // "will" fires before the animation on iOS, so the cap resizes in step with the // keyboard instead of jumping after; Android only reliably fires "did". const showEvent = Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow'; const hideEvent = Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide'; const showSub = Keyboard.addListener(showEvent, (event) => setHeight(event.endCoordinates.height)); const hideSub = Keyboard.addListener(hideEvent, () => setHeight(0)); return () => { showSub.remove(); hideSub.remove(); }; }, []); return height; }