about summary refs log tree commit diff
path: root/src/lib/hooks/useIsKeyboardVisible.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/hooks/useIsKeyboardVisible.ts')
-rw-r--r--src/lib/hooks/useIsKeyboardVisible.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/lib/hooks/useIsKeyboardVisible.ts b/src/lib/hooks/useIsKeyboardVisible.ts
new file mode 100644
index 000000000..5b2a86eb0
--- /dev/null
+++ b/src/lib/hooks/useIsKeyboardVisible.ts
@@ -0,0 +1,35 @@
+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 suppose 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]
+}