Files
bsky-social-app/src/lib/hooks/useIsKeyboardVisible.ts
T
dan a22685c345 Sort imports (#6009)
* Mark import sort/order/style rules as error

* npm run lint -- --fix
2024-10-29 23:02:48 +00:00

37 lines
937 B
TypeScript

import {useEffect, useState} from 'react'
import {Keyboard} from 'react-native'
import {isIOS} from '#/platform/detection'
export function useIsKeyboardVisible({
iosUseWillEvents,
}: {
iosUseWillEvents?: boolean
} = {}) {
const [isKeyboardVisible, setKeyboardVisible] = useState(false)
// NOTE
// only iOS supports the "will" events
// -prf
const showEvent =
isIOS && iosUseWillEvents ? 'keyboardWillShow' : 'keyboardDidShow'
const hideEvent =
isIOS && iosUseWillEvents ? 'keyboardWillHide' : 'keyboardDidHide'
useEffect(() => {
const keyboardShowListener = Keyboard.addListener(showEvent, () =>
setKeyboardVisible(true),
)
const keyboardHideListener = Keyboard.addListener(hideEvent, () =>
setKeyboardVisible(false),
)
return () => {
keyboardHideListener.remove()
keyboardShowListener.remove()
}
}, [showEvent, hideEvent])
return [isKeyboardVisible]
}