fix(webui): prevent mobile sidebar tooltip and double-tap navigation

Integrate the focused Sheet autofocus and touch hover corrections from
#5777 and #5641 on current main. Preserve caller focus overrides, keyboard
accessibility, and the existing viewport/safe-area policy.

Add App-level coverage for consecutive one-click topic selections and
drawer dismissal alongside focus and emitted-CSS regressions.

Co-authored-by: Nikko Gonzales <nikkoxgonzales@gmail.com>
Co-authored-by: moran <moranfong@gmail.com>
This commit is contained in:
committed 2026-09-21 16:26:57 +08:00
1 parent d37d7c07c0
commit 13319dfe15
6 files changed
+265 -17

No files matched your search

+36
View File
@@ -2199,6 +2199,42 @@ describe("App layout", () => {
expect(screen.queryByText("Daily repo check")).not.toBeInTheDocument();
}, 15_000);
it("opens a mobile topic with one click and closes the drawer without a search tooltip", async () => {
const user = userEvent.setup();
mockSessions = ["First", "Second"].map((title, index) => ({
key: `websocket:mobile-${index}`,
channel: "websocket",
chatId: `mobile-${index}`,
createdAt: "2026-04-16T10:00:00Z",
updatedAt: "2026-04-16T10:00:00Z",
preview: `${title} mobile chat`,
}));
vi.stubGlobal("matchMedia", vi.fn().mockImplementation((query: string) => ({
matches: !query.includes("1024px"),
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})));
render(<App />);
await waitFor(() => expect(connectSpy).toHaveBeenCalled());
for (const title of ["First", "Second"]) {
await user.click(await screen.findByRole("button", { name: "Toggle sidebar" }));
const sheet = await screen.findByRole("dialog");
await waitFor(() => expect(sheet).toHaveFocus());
expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
await user.click(await within(sheet).findByRole("button", { name: `${title} mobile chat` }));
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
await waitFor(() => expect(document.title).toBe(`${title} mobile chat · nanobot`));
expect(screen.getByRole("button", { name: `${title} mobile chat` }))
.toHaveAttribute("aria-current", "page");
}
});
it("keeps the mobile session action menu inside the sidebar sheet", async () => {
mockSessions = [
{
+141
View File
@@ -0,0 +1,141 @@
import { render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { createRef, type Ref } from "react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { Sheet, SheetContent, SheetTitle, SheetTrigger } from "@/components/ui/sheet";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
vi.mock("@/providers/ClientProvider", () => ({
useClient: () => ({ client: { status: "idle", onStatus: () => () => {} } }),
}));
function noop() {}
// Reproduces the #5770 chain: the mobile drawer autofocuses the search button
// (Radix focus-scope default), and focusing a tooltip trigger with no preceding
// pointerdown opens its tooltip.
function DrawerWithSearchTooltip(props: {
onOpenAutoFocus?: (event: Event) => void;
contentRef?: Ref<HTMLDivElement>;
}) {
return (
<TooltipProvider>
<Sheet open onOpenChange={noop}>
<SheetContent
ref={props.contentRef}
side="left"
showCloseButton={false}
aria-describedby={undefined}
onOpenAutoFocus={props.onOpenAutoFocus}
>
<SheetTitle className="sr-only">Navigation</SheetTitle>
<Tooltip>
<TooltipTrigger asChild>
<button type="button">search</button>
</TooltipTrigger>
<TooltipContent side="bottom">Search tip</TooltipContent>
</Tooltip>
</SheetContent>
</Sheet>
</TooltipProvider>
);
}
describe("SheetContent autofocus", () => {
// happy-dom reports tabIndex -1 for natively focusable elements without an
// explicit tabindex attribute (browsers report 0), which makes Radix
// getTabbableCandidates() find nothing. Stub browser behavior so the
// autofocus path under test actually runs.
const tabIndexDesc = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "tabIndex");
beforeEach(() => {
Object.defineProperty(HTMLElement.prototype, "tabIndex", {
configurable: true,
get(this: HTMLElement): number {
const attr = this.getAttribute("tabindex");
if (attr !== null) return Number(attr);
if (/^(BUTTON|A|INPUT|SELECT|TEXTAREA)$/.test(this.tagName)) return 0;
return -1;
},
});
});
afterEach(() => {
if (tabIndexDesc) Object.defineProperty(HTMLElement.prototype, "tabIndex", tabIndexDesc);
});
it("does not steal focus into the search button or pop its tooltip on open (#5770)", async () => {
render(<DrawerWithSearchTooltip />);
const dialog = await screen.findByRole("dialog");
await waitFor(() => expect(dialog).toHaveFocus());
const searchButton = within(dialog).getByRole("button", { name: "search" });
expect(searchButton).not.toHaveFocus();
expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
});
it("honors a caller-supplied onOpenAutoFocus handler", async () => {
const onOpenAutoFocus = vi.fn((event: Event) => event.preventDefault());
render(<DrawerWithSearchTooltip onOpenAutoFocus={onOpenAutoFocus} />);
await screen.findByRole("dialog");
await waitFor(() => expect(onOpenAutoFocus).toHaveBeenCalled());
});
it("keeps the caller's explicit focus target", async () => {
render(<DrawerWithSearchTooltip onOpenAutoFocus={(event) => {
event.preventDefault();
screen.getByRole("button", { name: "search" }).focus();
}} />);
await waitFor(() => expect(screen.getByRole("button", { name: "search" })).toHaveFocus());
expect(await screen.findByRole("tooltip")).toHaveTextContent("Search tip");
});
it("allows callers to retain Radix's default autofocus", async () => {
render(<DrawerWithSearchTooltip onOpenAutoFocus={noop} />);
await waitFor(() => expect(screen.getByRole("button", { name: "search" })).toHaveFocus());
});
it("preserves object refs while focusing the container", async () => {
const ref = createRef<HTMLDivElement>();
const { unmount } = render(<DrawerWithSearchTooltip contentRef={ref} />);
const dialog = await screen.findByRole("dialog");
await waitFor(() => expect(dialog).toHaveFocus());
expect(ref.current).toBe(dialog);
unmount();
expect(ref.current).toBeNull();
});
it("preserves callback refs on mount and unmount", async () => {
const ref = vi.fn();
const { unmount } = render(<DrawerWithSearchTooltip contentRef={ref} />);
const dialog = await screen.findByRole("dialog");
await waitFor(() => expect(dialog).toHaveFocus());
expect(ref).toHaveBeenCalledWith(dialog);
unmount();
expect(ref).toHaveBeenLastCalledWith(null);
});
it("keeps keyboard focus inside the drawer and restores it on Escape", async () => {
const user = userEvent.setup();
render(
<Sheet>
<SheetTrigger>Open drawer</SheetTrigger>
<SheetContent showCloseButton={false} aria-describedby={undefined}>
<SheetTitle>Navigation</SheetTitle>
<button>First action</button>
<button>Last action</button>
</SheetContent>
</Sheet>,
);
const trigger = screen.getByRole("button", { name: "Open drawer" });
await user.click(trigger);
await waitFor(() => expect(screen.getByRole("dialog")).toHaveFocus());
await user.tab();
expect(screen.getByRole("button", { name: "First action" })).toHaveFocus();
await user.tab();
expect(screen.getByRole("button", { name: "Last action" })).toHaveFocus();
await user.tab();
expect(screen.getByRole("button", { name: "First action" })).toHaveFocus();
await user.keyboard("{Escape}");
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
await waitFor(() => expect(trigger).toHaveFocus());
});
});
+45
View File
@@ -0,0 +1,45 @@
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import postcss from "postcss";
import tailwindcss from "tailwindcss";
import loadConfig from "tailwindcss/loadConfig";
import { describe, expect, it } from "vitest";
describe("sidebar touch hover isolation", () => {
it("gates ordinary and named-group hover CSS without gating keyboard focus", async () => {
const config = loadConfig(resolve(process.cwd(), "tailwind.config.js"));
const source = readFileSync(
resolve(process.cwd(), "src/components/ChatList.tsx"), "utf8",
);
const result = await postcss([tailwindcss({
...config,
content: [{ raw: source, extension: "tsx" }],
})]).process("@tailwind utilities;", { from: undefined });
const hoverSelectors: string[] = [];
const focusSelectors: string[] = [];
result.root.walkRules((rule) => {
if (rule.selector.includes(":hover")) {
hoverSelectors.push(rule.selector);
let parent = rule.parent;
let hoverMedia = false;
while (parent) {
if (parent.type === "atrule" && "name" in parent
&& parent.name === "media" && parent.params === "(hover: hover)") {
hoverMedia = true;
}
parent = parent.parent;
}
expect(hoverMedia, rule.selector).toBe(true);
}
if (rule.selector.includes(":focus-visible")) {
focusSelectors.push(rule.selector);
expect(rule.parent?.type).toBe("root");
}
});
expect(hoverSelectors.some((selector) => selector.includes("group:hover"))).toBe(true);
expect(hoverSelectors.some((selector) => selector.includes("group\\/tab:hover"))).toBe(true);
expect(hoverSelectors.some((selector) => selector.includes("group\\/pane:hover"))).toBe(true);
expect(focusSelectors.length).toBeGreaterThan(0);
});
});