fix(webui): stabilize modal dismissal and picker focus
This commit is contained in:
1 parent
883924febc
commit
56052ff583
3 files changed
+26
-3
No files matched your search
@@ -211,6 +211,7 @@ export function WorkspaceProjectPicker({ isHero, disabled, scope, defaultScope,
|
|||||||
const completionRequest = useRef(0);
|
const completionRequest = useRef(0);
|
||||||
const triggerRef = useRef<HTMLButtonElement>(null);
|
const triggerRef = useRef<HTMLButtonElement>(null);
|
||||||
const [keyboardInteraction, setKeyboardInteraction] = useState(false);
|
const [keyboardInteraction, setKeyboardInteraction] = useState(false);
|
||||||
|
const openedWithKeyboard = useRef(false);
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
const confirmRef = useRef<HTMLButtonElement>(null);
|
const confirmRef = useRef<HTMLButtonElement>(null);
|
||||||
const optionsId = useId();
|
const optionsId = useId();
|
||||||
@@ -562,7 +563,7 @@ export function WorkspaceProjectPicker({ isHero, disabled, scope, defaultScope,
|
|||||||
<Dialog open={open} onOpenChange={setOpen}>
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
<WorkspacePickerTooltip label={`${t("workspace.picker.switchDirectory")}\n${displayedScope?.project_path ?? ""}`}>
|
<WorkspacePickerTooltip label={`${t("workspace.picker.switchDirectory")}\n${displayedScope?.project_path ?? ""}`}>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
<button ref={triggerRef} onPointerDown={() => setKeyboardInteraction(false)} onKeyDown={() => setKeyboardInteraction(true)} type="button" disabled={disabled} aria-label={t("workspace.picker.switchDirectory")} className="thread-composer-workspace touch-target inline-flex h-8 min-w-0 max-w-full items-center gap-1.5 rounded-control px-2 text-[12px] font-medium text-muted-foreground outline-none transition-colors hover:bg-foreground/[0.055] hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-55">
|
<button ref={triggerRef} onClick={event => { openedWithKeyboard.current = event.detail === 0; }} onPointerDown={() => setKeyboardInteraction(false)} onKeyDown={() => setKeyboardInteraction(true)} type="button" disabled={disabled} aria-label={t("workspace.picker.switchDirectory")} className="thread-composer-workspace touch-target inline-flex h-8 min-w-0 max-w-full items-center gap-1.5 rounded-control px-2 text-[12px] font-medium text-muted-foreground outline-none transition-colors hover:bg-foreground/[0.055] hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-55">
|
||||||
<WorkspaceIcon className="h-3.5 w-3.5 shrink-0" />
|
<WorkspaceIcon className="h-3.5 w-3.5 shrink-0" />
|
||||||
<span className="min-w-0 truncate">{projectLabel}</span>
|
<span className="min-w-0 truncate">{projectLabel}</span>
|
||||||
</button>
|
</button>
|
||||||
@@ -580,7 +581,7 @@ export function WorkspaceProjectPicker({ isHero, disabled, scope, defaultScope,
|
|||||||
else if (event.altKey && event.key === "ArrowUp" && parentPath) { event.preventDefault(); navigate(parentPath); }
|
else if (event.altKey && event.key === "ArrowUp" && parentPath) { event.preventDefault(); navigate(parentPath); }
|
||||||
}}
|
}}
|
||||||
onEscapeKeyDown={event => { if (editingPath) { event.preventDefault(); cancelPathEditing(); } }}
|
onEscapeKeyDown={event => { if (editingPath) { event.preventDefault(); cancelPathEditing(); } }}
|
||||||
onCloseAutoFocus={event => { if (!keyboardInteraction) event.preventDefault(); }}>
|
onCloseAutoFocus={event => { if (!openedWithKeyboard.current) event.preventDefault(); }}>
|
||||||
<DialogTitle className="sr-only">{t("workspace.picker.title")}</DialogTitle>
|
<DialogTitle className="sr-only">{t("workspace.picker.title")}</DialogTitle>
|
||||||
<DialogDescription className="sr-only">{t("workspace.picker.description")}</DialogDescription>
|
<DialogDescription className="sr-only">{t("workspace.picker.description")}</DialogDescription>
|
||||||
<div className="flex shrink-0 items-center gap-1 bg-muted px-[var(--picker-toolbar-inset)] pt-[var(--picker-toolbar-inset)] pb-3">
|
<div className="flex shrink-0 items-center gap-1 bg-muted px-[var(--picker-toolbar-inset)] pt-[var(--picker-toolbar-inset)] pb-3">
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ export const floatingSurfaceElevationClassName =
|
|||||||
export const floatingSurfaceVisualClassName =
|
export const floatingSurfaceVisualClassName =
|
||||||
`rounded-floating p-1.5 ${floatingSurfaceElevationClassName}`;
|
`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 =
|
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 =
|
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)]";
|
"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)]";
|
||||||
|
|||||||
@@ -21,6 +21,26 @@ const directory: WorkspaceDirectoriesPayload = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
describe("Workspace project picker", () => {
|
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(<WorkspaceProjectPicker isHero scope={scope} defaultScope={scope} controls={catalog.controls} onBrowseDirectories={vi.fn().mockResolvedValue(directory)} onChange={onChange} />);
|
||||||
|
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([
|
it.each([
|
||||||
["/srv/中文 项目/src", ["/", "/srv", "/srv/中文 项目", "/srv/中文 项目/src"]],
|
["/srv/中文 项目/src", ["/", "/srv", "/srv/中文 项目", "/srv/中文 项目/src"]],
|
||||||
["C:\\Projects\\alpha", ["C:\\", "C:\\Projects", "C:\\Projects\\alpha"]],
|
["C:\\Projects\\alpha", ["C:\\", "C:\\Projects", "C:\\Projects\\alpha"]],
|
||||||
|
|||||||
Reference in new issue
Block a user