Skip to content

Commit

Permalink
Reliably focus keyboard on Android (bluesky-social#4427)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Jun 8, 2024
1 parent 5f63b8d commit a2d1cf6
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/view/com/composer/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down

0 comments on commit a2d1cf6

Please sign in to comment.