fix(webui): anchor mobile settings menu below its title

This commit is contained in:
Xubin Ren committed 2026-09-13 23:35:16 +08:00
1 parent f5c6be2608
commit aa799cec9f
2 files changed
+114 -72

No files matched your search

@@ -23,7 +23,13 @@ import {
} from "@/components/SidebarSelectionHighlight"; } from "@/components/SidebarSelectionHighlight";
import { isCapabilitySection, type SettingsSectionKey } from "@/components/settings/contracts"; import { isCapabilitySection, type SettingsSectionKey } from "@/components/settings/contracts";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Sheet, SheetContent, SheetTitle, SheetTrigger } from "@/components/ui/sheet"; import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { useMediaQuery } from "@/hooks/useMediaQuery"; import { useMediaQuery } from "@/hooks/useMediaQuery";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
@@ -184,7 +190,7 @@ export function SettingsSidebar(props: SettingsSidebarProps) {
); );
} }
// Mount only on narrow screens so switching to desktop also releases the sheet's focus/pointer lock. // Mount only on narrow screens so an open menu cannot survive a switch to desktop.
function MobileSettingsNavigation({ function MobileSettingsNavigation({
navSection, navSection,
activeLabel, activeLabel,
@@ -202,16 +208,18 @@ function MobileSettingsNavigation({
}) { }) {
const { t } = useTranslation(); const { t } = useTranslation();
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const restart = () => { setOpen(false); onRestart?.(); };
const restartTone = restartPending && !isRestarting
? "text-orange-600 hover:text-orange-700 dark:text-orange-400 dark:hover:text-orange-300"
: "text-muted-foreground hover:text-foreground";
const restartIcon = isRestarting ? <Loader2 className="h-4 w-4 animate-spin" aria-hidden />
: <RotateCcw className="h-4 w-4" aria-hidden />;
const restartAction = onRestart ? ( const restartAction = onRestart ? (
<Button type="button" variant="ghost" disabled={isRestarting} <Button type="button" variant="ghost" disabled={isRestarting}
onClick={() => { setOpen(false); onRestart(); }} onClick={restart}
className={cn("h-11 shrink-0 gap-2 px-3 text-sm font-normal", className={cn("h-11 shrink-0 gap-2 px-3 text-sm font-normal", restartTone)}
restartPending && !isRestarting
? "text-orange-600 hover:text-orange-700 dark:text-orange-400 dark:hover:text-orange-300"
: "text-muted-foreground hover:text-foreground")}
> >
{isRestarting ? <Loader2 className="h-4 w-4 animate-spin" aria-hidden /> {restartIcon}
: <RotateCcw className="h-4 w-4" aria-hidden />}
{restartLabel} {restartLabel}
</Button> </Button>
) : null; ) : null;
@@ -227,49 +235,45 @@ function MobileSettingsNavigation({
> >
<ChevronLeft className="h-5 w-5" aria-hidden /> <ChevronLeft className="h-5 w-5" aria-hidden />
</Button> </Button>
<Sheet open={open} onOpenChange={setOpen}> <DropdownMenu open={open} onOpenChange={setOpen} modal={false}>
<SheetTrigger asChild> <DropdownMenuTrigger asChild>
<Button type="button" variant="ghost" <Button type="button" variant="ghost"
aria-label={`${t("settings.sidebar.title")}: ${activeLabel}`} aria-label={`${t("settings.sidebar.title")}: ${activeLabel}`}
className="h-11 min-w-0 max-w-full justify-self-center gap-1.5 px-2 text-base font-medium" className="h-11 min-w-0 max-w-full justify-self-center gap-1.5 px-2 text-base font-medium"
> >
<span className="truncate">{activeLabel}</span> <span className="truncate">{activeLabel}</span>
<ChevronDown className="h-3.5 w-3.5 shrink-0 text-muted-foreground" aria-hidden /> <ChevronDown className={cn("h-3.5 w-3.5 shrink-0 text-muted-foreground transition-transform duration-150 motion-reduce:transition-none", open && "rotate-180")} aria-hidden />
</Button> </Button>
</SheetTrigger> </DropdownMenuTrigger>
<SheetContent side="bottom" showCloseButton={false} aria-describedby={undefined} <DropdownMenuContent side="bottom" align="center" sideOffset={6} collisionPadding={12}
className="mx-auto max-h-[85dvh] max-w-md gap-0 rounded-t-3xl px-3 pt-2 pb-[max(0.75rem,env(safe-area-inset-bottom))] data-[state=open]:duration-300 data-[state=closed]:duration-200" aria-label={t("settings.sidebar.ariaLabel")} aria-labelledby={undefined}
className="w-60 max-w-[calc(100vw-1.5rem)] data-[state=open]:slide-in-from-top-1 data-[state=closed]:slide-out-to-top-1 data-[state=open]:duration-150 data-[state=closed]:duration-100 motion-reduce:data-[state=open]:animate-none motion-reduce:data-[state=closed]:animate-none"
> >
<div className="flex shrink-0 items-center justify-between gap-3 px-3 pb-2"> {SETTINGS_NAV_ITEMS.map(({ key, icon: Icon, fallback }) => {
<SheetTitle className="text-base font-medium">{t("settings.sidebar.title")}</SheetTitle> const active = key === navSection;
<Button type="button" variant="ghost" onClick={() => setOpen(false)} return (
className="-mr-2 h-11 px-2 text-sm font-normal text-muted-foreground" <DropdownMenuItem key={key}
>{t("common.close")}</Button> aria-current={active ? "page" : undefined}
</div> onSelect={() => onSelectSection(key)}
<div className="min-h-0 overflow-y-auto overscroll-contain"> className={cn("min-h-11 gap-3 text-sm font-normal",
<nav aria-label={t("settings.sidebar.ariaLabel")} className="space-y-0.5"> active ? "bg-sidebar-accent text-foreground" : "text-sidebar-content")}
{SETTINGS_NAV_ITEMS.map(({ key, icon: Icon, fallback }) => { >
const active = key === navSection; <Icon aria-hidden />
return ( <span className="min-w-0 flex-1 truncate">
<Button key={key} type="button" variant="ghost" {t(`settings.nav.${key}`, { defaultValue: fallback })}
aria-current={active ? "page" : undefined} </span>
onClick={() => { setOpen(false); onSelectSection(key); }} {active ? <Check aria-hidden /> : null}
className={cn("h-11 w-full justify-start gap-3 rounded-xl px-3 text-sm font-normal", </DropdownMenuItem>
active ? "bg-sidebar-accent text-foreground" : "text-sidebar-content")} );
> })}
<Icon className="h-[18px] w-[18px] shrink-0" aria-hidden /> {onRestart ? <>
<span className="min-w-0 flex-1 truncate text-left"> <DropdownMenuSeparator />
{t(`settings.nav.${key}`, { defaultValue: fallback })} <DropdownMenuItem disabled={isRestarting} onSelect={restart}
</span> className={cn("min-h-11 gap-3 text-sm font-normal", restartTone)}
{active ? <Check className="h-4 w-4 shrink-0" aria-hidden /> : null} >{restartIcon}{restartLabel}</DropdownMenuItem>
</Button> </> : null}
); </DropdownMenuContent>
})} </DropdownMenu>
</nav>
{restartAction ? <div className="mt-2 border-t border-border/50 pt-2">{restartAction}</div> : null}
</div>
</SheetContent>
</Sheet>
</div> </div>
{onRestart && (restartPending || isRestarting) ? ( {onRestart && (restartPending || isRestarting) ? (
<div className="flex min-h-11 items-center justify-between gap-2 px-4 pb-1"> <div className="flex min-h-11 items-center justify-between gap-2 px-4 pb-1">
+65 -27
View File
@@ -38,7 +38,7 @@ function Sidebar(props: Partial<ComponentProps<typeof SettingsSidebar>>) {
describe("Settings navigation on mobile", () => { describe("Settings navigation on mobile", () => {
installSettingsViewTestHooks(); installSettingsViewTestHooks();
it("keeps only back and the current section in the header; selects sections in a sheet", async () => { it("keeps the single-row header and opens a section menu beneath its title", async () => {
mockMobileMedia(); mockMobileMedia();
const user = userEvent.setup(); const user = userEvent.setup();
const restart = vi.fn(); const restart = vi.fn();
@@ -48,13 +48,16 @@ describe("Settings navigation on mobile", () => {
expect(within(header).getAllByRole("button")).toHaveLength(2); expect(within(header).getAllByRole("button")).toHaveLength(2);
const trigger = screen.getByRole("button", { name: "Settings: Models" }); const trigger = screen.getByRole("button", { name: "Settings: Models" });
await user.click(trigger); await user.click(trigger);
const sheet = screen.getByRole("dialog", { name: "Settings" }); const menu = screen.getByRole("menu", { name: "Settings sections" });
expect(within(sheet).getByRole("button", { name: "Models", exact: true })) expect(trigger).toHaveAttribute("aria-haspopup", "menu");
expect(menu).toHaveAttribute("data-side", "bottom");
expect(within(menu).getByRole("menuitem", { name: "Models", exact: true }))
.toHaveAttribute("aria-current", "page"); .toHaveAttribute("aria-current", "page");
expect(within(sheet).getByRole("navigation", { name: "Settings sections" })).toBeVisible(); expect(within(menu).getByRole("menuitem", { name: "Restart" })).toBeVisible();
expect(within(sheet).getByRole("button", { name: "Restart" })).toBeVisible(); expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
await user.click(within(sheet).getByRole("button", { name: "Appearance" })); expect(screen.queryByRole("button", { name: "Close" })).not.toBeInTheDocument();
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); await user.click(within(menu).getByRole("menuitem", { name: "Appearance" }));
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(screen.getByRole("button", { name: "Settings: Appearance" })).toHaveFocus(); expect(screen.getByRole("button", { name: "Settings: Appearance" })).toHaveFocus();
expect(document.body.style.pointerEvents).not.toBe("none"); expect(document.body.style.pointerEvents).not.toBe("none");
expect(restart).not.toHaveBeenCalled(); expect(restart).not.toHaveBeenCalled();
@@ -67,40 +70,74 @@ describe("Settings navigation on mobile", () => {
const user = userEvent.setup(); const user = userEvent.setup();
render(<Sidebar activeSection="image" />); render(<Sidebar activeSection="image" />);
await user.click(screen.getByRole("button", { name: "Settings: Capabilities" })); await user.click(screen.getByRole("button", { name: "Settings: Capabilities" }));
expect(screen.getByRole("button", { name: "Capabilities", exact: true })) expect(screen.getByRole("menuitem", { name: "Capabilities", exact: true }))
.toHaveAttribute("aria-current", "page"); .toHaveAttribute("aria-current", "page");
expect(screen.queryByRole("button", { name: "Restart" })).not.toBeInTheDocument(); expect(screen.queryByRole("menuitem", { name: "Restart" })).not.toBeInTheDocument();
}); });
it.each(["close", "escape", "outside"])("dismisses with %s and restores focus and pointer events", async (method) => { it.each(["trigger", "escape"])("dismisses with %s and restores focus without locking the page", async (method) => {
mockMobileMedia(); mockMobileMedia();
const user = userEvent.setup(); const user = userEvent.setup();
render(<Sidebar />); render(<Sidebar />);
const trigger = screen.getByRole("button", { name: "Settings: Models" }); const trigger = screen.getByRole("button", { name: "Settings: Models" });
await user.click(trigger); await user.click(trigger);
if (method === "close") await user.click(screen.getByRole("button", { name: "Close" })); expect(document.body.style.pointerEvents).not.toBe("none");
else if (method === "escape") await user.keyboard("{Escape}"); if (method === "trigger") await user.click(trigger);
else { else await user.keyboard("{Escape}");
// The real overlay is outside the modal content; ignore the locked page behind it. await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
const overlay = document.querySelector<HTMLElement>('[data-state="open"][aria-hidden="true"]');
expect(overlay).not.toBeNull();
await user.click(overlay!);
}
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(trigger).toHaveFocus(); expect(trigger).toHaveFocus();
expect(document.body.style.pointerEvents).not.toBe("none"); expect(document.body.style.pointerEvents).not.toBe("none");
}); });
it("keeps manual restart in the sheet and closes it before handing off", async () => { it("dismisses on an outside click without swallowing the back action", async () => {
mockMobileMedia();
const user = userEvent.setup();
const back = vi.fn();
render(<Sidebar onBackToChat={back} />);
await user.click(screen.getByRole("button", { name: "Settings: Models" }));
const backButton = screen.getByRole("button", { name: "Back to chat" });
await user.click(backButton);
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(back).toHaveBeenCalledOnce();
expect(backButton).toHaveFocus();
expect(document.body.style.pointerEvents).not.toBe("none");
});
it("supports keyboard section selection", async () => {
mockMobileMedia();
const user = userEvent.setup();
render(<Sidebar />);
screen.getByRole("button", { name: "Settings: Models" }).focus();
await user.keyboard("{ArrowDown}");
expect(screen.getByRole("menuitem", { name: "Overview", exact: true })).toHaveFocus();
await user.keyboard("{ArrowDown}{Enter}");
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(screen.getByRole("button", { name: "Settings: Appearance" })).toHaveFocus();
});
it("opens and selects sections with touch taps", async () => {
mockMobileMedia();
const user = userEvent.setup();
render(<Sidebar />);
const tap = (target: HTMLElement) => user.pointer([
{ keys: "[TouchA>]", target }, { keys: "[/TouchA]", target },
]);
await tap(screen.getByRole("button", { name: "Settings: Models" }));
await tap(screen.getByRole("menuitem", { name: "System", exact: true }));
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(screen.getByRole("button", { name: "Settings: System" })).toBeVisible();
});
it("keeps manual restart in the menu and closes it on selection", async () => {
mockMobileMedia(); mockMobileMedia();
const user = userEvent.setup(); const user = userEvent.setup();
const restart = vi.fn(); const restart = vi.fn();
render(<Sidebar onRestart={restart} />); render(<Sidebar onRestart={restart} />);
expect(screen.queryByRole("button", { name: "Restart" })).not.toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Restart" })).not.toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Settings: Models" })); await user.click(screen.getByRole("button", { name: "Settings: Models" }));
await user.click(screen.getByRole("button", { name: "Restart" })); await user.click(screen.getByRole("menuitem", { name: "Restart" }));
expect(restart).toHaveBeenCalledOnce(); expect(restart).toHaveBeenCalledOnce();
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(document.body.style.pointerEvents).not.toBe("none"); expect(document.body.style.pointerEvents).not.toBe("none");
}); });
@@ -116,28 +153,29 @@ describe("Settings navigation on mobile", () => {
const runningLabel = screen.getByRole("status").textContent!; const runningLabel = screen.getByRole("status").textContent!;
expect(screen.getByRole("button", { name: runningLabel, exact: true })).toBeDisabled(); expect(screen.getByRole("button", { name: runningLabel, exact: true })).toBeDisabled();
await user.click(screen.getByRole("button", { name: "Settings: Models" })); await user.click(screen.getByRole("button", { name: "Settings: Models" }));
expect(within(screen.getByRole("dialog")).getByRole("button", { name: runningLabel })).toBeDisabled(); expect(within(screen.getByRole("menu")).getByRole("menuitem", { name: runningLabel }))
.toHaveAttribute("aria-disabled", "true");
await user.keyboard("{Escape}"); await user.keyboard("{Escape}");
rerender(<Sidebar onRestart={restart} restartPending={false} />); rerender(<Sidebar onRestart={restart} restartPending={false} />);
expect(screen.queryByRole("status")).not.toBeInTheDocument(); expect(screen.queryByRole("status")).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Restart" })).not.toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Restart" })).not.toBeInTheDocument();
}); });
it("releases the open sheet when crossing to desktop and does not reopen on return", async () => { it("dismisses the open menu when crossing to desktop and does not reopen on return", async () => {
const resize = mockMobileMedia(); const resize = mockMobileMedia();
const user = userEvent.setup(); const user = userEvent.setup();
const restart = vi.fn(); const restart = vi.fn();
render(<Sidebar onRestart={restart} />); render(<Sidebar onRestart={restart} />);
await user.click(screen.getByRole("button", { name: "Settings: Models" })); await user.click(screen.getByRole("button", { name: "Settings: Models" }));
resize(false); resize(false);
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(document.body.style.pointerEvents).not.toBe("none"); expect(document.body.style.pointerEvents).not.toBe("none");
expect(screen.queryByRole("button", { name: "Settings: Models" })).not.toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Settings: Models" })).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Models", exact: true })).toBeVisible(); expect(screen.getByRole("button", { name: "Models", exact: true })).toBeVisible();
await user.click(screen.getByRole("button", { name: "Restart" })); await user.click(screen.getByRole("button", { name: "Restart" }));
expect(restart).toHaveBeenCalledOnce(); expect(restart).toHaveBeenCalledOnce();
resize(true); resize(true);
expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); expect(screen.queryByRole("menu")).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Settings: Models" })).toBeVisible(); expect(screen.getByRole("button", { name: "Settings: Models" })).toBeVisible();
}); });
@@ -149,7 +187,7 @@ describe("Settings navigation on mobile", () => {
...settingsPayload(), requires_restart: true, ...settingsPayload(), requires_restart: true,
}, onBackToChat: back }); }, onBackToChat: back });
await user.click(screen.getByRole("button", { name: "Settings: Models" })); await user.click(screen.getByRole("button", { name: "Settings: Models" }));
await user.click(screen.getByRole("button", { name: "Appearance", exact: true })); await user.click(screen.getByRole("menuitem", { name: "Appearance", exact: true }));
await user.click(screen.getByRole("button", { name: "Back to chat" })); await user.click(screen.getByRole("button", { name: "Back to chat" }));
const dialog = screen.getByRole("dialog", { name: "Restart before leaving?" }); const dialog = screen.getByRole("dialog", { name: "Restart before leaving?" });
expect(back).not.toHaveBeenCalled(); expect(back).not.toHaveBeenCalled();