Reliably focus keyboard on Android (#4427)

zio/stable
dan 2024-06-08 06:27:16 +01:00 committed by GitHub
parent 5f63b8d40f
commit a2d1cf68b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 25 additions and 7 deletions

View File

@ -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])