Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(mobile): assistant no-autofocus + useIsMobile correct on first render (LFE-11067) (#15429)

Nikita Kabardin3779a7250cc6
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
index 08d1be7a10..06a8219a7c 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
@@ -34,6 +34,7 @@ import {
TooltipTrigger,
} from "@/src/components/ui/tooltip";
import { cn } from "@/src/utils/tailwind";
+import { useIsMobile } from "@/src/hooks/use-mobile";
import { formatApproximateDuration } from "@/src/utils/dates";
import {
InAppAgentMessage,
@@ -386,6 +387,8 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
screenContextDescription,
);
const capture = usePostHogClientCapture();
+ // No auto-focus on mobile — it springs the keyboard and buries the panel.
+ const isMobile = useIsMobile();
const isRateLimited = isInAppAgentRateLimited(error);
const isInputDisabled = baseIsInputDisabled || isRateLimited;
const viewportRef = useRef<HTMLDivElement>(null);
@@ -474,14 +477,16 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
(input: HTMLTextAreaElement | null) => {
inputRef.current = input;
+ // Skip on mobile: refocusing after a turn re-springs the keyboard, even
+ // for the quick-action flow that never focused the input.
const shouldRefocusInput =
- previousIsInputDisabledRef.current && !isInputDisabled;
+ previousIsInputDisabledRef.current && !isInputDisabled && !isMobile;
if (input && shouldRefocusInput) {
input.focus();
}
},
- [isInputDisabled],
+ [isInputDisabled, isMobile],
);
useEffect(() => {
@@ -914,7 +919,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
}}
>
<textarea
- autoFocus={!isExpanded}
+ autoFocus={!isExpanded && !isMobile}
ref={setInputRef}
value={input}
onChange={(event) => {
diff --git a/web/src/hooks/use-mobile.tsx b/web/src/hooks/use-mobile.tsx
index a333c39740..48a9fd6ddf 100644
--- a/web/src/hooks/use-mobile.tsx
+++ b/web/src/hooks/use-mobile.tsx
@@ -1,22 +1,29 @@
-import * as React from "react";
+import { useSyncExternalStore } from "react";
// tailwind default breakpoint, corresponds to `md`, https://tailwindcss.com/docs/responsive-design
const MOBILE_BREAKPOINT = 768;
+const MOBILE_QUERY = `(max-width: ${MOBILE_BREAKPOINT - 1}px)`;
-export function useIsMobile() {
- const [isMobile, setIsMobile] = React.useState<boolean | undefined>(
- undefined,
- );
+function subscribe(onStoreChange: () => void) {
+ if (typeof window === "undefined" || !window.matchMedia) return () => {};
+ const mql = window.matchMedia(MOBILE_QUERY);
+ mql.addEventListener("change", onStoreChange);
+ return () => mql.removeEventListener("change", onStoreChange);
+}
- React.useEffect(() => {
- const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
- const onChange = () => {
- setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
- };
- mql.addEventListener("change", onChange);
- setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
- return () => mql.removeEventListener("change", onChange);
- }, []);
+// Guarded so it's safe where `matchMedia` is absent (jsdom tests, SSR, old
+// browsers) — treat those as non-mobile.
+function getSnapshot() {
+ if (typeof window === "undefined" || !window.matchMedia) return false;
+ return window.matchMedia(MOBILE_QUERY).matches;
+}
- return !!isMobile;
+/**
+ * True when the viewport is below the `md` breakpoint. Reads the media query
+ * synchronously, so it is correct on the first client render (unlike an
+ * effect-initialized hook) — safe to gate mount-only behavior like `autoFocus`.
+ * SSR has no viewport, so it assumes desktop and hydration corrects it.
+ */
+export function useIsMobile() {
+ return useSyncExternalStore(subscribe, getSnapshot, () => false);
}