Skip to content

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

fix(assistant): keep the mobile drawer on the visible viewport (#16155)

Nikita Kabardin47f2f2938142
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindowHost.clienttest.tsx b/web/src/features/in-app-agent/components/InAppAgentWindowHost.clienttest.tsx
index c507108291..18055ef292 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindowHost.clienttest.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindowHost.clienttest.tsx
@@ -22,7 +22,9 @@ vi.mock(
"@/src/features/in-app-agent/components/ControlledInAppAgentWindow",
() => ({
ControlledInAppAgentWindow: () => (
- <div data-in-app-agent-window-drag-handle="true" data-testid="window" />
+ <div data-in-app-agent-window-drag-handle="true" data-testid="window">
+ <textarea data-testid="composer" />
+ </div>
),
}),
);
@@ -43,6 +45,43 @@ function firePointerEvent(
fireEvent(element, event);
}
+// The visual viewport is how a phone reports the on-screen keyboard: it shrinks
+// while the layout viewport (`innerHeight`) does not. jsdom implements neither
+// side, so the test owns both.
+function stubVisualViewport(layoutHeight: number) {
+ const viewport = Object.assign(new EventTarget(), {
+ height: layoutHeight,
+ offsetTop: 0,
+ });
+ Object.defineProperty(window, "innerHeight", {
+ configurable: true,
+ writable: true,
+ value: layoutHeight,
+ });
+ Object.defineProperty(window, "visualViewport", {
+ configurable: true,
+ writable: true,
+ value: viewport,
+ });
+
+ return function resizeTo(height: number, offsetTop = 0) {
+ viewport.height = height;
+ viewport.offsetTop = offsetTop;
+ viewport.dispatchEvent(new Event("resize"));
+ };
+}
+
+function stubHandheld() {
+ vi.stubGlobal(
+ "matchMedia",
+ vi.fn((query: string) => ({
+ matches: query.includes("pointer: coarse"),
+ addEventListener: vi.fn(),
+ removeEventListener: vi.fn(),
+ })),
+ );
+}
+
describe("InAppAgentWindowHost", () => {
beforeEach(() => {
mocks.open = false;
@@ -75,6 +114,9 @@ describe("InAppAgentWindowHost", () => {
afterEach(() => {
document.querySelector("[data-overlay-root]")?.remove();
vi.unstubAllGlobals();
+ // jsdom has no visual viewport, so leaving a stubbed one behind would hand
+ // the next test a phone it never asked for.
+ Reflect.deleteProperty(window, "visualViewport");
});
it("keeps geometry while open and resets it on close/reopen", () => {
@@ -136,14 +178,7 @@ describe("InAppAgentWindowHost", () => {
// coarse-pointer clause can match. Pins that the shell asks the handheld
// predicate, not the width-only one that sent a rotated phone back to the
// floating window.
- vi.stubGlobal(
- "matchMedia",
- vi.fn((query: string) => ({
- matches: query.includes("pointer: coarse"),
- addEventListener: vi.fn(),
- removeEventListener: vi.fn(),
- })),
- );
+ stubHandheld();
mocks.open = true;
const { rerender } = render(<InAppAgentWindowHost />);
@@ -162,4 +197,41 @@ describe("InAppAgentWindowHost", () => {
"closed",
);
});
+
+ it("re-anchors the handheld drawer to the visible viewport on every keyboard cycle", () => {
+ stubHandheld();
+ // A phone with a 669px viewport and a 290px keyboard, as reported.
+ const resizeViewportTo = stubVisualViewport(669);
+ mocks.open = true;
+
+ render(<InAppAgentWindowHost />);
+ const drawer = document.querySelector<HTMLElement>("#in-app-agent-drawer");
+ const composer = screen.getByTestId("composer");
+
+ // Repeated cycles are the point: the reported symptom needed "a couple of
+ // times", which is a flag falling out of phase rather than one bad
+ // measurement. iOS also reports the closing keyboard in more than one step
+ // and blurs the field on the way out.
+ for (const closingSteps of [[669], [520, 669], [669]]) {
+ composer.focus();
+ resizeViewportTo(669 - 290);
+ // Above the keyboard, and the anchors — never an inline height — own it.
+ expect(drawer?.style.bottom).toBe("290px");
+ expect(drawer?.style.height).toBe("");
+
+ composer.blur();
+ for (const height of closingSteps) {
+ resizeViewportTo(height);
+ }
+ expect(drawer?.style.bottom).toBe("0px");
+ expect(drawer?.style.height).toBe("");
+ }
+
+ // Safari can also scroll the visual viewport instead of shrinking it; the
+ // drawer follows the visible band rather than the layout viewport.
+ composer.focus();
+ resizeViewportTo(669 - 290, 290);
+ expect(drawer?.style.bottom).toBe("0px");
+ expect(drawer?.style.top).toBe("max(var(--banner-offset), 290px)");
+ });
});
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindowShell.tsx b/web/src/features/in-app-agent/components/InAppAgentWindowShell.tsx
index 4db036ced0..a0b4061552 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindowShell.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindowShell.tsx
@@ -1,6 +1,12 @@
"use client";
-import { useCallback, type ReactNode, type RefObject } from "react";
+import {
+ useCallback,
+ useEffect,
+ useState,
+ type ReactNode,
+ type RefObject,
+} from "react";
import {
MovableResizablePanel,
@@ -60,6 +66,58 @@ export function useInAppAgentWindowShellPanelControl({
});
}
+/**
+ * Holds a top/bottom-anchored fixed element on the *visible* viewport while the
+ * on-screen keyboard is up.
+ *
+ * A phone's keyboard shrinks the visual viewport but not the layout viewport
+ * that `position: fixed` measures from, so `bottom: 0` lands behind the
+ * keyboard. Recomputed from what the viewport reports on every event and never
+ * accumulated: no number of open/close cycles can leave it out of phase, and
+ * anything unexpected clamps back to the plain CSS anchoring. Written straight
+ * onto the element because a keyboard transition streams events and the
+ * assistant is an expensive tree to re-render.
+ */
+function useVisibleViewportAnchoring(
+ element: HTMLElement | null,
+ isEnabled: boolean,
+) {
+ useEffect(() => {
+ // External system: the visual viewport. `resize` fires when the keyboard
+ // opens or closes, `scroll` when Safari slides the visible band instead to
+ // reveal a focused field. Absent on older browsers — CSS then owns it.
+ const viewport = window.visualViewport;
+ if (!isEnabled || !element || !viewport) {
+ return;
+ }
+
+ const anchor = () => {
+ const bandTop = Math.max(0, Math.round(viewport.offsetTop));
+ const hiddenBelowBand = Math.max(
+ 0,
+ Math.round(window.innerHeight - viewport.height - bandTop),
+ );
+
+ element.style.bottom = `${hiddenBelowBand}px`;
+ // `max()` so the banner keeps the top edge whenever it is the lower of
+ // the two; written at all only while the band is scrolled.
+ element.style.top =
+ bandTop > 0 ? `max(var(--banner-offset), ${bandTop}px)` : "";
+ };
+
+ anchor();
+ viewport.addEventListener("resize", anchor);
+ viewport.addEventListener("scroll", anchor);
+
+ return () => {
+ viewport.removeEventListener("resize", anchor);
+ viewport.removeEventListener("scroll", anchor);
+ element.style.bottom = "";
+ element.style.top = "";
+ };
+ }, [element, isEnabled]);
+}
+
type InAppAgentWindowShellProps = {
children: (props: { isHeaderDragHandleEnabled: boolean }) => ReactNode;
floatingPanelHandle: MovableResizablePanelHandle;
@@ -80,6 +138,14 @@ export function InAppAgentWindowShell({
panelRef,
}: InAppAgentWindowShellProps) {
const isHandheld = useIsHandheld();
+ // State, not a ref: the drawer portals into its layer container, which itself
+ // resolves in an effect, so the node arrives a commit late and has to be the
+ // dependency that arms the anchoring.
+ const [drawerElement, setDrawerElement] = useState<HTMLDivElement | null>(
+ null,
+ );
+
+ useVisibleViewportAnchoring(drawerElement, isHandheld && open);
// A phone has one presentation: the whole screen below the banner. A real
// (Vaul) modal drawer — the same treatment as the support / migration
@@ -103,8 +169,15 @@ export function InAppAgentWindowShell({
}}
dismissible={false}
forceDirection="bottom"
+ // Vaul's own keyboard dodge is off: it TOGGLES a "keyboard is open" flag
+ // on every large visual-viewport step instead of reading the direction,
+ // so one extra step — iOS reports a closing keyboard in two — inverts it
+ // and the next dismissal is skipped, stranding the drawer at keyboard
+ // height. It also writes an inline `height`, over-constraining this box.
+ repositionInputs={false}
>
<DrawerContent
+ ref={setDrawerElement}
id="in-app-agent-drawer"
size="full"
// `h-auto` at every breakpoint so top/bottom anchoring owns the
@@ -112,8 +185,7 @@ export function InAppAgentWindowShell({
// and the `md:` half survives tailwind-merge — a landscape phone is
// both handheld and wider than `md`, and `top` + `bottom` + `height`
// over-constrains the box, dropping `bottom` and pushing the composer
- // off-screen by the banner offset. Vaul still overrides the height to
- // dodge the on-screen keyboard.
+ // off-screen by the banner offset.
className="top-banner-offset inset-x-0 bottom-0 h-auto rounded-none border-0 md:h-auto"
onEscapeKeyDown={(event) => {
event.preventDefault();