From 56052ff583855a9ee2c2078f2c6e093fa5bd0b62 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Thu, 8 Oct 2026 17:50:00 +0800 Subject: [PATCH] fix(webui): stabilize modal dismissal and picker focus --- .../components/thread/WorkspaceControls.tsx | 5 +++-- webui/src/components/ui/floating-surface.ts | 4 +++- webui/src/tests/workspace-picker.test.tsx | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/webui/src/components/thread/WorkspaceControls.tsx b/webui/src/components/thread/WorkspaceControls.tsx index 7b9bde031..79609ceb7 100644 --- a/webui/src/components/thread/WorkspaceControls.tsx +++ b/webui/src/components/thread/WorkspaceControls.tsx @@ -211,6 +211,7 @@ export function WorkspaceProjectPicker({ isHero, disabled, scope, defaultScope, const completionRequest = useRef(0); const triggerRef = useRef(null); const [keyboardInteraction, setKeyboardInteraction] = useState(false); + const openedWithKeyboard = useRef(false); const inputRef = useRef(null); const confirmRef = useRef(null); const optionsId = useId(); @@ -562,7 +563,7 @@ export function WorkspaceProjectPicker({ isHero, disabled, scope, defaultScope, - @@ -580,7 +581,7 @@ export function WorkspaceProjectPicker({ isHero, disabled, scope, defaultScope, else if (event.altKey && event.key === "ArrowUp" && parentPath) { event.preventDefault(); navigate(parentPath); } }} onEscapeKeyDown={event => { if (editingPath) { event.preventDefault(); cancelPathEditing(); } }} - onCloseAutoFocus={event => { if (!keyboardInteraction) event.preventDefault(); }}> + onCloseAutoFocus={event => { if (!openedWithKeyboard.current) event.preventDefault(); }}> {t("workspace.picker.title")} {t("workspace.picker.description")}
diff --git a/webui/src/components/ui/floating-surface.ts b/webui/src/components/ui/floating-surface.ts index d16c528eb..a28fcde1a 100644 --- a/webui/src/components/ui/floating-surface.ts +++ b/webui/src/components/ui/floating-surface.ts @@ -4,8 +4,10 @@ export const floatingSurfaceElevationClassName = export const floatingSurfaceVisualClassName = `rounded-floating p-1.5 ${floatingSurfaceElevationClassName}`; +// A content-owned exit can keep the overlay mounted after its fade finishes. +// Retain the transparent final frame until the modal's presence unmounts it. export const modalOverlayClassName = - "fixed inset-0 z-50 bg-black/25 backdrop-blur-[8px] data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0"; + "fixed inset-0 z-50 bg-black/25 backdrop-blur-[8px] data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:fill-mode-forwards data-[state=open]:fade-in-0"; export const modalSurfaceClassName = "bg-background text-foreground shadow-[0_12px_36px_rgba(15,23,42,0.12)] dark:bg-popover dark:shadow-[0_18px_44px_rgba(0,0,0,0.32)]"; diff --git a/webui/src/tests/workspace-picker.test.tsx b/webui/src/tests/workspace-picker.test.tsx index 3a4e2255d..816b2f129 100644 --- a/webui/src/tests/workspace-picker.test.tsx +++ b/webui/src/tests/workspace-picker.test.tsx @@ -21,6 +21,26 @@ const directory: WorkspaceDirectoriesPayload = { }; describe("Workspace project picker", () => { + it.each(["pointer", "keyboard"])("restores the opening focus context after %s entry and Escape", async (entry) => { + const user = userEvent.setup(); + const onChange = vi.fn(); + render(); + const trigger = screen.getByRole("button", { name: "Switch working directory" }); + if (entry === "pointer") await user.click(trigger); + else { + act(() => trigger.focus()); + await user.keyboard("{Enter}"); + } + await screen.findByRole("option", { name: "/srv/workspace/alpha" }); + await user.keyboard("{Escape}"); + await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); + // FocusScope restores focus in a task after its exit cleanup. + await act(() => new Promise(resolve => setTimeout(resolve, 0))); + if (entry === "keyboard") expect(trigger).toHaveFocus(); + else expect(trigger).not.toHaveFocus(); + expect(onChange).not.toHaveBeenCalled(); + }); + it.each([ ["/srv/中文 项目/src", ["/", "/srv", "/srv/中文 项目", "/srv/中文 项目/src"]], ["C:\\Projects\\alpha", ["C:\\", "C:\\Projects", "C:\\Projects\\alpha"]],