Skip to content

Commit 64a0d28

Browse files
fix(ui): preserve virtualizer scroll ownership
1 parent fa38efd commit 64a0d28

1 file changed

Lines changed: 9 additions & 23 deletions

File tree

‎ui/src/pages/chat/components/chat-thread.ts‎

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
// Chat-owned message thread presentation and thread-local interaction state.
22
import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice";
33
import { VirtualizerController } from "@tanstack/lit-virtual";
4-
import {
5-
defaultRangeExtractor,
6-
measureElement as measureVirtualElement,
7-
observeElementRect,
8-
} from "@tanstack/virtual-core";
4+
import { defaultRangeExtractor, observeElementRect } from "@tanstack/virtual-core";
95
import {
106
html,
117
nothing,
@@ -269,19 +265,16 @@ class ChatSessionVirtualizerHost implements ReactiveControllerHost {
269265
private readonly measureRowRefs = new Map<string, (element?: Element) => void>();
270266
private pruneDetachedRowsQueued = false;
271267
private pendingRowMeasureFrame: number | null = null;
272-
private measureRow(element: HTMLElement): void {
273-
const instance = this.virtualizerController.getVirtualizer();
274-
instance.measureElement(element);
275-
const index = instance.indexFromElement(element);
276-
// TanStack skips synchronous measurement while the user is scrolling.
277-
// Pane toggles can replace or resize rows inside that window, so seed the
278-
// cache directly after registering the ResizeObserver.
279-
instance.resizeItem(index, instance.options.measureElement(element, undefined, instance));
280-
}
281268
private measureConnectedRows(): void {
269+
// Only width invalidation owns forced DOM reads. Ordinary row refs stay on
270+
// TanStack's observer path so resizeItem cannot perturb scroll restoration.
271+
const instance = this.virtualizerController.getVirtualizer();
282272
for (const row of this.threadInnerElement?.querySelectorAll<HTMLElement>(".chat-virtual-row") ??
283273
[]) {
284-
this.measureRow(row);
274+
instance.resizeItem(
275+
instance.indexFromElement(row),
276+
row[instance.options.horizontal ? "offsetWidth" : "offsetHeight"],
277+
);
285278
}
286279
}
287280
private queueConnectedRowMeasure(): void {
@@ -298,7 +291,7 @@ class ChatSessionVirtualizerHost implements ReactiveControllerHost {
298291
if (!callback) {
299292
callback = (element?: Element) => {
300293
if (element instanceof HTMLElement) {
301-
this.measureRow(element);
294+
this.virtualizerController.getVirtualizer().measureElement(element);
302295
return;
303296
}
304297
// Re-stamps (e.g. the chat<->dashboard face switch) re-invoke each
@@ -338,13 +331,6 @@ class ChatSessionVirtualizerHost implements ReactiveControllerHost {
338331
getScrollElement: () => this.scrollElement,
339332
estimateSize: () => CHAT_TRANSCRIPT_ESTIMATED_ROW_PX,
340333
getItemKey: () => "",
341-
// Ref callbacks and explicit pane-width resets need the current wrapped
342-
// height immediately. TanStack's default no-entry path reuses its cache,
343-
// which still contains the pre-reset estimate and can overlap rows.
344-
measureElement: (element, entry, instance) =>
345-
entry
346-
? measureVirtualElement(element, entry, instance)
347-
: element[instance.options.horizontal ? "offsetWidth" : "offsetHeight"],
348334
initialRect: initialTranscriptRect(host),
349335
initialOffset: initialOffset ?? Number.MAX_SAFE_INTEGER,
350336
scrollMargin: initialTranscriptScrollMargin(host),

0 commit comments

Comments
 (0)