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";
import { isCapabilitySection, type SettingsSectionKey } from "@/components/settings/contracts";
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 { 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({
navSection,
activeLabel,
@@ -202,16 +208,18 @@ function MobileSettingsNavigation({
}) {
const { t } = useTranslation();
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 ? (
<Button type="button" variant="ghost" disabled={isRestarting}
onClick={() => { setOpen(false); onRestart(); }}
className={cn("h-11 shrink-0 gap-2 px-3 text-sm font-normal",
restartPending && !isRestarting
? "text-orange-600 hover:text-orange-700 dark:text-orange-400 dark:hover:text-orange-300"
: "text-muted-foreground hover:text-foreground")}
onClick={restart}
className={cn("h-11 shrink-0 gap-2 px-3 text-sm font-normal", restartTone)}
>
{isRestarting ? <Loader2 className="h-4 w-4 animate-spin" aria-hidden />
: <RotateCcw className="h-4 w-4" aria-hidden />}
{restartIcon}
{restartLabel}
</Button>
) : null;
@@ -227,49 +235,45 @@ function MobileSettingsNavigation({
>
<ChevronLeft className="h-5 w-5" aria-hidden />
</Button>
<Sheet open={open} onOpenChange={setOpen}>
<SheetTrigger asChild>
<DropdownMenu open={open} onOpenChange={setOpen} modal={false}>
<DropdownMenuTrigger asChild>
<Button type="button" variant="ghost"
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"
>
<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>
</SheetTrigger>
<SheetContent side="bottom" showCloseButton={false} aria-describedby={undefined}
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"
</DropdownMenuTrigger>
<DropdownMenuContent side="bottom" align="center" sideOffset={6} collisionPadding={12}
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">
<SheetTitle className="text-base font-medium">{t("settings.sidebar.title")}</SheetTitle>
<Button type="button" variant="ghost" onClick={() => setOpen(false)}
className="-mr-2 h-11 px-2 text-sm font-normal text-muted-foreground"
>{t("common.close")}</Button>
</div>
<div className="min-h-0 overflow-y-auto overscroll-contain">
<nav aria-label={t("settings.sidebar.ariaLabel")} className="space-y-0.5">
{SETTINGS_NAV_ITEMS.map(({ key, icon: Icon, fallback }) => {
const active = key === navSection;
return (
<Button key={key} type="button" variant="ghost"
aria-current={active ? "page" : undefined}
onClick={() => { setOpen(false); onSelectSection(key); }}
className={cn("h-11 w-full justify-start gap-3 rounded-xl px-3 text-sm font-normal",
active ? "bg-sidebar-accent text-foreground" : "text-sidebar-content")}
>
<Icon className="h-[18px] w-[18px] shrink-0" aria-hidden />
<span className="min-w-0 flex-1 truncate text-left">
{t(`settings.nav.${key}`, { defaultValue: fallback })}
</span>
{active ? <Check className="h-4 w-4 shrink-0" aria-hidden /> : null}
</Button>
);
})}
</nav>
{restartAction ? <div className="mt-2 border-t border-border/50 pt-2">{restartAction}</div> : null}
</div>
</SheetContent>
</Sheet>
{SETTINGS_NAV_ITEMS.map(({ key, icon: Icon, fallback }) => {
const active = key === navSection;
return (
<DropdownMenuItem key={key}
aria-current={active ? "page" : undefined}
onSelect={() => onSelectSection(key)}
className={cn("min-h-11 gap-3 text-sm font-normal",
active ? "bg-sidebar-accent text-foreground" : "text-sidebar-content")}
>
<Icon aria-hidden />
<span className="min-w-0 flex-1 truncate">
{t(`settings.nav.${key}`, { defaultValue: fallback })}
</span>
{active ? <Check aria-hidden /> : null}
</DropdownMenuItem>
);
})}
{onRestart ? <>
<DropdownMenuSeparator />
<DropdownMenuItem disabled={isRestarting} onSelect={restart}
className={cn("min-h-11 gap-3 text-sm font-normal", restartTone)}
>{restartIcon}{restartLabel}</DropdownMenuItem>
</> : null}
</DropdownMenuContent>
</DropdownMenu>
</div>
{onRestart && (restartPending || isRestarting) ? (
<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", () => {
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();
const user = userEvent.setup();
const restart = vi.fn();
@@ -48,13 +48,16 @@ describe("Settings navigation on mobile", () => {
expect(within(header).getAllByRole("button")).toHaveLength(2);
const trigger = screen.getByRole("button", { name: "Settings: Models" });
await user.click(trigger);
const sheet = screen.getByRole("dialog", { name: "Settings" });
expect(within(sheet).getByRole("button", { name: "Models", exact: true }))
const menu = screen.getByRole("menu", { name: "Settings sections" });
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");
expect(within(sheet).getByRole("navigation", { name: "Settings sections" })).toBeVisible();
expect(within(sheet).getByRole("button", { name: "Restart" })).toBeVisible();
await user.click(within(sheet).getByRole("button", { name: "Appearance" }));
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(within(menu).getByRole("menuitem", { name: "Restart" })).toBeVisible();
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Close" })).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(document.body.style.pointerEvents).not.toBe("none");
expect(restart).not.toHaveBeenCalled();
@@ -67,40 +70,74 @@ describe("Settings navigation on mobile", () => {
const user = userEvent.setup();
render(<Sidebar activeSection="image" />);
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");
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();
const user = userEvent.setup();
render(<Sidebar />);
const trigger = screen.getByRole("button", { name: "Settings: Models" });
await user.click(trigger);
if (method === "close") await user.click(screen.getByRole("button", { name: "Close" }));
else if (method === "escape") await user.keyboard("{Escape}");
else {
// The real overlay is outside the modal content; ignore the locked page behind it.
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(document.body.style.pointerEvents).not.toBe("none");
if (method === "trigger") await user.click(trigger);
else await user.keyboard("{Escape}");
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(trigger).toHaveFocus();
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();
const user = userEvent.setup();
const restart = vi.fn();
render(<Sidebar onRestart={restart} />);
expect(screen.queryByRole("button", { name: "Restart" })).not.toBeInTheDocument();
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();
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(document.body.style.pointerEvents).not.toBe("none");
});
@@ -116,28 +153,29 @@ describe("Settings navigation on mobile", () => {
const runningLabel = screen.getByRole("status").textContent!;
expect(screen.getByRole("button", { name: runningLabel, exact: true })).toBeDisabled();
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}");
rerender(<Sidebar onRestart={restart} restartPending={false} />);
expect(screen.queryByRole("status")).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 user = userEvent.setup();
const restart = vi.fn();
render(<Sidebar onRestart={restart} />);
await user.click(screen.getByRole("button", { name: "Settings: Models" }));
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(screen.queryByRole("button", { name: "Settings: Models" })).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Models", exact: true })).toBeVisible();
await user.click(screen.getByRole("button", { name: "Restart" }));
expect(restart).toHaveBeenCalledOnce();
resize(true);
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(screen.queryByRole("menu")).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Settings: Models" })).toBeVisible();
});
@@ -149,7 +187,7 @@ describe("Settings navigation on mobile", () => {
...settingsPayload(), requires_restart: true,
}, onBackToChat: back });
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" }));
const dialog = screen.getByRole("dialog", { name: "Restart before leaving?" });
expect(back).not.toHaveBeenCalled();