From a8e055495047c7134fb910bfa1b410947d5da905 Mon Sep 17 00:00:00 2001 From: 555aaditya <107977627+555aaditya@users.noreply.github.com> Date: Wed, 28 Oct 2026 14:19:44 +0530 Subject: [PATCH 1/2] feat: close the textarea AI edit and AI caret menus through useClickOutside instead of their own mousedown and Escape listeners, so they dismiss like every other floating layer --- web/src/components/app/ai/TextareaAICaret.tsx | 26 ++--------- web/src/components/app/ai/TextareaAIEdit.tsx | 32 ++------------ .../components/app/ai/textareaAIEdit.test.tsx | 44 ++++++++++++++++++- 3 files changed, 49 insertions(+), 53 deletions(-) diff --git a/web/src/components/app/ai/TextareaAICaret.tsx b/web/src/components/app/ai/TextareaAICaret.tsx index 02d6c4a3e..e3026da86 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 06566962f..726422337 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"; @@ -196,23 +197,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. @@ -221,18 +207,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 7949e89a4..fff6af52a 100644 --- a/web/src/components/app/ai/textareaAIEdit.test.tsx +++ b/web/src/components/app/ai/textareaAIEdit.test.tsx @@ -5,7 +5,7 @@ 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: "", credits_charged: 1, tokens_used: 10 }; @@ -180,3 +180,45 @@ 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)); + }); +}); From c3fd3439b8e518a25935cfc5fb34fa59545989df Mon Sep 17 00:00:00 2001 From: Matthew Meszaros Date: Sat, 7 Nov 2026 12:08:11 +0100 Subject: [PATCH 2/2] feat: close a sibling floating layer when a layer rooted on its own data-floating panel opens, so opening the textarea caret AI menu closes the selection edit menu and drops its pending rewrite, with hook and composer regressions --- .../components/app/ai/textareaAIEdit.test.tsx | 73 ++++++++++++++++++- web/src/hooks/useClickOutside.test.tsx | 37 ++++++++++ web/src/hooks/useClickOutside.ts | 3 +- 3 files changed, 111 insertions(+), 2 deletions(-) diff --git a/web/src/components/app/ai/textareaAIEdit.test.tsx b/web/src/components/app/ai/textareaAIEdit.test.tsx index a53cd542c..952aa649d 100644 --- a/web/src/components/app/ai/textareaAIEdit.test.tsx +++ b/web/src/components/app/ai/textareaAIEdit.test.tsx @@ -10,6 +10,9 @@ 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; @@ -222,3 +232,64 @@ describe("TextareaAIEdit closes like every floating layer", () => { 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 ( + <> +