fix(webui): keep composer palettes inside the mobile viewport

This commit is contained in:
Xubin Ren committed 2026-10-04 20:05:12 +08:00
1 parent ed022e447f
commit 5c5027ac0b
3 files changed
+59 -2

No files matched your search

+4 -1
View File
@@ -190,7 +190,10 @@ does not proxy pages or bypass those restrictions; use **Open in browser** inste
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. In very short viewports (such
stay in view while messages scroll independently. The `@` mention and `/` command
menus use the visible app area above or below the composer, including when the
keyboard pans the page. Scroll within a menu to reach more results.
In very short viewports (such
as landscape with the keyboard open), scroll within the composer to reach its
controls. If the browser bars and keyboard leave no usable page area, dismiss
the keyboard or return to portrait. Pinch zoom keeps the existing layout instead of resizing it to the
@@ -614,7 +614,13 @@ function visualViewportBounds(): { top: number; bottom: number; height: number }
}
function getVisibleBounds(el: HTMLElement): { top: number; bottom: number } {
const viewport = visualViewportBounds();
// The app already follows the visual viewport on touch browsers. Measure
// its frame in the same coordinates as the composer, not WebKit's pan offset.
// During pinch zoom the app stops fitting, so use the zoomed viewport again.
const fittedRoot = window.visualViewport?.scale === 1
? el.closest("#root.visual-viewport")
: null;
const viewport = fittedRoot?.getBoundingClientRect() ?? visualViewportBounds();
let top = viewport.top;
let bottom = viewport.bottom;
let parent = el.parentElement;
+48
View File
@@ -2740,6 +2740,54 @@ describe("ThreadComposer", () => {
});
});
it.each(["/", "@"]) ("fits %s suggestions in the app frame while iOS pans the keyboard", (trigger) => {
stubVisualViewport({ height: 396, offsetTop: 350 });
let formTop = 274;
let frameHeight = 396;
vi.spyOn(HTMLFormElement.prototype, "getBoundingClientRect").mockImplementation(
() => rect({ top: formTop, width: 388, height: 112 }),
);
vi.spyOn(HTMLDivElement.prototype, "getBoundingClientRect").mockImplementation(
() => rect({ top: 0, width: 420, height: frameHeight }),
);
render(
<div id="root" className="visual-viewport" style={{ overflowY: "hidden" }}>
<ThreadComposer onSend={vi.fn()} slashCommands={COMMANDS} cliApps={CLI_APPS} />
</div>,
);
const input = screen.getByLabelText("Message input");
fireEvent.change(input, { target: { value: trigger, selectionStart: 1 } });
const palette = screen.getByRole("listbox");
expect(palette).toHaveClass("bottom-full");
expect(palette).toHaveStyle({ maxHeight: "266px" });
// Keyboard dismissal moves the composer; keep measuring the same frame.
formTop = 620;
frameHeight = 746;
Object.assign(window.visualViewport!, { height: 746, offsetTop: 0 });
act(() => window.visualViewport!.dispatchEvent(new Event("resize")));
expect(palette).toHaveStyle({ maxHeight: "288px" });
fireEvent.keyDown(input, { key: "Enter" });
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
expect(input).not.toHaveValue(trigger);
});
it.each([false, true])("uses visual bounds outside fitting (pinch zoom: %s)", (zoomed) => {
stubVisualViewport({ height: 300, offsetTop: 100 });
if (zoomed) Object.assign(window.visualViewport!, { scale: 2 });
vi.spyOn(HTMLFormElement.prototype, "getBoundingClientRect").mockReturnValue(
rect({ top: 220, width: 390, height: 100 }),
);
render(
<div id="root" className={zoomed ? "visual-viewport" : undefined}>
<ThreadComposer onSend={vi.fn()} slashCommands={COMMANDS} />
</div>,
);
fireEvent.change(screen.getByLabelText("Message input"), { target: { value: "/" } });
expect(screen.getByRole("listbox")).toHaveStyle({ maxHeight: "112px" });
expect(screen.getByRole("listbox")).toHaveClass("bottom-full");
});
it("dismisses the slash command palette on outside click", () => {
render(
<div>