TypeScript
fix(web): restore palette focus when no composer is mounted (#18850)
Alex Southwell01d2aa87f5dd
diff --git a/apps/web/src/components/CommandPalette.focus.test.tsx b/apps/web/src/components/CommandPalette.focus.test.tsxnew file mode 100644index 00000000000..641a79c328a--- /dev/null+++ b/apps/web/src/components/CommandPalette.focus.test.tsx@@ -0,0 +1,208 @@+// @vitest-environment jsdom++import { act, useImperativeHandle, useRef } from "react";+import { createRoot, type Root } from "react-dom/client";+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";+import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";+import { CommandPalette } from "./CommandPalette";+import { useComposerHandleContext } from "../composerHandleContext";+import { openCommandPalette } from "../commandPaletteBus";+import { primaryServerKeybindingsAtom } from "../state/server";++vi.mock("@tanstack/react-router", () => ({+ useNavigate: () => vi.fn(),+ useParams: () => null,+ useLocation: () => "/settings",+}));+vi.mock("@effect/atom-react", async (importOriginal) => {+ const original = await importOriginal<typeof import("@effect/atom-react")>();+ return {+ ...original,+ useAtomValue: (atom: unknown) =>+ atom === primaryServerKeybindingsAtom ? DEFAULT_RESOLVED_KEYBINDINGS : [],+ };+});+vi.mock("../hooks/useTheme", () => ({+ useTheme: () => ({ theme: "default", resolvedTheme: "light", appearanceMode: "light" }),+}));+vi.mock("../hooks/useCustomThemes", () => ({ useCustomThemes: () => [] }));+vi.mock("../hooks/useEnvironmentTheme", () => ({ useEnvironmentThemeDefinitions: () => [] }));+vi.mock("../hooks/useSettings", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../hooks/useSettings")>()),+ useClientSettings: () => ({}),+}));+vi.mock("../hooks/useScratchProject", () => ({+ useScratchProject: () => ({ scratchEnvironmentId: () => null }),+}));+vi.mock("../hooks/useNewProject", () => ({ useNewProject: () => vi.fn() }));+vi.mock("../hooks/useHandleNewThread", () => ({ useHandleNewThread: () => ({}) }));+vi.mock("../hooks/useOpenPanelPullRequestUrl", () => ({ useOpenPanelPullRequestUrl: () => null }));+vi.mock("../connection/useDesktopLocalBootstraps", () => ({ useDesktopLocalBootstraps: () => [] }));+vi.mock("../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() }));+vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() }));+vi.mock("../state/environments", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../state/environments")>()),+ useEnvironments: () => ({ environments: [] }),+ usePrimaryEnvironmentId: () => null,+}));+vi.mock("../state/entities", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../state/entities")>()),+ useProjects: () => [],+ useThreadShells: () => [],+ useServerConfigs: () => new Map(),+}));+vi.mock("../state/queries", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../state/queries")>()),+ useThreadSearch: () => ({ matches: [] }),+}));+vi.mock("../state/session", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../state/session")>()),+ useEnvironmentScope: () => false,+}));+vi.mock("../state/query", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../state/query")>()),+ useEnvironmentQuery: () => ({}),+}));+vi.mock("./settings/useAvailableSettingsSearchItems", () => ({+ useAvailableSettingsSearchItems: () => [],+}));++let root: Root;+let container: HTMLDivElement;++beforeEach(() => {+ vi.useFakeTimers();+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);+ vi.stubGlobal(+ "ResizeObserver",+ class {+ observe() {}+ disconnect() {}+ unobserve() {}+ },+ );+ Object.defineProperty(Element.prototype, "getAnimations", {+ configurable: true,+ value: () => [],+ });+ container = document.createElement("div");+ document.body.append(container);+ root = createRoot(container);+});++afterEach(async () => {+ await act(async () => {+ root.unmount();+ await vi.runOnlyPendingTimersAsync();+ });+ container.remove();+ vi.unstubAllGlobals();+ vi.useRealTimers();+});++async function flushFocus() {+ await act(() => vi.advanceTimersByTimeAsync(0));+ await act(() => vi.advanceTimersToNextFrame());+}++async function dismissPalette() {+ await act(() =>+ document.activeElement?.dispatchEvent(+ new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }),+ ),+ );+ await flushFocus();+ expect(document.querySelector("[data-command-palette]")).toBeNull();+}++function Composer() {+ const handle = useComposerHandleContext();+ const input = useRef<HTMLTextAreaElement>(null);+ useImperativeHandle(+ handle,+ () =>+ ({+ focusAtEnd: () => {+ input.current?.focus();+ input.current?.setSelectionRange(input.current.value.length, input.current.value.length);+ },+ }) as NonNullable<NonNullable<typeof handle>["current"]>,+ );+ return <textarea ref={input} aria-label="Composer" defaultValue="Draft prompt" />;+}++describe("command palette focus restoration", () => {+ it("returns to the previously focused Settings field when no composer is mounted", async () => {+ await act(() =>+ root.render(+ <CommandPalette>+ <input aria-label="Settings field" />+ </CommandPalette>,+ ),+ );+ const field = container.querySelector("input")!;+ await act(() => field.focus());+ await act(() => openCommandPalette());+ await flushFocus();+ expect(document.activeElement?.closest("[data-command-palette]")).not.toBeNull();+ await dismissPalette();+ expect(document.activeElement).toBe(field);+ });++ it("focuses the replacement command input after leaving the file picker", async () => {+ await act(() =>+ root.render(+ <CommandPalette>+ <input aria-label="Settings field" />+ </CommandPalette>,+ ),+ );+ const field = container.querySelector("input")!;+ await act(() => field.focus());+ await act(() =>+ field.dispatchEvent(+ new KeyboardEvent("keydown", {+ key: "p",+ ctrlKey: true,+ bubbles: true,+ cancelable: true,+ }),+ ),+ );+ await flushFocus();+ expect(document.querySelector('[data-palette-mode="files"]')).not.toBeNull();+ await act(() =>+ document.activeElement?.dispatchEvent(+ new KeyboardEvent("keydown", {+ key: "Escape",+ bubbles: true,+ cancelable: true,+ }),+ ),+ );+ await flushFocus();+ expect(document.activeElement?.closest('[data-palette-mode="command"]')).not.toBeNull();+ expect(document.activeElement?.tagName).toBe("INPUT");+ await dismissPalette();+ expect(document.activeElement).toBe(field);+ });++ it("focuses the mounted composer at the end instead of the previous field", async () => {+ await act(() =>+ root.render(+ <CommandPalette>+ <input aria-label="Previous field" />+ <Composer />+ </CommandPalette>,+ ),+ );+ await act(() => container.querySelector("input")!.focus());+ await act(() => openCommandPalette());+ await flushFocus();+ expect(document.activeElement?.closest("[data-command-palette]")).not.toBeNull();+ await dismissPalette();+ const composer = container.querySelector("textarea")!;+ expect(document.activeElement).toBe(composer);+ expect(composer.selectionStart).toBe(composer.value.length);+ });+});diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsxindex 1859018f3a6..a7b93881e8b 100644--- a/apps/web/src/components/CommandPalette.tsx+++ b/apps/web/src/components/CommandPalette.tsx@@ -668,7 +668,9 @@ function CommandPaletteDialog(props: {data-palette-mode={props.mode}data-testid="command-palette"finalFocus={() => {- composerHandleRef?.current?.focusAtEnd();+ const composer = composerHandleRef?.current;+ if (!composer) return true;+ composer.focusAtEnd();return false;}}onBackdropPointerDown={() => {diff --git a/apps/web/src/components/CommandPaletteContent.tsx b/apps/web/src/components/CommandPaletteContent.tsxindex 3097a99131c..f0bc1b31e0f 100644--- a/apps/web/src/components/CommandPaletteContent.tsx+++ b/apps/web/src/components/CommandPaletteContent.tsx@@ -1,5 +1,5 @@import { ArrowDownIcon, ArrowUpIcon } from "lucide-react";-import { type ComponentProps, type ReactNode, useLayoutEffect, useRef } from "react";+import { type ComponentProps, type ReactNode, useEffect, useRef } from "react";import { Command, CommandFooter, CommandInput, CommandPanel } from "./ui/command";import { Kbd, KbdGroup } from "./ui/kbd";@@ -32,16 +32,18 @@ export function CommandPaletteContent({// Direct-open flows replace the initial palette view after the dialog has// already moved focus. Reclaim it when the replacement input mounts so- // typing cannot continue in the composer behind the modal.- useLayoutEffect(() => {- inputRef.current?.focus();+ // typing cannot continue in the composer behind the modal. Defer until the+ // dialog has recorded the previous focus target for restoration on close.+ useEffect(() => {+ const frame = requestAnimationFrame(() => inputRef.current?.focus());+ return () => cancelAnimationFrame(frame);}, []);return (<div className="contents" data-testid={testId}><Command {...commandProps}><div className="relative">- <CommandInput {...inputProps} ref={inputRef} />+ <CommandInput autoFocus={false} {...inputProps} ref={inputRef} />{inputAccessory}</div><CommandPanel