about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authordan <dan.abramov@gmail.com>2024-06-08 06:27:16 +0100
committerGitHub <noreply@github.com>2024-06-07 22:27:16 -0700
commita2d1cf68b9cf6b2d935439e522c838359a94a9a4 (patch)
tree4423b04d9b78055fff846b12703bb0348a044225 /src
parent5f63b8d40f7ede8bbaaf1d1f68200126896f061b (diff)
downloadvoidsky-a2d1cf68b9cf6b2d935439e522c838359a94a9a4.tar.zst
Reliably focus keyboard on Android (#4427)
Diffstat (limited to 'src')
-rw-r--r--src/view/com/composer/Composer.tsx32
1 files changed, 25 insertions, 7 deletions
diff --git a/src/view/com/composer/Composer.tsx b/src/view/com/composer/Composer.tsx
index 7872ea186..fac08a711 100644
--- a/src/view/com/composer/Composer.tsx
+++ b/src/view/com/composer/Composer.tsx
@@ -405,13 +405,31 @@ export const ComposePost = observer(function ComposePost({
   // Backup focus on android, if the keyboard *still* refuses to show
   useEffect(() => {
     if (!isAndroid) return
-    if (isModalReady) {
-      setTimeout(() => {
-        if (!Keyboard.isVisible()) {
-          textInput.current?.blur()
-          textInput.current?.focus()
-        }
-      }, 300)
+    if (!isModalReady) return
+
+    function tryFocus() {
+      if (!Keyboard.isVisible()) {
+        textInput.current?.blur()
+        textInput.current?.focus()
+      }
+    }
+
+    tryFocus()
+    // Retry with enough gap to avoid interrupting the previous attempt.
+    // Unfortunately we don't know which attempt will succeed.
+    const retryInterval = setInterval(tryFocus, 500)
+
+    function stopTrying() {
+      clearInterval(retryInterval)
+    }
+
+    // Deactivate this fallback as soon as anything happens.
+    const sub1 = Keyboard.addListener('keyboardDidShow', stopTrying)
+    const sub2 = Keyboard.addListener('keyboardDidHide', stopTrying)
+    return () => {
+      clearInterval(retryInterval)
+      sub1.remove()
+      sub2.remove()
     }
   }, [isModalReady])