fix(webui): preserve search results and dialog scrolling with landscape keyboards

This commit is contained in:
Xubin Ren committed 2026-10-04 20:14:21 +08:00
1 parent 2442d88b91
commit ce89f7caf7
6 files changed
+99 -10

No files matched your search

+3 -1
View File
@@ -192,7 +192,9 @@ 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 when the on-screen keyboard opens or pans the page. Navigation and the composer
stay in view while messages scroll independently. Session search also follows stay in view while messages scroll independently. Session search also follows
the visible area: the search field stays above the keyboard and results scroll the visible area: the search field stays above the keyboard and results scroll
inside the dialog. The `@` mention and `/` command inside the dialog. In short landscape viewports, its input and results sit side
by side so a result remains reachable. Taller dialogs scroll from their top
instead of centering content outside the visible area. The `@` mention and `/` command
menus use the visible app area above or below the composer, including when the 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. Mention rows keyboard pans the page. Scroll within a menu to reach more results. Mention rows
use the app's larger touch targets on phones while retaining desktop density. use the app's larger touch targets on phones while retaining desktop density.
+3 -3
View File
@@ -140,14 +140,14 @@ export function SessionSearchDialog({
<DialogContent <DialogContent
showCloseButton={false} showCloseButton={false}
className={cn( className={cn(
"flex max-h-[min(40rem,100%)] w-[calc(100vw-2rem)] max-w-[42rem] flex-col gap-0 overflow-hidden p-0", "session-search-dialog flex max-h-[min(40rem,100%)] w-[calc(100vw-2rem)] max-w-[42rem] flex-col gap-0 overflow-hidden p-0",
)} )}
> >
<DialogTitle className="sr-only">{t("sidebar.searchAria")}</DialogTitle> <DialogTitle className="sr-only">{t("sidebar.searchAria")}</DialogTitle>
<DialogDescription className="sr-only"> <DialogDescription className="sr-only">
{t("sidebar.searchPlaceholder")} {t("sidebar.searchPlaceholder")}
</DialogDescription> </DialogDescription>
<div className="flex h-[62px] shrink-0 items-center gap-3 border-b border-border px-[18px]"> <div className="session-search-input flex h-[62px] shrink-0 items-center gap-3 border-b border-border px-[18px]">
<Search <Search
className="h-[18px] w-[18px] shrink-0 text-muted-foreground" className="h-[18px] w-[18px] shrink-0 text-muted-foreground"
aria-hidden aria-hidden
@@ -170,7 +170,7 @@ export function SessionSearchDialog({
className="min-h-0 flex-1 overflow-y-auto overscroll-contain p-2.5 scrollbar-thin scrollbar-track-transparent" className="min-h-0 flex-1 overflow-y-auto overscroll-contain p-2.5 scrollbar-thin scrollbar-track-transparent"
> >
<section> <section>
<div className="px-2.5 pb-1.5 pt-1 text-[12px] font-medium text-muted-foreground"> <div className="session-search-label px-2.5 pb-1.5 pt-1 text-[12px] font-medium text-muted-foreground">
{sectionLabel} {sectionLabel}
</div> </div>
+8 -5
View File
@@ -24,7 +24,7 @@ const DialogOverlay = React.forwardRef<
ref={ref} ref={ref}
className={cn( className={cn(
modalOverlayClassName, modalOverlayClassName,
"motion-reduce:animate-none", "duration-200 motion-reduce:animate-none",
className, className,
)} )}
{...props} {...props}
@@ -34,6 +34,8 @@ DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
// The portal's presence ref must reach the animated content, not the plain // The portal's presence ref must reach the animated content, not the plain
// positioning wrapper; otherwise the wrapper unmounts before the exit finishes. // positioning wrapper; otherwise the wrapper unmounts before the exit finishes.
// Auto margins center/bottom-align surfaces when they fit, but collapse to zero
// when they overflow so the beginning of a tall dialog remains scrollable.
const DialogPositionedContent = React.forwardRef< const DialogPositionedContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>, React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & { React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
@@ -41,7 +43,7 @@ const DialogPositionedContent = React.forwardRef<
placement?: "center" | "bottom"; placement?: "center" | "bottom";
} }
>(({ positionerStyle, placement, ...props }, ref) => ( >(({ positionerStyle, placement, ...props }, ref) => (
<div className={cn("fixed inset-0 z-50 flex justify-center", placement === "bottom" ? "items-end" : "items-center p-4")} style={{ <div className={cn("fixed inset-0 z-50 flex items-start justify-center overflow-y-auto overscroll-contain", placement !== "bottom" && "p-4")} style={{
top: "var(--app-viewport-top, 0px)", top: "var(--app-viewport-top, 0px)",
height: "var(--app-viewport-height, 100%)", height: "var(--app-viewport-height, 100%)",
bottom: "auto", bottom: "auto",
@@ -97,7 +99,7 @@ const DialogContent = React.forwardRef<
}, [ref]); }, [ref]);
return ( return (
<DialogPortal> <DialogPortal>
<DialogOverlay className={overlayClassName} /> <DialogOverlay className={overlayClassName}>
<DialogPositionedContent <DialogPositionedContent
positionerStyle={layout} positionerStyle={layout}
placement={placement} placement={placement}
@@ -113,8 +115,8 @@ const DialogContent = React.forwardRef<
modalSurfaceClassName, modalSurfaceClassName,
"relative grid w-full max-w-lg gap-4 p-6 duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 motion-reduce:animate-none", "relative grid w-full max-w-lg gap-4 p-6 duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 motion-reduce:animate-none",
placement === "bottom" placement === "bottom"
? "max-h-[85dvh] overflow-y-auto overscroll-contain rounded-t-modal pb-[max(1rem,env(safe-area-inset-bottom))] data-[state=open]:slide-in-from-bottom-4 data-[state=closed]:slide-out-to-bottom-4" ? "mt-auto max-h-[85dvh] overflow-y-auto overscroll-contain rounded-t-modal pb-[max(1rem,env(safe-area-inset-bottom))] data-[state=open]:slide-in-from-bottom-4 data-[state=closed]:slide-out-to-bottom-4"
: "origin-center rounded-modal data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95", : "my-auto origin-center rounded-modal data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
className, className,
)} )}
{...props} {...props}
@@ -129,6 +131,7 @@ const DialogContent = React.forwardRef<
</DialogPrimitive.Close> </DialogPrimitive.Close>
) : null} ) : null}
</DialogPositionedContent> </DialogPositionedContent>
</DialogOverlay>
</DialogPortal> </DialogPortal>
); );
}); });
+19
View File
@@ -141,6 +141,25 @@
overflow-y: auto; overflow-y: auto;
} }
/* Body portals share the viewport owner's short-frame state. A landscape
keyboard needs input and results beside each other, not stacked. */
@media (min-width: 640px) {
body:has(> #root.short-visual-viewport) .session-search-dialog {
flex-direction: row;
}
body:has(> #root.short-visual-viewport) .session-search-input {
width: 40%;
height: auto;
border-bottom-width: 0;
border-right-width: 1px;
}
body:has(> #root.short-visual-viewport) .session-search-label {
@apply sr-only;
}
}
* { * {
scrollbar-color: var(--scrollbar-thumb) transparent; scrollbar-color: var(--scrollbar-thumb) transparent;
scrollbar-width: thin; scrollbar-width: thin;
+54 -1
View File
@@ -1,7 +1,9 @@
import { fireEvent, render, screen } from "@testing-library/react"; import { useState } from "react";
import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
import { SessionSearchDialog } from "@/components/SessionSearchDialog"; import { SessionSearchDialog } from "@/components/SessionSearchDialog";
import { useAppViewport } from "@/hooks/useAppViewport";
import type { ChatSummary } from "@/lib/types"; import type { ChatSummary } from "@/lib/types";
function session(index: number): ChatSummary { function session(index: number): ChatSummary {
@@ -29,9 +31,60 @@ describe("SessionSearchDialog", () => {
expect(screen.getAllByRole("button").length).toBeLessThanOrEqual(24); expect(screen.getAllByRole("button").length).toBeLessThanOrEqual(24);
}); });
afterEach(() => { afterEach(() => {
cleanup();
vi.unstubAllGlobals();
vi.restoreAllMocks(); vi.restoreAllMocks();
}); });
it("shares keyboard rotation and dismissal with the body portal without losing a draft", () => {
vi.stubGlobal("matchMedia", vi.fn(() => ({
matches: true, addEventListener: vi.fn(), removeEventListener: vi.fn(),
})));
const viewport = Object.assign(new EventTarget(), { height: 428, offsetTop: 0, scale: 1 });
vi.stubGlobal("visualViewport", viewport);
const onSelect = vi.fn();
function SearchApp() {
useAppViewport();
const [open, setOpen] = useState(true);
return <>
<textarea aria-label="Draft" defaultValue="Unsent draft" />
<SessionSearchDialog open={open} sessions={[session(1), session(2)]}
activeKey={null} loading={false} onOpenChange={setOpen} onSelect={onSelect} />
</>;
}
const root = document.createElement("div");
root.id = "root";
document.body.append(root);
const app = render(<SearchApp />, { container: root });
try {
const dialog = screen.getByRole("dialog");
expect(root.contains(dialog)).toBe(false);
act(() => {
Object.assign(viewport, { height: 128, offsetTop: -68 });
viewport.dispatchEvent(new Event("resize"));
});
expect(root).toHaveClass("short-visual-viewport");
expect(document.documentElement.style.getPropertyValue("--app-viewport-height")).toBe("128px");
expect(dialog).toHaveClass("session-search-dialog");
expect(document.documentElement.style.getPropertyValue("--app-viewport-top")).toBe("0px");
act(() => {
Object.assign(viewport, { height: 428, offsetTop: 0 });
viewport.dispatchEvent(new Event("resize"));
});
expect(root).not.toHaveClass("short-visual-viewport");
fireEvent.change(screen.getByRole("textbox", { name: "Search" }), { target: { value: "Chat 2" } });
expect(screen.getAllByRole("button")).toHaveLength(1);
fireEvent.click(screen.getByRole("button", { name: /Chat 2/ }));
expect(onSelect).toHaveBeenCalledOnce();
expect(onSelect).toHaveBeenCalledWith("websocket:chat-2");
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(screen.getByRole("textbox", { name: "Draft" })).toHaveValue("Unsent draft");
} finally {
app.unmount();
root.remove();
}
});
it("uses a solid compact command palette surface", () => { it("uses a solid compact command palette surface", () => {
render( render(
<SessionSearchDialog <SessionSearchDialog
+12
View File
@@ -24,6 +24,18 @@ import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea"; import { Textarea } from "@/components/ui/textarea";
describe("UI shape system", () => { describe("UI shape system", () => {
it.each(["center", "bottom"] as const)("keeps an oversized %s dialog scrollable from its start", (placement) => {
render(<Dialog open><DialogContent placement={placement}>
<DialogTitle>Long dialog</DialogTitle>
<DialogDescription>Content that can outgrow a keyboard-fitted frame.</DialogDescription>
</DialogContent></Dialog>);
const dialog = screen.getByRole("dialog");
expect(dialog.parentElement).toHaveClass("items-start", "overflow-y-auto");
// The scroll owner stays inside Radix's overlay/scroll-lock boundary.
expect(dialog.parentElement?.parentElement).toHaveClass("backdrop-blur-[8px]", "duration-200");
expect(dialog).toHaveClass(placement === "bottom" ? "mt-auto" : "my-auto");
});
it("uses pill-shaped inputs and the shared radius for other controls", () => { it("uses pill-shaped inputs and the shared radius for other controls", () => {
render( render(
<> <>