import { useEffect, useState } from 'react' import { KeyboardEvents } from 'react-native-keyboard-controller' type KeyboardControllerState = { height: number isVisible: boolean } /** * Uses `react-native-keyboard-controller` `KeyboardEvents` rather than the standard RN `Keyboard` API because it fires `keyboardWillShow`/`keyboardWillHide` on both platforms with accurate animated height values; the native `KeyboardProvider` must wrap the app tree or these events will never fire. */ export function useKeyboardController() { const [keyboardState, setKeyboardState] = useState({ isVisible: false, height: 0 }) useEffect(() => { const show = KeyboardEvents.addListener('keyboardWillShow', event => { setKeyboardState({ isVisible: true, height: event?.height }) }) const hide = KeyboardEvents.addListener('keyboardWillHide', event => { setKeyboardState(prev => ({ isVisible: false, height: prev?.height })) }) return () => { show.remove() hide.remove() } }, []) return keyboardState }