about summary refs log tree commit diff
path: root/src/lib/hooks/useIsKeyboardVisible.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-07-06 20:28:10 -0500
committerGitHub <noreply@github.com>2023-07-06 20:28:10 -0500
commite14c9783e0cea73ada1d20e8a798738c39319315 (patch)
tree41be4e050c1e7cf4ade0e0ff1c66342599618935 /src/lib/hooks/useIsKeyboardVisible.ts
parentf05c2f06d665cb3a9989154fbc82a2b0ea60669a (diff)
downloadvoidsky-e14c9783e0cea73ada1d20e8a798738c39319315.tar.zst
[APP-735] Post language improvements (#982)
* Fix composer character-counter bouncing around UI elements

* Fix composer toolbar padding when keyboard is dismissed on iOS

* Use the full name of the language in the composer footer

* Add headings to the DropdownButton

* Update the composer language control to use a simpler dropdown

* Fix lint

* Add translate link to Post component used in notifications

* Fix lint
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]
+}