From 4e77584eb7b1c16b027ba992b7b24c83aee673ef Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Sun, 4 Oct 2026 00:09:49 +0800 Subject: [PATCH] fix(webui): keep touch app chrome inside the visual viewport --- docs/webui.md | 6 ++ webui/src/App.tsx | 2 + .../src/components/thread/ThreadViewport.tsx | 3 + webui/src/globals.css | 8 ++ webui/src/hooks/useAppViewport.ts | 32 +++++++ webui/src/tests/thread-viewport.test.tsx | 16 ++++ webui/src/tests/useAppViewport.test.tsx | 90 +++++++++++++++++++ 7 files changed, 157 insertions(+) create mode 100644 webui/src/hooks/useAppViewport.ts create mode 100644 webui/src/tests/useAppViewport.test.tsx diff --git a/docs/webui.md b/docs/webui.md index 19da0bfc0..4dc683bd9 100644 --- a/docs/webui.md +++ b/docs/webui.md @@ -188,6 +188,12 @@ explains the restriction and keeps **Open in browser** and **Copy link** availab Sites can still refuse embedding through their own security headers. nanobot does not proxy pages or bypass those restrictions; use **Open in browser** instead. +On touch devices with Visual Viewport support, the app follows the visible area +when the on-screen keyboard opens or pans the page. Navigation and the composer +stay in view while messages scroll independently. Pinch zoom keeps the existing +layout instead of resizing it to the magnified area; normal fitting resumes when +you return to the default zoom. Desktop and native-host layout remain unchanged. + Open the context indicator beside the composer model badge to see how much of the model's context window is in use. The **Recent rounds** chart shows input tokens for each logical model round, including tool-call rounds. Hover or focus diff --git a/webui/src/App.tsx b/webui/src/App.tsx index 306d05f91..8f29e11ff 100644 --- a/webui/src/App.tsx +++ b/webui/src/App.tsx @@ -54,6 +54,7 @@ import { useSkills } from "@/hooks/useSkills"; import { useLogoFallback } from "@/hooks/useLogoFallback"; import { usePageVisibility } from "@/hooks/usePageVisibility"; import { useMediaQuery } from "@/hooks/useMediaQuery"; +import { useAppViewport } from "@/hooks/useAppViewport"; import type { SendAttachment, SendOptions } from "@/hooks/useNanobotStream"; import { ThemeProvider, useTheme } from "@/hooks/useTheme"; import { logoFallbackUrls } from "@/lib/provider-brand"; @@ -894,6 +895,7 @@ function resolveRuntimeSurface( } export default function App() { + useAppViewport(); const { t } = useTranslation(); const [state, setState] = useState({ status: "loading" }); const bootstrapSecretRef = useRef(""); diff --git a/webui/src/components/thread/ThreadViewport.tsx b/webui/src/components/thread/ThreadViewport.tsx index 56c2ade5d..3e2ad0028 100644 --- a/webui/src/components/thread/ThreadViewport.tsx +++ b/webui/src/components/thread/ThreadViewport.tsx @@ -219,6 +219,9 @@ function canScrollInDirection( } function readSoftKeyboardInsetBottom(container: HTMLElement | null): number { + // The touch App shell already fits the visual viewport, including portaled + // composers. Keep the existing local inset only for unfitted/native layouts. + if (container?.closest("#root.visual-viewport")) return 0; const viewport = window.visualViewport; if (!viewport) return 0; const active = document.activeElement; diff --git a/webui/src/globals.css b/webui/src/globals.css index 812909c68..1406cf595 100644 --- a/webui/src/globals.css +++ b/webui/src/globals.css @@ -122,6 +122,14 @@ overflow: hidden; } + #root.visual-viewport { + position: fixed; + top: var(--app-viewport-top); + left: 0; + width: 100%; + height: var(--app-viewport-height); + } + * { scrollbar-color: var(--scrollbar-thumb) transparent; scrollbar-width: thin; diff --git a/webui/src/hooks/useAppViewport.ts b/webui/src/hooks/useAppViewport.ts new file mode 100644 index 000000000..a00814359 --- /dev/null +++ b/webui/src/hooks/useAppViewport.ts @@ -0,0 +1,32 @@ +import { useLayoutEffect } from "react"; +import { useMediaQuery } from "@/hooks/useMediaQuery"; +import { isNativeRuntime } from "@/lib/runtime"; + +/** One viewport owner for navigation, workbench composers and ordinary threads. */ +export function useAppViewport() { + const touch = useMediaQuery("(any-pointer: coarse)"); + useLayoutEffect(() => { + const viewport = window.visualViewport; + const root = document.getElementById("root"); + if (!touch || !viewport || !root || isNativeRuntime()) return; + + const update = () => { + // Pinch zoom pans the existing layout; it must not reflow it or chase the + // user's magnified viewport. Resume fitting when the scale returns to 1. + if (viewport.scale !== 1) return; + root.style.setProperty("--app-viewport-height", `${viewport.height}px`); + root.style.setProperty("--app-viewport-top", `${viewport.offsetTop}px`); + root.classList.add("visual-viewport"); + }; + update(); + viewport.addEventListener("resize", update); + viewport.addEventListener("scroll", update); + return () => { + viewport.removeEventListener("resize", update); + viewport.removeEventListener("scroll", update); + root.classList.remove("visual-viewport"); + root.style.removeProperty("--app-viewport-height"); + root.style.removeProperty("--app-viewport-top"); + }; + }, [touch]); +} diff --git a/webui/src/tests/thread-viewport.test.tsx b/webui/src/tests/thread-viewport.test.tsx index c10954848..fb6a46b32 100644 --- a/webui/src/tests/thread-viewport.test.tsx +++ b/webui/src/tests/thread-viewport.test.tsx @@ -1289,6 +1289,22 @@ describe("ThreadViewport", () => { } }); + it("does not apply the keyboard inset twice when the App shell owns the viewport", () => { + const visualViewport = stubVisualViewport({ innerHeight: 800, height: 480 }); + try { + const { container } = render( +
+ } /> +
, + ); + act(() => { screen.getByLabelText("Message input").focus(); }); + expect(container.querySelector(".thread-viewport-frame")).not.toHaveStyle({ bottom: "320px" }); + } finally { + visualViewport.restore(); + } + }); + it("keeps the welcome composer above a mobile soft keyboard", async () => { const visualViewport = stubVisualViewport({ innerHeight: 800, height: 480 }); try { diff --git a/webui/src/tests/useAppViewport.test.tsx b/webui/src/tests/useAppViewport.test.tsx new file mode 100644 index 000000000..9bfd0b1d4 --- /dev/null +++ b/webui/src/tests/useAppViewport.test.tsx @@ -0,0 +1,90 @@ +import { act, cleanup, render } from "@testing-library/react"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { useAppViewport } from "@/hooks/useAppViewport"; + +let touch = true; +let native = false; +vi.mock("@/hooks/useMediaQuery", () => ({ useMediaQuery: () => touch })); +vi.mock("@/lib/runtime", () => ({ isNativeRuntime: () => native })); + +function AppSurface() { + useAppViewport(); + return <>
Navigation