Skip to content
TypeScript

fix(web): keep typed Settings text when Esc closes Settings (#25131)

Aatman Jain (AJ)f8278d851609
diff --git a/apps/web/src/components/AppRouteModal.browser.test.tsx b/apps/web/src/components/AppRouteModal.browser.test.tsx
new file mode 100644
index 00000000000..cb31eeca45a
--- /dev/null
+++ b/apps/web/src/components/AppRouteModal.browser.test.tsx
@@ -0,0 +1,27 @@
+import { describe, expect, it, vi } from "vite-plus/test";
+import { page, userEvent } from "vite-plus/test/browser";
+
+import { render } from "../../test/render";
+import { useCommitOnBlur } from "../hooks/useCommitOnBlur";
+import { AppRouteModal } from "./AppRouteModal";
+
+function CommitOnBlurField({ onCommit }: { onCommit: (next: string) => void }) {
+ return <input aria-label="Instructions" {...useCommitOnBlur("", onCommit)} />;
+}
+
+describe("AppRouteModal", () => {
+ it("commits a focused field when Esc closes it", async () => {
+ const onCommit = vi.fn();
+ await using _screen = await render(
+ <AppRouteModal label="Settings">
+ <CommitOnBlurField onCommit={onCommit} />
+ </AppRouteModal>,
+ { router: "/settings/general" },
+ );
+
+ await page.getByRole("textbox", { name: "Instructions" }).fill("Keep titles short");
+ await userEvent.keyboard("{Escape}");
+
+ expect(onCommit).toHaveBeenCalledWith("Keep titles short");
+ });
+});
diff --git a/apps/web/src/components/AppRouteModal.tsx b/apps/web/src/components/AppRouteModal.tsx
index 89e676da2e3..55d7c4112f9 100644
--- a/apps/web/src/components/AppRouteModal.tsx
+++ b/apps/web/src/components/AppRouteModal.tsx
@@ -27,7 +27,12 @@ export function AppRouteModal({ label, children }: { label: string; children: Re
// Portaled layers outside this tree (toasts, Clerk, confirm dialogs) are not backdrop presses.
disablePointerDismissal
onOpenChange={(nextOpen) => {
- if (!nextOpen) setOpen(false);
+ if (nextOpen) return;
+ // Closing unmounts a focused field without a blur, so commit-on-blur rows would
+ // drop what was typed. Blur first so they commit.
+ const focused = document.activeElement;
+ if (focused instanceof HTMLElement && popupRef.current?.contains(focused)) focused.blur();
+ setOpen(false);
}}
onOpenChangeComplete={(nextOpen) => {
if (!nextOpen) void closeAppModal();