Skip to content

Commit b987b57

Browse files
juliusmarmingePixPMusicclaude
authored
fix(android): a composer focused through an app switch stays above the keyboard (#20803)
Co-authored-by: Pixel Perfect <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 23a5c77 commit b987b57

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎apps/mobile/src/features/threads/QuestionAttachments.tsx‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { convertPastedImagesToAttachments } from "../../lib/composerImages";
66
import { PROVIDER_SEND_TURN_MAX_ATTACHMENTS, type UserInputQuestion } from "@t3tools/contracts";
77
import { useAtomValue } from "@effect/atom-react";
88
import { View } from "react-native";
9-
import { useEffect, useRef, useState } from "react";
9+
import { useEffect, useEffectEvent, useRef, useState } from "react";
1010
import { ComposerAttachmentButton } from "./ComposerAttachmentButton";
1111
import { ComposerAttachmentStrip } from "../../components/ComposerAttachmentStrip";
1212
import { pickComposerFiles, pickComposerMedia } from "../../lib/composerImages";
@@ -49,6 +49,14 @@ export function QuestionAttachments(props: {
4949
props.requestId,
5050
props.question.id,
5151
]);
52+
// A focused input that unmounts never delivers its onBlur, so release focus here or
53+
// the screen would keep treating the keyboard as owned by a removed input.
54+
const focusedRef = useRef(false);
55+
const onInputFocusChange = props.onInputFocusChange;
56+
const releaseFocus = useEffectEvent(() => {
57+
if (focusedRef.current) onInputFocusChange?.(false);
58+
});
59+
useEffect(() => () => releaseFocus(), []);
5260
const pickerScope = useRef<{ key: string; active: boolean } | null>(null);
5361
useEffect(() => {
5462
const scope = { key: scopeKey, active: true };
@@ -197,8 +205,14 @@ export function QuestionAttachments(props: {
197205
submitBehavior={props.question.initialAnswer !== undefined ? "newline" : undefined}
198206
editable={!props.disabled}
199207
onChangeText={props.onChangeText}
200-
onFocus={() => props.onInputFocusChange?.(true)}
201-
onBlur={() => props.onInputFocusChange?.(false)}
208+
onFocus={() => {
209+
focusedRef.current = true;
210+
onInputFocusChange?.(true);
211+
}}
212+
onBlur={() => {
213+
focusedRef.current = false;
214+
onInputFocusChange?.(false);
215+
}}
202216
placeholder="Or type a custom answer"
203217
className="min-h-[54px] rounded-2xl border border-input-border bg-input px-3.5 py-3 font-sans text-base text-foreground"
204218
/>

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,15 +363,19 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
363363
// Android can swallow the IME hide callbacks when the app is backgrounded mid keyboard-hide
364364
// (the reported repro: send — which blurs and starts the hide — then Home within a second). The
365365
// keyboard library's height AND visibility then stay frozen open, so gating the sticky
366-
// translation on visibility alone still strands the composer after resume.
366+
// translation on visibility alone still strands the composer after resume. An input that kept
367+
// focus through the switch is exempt: Android restores its keyboard unchanged, so no keyboard
368+
// or focus event would ever lift the quarantine. Hiding the keyboard with Back keeps focus, so
369+
// Back then Home mid-hide takes the exemption and can still strand the composer.
367370
const [keyboardStateSuspect, setKeyboardStateSuspect] = useState(false);
371+
const ownedInputFocusedRef = useRef(false);
368372
useEffect(() => {
369373
if (Platform.OS !== "android") {
370374
return;
371375
}
372376
const subscription = AppState.addEventListener("change", (state) => {
373377
if (state === "active") {
374-
setKeyboardStateSuspect(true);
378+
setKeyboardStateSuspect(!ownedInputFocusedRef.current);
375379
}
376380
});
377381
return () => {
@@ -382,6 +386,7 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
382386
setKeyboardStateSuspect(false);
383387
}, [isKeyboardVisible, liveKeyboardHeight]);
384388
const handleOwnedInputFocusChange = useCallback((focused: boolean) => {
389+
ownedInputFocusedRef.current = focused;
385390
if (focused) {
386391
setKeyboardStateSuspect(false);
387392
}

0 commit comments

Comments
 (0)