fix(webui): keep touch app chrome inside the visual viewport

This commit is contained in:
Xubin Ren committed 2026-10-04 10:40:09 +08:00
1 parent 69a368fdf8
commit 4e77584eb7
7 files changed
+157

No files matched your search

+6
View File
@@ -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
+2
View File
@@ -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<BootState>({ status: "loading" });
const bootstrapSecretRef = useRef("");
@@ -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;
+8
View File
@@ -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;
+32
View File
@@ -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]);
}
+16
View File
@@ -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(
<div id="root" className="visual-viewport">
<ThreadViewport messages={messages} isStreaming={false}
composer={<textarea aria-label="Message input" />} />
</div>,
);
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 {
+90
View File
@@ -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 <><header>Navigation</header><textarea aria-label="Draft" defaultValue="中文草稿" /></>;
}
describe("app visual viewport", () => {
let root: HTMLDivElement;
let viewport: EventTarget & { height: number; offsetTop: number; scale: number };
beforeEach(() => {
touch = true;
native = false;
root = document.createElement("div");
root.id = "root";
document.body.append(root);
viewport = Object.assign(new EventTarget(), { height: 746, offsetTop: 0, scale: 1 });
vi.stubGlobal("visualViewport", viewport);
});
afterEach(() => { cleanup(); root.remove(); vi.unstubAllGlobals(); });
function resize(height: number, offsetTop: number, scale = 1) {
act(() => {
Object.assign(viewport, { height, offsetTop, scale });
viewport.dispatchEvent(new Event("resize"));
});
}
it("fits the whole app to keyboard height and pan, preserving draft and focus through dismissal", () => {
render(<AppSurface />, { container: root });
const input = root.querySelector("textarea")!;
input.focus();
resize(396, 350);
expect(root).toHaveClass("visual-viewport");
expect(root.style.getPropertyValue("--app-viewport-height")).toBe("396px");
expect(root.style.getPropertyValue("--app-viewport-top")).toBe("350px");
act(() => {
viewport.offsetTop = 320;
viewport.dispatchEvent(new Event("scroll"));
});
expect(root.style.getPropertyValue("--app-viewport-top")).toBe("320px");
expect(document.activeElement).toBe(input);
expect(input).toHaveValue("中文草稿");
resize(746, 0);
expect(root.style.getPropertyValue("--app-viewport-height")).toBe("746px");
expect(root.style.getPropertyValue("--app-viewport-top")).toBe("0px");
});
it("does not reflow or chase the viewport during pinch zoom, then resumes on reset", () => {
render(<AppSurface />, { container: root });
resize(373, 120, 2);
expect(root.style.getPropertyValue("--app-viewport-height")).toBe("746px");
expect(root.style.getPropertyValue("--app-viewport-top")).toBe("0px");
resize(320, 0);
expect(root.style.getPropertyValue("--app-viewport-height")).toBe("320px");
});
it.each(["desktop", "native", "unsupported"])("leaves %s layout ownership unchanged", (kind) => {
touch = kind !== "desktop";
native = kind === "native";
if (kind === "unsupported") vi.stubGlobal("visualViewport", undefined);
render(<AppSurface />, { container: root });
resize(396, 350);
expect(root).not.toHaveClass("visual-viewport");
expect(root.style.getPropertyValue("--app-viewport-height")).toBe("");
});
it("releases listeners and owned styles when touch layout is disabled or the app unmounts", () => {
const { rerender, unmount } = render(<AppSurface />, { container: root });
touch = false;
rerender(<AppSurface />);
resize(396, 350);
expect(root).not.toHaveClass("visual-viewport");
expect(root.style.getPropertyValue("--app-viewport-height")).toBe("");
touch = true;
rerender(<AppSurface />);
expect(root).toHaveClass("visual-viewport");
unmount();
resize(746, 0);
expect(root).not.toHaveClass("visual-viewport");
expect(root.style.getPropertyValue("--app-viewport-top")).toBe("");
});
});