From f0601a1da59a4f73115500ad383d2ead8a920faa Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Sun, 22 Nov 2026 19:39:21 +0800 Subject: [PATCH] fix(agents): clear the selection highlight on off-screen conversation rows Selecting new text in an agent conversation no longer leaves the previous selection highlighted on messages that were scrolled out of view. WebKit doesn't clear the highlight of rows that skip rendering off screen (content-visibility: auto), so every row now renders while the conversation holds a selection, until the frame after it is cleared. --- src/features/agents/Session.module.css | 4 ++++ src/features/agents/session/Stream.tsx | 33 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/features/agents/Session.module.css b/src/features/agents/Session.module.css index 5eadb784f0..cf1de13d6e 100644 --- a/src/features/agents/Session.module.css +++ b/src/features/agents/Session.module.css @@ -228,6 +228,10 @@ .conversationRow[data-measured] { content-visibility: auto; } +/* WebKit leaves a stale selection highlight on rows that skipped rendering (`useOffscreenRows`). */ +.stream[data-selection] > .conversationRow[data-measured] { + content-visibility: visible; +} /* Keep turns distinct and the work within a turn close to its author. Not `* + *`: a universal adjacent-sibling rule makes Chromium restyle whole sibling subtrees on every DOM insertion anywhere in the window, which costs every editor scroll frame. */ diff --git a/src/features/agents/session/Stream.tsx b/src/features/agents/session/Stream.tsx index 94e8372848..4d19cc6895 100644 --- a/src/features/agents/session/Stream.tsx +++ b/src/features/agents/session/Stream.tsx @@ -144,6 +144,39 @@ function useOffscreenRows( resize.disconnect(); }; }, [ref, visible, enabled, fitWindowWidth, fontZoom]); + // WebKit never clears the selection highlight of a row that skips rendering: once the + // selection moves, rows it covered off screen still show it when scrolled back. While the + // stream holds a selection every row renders (`data-selection`), and keeps rendering until + // the frame after it goes, when WebKit applies the change. + useEffect(() => { + const el = ref.current; + if (!el || !enabled || !visible) return; + let frame = 0; + const selected = () => { + const sel = window.getSelection(); + return !!sel && !sel.isCollapsed && sel.rangeCount > 0 && sel.getRangeAt(0).intersectsNode(el); + }; + const update = () => { + cancelAnimationFrame(frame); + if (selected()) { + el.setAttribute("data-selection", ""); + return; + } + if (!el.hasAttribute("data-selection")) return; + frame = requestAnimationFrame(() => { + frame = requestAnimationFrame(() => { + if (!selected()) el.removeAttribute("data-selection"); + }); + }); + }; + update(); + document.addEventListener("selectionchange", update); + return () => { + cancelAnimationFrame(frame); + document.removeEventListener("selectionchange", update); + el.removeAttribute("data-selection"); + }; + }, [ref, visible, enabled]); } interface StreamProps {