bsky-app/src/lib/hooks/useIsKeyboardVisible.ts
Paul Frazee ebad6d2b1a
ListAddUser modal UX improvements (#1809)
* typo

* Add loading state to ListAddUser

* Improve UI/UX of ListAddUser
2023-11-03 16:44:00 -07:00

35 lines
934 B
TypeScript

import {useState, useEffect} 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]
}