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