Reliably focus keyboard on Android (#4427)
(cherry picked from commit a2d1cf68b9)
This commit is contained in:
@@ -134,8 +134,8 @@ export const ComposePost = observer(function ComposePost({
|
|||||||
// See https://github.com/bluesky-social/social-app/pull/4399
|
// See https://github.com/bluesky-social/social-app/pull/4399
|
||||||
const {setEnabled} = useKeyboardContext()
|
const {setEnabled} = useKeyboardContext()
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
|
if (!isAndroid) return
|
||||||
setEnabled(false)
|
setEnabled(false)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
setEnabled(true)
|
setEnabled(true)
|
||||||
}
|
}
|
||||||
@@ -402,6 +402,37 @@ export const ComposePost = observer(function ComposePost({
|
|||||||
bottomBarAnimatedStyle,
|
bottomBarAnimatedStyle,
|
||||||
} = useAnimatedBorders()
|
} = useAnimatedBorders()
|
||||||
|
|
||||||
|
// Backup focus on android, if the keyboard *still* refuses to show
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isAndroid) return
|
||||||
|
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])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<KeyboardAvoidingView
|
<KeyboardAvoidingView
|
||||||
|
|||||||
Reference in New Issue
Block a user