mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {useState, useEffect} from 'react'
2import {Keyboard} from 'react-native'
3import {isIOS} from 'platform/detection'
4
5export function useIsKeyboardVisible({
6 iosUseWillEvents,
7}: {
8 iosUseWillEvents?: boolean
9} = {}) {
10 const [isKeyboardVisible, setKeyboardVisible] = useState(false)
11
12 // NOTE
13 // only iOS supports the "will" events
14 // -prf
15 const showEvent =
16 isIOS && iosUseWillEvents ? 'keyboardWillShow' : 'keyboardDidShow'
17 const hideEvent =
18 isIOS && iosUseWillEvents ? 'keyboardWillHide' : 'keyboardDidHide'
19
20 useEffect(() => {
21 const keyboardShowListener = Keyboard.addListener(showEvent, () =>
22 setKeyboardVisible(true),
23 )
24 const keyboardHideListener = Keyboard.addListener(hideEvent, () =>
25 setKeyboardVisible(false),
26 )
27
28 return () => {
29 keyboardHideListener.remove()
30 keyboardShowListener.remove()
31 }
32 }, [showEvent, hideEvent])
33
34 return [isKeyboardVisible]
35}