diff --git a/web/src/components/app/ai/TextareaAICaret.tsx b/web/src/components/app/ai/TextareaAICaret.tsx
index 5e82ed9f0..f7acb4fee 100644
--- a/web/src/components/app/ai/TextareaAICaret.tsx
+++ b/web/src/components/app/ai/TextareaAICaret.tsx
@@ -9,6 +9,7 @@
// the two are naturally exclusive: this renders only for a collapsed caret.
import React from "react";
+import useClickOutside from "@/hooks/useClickOutside";
import { createPortal } from "react-dom";
import { AnimatePresence, motion } from "framer-motion";
import {
@@ -210,29 +211,8 @@ export default function TextareaAICaret({
};
}, [caret, measure]);
- // Click-away and Escape while open.
- React.useEffect(() => {
- if (!open) return;
- const onDown = (e: MouseEvent | TouchEvent) => {
- const t = e.target as Node | null;
- if (rootRef.current?.contains(t)) return;
- closeAll();
- };
- const onKey = (e: KeyboardEvent) => {
- if (e.key === "Escape") {
- e.stopPropagation();
- closeAll();
- }
- };
- document.addEventListener("mousedown", onDown, true);
- document.addEventListener("touchstart", onDown, true);
- document.addEventListener("keydown", onKey, true);
- return () => {
- document.removeEventListener("mousedown", onDown, true);
- document.removeEventListener("touchstart", onDown, true);
- document.removeEventListener("keydown", onKey, true);
- };
- }, [open, closeAll]);
+ // Click-away and Escape while open; a press in the textarea closes it too.
+ useClickOutside(open, () => closeAll(), rootRef);
// External edits while open (typing, draft bar) close the menu.
React.useEffect(() => {
diff --git a/web/src/components/app/ai/TextareaAIEdit.tsx b/web/src/components/app/ai/TextareaAIEdit.tsx
index 82aec5889..1fe139020 100644
--- a/web/src/components/app/ai/TextareaAIEdit.tsx
+++ b/web/src/components/app/ai/TextareaAIEdit.tsx
@@ -9,6 +9,7 @@
// click-outside handlers treat it as part of the floating layer.
import React from "react";
+import useClickOutside from "@/hooks/useClickOutside";
import { createPortal } from "react-dom";
import { AnimatePresence, motion } from "framer-motion";
import { SparklesIcon } from "lucide-react";
@@ -194,23 +195,8 @@ export default function TextareaAIEdit({
};
}, [sel, syncRects]);
- // Dismiss when the user clicks anywhere outside the floating layer and
- // the textarea (like useClickOutside, plus the textarea exception).
- React.useEffect(() => {
- if (!open) return;
- const onDown = (e: MouseEvent | TouchEvent) => {
- const t = e.target as Node | null;
- if (rootRef.current?.contains(t)) return;
- if (textareaRef.current?.contains(t)) return;
- closeAll();
- };
- document.addEventListener("mousedown", onDown, true);
- document.addEventListener("touchstart", onDown, true);
- return () => {
- document.removeEventListener("mousedown", onDown, true);
- document.removeEventListener("touchstart", onDown, true);
- };
- }, [open, closeAll, textareaRef]);
+ // Click-away (the textarea counts as inside) and Escape while open.
+ useClickOutside(open, () => closeAll(), [textareaRef, rootRef]);
// External edits (typing, template insert, discard) while open: bail out
// rather than rewriting stale ranges.
@@ -219,18 +205,6 @@ export default function TextareaAIEdit({
if (expectedValue.current !== null && value !== expectedValue.current) closeAll();
}, [value, open, closeAll]);
- React.useEffect(() => {
- if (!open) return;
- const onKey = (e: KeyboardEvent) => {
- if (e.key === "Escape") {
- e.stopPropagation();
- closeAll();
- }
- };
- document.addEventListener("keydown", onKey, true);
- return () => document.removeEventListener("keydown", onKey, true);
- }, [open, closeAll]);
-
const applyResult = React.useCallback(
(
target: Selection,
diff --git a/web/src/components/app/ai/textareaAIEdit.test.tsx b/web/src/components/app/ai/textareaAIEdit.test.tsx
index c7226a158..952aa649d 100644
--- a/web/src/components/app/ai/textareaAIEdit.test.tsx
+++ b/web/src/components/app/ai/textareaAIEdit.test.tsx
@@ -5,11 +5,14 @@
import React from "react";
import { describe, it, expect, vi, beforeEach } from "vitest";
-import { render, screen, act, fireEvent } from "@testing-library/react";
+import { render, screen, act, fireEvent, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
let reply = { text: "", charged_usd: 0.01, tokens_used: 10 };
let sent: { text: string; instruction: string } | null = null;
+// When set, the rewrite stays in flight until the test answers it.
+let holdReply = false;
+let answer: (() => void) | null = null;
vi.mock("@/lib/api/hooks/app/generation/useGenerateEdit", () => ({
default: () => ({
isPending: false,
@@ -18,10 +21,16 @@ vi.mock("@/lib/api/hooks/app/generation/useGenerateEdit", () => ({
opts?: { onSuccess?: (res: unknown) => void },
) => {
sent = vars;
- opts?.onSuccess?.(reply);
+ if (holdReply) answer = () => opts?.onSuccess?.(reply);
+ else opts?.onSuccess?.(reply);
},
}),
}));
+vi.mock("@/lib/api/hooks/app/generation/useGenerateWrite", () => ({
+ default: () => ({ isPending: false, mutate: () => {} }),
+}));
+vi.mock("@/hooks/usePermission", () => ({ usePermission: () => true }));
+vi.mock("@/hooks/useAiMetered", () => ({ default: () => false }));
// The typewriter animates the rewrite in; the test wants its end state.
vi.mock("./useTypewriter", () => ({
default: () => ({
@@ -41,6 +50,7 @@ vi.mock("./textareaRange", () => ({
}));
import TextareaAIEdit from "./TextareaAIEdit";
+import TextareaAICaret from "./TextareaAICaret";
const MAX = 20;
@@ -180,3 +190,106 @@ describe("TextareaAIEdit against the composer's length cap", () => {
expect(sent?.text).toBe("two");
});
});
+
+describe("TextareaAIEdit closes like every floating layer", () => {
+ async function openPopover() {
+ const { container } = render(
+
+
+ ,
+ );
+ const ta = container.querySelector("textarea")!;
+ await act(async () => {
+ ta.focus();
+ ta.setSelectionRange(4, 7);
+ document.dispatchEvent(new Event("selectionchange"));
+ });
+ await act(async () => {
+ fireEvent.mouseDown(await screen.findByText("Edit with AI"));
+ });
+ await screen.findByPlaceholderText("Tell AI how to change it…");
+ return ta;
+ }
+ const isOpen = () => screen.queryByPlaceholderText("Tell AI how to change it…") !== null;
+
+ it("stays open on a press in the textarea and closes on a press anywhere else", async () => {
+ const ta = await openPopover();
+ await act(async () => {
+ fireEvent.pointerDown(ta);
+ });
+ expect(isOpen()).toBe(true);
+ await act(async () => {
+ fireEvent.pointerDown(document.body);
+ });
+ await waitFor(() => expect(isOpen()).toBe(false));
+ });
+
+ it("closes on Escape", async () => {
+ await openPopover();
+ await act(async () => {
+ fireEvent.keyDown(document.activeElement ?? document.body, { key: "Escape" });
+ });
+ await waitFor(() => expect(isOpen()).toBe(false));
+ });
+});
+
+describe("TextareaAIEdit beside the caret menu", () => {
+ beforeEach(() => {
+ holdReply = false;
+ answer = null;
+ });
+
+ it("closes, and drops its pending rewrite, when the caret menu opens after the selection collapses", async () => {
+ function Both() {
+ const ref = React.useRef(null);
+ const [value, setValue] = React.useState("one two three");
+ return (
+ <>
+