From 13319dfe15110c38a3ed98fec03b0d762ef6458d Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Mon, 21 Sep 2026 12:59:15 +0800 Subject: [PATCH] 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 Co-authored-by: moran --- webui/src/components/ChatList.tsx | 30 ++--- webui/src/components/ui/sheet.tsx | 19 ++- webui/src/tests/app-layout.test.tsx | 36 ++++++ webui/src/tests/sheet-autofocus.test.tsx | 141 ++++++++++++++++++++++ webui/src/tests/sidebar-hover-css.test.ts | 45 +++++++ webui/tailwind.config.js | 11 +- 6 files changed, 265 insertions(+), 17 deletions(-) create mode 100644 webui/src/tests/sheet-autofocus.test.tsx create mode 100644 webui/src/tests/sidebar-hover-css.test.ts diff --git a/webui/src/components/ChatList.tsx b/webui/src/components/ChatList.tsx index b38aa9dac..b015d5147 100644 --- a/webui/src/components/ChatList.tsx +++ b/webui/src/components/ChatList.tsx @@ -962,7 +962,7 @@ export const ChatList = memo(function ChatList({ compact ? "min-h-7" : "min-h-8", topicActive ? "text-sidebar-foreground" - : "text-sidebar-content hover:text-sidebar-accent-foreground", + : "text-sidebar-content media-hover:hover:text-sidebar-accent-foreground", deleteSelectionMode && (tabSelected || tabPartiallySelected) && "bg-sidebar-accent/55 text-sidebar-accent-foreground", )} @@ -1049,7 +1049,7 @@ export const ChatList = memo(function ChatList({ {t("chat.showMore", { count: hiddenSessionCount })} @@ -1160,7 +1160,7 @@ export const ChatList = memo(function ChatList({ aria-label={t("chat.cancelSelection", { defaultValue: "Cancel selection", })} - className="inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-full text-sidebar-muted-foreground transition-colors hover:bg-accent/60 hover:text-foreground" + className="inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-full text-sidebar-muted-foreground transition-colors media-hover:hover:bg-accent/60 media-hover:hover:text-foreground" > @@ -1174,7 +1174,7 @@ export const ChatList = memo(function ChatList({ type="button" disabled={selectedDeleteKeys.size === 0} onClick={confirmDeleteSelection} - className="inline-flex h-8 shrink-0 items-center gap-1.5 rounded-full bg-destructive px-3 text-[12px] font-semibold text-destructive-foreground transition-colors hover:bg-destructive/90 disabled:pointer-events-none disabled:opacity-40" + className="inline-flex h-8 shrink-0 items-center gap-1.5 rounded-full bg-destructive px-3 text-[12px] font-semibold text-destructive-foreground transition-colors media-hover:hover:bg-destructive/90 disabled:pointer-events-none disabled:opacity-40" > {t("chat.deleteSelected", { defaultValue: "Delete" })} @@ -1246,7 +1246,7 @@ function WorkbenchTabHeader({ className={cn( "relative inline-flex h-6 w-3.5 shrink-0 items-center justify-center rounded-md before:absolute before:-inset-x-1 before:inset-y-0", "text-sidebar-muted-foreground transition-[background-color,color,transform] duration-150 ease-out", - "hover:bg-sidebar-accent hover:text-sidebar-foreground active:scale-[0.96]", + "media-hover:hover:bg-sidebar-accent media-hover:hover:text-sidebar-foreground active:scale-[0.96]", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/60", "motion-reduce:transition-none motion-reduce:active:scale-100", )} @@ -1297,7 +1297,7 @@ function WorkbenchTabHeader({ className={cn( "inline-flex h-6 w-6 shrink-0 items-center justify-center rounded-md", "text-sidebar-muted-foreground opacity-0 transition-opacity", - "hover:bg-sidebar-accent hover:text-sidebar-foreground group-hover/tab:opacity-100", + "media-hover:hover:bg-sidebar-accent media-hover:hover:text-sidebar-foreground media-hover:group-hover/tab:opacity-100", "focus-visible:opacity-100 data-[state=open]:opacity-100", )} aria-label={t("chat.actions", { title })} @@ -1441,7 +1441,7 @@ function ActivePaneRows({ compact ? "min-h-7" : "min-h-8", active ? "text-sidebar-foreground" - : "text-sidebar-content hover:text-sidebar-accent-foreground", + : "text-sidebar-content media-hover:hover:text-sidebar-accent-foreground", deleteSelectionMode && selected && "bg-sidebar-accent/55 text-sidebar-accent-foreground", )} @@ -1492,7 +1492,7 @@ function ActivePaneRows({ @@ -1726,7 +1726,7 @@ function ProjectGroupHeader({ aria-expanded={!collapsed} onClick={onToggle} className={cn( - "flex min-w-0 flex-1 items-center gap-2 rounded-lg py-1 text-left transition-colors hover:text-sidebar-foreground", + "flex min-w-0 flex-1 items-center gap-2 rounded-lg py-1 text-left transition-colors media-hover:hover:text-sidebar-foreground", active ? "font-medium text-sidebar-foreground" : "font-normal", )} > @@ -1751,7 +1751,7 @@ function ProjectGroupHeader({ className={cn( "relative inline-flex h-6 w-3.5 shrink-0 items-center justify-center rounded-md before:absolute before:-inset-x-1 before:inset-y-0", "text-sidebar-muted-foreground transition-[background-color,color,transform] duration-150 ease-out", - "hover:bg-sidebar-accent hover:text-sidebar-foreground active:scale-[0.96]", + "media-hover:hover:bg-sidebar-accent media-hover:hover:text-sidebar-foreground active:scale-[0.96]", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/60", "motion-reduce:transition-none motion-reduce:active:scale-100", )} @@ -1788,7 +1788,7 @@ function ProjectGroupHeader({ {folded diff --git a/webui/src/components/ui/sheet.tsx b/webui/src/components/ui/sheet.tsx index dcafcb179..8af54f504 100644 --- a/webui/src/components/ui/sheet.tsx +++ b/webui/src/components/ui/sheet.tsx @@ -74,16 +74,33 @@ const SheetContent = React.forwardRef< children, closeButtonClassName, showCloseButton = true, + onOpenAutoFocus, ...props }, ref, ) => { const { t } = useTranslation(); + const contentNode = React.useRef(null); + const contentRef = React.useCallback( + (node: HTMLDivElement | null) => { + contentNode.current = node; + if (typeof ref === "function") ref(node); + else if (ref) ref.current = node; + }, + [ref], + ); return ( { + if (onOpenAutoFocus) onOpenAutoFocus(event); + else { + event.preventDefault(); + contentNode.current?.focus({ preventScroll: true }); + } + }} className={cn( sheetVariants({ side }), "data-[state=open]:animate-in data-[state=closed]:animate-out", diff --git a/webui/src/tests/app-layout.test.tsx b/webui/src/tests/app-layout.test.tsx index 856ce4dd7..17d1992bf 100644 --- a/webui/src/tests/app-layout.test.tsx +++ b/webui/src/tests/app-layout.test.tsx @@ -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(); + 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 = [ { diff --git a/webui/src/tests/sheet-autofocus.test.tsx b/webui/src/tests/sheet-autofocus.test.tsx new file mode 100644 index 000000000..3bfb0adc4 --- /dev/null +++ b/webui/src/tests/sheet-autofocus.test.tsx @@ -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; +}) { + return ( + + + + Navigation + + + + + Search tip + + + + + ); +} + +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(); + 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(); + await screen.findByRole("dialog"); + await waitFor(() => expect(onOpenAutoFocus).toHaveBeenCalled()); + }); + + it("keeps the caller's explicit focus target", async () => { + render( { + 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(); + await waitFor(() => expect(screen.getByRole("button", { name: "search" })).toHaveFocus()); + }); + + it("preserves object refs while focusing the container", async () => { + const ref = createRef(); + const { unmount } = render(); + 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(); + 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( + + Open drawer + + Navigation + + + + , + ); + 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()); + }); +}); diff --git a/webui/src/tests/sidebar-hover-css.test.ts b/webui/src/tests/sidebar-hover-css.test.ts new file mode 100644 index 000000000..ac89cc5e9 --- /dev/null +++ b/webui/src/tests/sidebar-hover-css.test.ts @@ -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); + }); +}); diff --git a/webui/tailwind.config.js b/webui/tailwind.config.js index 89769ca77..c6549ca26 100644 --- a/webui/tailwind.config.js +++ b/webui/tailwind.config.js @@ -1,5 +1,6 @@ import animate from "tailwindcss-animate"; import typography from "@tailwindcss/typography"; +import plugin from "tailwindcss/plugin"; /** @type {import('tailwindcss').Config} */ export default { @@ -124,5 +125,13 @@ export default { }, }, }, - plugins: [animate, typography], + plugins: [ + animate, + typography, + plugin(({ addVariant }) => { + // Revealing controls on emulated hover can consume the first Safari + // tap. Keep these sidebar hover effects on hover-capable pointers. + addVariant("media-hover", "@media (hover: hover)"); + }), + ], };