diff --git a/packages/app/src/session/timeline/virtualizer.tsx b/packages/app/src/session/timeline/virtualizer.tsx index 76b98e2fa675..40b2bc760c28 100644 --- a/packages/app/src/session/timeline/virtualizer.tsx +++ b/packages/app/src/session/timeline/virtualizer.tsx @@ -134,6 +134,7 @@ export function createTimelineVirtualizer(input: Input) { const measuredElements = new WeakSet() let touchStart: number | undefined let pointerHeld = false + let upwardIntent = false let maxScroll = 0 let virtualContent: HTMLDivElement | undefined let scrollTop = 0 @@ -343,21 +344,43 @@ export function createTimelineVirtualizer(input: Input) { const bindListRoot = (root: HTMLDivElement) => { if (root === listRoot()) return - // TanStack owns anchoring; browser scroll anchoring would fight its adjustments. - root.style.overflowAnchor = "none" - setListRoot(root) - scrollTop = root.scrollTop - maxScroll = root.scrollHeight - root.clientHeight - input.setScrollRef(root) - viewportObserver?.observe(root) - settleColdBottom() + let adoptionObserver: ResizeObserver | undefined + let active = true + const bind = () => { + // Solid can mount inside an inert template document. TanStack captures its + // window on attachment and cannot install observers until the node is adopted. + if (!active || root === listRoot() || !root.ownerDocument.defaultView) return + adoptionObserver?.disconnect() + // TanStack owns anchoring; browser scroll anchoring would fight its adjustments. + root.style.overflowAnchor = "none" + setListRoot(root) + scrollTop = root.scrollTop + maxScroll = root.scrollHeight - root.clientHeight + input.setScrollRef(root) + viewportObserver?.observe(root) + settleColdBottom() + } + if (root.ownerDocument.defaultView) { + bind() + return + } + adoptionObserver = new ResizeObserver(bind) + adoptionObserver.observe(root) + onCleanup(() => { + active = false + adoptionObserver?.disconnect() + }) + queueMicrotask(bind) } // Upward input is the one intent geometry cannot recover: nudging up while still a pixel from // the end must stop following, even though the resulting position still looks like the end. const handleListWheel = (event: WheelEvent & { currentTarget: HTMLDivElement }) => { input.onUserScroll(event.target) - if (event.deltaY < 0) input.onUnpin() + if (event.deltaY < 0) { + upwardIntent = true + input.onUnpin() + } } const handleListTouchStart = (event: TouchEvent) => { @@ -371,6 +394,7 @@ export function createTimelineVirtualizer(input: Input) { // Dragging the content downward reveals earlier messages. if (current <= touchStart) return touchStart = current + upwardIntent = true input.onUnpin() } @@ -398,7 +422,10 @@ export function createTimelineVirtualizer(input: Input) { if (!isScrollKeyTarget(event.target, key)) return if (scrollKeyOwner(event.currentTarget, event.target, key) !== event.currentTarget) return input.onUserScroll(event.currentTarget) - if (upwardKeys.has(key)) input.onUnpin() + if (upwardKeys.has(key)) { + upwardIntent = true + input.onUnpin() + } } // Following resumes by arriving at the end, either by scrolling there or by content shrinking @@ -414,11 +441,39 @@ export function createTimelineVirtualizer(input: Input) { const arrived = scrollTop > previousTop + endEpsilon || maxScroll < previousMaxScroll if (maxScroll <= 1 || (atEnd && arrived)) input.onPin() else if (pointerHeld && scrollTop < previousTop - endEpsilon) input.onUnpin() + upwardIntent = false settleColdBottom() input.onScheduleScrollState(root) input.onHistoryScroll() } + let offsetCheck: number | undefined + onMount(() => { + // A remounted scroll view can reset scrollTop without dispatching scroll. Restore + // the last observed position before TanStack paints rows for the wrong offset. + offsetCheck = window.setInterval(() => { + const root = listRoot() + if (!root?.isConnected || !reportOffset) return + const observed = virtualizer.scrollOffset ?? 0 + if (Math.abs(root.scrollTop - observed) <= 1) return + if (pointerHeld && root.scrollTop < observed) input.onUnpin() + if (!pointerHeld && !upwardIntent && root.scrollTop < observed - 1 && scrollTop > 1) { + const end = root.scrollHeight - root.clientHeight + const target = maxScroll - scrollTop <= endEpsilon ? end : Math.min(scrollTop, end) + if (target > root.scrollTop + 1) root.scrollTop = target + } + reportOffset(root.scrollTop, false) + if (input.pinned()) virtualizer.scrollToEnd() + scrollTop = root.scrollTop + maxScroll = root.scrollHeight - root.clientHeight + input.onScheduleScrollState(root) + input.onHistoryScroll() + }, 100) + }) + onCleanup(() => { + if (offsetCheck !== undefined) window.clearInterval(offsetCheck) + }) + function View(props: ViewProps) { function VirtualRow(rowProps: { rowKey: string }) { let element: HTMLDivElement