From d68f92029e47374d794275533ba0163f75091e2a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 8 Nov 2026 02:18:17 +0000 Subject: [PATCH 1/9] fix(web): chat stays pinned when the terminal drawer opens during a turn With a turn running, the timeline follows the end with an animated maintainScrollAtEnd so streamed growth scrolls smoothly. LegendList used that one animated flag for every trigger, so a viewport shrink (opening the terminal drawer) also eased to the end over ~550 ms instead of pinning on the next frame. Extend the existing @legendapp/list 3.3.5 patch (react.js/react.mjs, the web entry points): viewport layout pins are always instant, and an instant pin is not deferred by an in-flight animated one, since an instant scroll write halts a smooth scroll. dataChange and itemLayout pins keep their smooth behaviour. Co-Authored-By: Claude Opus 5.5 --- patches/@legendapp__list@3.3.5.patch | 190 ++++++++++++++++++++++----- pnpm-lock.yaml | 8 +- 2 files changed, 158 insertions(+), 40 deletions(-) diff --git a/patches/@legendapp__list@3.3.5.patch b/patches/@legendapp__list@3.3.5.patch index 42572476c6c9a..3c4338db21ec4 100644 --- a/patches/@legendapp__list@3.3.5.patch +++ b/patches/@legendapp__list@3.3.5.patch @@ -1619,10 +1619,60 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..d7cbe1cb43449cc29822b2646deabbdd } } diff --git a/react.js b/react.js -index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b7dee26dc 100644 +index 914d2dafaafa001c9ab6791a0a0581659295e333..4b4663075faf6c463468c2bf023f1f687d40cfd2 100644 --- a/react.js +++ b/react.js -@@ -1908,6 +1908,10 @@ function updateScroll(ctx, newScroll, forceUpdate, options) { +@@ -1426,7 +1426,7 @@ function listenForScrollEnd(ctx, params) { + } + + // src/core/doMaintainScrollAtEnd.ts +-function doMaintainScrollAtEnd(ctx) { ++function doMaintainScrollAtEnd(ctx, instant = false) { + const state = ctx.state; + const { + didContainersLayout, +@@ -1446,9 +1446,12 @@ function doMaintainScrollAtEnd(ctx) { + if (contentSize < state.scrollLength) { + state.scroll = 0; + } +- if (!state.maintainingScrollAtEnd) { +- const pendingState = maintainScrollAtEnd.animated ? "pending-animated" : "pending-instant"; +- const activeState = maintainScrollAtEnd.animated ? "animated" : "instant"; ++ const animated = !instant && maintainScrollAtEnd.animated; ++ const isAnimating = state.maintainingScrollAtEnd === "pending-animated" || state.maintainingScrollAtEnd === "animated"; ++ // An instant scroll write halts an in-flight smooth one, so it need not wait for it. ++ if (!state.maintainingScrollAtEnd || !animated && isAnimating) { ++ const pendingState = animated ? "pending-animated" : "pending-instant"; ++ const activeState = animated ? "animated" : "instant"; + const scrollAtRequest = state.scroll; + state.maintainingScrollAtEnd = pendingState; + requestAnimationFrame(() => { +@@ -1462,13 +1465,13 @@ function doMaintainScrollAtEnd(ctx) { + const logicalEndOffset = getLogicalHorizontalMaxOffset(state, currentContentSize); + const nativeOffset = toNativeHorizontalOffset(state, logicalEndOffset, currentContentSize); + scroller == null ? void 0 : scroller.scrollTo({ +- animated: maintainScrollAtEnd.animated, ++ animated, + x: nativeOffset, + y: 0 + }); + } else { + scroller == null ? void 0 : scroller.scrollToEnd({ +- animated: maintainScrollAtEnd.animated ++ animated + }); + } + setTimeout( +@@ -1480,7 +1483,7 @@ function doMaintainScrollAtEnd(ctx) { + } + } + }, +- maintainScrollAtEnd.animated ? 500 : 0 ++ animated ? 500 : 0 + ); + } else if (state.maintainingScrollAtEnd === pendingState) { + state.maintainingScrollAtEnd = void 0; +@@ -1908,6 +1911,10 @@ function updateScroll(ctx, newScroll, forceUpdate, options) { state.scrollTime = currentTime; const scrollDelta = Math.abs(newScroll - prevScroll); const isUserScrollEvent = !!(options == null ? void 0 : options.fromNativeScrollEvent) && scrollDelta > 0.1 && !adjustChanged && scrollingTo === void 0 && !state.pendingNativeMVCPAdjust; @@ -1633,7 +1683,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b const allowedEdge = isUserScrollEvent ? beginReachedEdgeUserScroll(ctx, newScroll - prevScroll) : void 0; const didResolvePendingNativeMVCPAdjust = resolvePendingNativeMVCPAdjust(ctx, newScroll); const scrollLength = state.scrollLength; -@@ -3257,6 +3261,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3257,6 +3264,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { const itemKey = (_a3 = state.idCache[itemIndex]) != null ? _a3 : getId(state, itemIndex); const updateLayout = (_b = options == null ? void 0 : options.updateLayout) != null ? _b : true; let didChangePosition = false; @@ -1641,7 +1691,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b let didRefreshData = false; if (updateLayout) { const positionValue = positions[itemIndex]; -@@ -3286,6 +3291,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3286,6 +3294,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { const prevIndex = peek$(ctx, `containerItemIndex${containerIndex}`); if (prevIndex !== itemIndex) { set$(ctx, `containerItemIndex${containerIndex}`, itemIndex); @@ -1649,7 +1699,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b } const prevData = peek$(ctx, `containerItemData${containerIndex}`); const updateData = () => { -@@ -3324,7 +3330,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3324,7 +3333,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { } } } @@ -1658,7 +1708,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b } // src/core/prepareColumnStartState.ts -@@ -3542,7 +3548,11 @@ function updateItemPositions(ctx, dataChanged, { +@@ -3542,7 +3551,11 @@ function updateItemPositions(ctx, dataChanged, { } if (shouldOptimize && breakAt === void 0 && !scrollingTo && !dataChanged && currentRowTop > maxVisibleArea) { const itemsPerRow = hasColumns ? numColumns : 1; @@ -1671,7 +1721,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b } const id = (_e = idCache[i]) != null ? _e : getId(state, i); let span = 1; -@@ -4243,6 +4253,7 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4243,6 +4256,7 @@ function calculateItemsInView(ctx, params = {}) { } let totalSize = getContentSize(ctx); let changedContainerIds; @@ -1679,7 +1729,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b const topPad = peek$(ctx, "stylePaddingTop") + peek$(ctx, "alignItemsAtEndPadding") + peek$(ctx, "headerSize"); const numColumns = peek$(ctx, "numColumns"); const speed = (_b = params.scrollVelocity) != null ? _b : getScrollVelocity(state); -@@ -4545,6 +4556,9 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4545,6 +4559,9 @@ function calculateItemsInView(ctx, params = {}) { const containerIndex = allocation.containerIndex; const id = (_o = idCache[i]) != null ? _o : getId(state, i); const oldKey = peek$(ctx, `containerItemKey${containerIndex}`); @@ -1689,7 +1739,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b if (oldKey && oldKey !== id) { containerItemKeys.delete(oldKey); } -@@ -4629,10 +4643,12 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4629,10 +4646,12 @@ function calculateItemsInView(ctx, params = {}) { } else { const itemIndex = indexByKey.get(itemKey); if (itemIndex !== void 0) { @@ -1704,7 +1754,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b } } } -@@ -4642,6 +4658,9 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4642,6 +4661,9 @@ function calculateItemsInView(ctx, params = {}) { if (didChangePositions) { set$(ctx, "lastPositionUpdate", Date.now()); } @@ -1714,7 +1764,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b if (suppressInitialScrollSideEffects) { evaluateBootstrapInitialScroll(ctx); return; -@@ -4702,7 +4721,8 @@ function maybeUpdateAnchoredEndSpace(ctx) { +@@ -4702,7 +4724,8 @@ function maybeUpdateAnchoredEndSpace(ctx) { contentBelowAnchor = Math.max(0, contentBelowAnchor - ctx.scrollAxisGap); contentBelowAnchor += (ctx.values.get("footerSize") || 0) + getStylePaddingEnd(state.props); isReady = !hasUnknownTailSize; @@ -1724,7 +1774,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b } else if (anchorIndex >= 0) { isReady = false; } -@@ -4722,6 +4742,12 @@ function maybeUpdateAnchoredEndSpace(ctx) { +@@ -4722,6 +4745,12 @@ function maybeUpdateAnchoredEndSpace(ctx) { updateScroll(ctx, state.scroll, true, { markHasScrolled: false }); } (_b = anchoredEndSpace == null ? void 0 : anchoredEndSpace.onReady) == null ? void 0 : _b.call(anchoredEndSpace, { anchorIndex: nextAnchorIndex, anchorKey: nextAnchorKey, size: nextSize }); @@ -1737,7 +1787,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b } return nextSize; } -@@ -4956,7 +4982,8 @@ function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { +@@ -4956,7 +4985,8 @@ function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { const itemKey = peek$(ctx, `containerItemKey${containerId}`); const element = viewRef == null ? void 0 : viewRef.current; if (itemKey !== void 0 && element) { @@ -1747,7 +1797,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b setContainerLayoutBaseline(element, rect); measurements.push({ containerId, -@@ -4978,12 +5005,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo +@@ -4978,12 +5008,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo }) { const ctx = useStateContext(); const [containerLayoutEpoch] = useArr$(["containerLayoutEpoch"]); @@ -1779,7 +1829,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b } }, [ctx, containerLayoutEpoch]); return children; -@@ -5651,6 +5692,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) { +@@ -5651,6 +5695,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) { }); // src/utils/reordering.ts @@ -1798,7 +1848,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b function sortDOMElements(container, indexByElement) { const elements = Array.from(container.children); if (elements.length <= 1) return elements; -@@ -5690,9 +5743,9 @@ function sortDOMElements(container, indexByElement) { +@@ -5690,9 +5746,9 @@ function sortDOMElements(container, indexByElement) { } } if (nextStableElement) { @@ -1810,7 +1860,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b } } } -@@ -5734,37 +5787,55 @@ function findLIS(arr) { +@@ -5734,37 +5790,55 @@ function findLIS(arr) { } // src/hooks/useDOMOrder.ts @@ -1892,7 +1942,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b } function useFreshDataTransitionVisibility(readyToRender, transitionEpoch) { const [completedTransitionEpoch, setCompletedTransitionEpoch] = React3.useState(transitionEpoch); -@@ -6446,8 +6517,8 @@ function ScrollAdjust() { +@@ -6446,8 +6520,8 @@ function ScrollAdjust() { window.getComputedStyle(contentNode)[axis.paddingEndProp] ); const temporaryPaddingEnd = `${(currentPaddingEnd || 0) + pad}px`; @@ -1902,11 +1952,70 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..8c320b0dfb3ea60ef145274b37b1297b void contentNode.offsetHeight; scrollBy(); if (resetPaddingRafRef.current !== void 0) { +@@ -6938,7 +7012,7 @@ function handleLayout(ctx, layoutParam, setCanRender) { + set$(ctx, "scrollSize", { height: layout.height, width: layout.width }); + } + if (maintainScrollAtEnd == null ? void 0 : maintainScrollAtEnd.onLayout) { +- doMaintainScrollAtEnd(ctx); ++ doMaintainScrollAtEnd(ctx, true); + } + checkThresholds(ctx); + if (state) { diff --git a/react.mjs b/react.mjs -index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff754f1c899 100644 +index 95465f2ab89ce41a10553f58af83618f7310e83c..06a9d04a623b59058de1767436ea57dcf561f70e 100644 --- a/react.mjs +++ b/react.mjs -@@ -1887,6 +1887,10 @@ function updateScroll(ctx, newScroll, forceUpdate, options) { +@@ -1405,7 +1405,7 @@ function listenForScrollEnd(ctx, params) { + } + + // src/core/doMaintainScrollAtEnd.ts +-function doMaintainScrollAtEnd(ctx) { ++function doMaintainScrollAtEnd(ctx, instant = false) { + const state = ctx.state; + const { + didContainersLayout, +@@ -1425,9 +1425,12 @@ function doMaintainScrollAtEnd(ctx) { + if (contentSize < state.scrollLength) { + state.scroll = 0; + } +- if (!state.maintainingScrollAtEnd) { +- const pendingState = maintainScrollAtEnd.animated ? "pending-animated" : "pending-instant"; +- const activeState = maintainScrollAtEnd.animated ? "animated" : "instant"; ++ const animated = !instant && maintainScrollAtEnd.animated; ++ const isAnimating = state.maintainingScrollAtEnd === "pending-animated" || state.maintainingScrollAtEnd === "animated"; ++ // An instant scroll write halts an in-flight smooth one, so it need not wait for it. ++ if (!state.maintainingScrollAtEnd || !animated && isAnimating) { ++ const pendingState = animated ? "pending-animated" : "pending-instant"; ++ const activeState = animated ? "animated" : "instant"; + const scrollAtRequest = state.scroll; + state.maintainingScrollAtEnd = pendingState; + requestAnimationFrame(() => { +@@ -1441,13 +1444,13 @@ function doMaintainScrollAtEnd(ctx) { + const logicalEndOffset = getLogicalHorizontalMaxOffset(state, currentContentSize); + const nativeOffset = toNativeHorizontalOffset(state, logicalEndOffset, currentContentSize); + scroller == null ? void 0 : scroller.scrollTo({ +- animated: maintainScrollAtEnd.animated, ++ animated, + x: nativeOffset, + y: 0 + }); + } else { + scroller == null ? void 0 : scroller.scrollToEnd({ +- animated: maintainScrollAtEnd.animated ++ animated + }); + } + setTimeout( +@@ -1459,7 +1462,7 @@ function doMaintainScrollAtEnd(ctx) { + } + } + }, +- maintainScrollAtEnd.animated ? 500 : 0 ++ animated ? 500 : 0 + ); + } else if (state.maintainingScrollAtEnd === pendingState) { + state.maintainingScrollAtEnd = void 0; +@@ -1887,6 +1890,10 @@ function updateScroll(ctx, newScroll, forceUpdate, options) { state.scrollTime = currentTime; const scrollDelta = Math.abs(newScroll - prevScroll); const isUserScrollEvent = !!(options == null ? void 0 : options.fromNativeScrollEvent) && scrollDelta > 0.1 && !adjustChanged && scrollingTo === void 0 && !state.pendingNativeMVCPAdjust; @@ -1917,7 +2026,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 const allowedEdge = isUserScrollEvent ? beginReachedEdgeUserScroll(ctx, newScroll - prevScroll) : void 0; const didResolvePendingNativeMVCPAdjust = resolvePendingNativeMVCPAdjust(ctx, newScroll); const scrollLength = state.scrollLength; -@@ -3236,6 +3240,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3236,6 +3243,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { const itemKey = (_a3 = state.idCache[itemIndex]) != null ? _a3 : getId(state, itemIndex); const updateLayout = (_b = options == null ? void 0 : options.updateLayout) != null ? _b : true; let didChangePosition = false; @@ -1925,7 +2034,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 let didRefreshData = false; if (updateLayout) { const positionValue = positions[itemIndex]; -@@ -3265,6 +3270,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3265,6 +3273,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { const prevIndex = peek$(ctx, `containerItemIndex${containerIndex}`); if (prevIndex !== itemIndex) { set$(ctx, `containerItemIndex${containerIndex}`, itemIndex); @@ -1933,7 +2042,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 } const prevData = peek$(ctx, `containerItemData${containerIndex}`); const updateData = () => { -@@ -3303,7 +3309,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3303,7 +3312,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { } } } @@ -1942,7 +2051,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 } // src/core/prepareColumnStartState.ts -@@ -3521,7 +3527,11 @@ function updateItemPositions(ctx, dataChanged, { +@@ -3521,7 +3530,11 @@ function updateItemPositions(ctx, dataChanged, { } if (shouldOptimize && breakAt === void 0 && !scrollingTo && !dataChanged && currentRowTop > maxVisibleArea) { const itemsPerRow = hasColumns ? numColumns : 1; @@ -1955,7 +2064,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 } const id = (_e = idCache[i]) != null ? _e : getId(state, i); let span = 1; -@@ -4222,6 +4232,7 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4222,6 +4235,7 @@ function calculateItemsInView(ctx, params = {}) { } let totalSize = getContentSize(ctx); let changedContainerIds; @@ -1963,7 +2072,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 const topPad = peek$(ctx, "stylePaddingTop") + peek$(ctx, "alignItemsAtEndPadding") + peek$(ctx, "headerSize"); const numColumns = peek$(ctx, "numColumns"); const speed = (_b = params.scrollVelocity) != null ? _b : getScrollVelocity(state); -@@ -4524,6 +4535,9 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4524,6 +4538,9 @@ function calculateItemsInView(ctx, params = {}) { const containerIndex = allocation.containerIndex; const id = (_o = idCache[i]) != null ? _o : getId(state, i); const oldKey = peek$(ctx, `containerItemKey${containerIndex}`); @@ -1973,7 +2082,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 if (oldKey && oldKey !== id) { containerItemKeys.delete(oldKey); } -@@ -4608,10 +4622,12 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4608,10 +4625,12 @@ function calculateItemsInView(ctx, params = {}) { } else { const itemIndex = indexByKey.get(itemKey); if (itemIndex !== void 0) { @@ -1988,7 +2097,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 } } } -@@ -4621,6 +4637,9 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4621,6 +4640,9 @@ function calculateItemsInView(ctx, params = {}) { if (didChangePositions) { set$(ctx, "lastPositionUpdate", Date.now()); } @@ -1998,7 +2107,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 if (suppressInitialScrollSideEffects) { evaluateBootstrapInitialScroll(ctx); return; -@@ -4681,7 +4700,8 @@ function maybeUpdateAnchoredEndSpace(ctx) { +@@ -4681,7 +4703,8 @@ function maybeUpdateAnchoredEndSpace(ctx) { contentBelowAnchor = Math.max(0, contentBelowAnchor - ctx.scrollAxisGap); contentBelowAnchor += (ctx.values.get("footerSize") || 0) + getStylePaddingEnd(state.props); isReady = !hasUnknownTailSize; @@ -2008,7 +2117,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 } else if (anchorIndex >= 0) { isReady = false; } -@@ -4701,6 +4721,12 @@ function maybeUpdateAnchoredEndSpace(ctx) { +@@ -4701,6 +4724,12 @@ function maybeUpdateAnchoredEndSpace(ctx) { updateScroll(ctx, state.scroll, true, { markHasScrolled: false }); } (_b = anchoredEndSpace == null ? void 0 : anchoredEndSpace.onReady) == null ? void 0 : _b.call(anchoredEndSpace, { anchorIndex: nextAnchorIndex, anchorKey: nextAnchorKey, size: nextSize }); @@ -2021,7 +2130,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 } return nextSize; } -@@ -4935,7 +4961,8 @@ function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { +@@ -4935,7 +4964,8 @@ function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { const itemKey = peek$(ctx, `containerItemKey${containerId}`); const element = viewRef == null ? void 0 : viewRef.current; if (itemKey !== void 0 && element) { @@ -2031,7 +2140,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 setContainerLayoutBaseline(element, rect); measurements.push({ containerId, -@@ -4957,12 +4984,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo +@@ -4957,12 +4987,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo }) { const ctx = useStateContext(); const [containerLayoutEpoch] = useArr$(["containerLayoutEpoch"]); @@ -2063,7 +2172,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 } }, [ctx, containerLayoutEpoch]); return children; -@@ -5630,6 +5671,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) { +@@ -5630,6 +5674,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) { }); // src/utils/reordering.ts @@ -2082,7 +2191,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 function sortDOMElements(container, indexByElement) { const elements = Array.from(container.children); if (elements.length <= 1) return elements; -@@ -5669,9 +5722,9 @@ function sortDOMElements(container, indexByElement) { +@@ -5669,9 +5725,9 @@ function sortDOMElements(container, indexByElement) { } } if (nextStableElement) { @@ -2094,7 +2203,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 } } } -@@ -5713,37 +5766,55 @@ function findLIS(arr) { +@@ -5713,37 +5769,55 @@ function findLIS(arr) { } // src/hooks/useDOMOrder.ts @@ -2176,7 +2285,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 } function useFreshDataTransitionVisibility(readyToRender, transitionEpoch) { const [completedTransitionEpoch, setCompletedTransitionEpoch] = useState(transitionEpoch); -@@ -6425,8 +6496,8 @@ function ScrollAdjust() { +@@ -6425,8 +6499,8 @@ function ScrollAdjust() { window.getComputedStyle(contentNode)[axis.paddingEndProp] ); const temporaryPaddingEnd = `${(currentPaddingEnd || 0) + pad}px`; @@ -2186,6 +2295,15 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..252a9d5b23e271c089d30c357a78dff7 void contentNode.offsetHeight; scrollBy(); if (resetPaddingRafRef.current !== void 0) { +@@ -6917,7 +6991,7 @@ function handleLayout(ctx, layoutParam, setCanRender) { + set$(ctx, "scrollSize", { height: layout.height, width: layout.width }); + } + if (maintainScrollAtEnd == null ? void 0 : maintainScrollAtEnd.onLayout) { +- doMaintainScrollAtEnd(ctx); ++ doMaintainScrollAtEnd(ctx, true); + } + checkThresholds(ctx); + if (state) { diff --git a/reanimated.d.ts b/reanimated.d.ts index e5043320700b12f34f4c0babbc341f85ca8135c1..e8cbd461325a2e2e9ffd61de26a85ae4d108555c 100644 --- a/reanimated.d.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 697191c410823..ff313bb014c97 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -361,7 +361,7 @@ patchedDependencies: '@expo/metro-config@58.0.14': 90fa980e8b6480631fb2fbb04559834e3c612db40c550cd828f11be297d8d760 '@expo/ui@58.0.20': 64559cb60723e19a8a72256d4c78d1315d937a7af3b32d19860ea596f88736ce '@ff-labs/fff-node@0.11.0': cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c - '@legendapp/list@3.3.5': 6cbdacd8c380d9a5afb9f73d4198dc2cc44be6ef28a955b1183c781469307896 + '@legendapp/list@3.3.5': 6fb57f5fb719b4bc788bb73525099ef15387e904408d034c6f70212fa3d5a580 '@muse-code/sdk@1.4.6': 7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101 '@pierre/diffs@1.6.1': 322ac5f5db8b5715189c010802047f8235c3a45d8ed5ca0ba60fac266e2eba34 '@react-native-ai/apple@0.12.0': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d @@ -545,7 +545,7 @@ importers: version: 58.0.20(patch_hash=64559cb60723e19a8a72256d4c78d1315d937a7af3b32d19860ea596f88736ce)(@babel/core@7.29.7(supports-color@8.1.1))(expo@58.0.14)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)(supports-color@8.1.1))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@legendapp/list': specifier: 3.3.5 - version: 3.3.5(patch_hash=6cbdacd8c380d9a5afb9f73d4198dc2cc44be6ef28a955b1183c781469307896)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 3.3.5(patch_hash=6fb57f5fb719b4bc788bb73525099ef15387e904408d034c6f70212fa3d5a580)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@material/material-color-utilities': specifier: 0.3.0 version: 0.3.0 @@ -1060,7 +1060,7 @@ importers: version: 5.3.0 '@legendapp/list': specifier: 3.3.5 - version: 3.3.5(patch_hash=6cbdacd8c380d9a5afb9f73d4198dc2cc44be6ef28a955b1183c781469307896)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 3.3.5(patch_hash=6fb57f5fb719b4bc788bb73525099ef15387e904408d034c6f70212fa3d5a580)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@noble/hashes': specifier: 'catalog:' version: 1.8.0 @@ -14899,7 +14899,7 @@ snapshots: '@koromix/koffi-win32-x64@3.3.6': optional: true - '@legendapp/list@3.3.5(patch_hash=6cbdacd8c380d9a5afb9f73d4198dc2cc44be6ef28a955b1183c781469307896)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)': + '@legendapp/list@3.3.5(patch_hash=6fb57f5fb719b4bc788bb73525099ef15387e904408d034c6f70212fa3d5a580)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)': dependencies: react: 19.3.0 use-sync-external-store: 1.6.0(react@19.3.0) From 096f18acf9e1fac54b70709a385f37759fb662e7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 8 Nov 2026 02:32:20 +0000 Subject: [PATCH 2/9] fix(web): chat stays pinned on every frame of a viewport resize during a turn The previous fix made viewport layout pins instant, but a layout pin that arrived while an instant pin was finishing (its zero-delay cleanup still pending) was coalesced and replayed without the instant flag. During a continuous resize such as the terminal drawer opening, pins alternated between instant and smooth, so the end of the chat lagged every other frame and finished with a smooth ease. In the @legendapp/list 3.3.5 patch (react.js/react.mjs), a layout pin on an animated list now schedules its own instant pin unless one is already queued, and never queues a smooth replay. Content-growth pins stay smooth, and lists without an animated maintainScrollAtEnd keep upstream behaviour. Co-Authored-By: Claude Opus 5.5 --- patches/@legendapp__list@3.3.5.patch | 36 ++++++-- pnpm-lock.yaml | 8 +- ...legend-list-maintain-scroll-at-end.test.ts | 91 +++++++++++++++++++ 3 files changed, 121 insertions(+), 14 deletions(-) create mode 100644 scripts/legend-list-maintain-scroll-at-end.test.ts diff --git a/patches/@legendapp__list@3.3.5.patch b/patches/@legendapp__list@3.3.5.patch index 3c4338db21ec4..27240693ee8f5 100644 --- a/patches/@legendapp__list@3.3.5.patch +++ b/patches/@legendapp__list@3.3.5.patch @@ -1619,7 +1619,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..d7cbe1cb43449cc29822b2646deabbdd } } diff --git a/react.js b/react.js -index 914d2dafaafa001c9ab6791a0a0581659295e333..4b4663075faf6c463468c2bf023f1f687d40cfd2 100644 +index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c10b1c56f9 100644 --- a/react.js +++ b/react.js @@ -1426,7 +1426,7 @@ function listenForScrollEnd(ctx, params) { @@ -1638,10 +1638,10 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..4b4663075faf6c463468c2bf023f1f68 - if (!state.maintainingScrollAtEnd) { - const pendingState = maintainScrollAtEnd.animated ? "pending-animated" : "pending-instant"; - const activeState = maintainScrollAtEnd.animated ? "animated" : "instant"; ++ const jump = instant && !!maintainScrollAtEnd.animated; + const animated = !instant && maintainScrollAtEnd.animated; -+ const isAnimating = state.maintainingScrollAtEnd === "pending-animated" || state.maintainingScrollAtEnd === "animated"; -+ // An instant scroll write halts an in-flight smooth one, so it need not wait for it. -+ if (!state.maintainingScrollAtEnd || !animated && isAnimating) { ++ // An instant scroll write halts an in-flight one; a queued instant rAF already reaches the end. ++ if (!state.maintainingScrollAtEnd || jump && state.maintainingScrollAtEnd !== "pending-instant") { + const pendingState = animated ? "pending-animated" : "pending-instant"; + const activeState = animated ? "animated" : "instant"; const scrollAtRequest = state.scroll; @@ -1663,7 +1663,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..4b4663075faf6c463468c2bf023f1f68 }); } setTimeout( -@@ -1480,7 +1483,7 @@ function doMaintainScrollAtEnd(ctx) { +@@ -1480,14 +1483,14 @@ function doMaintainScrollAtEnd(ctx) { } } }, @@ -1672,6 +1672,14 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..4b4663075faf6c463468c2bf023f1f68 ); } else if (state.maintainingScrollAtEnd === pendingState) { state.maintainingScrollAtEnd = void 0; + state.pendingMaintainScrollAtEnd = false; + } + }); +- } else { ++ } else if (!jump) { + state.pendingMaintainScrollAtEnd = true; + } + return true; @@ -1908,6 +1911,10 @@ function updateScroll(ctx, newScroll, forceUpdate, options) { state.scrollTime = currentTime; const scrollDelta = Math.abs(newScroll - prevScroll); @@ -1962,7 +1970,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..4b4663075faf6c463468c2bf023f1f68 checkThresholds(ctx); if (state) { diff --git a/react.mjs b/react.mjs -index 95465f2ab89ce41a10553f58af83618f7310e83c..06a9d04a623b59058de1767436ea57dcf561f70e 100644 +index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d5598027c 100644 --- a/react.mjs +++ b/react.mjs @@ -1405,7 +1405,7 @@ function listenForScrollEnd(ctx, params) { @@ -1981,10 +1989,10 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..06a9d04a623b59058de1767436ea57dc - if (!state.maintainingScrollAtEnd) { - const pendingState = maintainScrollAtEnd.animated ? "pending-animated" : "pending-instant"; - const activeState = maintainScrollAtEnd.animated ? "animated" : "instant"; ++ const jump = instant && !!maintainScrollAtEnd.animated; + const animated = !instant && maintainScrollAtEnd.animated; -+ const isAnimating = state.maintainingScrollAtEnd === "pending-animated" || state.maintainingScrollAtEnd === "animated"; -+ // An instant scroll write halts an in-flight smooth one, so it need not wait for it. -+ if (!state.maintainingScrollAtEnd || !animated && isAnimating) { ++ // An instant scroll write halts an in-flight one; a queued instant rAF already reaches the end. ++ if (!state.maintainingScrollAtEnd || jump && state.maintainingScrollAtEnd !== "pending-instant") { + const pendingState = animated ? "pending-animated" : "pending-instant"; + const activeState = animated ? "animated" : "instant"; const scrollAtRequest = state.scroll; @@ -2006,7 +2014,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..06a9d04a623b59058de1767436ea57dc }); } setTimeout( -@@ -1459,7 +1462,7 @@ function doMaintainScrollAtEnd(ctx) { +@@ -1459,14 +1462,14 @@ function doMaintainScrollAtEnd(ctx) { } } }, @@ -2015,6 +2023,14 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..06a9d04a623b59058de1767436ea57dc ); } else if (state.maintainingScrollAtEnd === pendingState) { state.maintainingScrollAtEnd = void 0; + state.pendingMaintainScrollAtEnd = false; + } + }); +- } else { ++ } else if (!jump) { + state.pendingMaintainScrollAtEnd = true; + } + return true; @@ -1887,6 +1890,10 @@ function updateScroll(ctx, newScroll, forceUpdate, options) { state.scrollTime = currentTime; const scrollDelta = Math.abs(newScroll - prevScroll); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ff313bb014c97..2a76be73c32c4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -361,7 +361,7 @@ patchedDependencies: '@expo/metro-config@58.0.14': 90fa980e8b6480631fb2fbb04559834e3c612db40c550cd828f11be297d8d760 '@expo/ui@58.0.20': 64559cb60723e19a8a72256d4c78d1315d937a7af3b32d19860ea596f88736ce '@ff-labs/fff-node@0.11.0': cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c - '@legendapp/list@3.3.5': 6fb57f5fb719b4bc788bb73525099ef15387e904408d034c6f70212fa3d5a580 + '@legendapp/list@3.3.5': c46f08070768056035524bc6b8b5eab092efb9fe2deacb9bf5c309c0bc0fb538 '@muse-code/sdk@1.4.6': 7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101 '@pierre/diffs@1.6.1': 322ac5f5db8b5715189c010802047f8235c3a45d8ed5ca0ba60fac266e2eba34 '@react-native-ai/apple@0.12.0': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d @@ -545,7 +545,7 @@ importers: version: 58.0.20(patch_hash=64559cb60723e19a8a72256d4c78d1315d937a7af3b32d19860ea596f88736ce)(@babel/core@7.29.7(supports-color@8.1.1))(expo@58.0.14)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)(supports-color@8.1.1))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@legendapp/list': specifier: 3.3.5 - version: 3.3.5(patch_hash=6fb57f5fb719b4bc788bb73525099ef15387e904408d034c6f70212fa3d5a580)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 3.3.5(patch_hash=c46f08070768056035524bc6b8b5eab092efb9fe2deacb9bf5c309c0bc0fb538)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@material/material-color-utilities': specifier: 0.3.0 version: 0.3.0 @@ -1060,7 +1060,7 @@ importers: version: 5.3.0 '@legendapp/list': specifier: 3.3.5 - version: 3.3.5(patch_hash=6fb57f5fb719b4bc788bb73525099ef15387e904408d034c6f70212fa3d5a580)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 3.3.5(patch_hash=c46f08070768056035524bc6b8b5eab092efb9fe2deacb9bf5c309c0bc0fb538)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@noble/hashes': specifier: 'catalog:' version: 1.8.0 @@ -14899,7 +14899,7 @@ snapshots: '@koromix/koffi-win32-x64@3.3.6': optional: true - '@legendapp/list@3.3.5(patch_hash=6fb57f5fb719b4bc788bb73525099ef15387e904408d034c6f70212fa3d5a580)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)': + '@legendapp/list@3.3.5(patch_hash=c46f08070768056035524bc6b8b5eab092efb9fe2deacb9bf5c309c0bc0fb538)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)': dependencies: react: 19.3.0 use-sync-external-store: 1.6.0(react@19.3.0) diff --git a/scripts/legend-list-maintain-scroll-at-end.test.ts b/scripts/legend-list-maintain-scroll-at-end.test.ts new file mode 100644 index 0000000000000..991bccc5bae19 --- /dev/null +++ b/scripts/legend-list-maintain-scroll-at-end.test.ts @@ -0,0 +1,91 @@ +// @effect-diagnostics nodeBuiltinImport:off - reads the installed web JS bundle for synchronous VM tests. +import * as NodeFS from "node:fs"; +import * as NodeVM from "node:vm"; +import { describe, expect, it, vi } from "vite-plus/test"; + +const FRAME_MS = 16; + +function createList(bundle: string) { + const source = NodeFS.readFileSync( + new URL(`../apps/web/node_modules/@legendapp/list/${bundle}`, import.meta.url), + "utf8", + ); + const slice = (start: string, end: string) => + source.slice(source.indexOf(start), source.indexOf(end)); + const maintain = slice("// src/core/doMaintainScrollAtEnd.ts", "// src/utils/requestAdjust.ts"); + const layout = slice("// src/core/handleLayout.ts", "// src/core/onScroll.ts"); + const frames: Array<() => void> = []; + let tasks: Array<{ at: number; run: () => void }> = []; + let now = 0; + const scrollToEnd = vi.fn(); + const state = { + props: { maintainScrollAtEnd: { animated: true, onLayout: true, onDataChange: true } }, + didContainersLayout: true, + pendingNativeMVCPAdjust: undefined, + refScroller: { current: { scrollToEnd } }, + scroll: 2000, + scrollLength: 800, + otherAxisSize: 600, + lastLayout: { x: 0, y: 0, width: 600, height: 800 }, + }; + const ctx = { state }; + const api = NodeVM.runInNewContext( + `${maintain}\n${layout}\n({ doMaintainScrollAtEnd, handleLayout })`, + { + requestAnimationFrame: (callback: () => void) => frames.push(callback), + setTimeout: (run: () => void, delay: number) => tasks.push({ at: now + delay, run }), + peek$: (_ctx: unknown, key: string) => key === "isWithinMaintainScrollAtEndThreshold", + set$: vi.fn(), + getContentSize: () => 3000, + isHorizontalRTL: () => false, + updateContentMetricsState: vi.fn(), + doInitialAllocateContainers: vi.fn(), + calculateItemsInView: vi.fn(), + checkThresholds: vi.fn(), + IS_DEV: false, + }, + ) as { + doMaintainScrollAtEnd: (context: typeof ctx) => boolean; + handleLayout: (context: typeof ctx, layout: object, setCanRender: () => void) => void; + }; + const runDueTasks = () => { + const due = tasks.filter((task) => task.at <= now); + tasks = tasks.filter((task) => task.at > now); + for (const task of due) task.run(); + }; + return { + scrollToEnd, + dataChange: () => api.doMaintainScrollAtEnd(ctx), + // Browser order per frame: rAF callbacks, then ResizeObserver layout, then queued tasks. + frame(viewportHeight?: number) { + now += FRAME_MS; + for (const callback of frames.splice(0)) callback(); + if (viewportHeight !== undefined) { + api.handleLayout(ctx, { x: 0, y: 0, width: 600, height: viewportHeight }, () => {}); + } + runDueTasks(); + }, + }; +} + +describe.each(["react.mjs", "react.js"])("maintainScrollAtEnd while animated (%s)", (bundle) => { + it("pins every frame of a continuous viewport resize instantly", () => { + const list = createList(bundle); + for (let index = 1; index <= 12; index++) list.frame(800 - index * 20); + list.frame(); + expect(list.scrollToEnd.mock.calls).toEqual( + Array.from({ length: 12 }, () => [{ animated: false }]), + ); + }); + + it("keeps a content-growth pin smooth and pins the following resize instantly", () => { + const list = createList(bundle); + list.dataChange(); + for (let index = 1; index <= 6; index++) list.frame(800 - index * 20); + list.frame(); + expect(list.scrollToEnd.mock.calls).toEqual([ + [{ animated: true }], + ...Array.from({ length: 6 }, () => [{ animated: false }]), + ]); + }); +}); From 4c2d323a957d0bb59fc1ed8dcd53f5c75a5a87bf Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 8 Nov 2026 04:06:56 +0000 Subject: [PATCH 3/9] fix(web): chat keeps following a stream after the terminal drawer opens When the drawer opened while the timeline was trailing a stream by a line or more, the gap after the shrink could exceed the new viewport height, which is the maintain-at-end threshold. The instant layout pin then gave up at rAF time if a smooth scroll had moved the list since the request. Even when the pin landed, the list's scroll offset stayed stale until the next scroll event, so a size adjustment in that window scrolled the view back. Either way the list ended beyond the threshold and stopped following until the drawer closed. In the @legendapp/list 3.3.5 patch (react.js/react.mjs), a layout pin on an animated list is cancelled only when follow is released (the prop turns off), not by the resize-shifted threshold, and it syncs the list's scroll offset to the pinned position right after writing it. Co-Authored-By: Claude Opus 5.5 --- patches/@legendapp__list@3.3.5.patch | 116 +++++++++++------- pnpm-lock.yaml | 8 +- ...legend-list-maintain-scroll-at-end.test.ts | 52 +++++++- 3 files changed, 124 insertions(+), 52 deletions(-) diff --git a/patches/@legendapp__list@3.3.5.patch b/patches/@legendapp__list@3.3.5.patch index 27240693ee8f5..3d41a86e95dee 100644 --- a/patches/@legendapp__list@3.3.5.patch +++ b/patches/@legendapp__list@3.3.5.patch @@ -1619,7 +1619,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..d7cbe1cb43449cc29822b2646deabbdd } } diff --git a/react.js b/react.js -index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c10b1c56f9 100644 +index 914d2dafaafa001c9ab6791a0a0581659295e333..5f9d41106323b4faf39ddea19125407b4f60bbf8 100644 --- a/react.js +++ b/react.js @@ -1426,7 +1426,7 @@ function listenForScrollEnd(ctx, params) { @@ -1631,7 +1631,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 const state = ctx.state; const { didContainersLayout, -@@ -1446,9 +1446,12 @@ function doMaintainScrollAtEnd(ctx) { +@@ -1446,15 +1446,19 @@ function doMaintainScrollAtEnd(ctx) { if (contentSize < state.scrollLength) { state.scroll = 0; } @@ -1647,7 +1647,15 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 const scrollAtRequest = state.scroll; state.maintainingScrollAtEnd = pendingState; requestAnimationFrame(() => { -@@ -1462,13 +1465,13 @@ function doMaintainScrollAtEnd(ctx) { + const isStillWithinThreshold = peek$(ctx, "isWithinMaintainScrollAtEndThreshold"); + const didScrollSinceRequest = state.scroll !== scrollAtRequest; +- if (isStillWithinThreshold || !didScrollSinceRequest) { ++ // A resize moves the end itself, so only a release of follow cancels a layout pin. ++ if (jump ? state.props.maintainScrollAtEnd : isStillWithinThreshold || !didScrollSinceRequest) { + state.maintainingScrollAtEnd = activeState; + const scroller = refScroller.current; + if (state.props.horizontal && isHorizontalRTL(state)) { +@@ -1462,14 +1466,19 @@ function doMaintainScrollAtEnd(ctx) { const logicalEndOffset = getLogicalHorizontalMaxOffset(state, currentContentSize); const nativeOffset = toNativeHorizontalOffset(state, logicalEndOffset, currentContentSize); scroller == null ? void 0 : scroller.scrollTo({ @@ -1661,9 +1669,15 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 - animated: maintainScrollAtEnd.animated + animated }); ++ // MVCP adjustments rebase on state.scroll, which the scroll event would only update a frame later. ++ const pinnedOffset = jump ? scroller == null ? void 0 : scroller.getCurrentScrollOffset == null ? void 0 : scroller.getCurrentScrollOffset() : void 0; ++ if (pinnedOffset !== void 0) { ++ updateScroll(ctx, pinnedOffset, true, { markHasScrolled: false }); ++ } } setTimeout( -@@ -1480,14 +1483,14 @@ function doMaintainScrollAtEnd(ctx) { + () => { +@@ -1480,14 +1489,14 @@ function doMaintainScrollAtEnd(ctx) { } } }, @@ -1680,7 +1694,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 state.pendingMaintainScrollAtEnd = true; } return true; -@@ -1908,6 +1911,10 @@ function updateScroll(ctx, newScroll, forceUpdate, options) { +@@ -1908,6 +1917,10 @@ function updateScroll(ctx, newScroll, forceUpdate, options) { state.scrollTime = currentTime; const scrollDelta = Math.abs(newScroll - prevScroll); const isUserScrollEvent = !!(options == null ? void 0 : options.fromNativeScrollEvent) && scrollDelta > 0.1 && !adjustChanged && scrollingTo === void 0 && !state.pendingNativeMVCPAdjust; @@ -1691,7 +1705,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 const allowedEdge = isUserScrollEvent ? beginReachedEdgeUserScroll(ctx, newScroll - prevScroll) : void 0; const didResolvePendingNativeMVCPAdjust = resolvePendingNativeMVCPAdjust(ctx, newScroll); const scrollLength = state.scrollLength; -@@ -3257,6 +3264,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3257,6 +3270,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { const itemKey = (_a3 = state.idCache[itemIndex]) != null ? _a3 : getId(state, itemIndex); const updateLayout = (_b = options == null ? void 0 : options.updateLayout) != null ? _b : true; let didChangePosition = false; @@ -1699,7 +1713,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 let didRefreshData = false; if (updateLayout) { const positionValue = positions[itemIndex]; -@@ -3286,6 +3294,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3286,6 +3300,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { const prevIndex = peek$(ctx, `containerItemIndex${containerIndex}`); if (prevIndex !== itemIndex) { set$(ctx, `containerItemIndex${containerIndex}`, itemIndex); @@ -1707,7 +1721,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 } const prevData = peek$(ctx, `containerItemData${containerIndex}`); const updateData = () => { -@@ -3324,7 +3333,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3324,7 +3339,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { } } } @@ -1716,7 +1730,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 } // src/core/prepareColumnStartState.ts -@@ -3542,7 +3551,11 @@ function updateItemPositions(ctx, dataChanged, { +@@ -3542,7 +3557,11 @@ function updateItemPositions(ctx, dataChanged, { } if (shouldOptimize && breakAt === void 0 && !scrollingTo && !dataChanged && currentRowTop > maxVisibleArea) { const itemsPerRow = hasColumns ? numColumns : 1; @@ -1729,7 +1743,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 } const id = (_e = idCache[i]) != null ? _e : getId(state, i); let span = 1; -@@ -4243,6 +4256,7 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4243,6 +4262,7 @@ function calculateItemsInView(ctx, params = {}) { } let totalSize = getContentSize(ctx); let changedContainerIds; @@ -1737,7 +1751,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 const topPad = peek$(ctx, "stylePaddingTop") + peek$(ctx, "alignItemsAtEndPadding") + peek$(ctx, "headerSize"); const numColumns = peek$(ctx, "numColumns"); const speed = (_b = params.scrollVelocity) != null ? _b : getScrollVelocity(state); -@@ -4545,6 +4559,9 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4545,6 +4565,9 @@ function calculateItemsInView(ctx, params = {}) { const containerIndex = allocation.containerIndex; const id = (_o = idCache[i]) != null ? _o : getId(state, i); const oldKey = peek$(ctx, `containerItemKey${containerIndex}`); @@ -1747,7 +1761,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 if (oldKey && oldKey !== id) { containerItemKeys.delete(oldKey); } -@@ -4629,10 +4646,12 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4629,10 +4652,12 @@ function calculateItemsInView(ctx, params = {}) { } else { const itemIndex = indexByKey.get(itemKey); if (itemIndex !== void 0) { @@ -1762,7 +1776,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 } } } -@@ -4642,6 +4661,9 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4642,6 +4667,9 @@ function calculateItemsInView(ctx, params = {}) { if (didChangePositions) { set$(ctx, "lastPositionUpdate", Date.now()); } @@ -1772,7 +1786,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 if (suppressInitialScrollSideEffects) { evaluateBootstrapInitialScroll(ctx); return; -@@ -4702,7 +4724,8 @@ function maybeUpdateAnchoredEndSpace(ctx) { +@@ -4702,7 +4730,8 @@ function maybeUpdateAnchoredEndSpace(ctx) { contentBelowAnchor = Math.max(0, contentBelowAnchor - ctx.scrollAxisGap); contentBelowAnchor += (ctx.values.get("footerSize") || 0) + getStylePaddingEnd(state.props); isReady = !hasUnknownTailSize; @@ -1782,7 +1796,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 } else if (anchorIndex >= 0) { isReady = false; } -@@ -4722,6 +4745,12 @@ function maybeUpdateAnchoredEndSpace(ctx) { +@@ -4722,6 +4751,12 @@ function maybeUpdateAnchoredEndSpace(ctx) { updateScroll(ctx, state.scroll, true, { markHasScrolled: false }); } (_b = anchoredEndSpace == null ? void 0 : anchoredEndSpace.onReady) == null ? void 0 : _b.call(anchoredEndSpace, { anchorIndex: nextAnchorIndex, anchorKey: nextAnchorKey, size: nextSize }); @@ -1795,7 +1809,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 } return nextSize; } -@@ -4956,7 +4985,8 @@ function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { +@@ -4956,7 +4991,8 @@ function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { const itemKey = peek$(ctx, `containerItemKey${containerId}`); const element = viewRef == null ? void 0 : viewRef.current; if (itemKey !== void 0 && element) { @@ -1805,7 +1819,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 setContainerLayoutBaseline(element, rect); measurements.push({ containerId, -@@ -4978,12 +5008,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo +@@ -4978,12 +5014,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo }) { const ctx = useStateContext(); const [containerLayoutEpoch] = useArr$(["containerLayoutEpoch"]); @@ -1837,7 +1851,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 } }, [ctx, containerLayoutEpoch]); return children; -@@ -5651,6 +5695,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) { +@@ -5651,6 +5701,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) { }); // src/utils/reordering.ts @@ -1856,7 +1870,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 function sortDOMElements(container, indexByElement) { const elements = Array.from(container.children); if (elements.length <= 1) return elements; -@@ -5690,9 +5746,9 @@ function sortDOMElements(container, indexByElement) { +@@ -5690,9 +5752,9 @@ function sortDOMElements(container, indexByElement) { } } if (nextStableElement) { @@ -1868,7 +1882,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 } } } -@@ -5734,37 +5790,55 @@ function findLIS(arr) { +@@ -5734,37 +5796,55 @@ function findLIS(arr) { } // src/hooks/useDOMOrder.ts @@ -1950,7 +1964,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 } function useFreshDataTransitionVisibility(readyToRender, transitionEpoch) { const [completedTransitionEpoch, setCompletedTransitionEpoch] = React3.useState(transitionEpoch); -@@ -6446,8 +6520,8 @@ function ScrollAdjust() { +@@ -6446,8 +6526,8 @@ function ScrollAdjust() { window.getComputedStyle(contentNode)[axis.paddingEndProp] ); const temporaryPaddingEnd = `${(currentPaddingEnd || 0) + pad}px`; @@ -1960,7 +1974,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 void contentNode.offsetHeight; scrollBy(); if (resetPaddingRafRef.current !== void 0) { -@@ -6938,7 +7012,7 @@ function handleLayout(ctx, layoutParam, setCanRender) { +@@ -6938,7 +7018,7 @@ function handleLayout(ctx, layoutParam, setCanRender) { set$(ctx, "scrollSize", { height: layout.height, width: layout.width }); } if (maintainScrollAtEnd == null ? void 0 : maintainScrollAtEnd.onLayout) { @@ -1970,7 +1984,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..157ff0ced154c482b8808ca4d647d8c1 checkThresholds(ctx); if (state) { diff --git a/react.mjs b/react.mjs -index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d5598027c 100644 +index 95465f2ab89ce41a10553f58af83618f7310e83c..f5773da48a984b25d6ab0fd2c76512a60f8b460f 100644 --- a/react.mjs +++ b/react.mjs @@ -1405,7 +1405,7 @@ function listenForScrollEnd(ctx, params) { @@ -1982,7 +1996,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d const state = ctx.state; const { didContainersLayout, -@@ -1425,9 +1425,12 @@ function doMaintainScrollAtEnd(ctx) { +@@ -1425,15 +1425,19 @@ function doMaintainScrollAtEnd(ctx) { if (contentSize < state.scrollLength) { state.scroll = 0; } @@ -1998,7 +2012,15 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d const scrollAtRequest = state.scroll; state.maintainingScrollAtEnd = pendingState; requestAnimationFrame(() => { -@@ -1441,13 +1444,13 @@ function doMaintainScrollAtEnd(ctx) { + const isStillWithinThreshold = peek$(ctx, "isWithinMaintainScrollAtEndThreshold"); + const didScrollSinceRequest = state.scroll !== scrollAtRequest; +- if (isStillWithinThreshold || !didScrollSinceRequest) { ++ // A resize moves the end itself, so only a release of follow cancels a layout pin. ++ if (jump ? state.props.maintainScrollAtEnd : isStillWithinThreshold || !didScrollSinceRequest) { + state.maintainingScrollAtEnd = activeState; + const scroller = refScroller.current; + if (state.props.horizontal && isHorizontalRTL(state)) { +@@ -1441,14 +1445,19 @@ function doMaintainScrollAtEnd(ctx) { const logicalEndOffset = getLogicalHorizontalMaxOffset(state, currentContentSize); const nativeOffset = toNativeHorizontalOffset(state, logicalEndOffset, currentContentSize); scroller == null ? void 0 : scroller.scrollTo({ @@ -2012,9 +2034,15 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d - animated: maintainScrollAtEnd.animated + animated }); ++ // MVCP adjustments rebase on state.scroll, which the scroll event would only update a frame later. ++ const pinnedOffset = jump ? scroller == null ? void 0 : scroller.getCurrentScrollOffset == null ? void 0 : scroller.getCurrentScrollOffset() : void 0; ++ if (pinnedOffset !== void 0) { ++ updateScroll(ctx, pinnedOffset, true, { markHasScrolled: false }); ++ } } setTimeout( -@@ -1459,14 +1462,14 @@ function doMaintainScrollAtEnd(ctx) { + () => { +@@ -1459,14 +1468,14 @@ function doMaintainScrollAtEnd(ctx) { } } }, @@ -2031,7 +2059,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d state.pendingMaintainScrollAtEnd = true; } return true; -@@ -1887,6 +1890,10 @@ function updateScroll(ctx, newScroll, forceUpdate, options) { +@@ -1887,6 +1896,10 @@ function updateScroll(ctx, newScroll, forceUpdate, options) { state.scrollTime = currentTime; const scrollDelta = Math.abs(newScroll - prevScroll); const isUserScrollEvent = !!(options == null ? void 0 : options.fromNativeScrollEvent) && scrollDelta > 0.1 && !adjustChanged && scrollingTo === void 0 && !state.pendingNativeMVCPAdjust; @@ -2042,7 +2070,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d const allowedEdge = isUserScrollEvent ? beginReachedEdgeUserScroll(ctx, newScroll - prevScroll) : void 0; const didResolvePendingNativeMVCPAdjust = resolvePendingNativeMVCPAdjust(ctx, newScroll); const scrollLength = state.scrollLength; -@@ -3236,6 +3243,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3236,6 +3249,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { const itemKey = (_a3 = state.idCache[itemIndex]) != null ? _a3 : getId(state, itemIndex); const updateLayout = (_b = options == null ? void 0 : options.updateLayout) != null ? _b : true; let didChangePosition = false; @@ -2050,7 +2078,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d let didRefreshData = false; if (updateLayout) { const positionValue = positions[itemIndex]; -@@ -3265,6 +3273,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3265,6 +3279,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { const prevIndex = peek$(ctx, `containerItemIndex${containerIndex}`); if (prevIndex !== itemIndex) { set$(ctx, `containerItemIndex${containerIndex}`, itemIndex); @@ -2058,7 +2086,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d } const prevData = peek$(ctx, `containerItemData${containerIndex}`); const updateData = () => { -@@ -3303,7 +3312,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { +@@ -3303,7 +3318,7 @@ function syncMountedContainer(ctx, containerIndex, itemIndex, options) { } } } @@ -2067,7 +2095,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d } // src/core/prepareColumnStartState.ts -@@ -3521,7 +3530,11 @@ function updateItemPositions(ctx, dataChanged, { +@@ -3521,7 +3536,11 @@ function updateItemPositions(ctx, dataChanged, { } if (shouldOptimize && breakAt === void 0 && !scrollingTo && !dataChanged && currentRowTop > maxVisibleArea) { const itemsPerRow = hasColumns ? numColumns : 1; @@ -2080,7 +2108,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d } const id = (_e = idCache[i]) != null ? _e : getId(state, i); let span = 1; -@@ -4222,6 +4235,7 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4222,6 +4241,7 @@ function calculateItemsInView(ctx, params = {}) { } let totalSize = getContentSize(ctx); let changedContainerIds; @@ -2088,7 +2116,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d const topPad = peek$(ctx, "stylePaddingTop") + peek$(ctx, "alignItemsAtEndPadding") + peek$(ctx, "headerSize"); const numColumns = peek$(ctx, "numColumns"); const speed = (_b = params.scrollVelocity) != null ? _b : getScrollVelocity(state); -@@ -4524,6 +4538,9 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4524,6 +4544,9 @@ function calculateItemsInView(ctx, params = {}) { const containerIndex = allocation.containerIndex; const id = (_o = idCache[i]) != null ? _o : getId(state, i); const oldKey = peek$(ctx, `containerItemKey${containerIndex}`); @@ -2098,7 +2126,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d if (oldKey && oldKey !== id) { containerItemKeys.delete(oldKey); } -@@ -4608,10 +4625,12 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4608,10 +4631,12 @@ function calculateItemsInView(ctx, params = {}) { } else { const itemIndex = indexByKey.get(itemKey); if (itemIndex !== void 0) { @@ -2113,7 +2141,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d } } } -@@ -4621,6 +4640,9 @@ function calculateItemsInView(ctx, params = {}) { +@@ -4621,6 +4646,9 @@ function calculateItemsInView(ctx, params = {}) { if (didChangePositions) { set$(ctx, "lastPositionUpdate", Date.now()); } @@ -2123,7 +2151,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d if (suppressInitialScrollSideEffects) { evaluateBootstrapInitialScroll(ctx); return; -@@ -4681,7 +4703,8 @@ function maybeUpdateAnchoredEndSpace(ctx) { +@@ -4681,7 +4709,8 @@ function maybeUpdateAnchoredEndSpace(ctx) { contentBelowAnchor = Math.max(0, contentBelowAnchor - ctx.scrollAxisGap); contentBelowAnchor += (ctx.values.get("footerSize") || 0) + getStylePaddingEnd(state.props); isReady = !hasUnknownTailSize; @@ -2133,7 +2161,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d } else if (anchorIndex >= 0) { isReady = false; } -@@ -4701,6 +4724,12 @@ function maybeUpdateAnchoredEndSpace(ctx) { +@@ -4701,6 +4730,12 @@ function maybeUpdateAnchoredEndSpace(ctx) { updateScroll(ctx, state.scroll, true, { markHasScrolled: false }); } (_b = anchoredEndSpace == null ? void 0 : anchoredEndSpace.onReady) == null ? void 0 : _b.call(anchoredEndSpace, { anchorIndex: nextAnchorIndex, anchorKey: nextAnchorKey, size: nextSize }); @@ -2146,7 +2174,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d } return nextSize; } -@@ -4935,7 +4964,8 @@ function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { +@@ -4935,7 +4970,8 @@ function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { const itemKey = peek$(ctx, `containerItemKey${containerId}`); const element = viewRef == null ? void 0 : viewRef.current; if (itemKey !== void 0 && element) { @@ -2156,7 +2184,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d setContainerLayoutBaseline(element, rect); measurements.push({ containerId, -@@ -4957,12 +4987,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo +@@ -4957,12 +4993,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo }) { const ctx = useStateContext(); const [containerLayoutEpoch] = useArr$(["containerLayoutEpoch"]); @@ -2188,7 +2216,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d } }, [ctx, containerLayoutEpoch]); return children; -@@ -5630,6 +5674,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) { +@@ -5630,6 +5680,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) { }); // src/utils/reordering.ts @@ -2207,7 +2235,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d function sortDOMElements(container, indexByElement) { const elements = Array.from(container.children); if (elements.length <= 1) return elements; -@@ -5669,9 +5725,9 @@ function sortDOMElements(container, indexByElement) { +@@ -5669,9 +5731,9 @@ function sortDOMElements(container, indexByElement) { } } if (nextStableElement) { @@ -2219,7 +2247,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d } } } -@@ -5713,37 +5769,55 @@ function findLIS(arr) { +@@ -5713,37 +5775,55 @@ function findLIS(arr) { } // src/hooks/useDOMOrder.ts @@ -2301,7 +2329,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d } function useFreshDataTransitionVisibility(readyToRender, transitionEpoch) { const [completedTransitionEpoch, setCompletedTransitionEpoch] = useState(transitionEpoch); -@@ -6425,8 +6499,8 @@ function ScrollAdjust() { +@@ -6425,8 +6505,8 @@ function ScrollAdjust() { window.getComputedStyle(contentNode)[axis.paddingEndProp] ); const temporaryPaddingEnd = `${(currentPaddingEnd || 0) + pad}px`; @@ -2311,7 +2339,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..3551685de11d2d6dfdd78436d1435f4d void contentNode.offsetHeight; scrollBy(); if (resetPaddingRafRef.current !== void 0) { -@@ -6917,7 +6991,7 @@ function handleLayout(ctx, layoutParam, setCanRender) { +@@ -6917,7 +6997,7 @@ function handleLayout(ctx, layoutParam, setCanRender) { set$(ctx, "scrollSize", { height: layout.height, width: layout.width }); } if (maintainScrollAtEnd == null ? void 0 : maintainScrollAtEnd.onLayout) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2a76be73c32c4..d525132c664fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -361,7 +361,7 @@ patchedDependencies: '@expo/metro-config@58.0.14': 90fa980e8b6480631fb2fbb04559834e3c612db40c550cd828f11be297d8d760 '@expo/ui@58.0.20': 64559cb60723e19a8a72256d4c78d1315d937a7af3b32d19860ea596f88736ce '@ff-labs/fff-node@0.11.0': cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c - '@legendapp/list@3.3.5': c46f08070768056035524bc6b8b5eab092efb9fe2deacb9bf5c309c0bc0fb538 + '@legendapp/list@3.3.5': 44e50142f30444153298ac3b0a6cd27a1254a3a8f449cfd12e9ec0bd428dc226 '@muse-code/sdk@1.4.6': 7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101 '@pierre/diffs@1.6.1': 322ac5f5db8b5715189c010802047f8235c3a45d8ed5ca0ba60fac266e2eba34 '@react-native-ai/apple@0.12.0': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d @@ -545,7 +545,7 @@ importers: version: 58.0.20(patch_hash=64559cb60723e19a8a72256d4c78d1315d937a7af3b32d19860ea596f88736ce)(@babel/core@7.29.7(supports-color@8.1.1))(expo@58.0.14)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)(supports-color@8.1.1))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@legendapp/list': specifier: 3.3.5 - version: 3.3.5(patch_hash=c46f08070768056035524bc6b8b5eab092efb9fe2deacb9bf5c309c0bc0fb538)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 3.3.5(patch_hash=44e50142f30444153298ac3b0a6cd27a1254a3a8f449cfd12e9ec0bd428dc226)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@material/material-color-utilities': specifier: 0.3.0 version: 0.3.0 @@ -1060,7 +1060,7 @@ importers: version: 5.3.0 '@legendapp/list': specifier: 3.3.5 - version: 3.3.5(patch_hash=c46f08070768056035524bc6b8b5eab092efb9fe2deacb9bf5c309c0bc0fb538)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 3.3.5(patch_hash=44e50142f30444153298ac3b0a6cd27a1254a3a8f449cfd12e9ec0bd428dc226)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@noble/hashes': specifier: 'catalog:' version: 1.8.0 @@ -14899,7 +14899,7 @@ snapshots: '@koromix/koffi-win32-x64@3.3.6': optional: true - '@legendapp/list@3.3.5(patch_hash=c46f08070768056035524bc6b8b5eab092efb9fe2deacb9bf5c309c0bc0fb538)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)': + '@legendapp/list@3.3.5(patch_hash=44e50142f30444153298ac3b0a6cd27a1254a3a8f449cfd12e9ec0bd428dc226)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)': dependencies: react: 19.3.0 use-sync-external-store: 1.6.0(react@19.3.0) diff --git a/scripts/legend-list-maintain-scroll-at-end.test.ts b/scripts/legend-list-maintain-scroll-at-end.test.ts index 991bccc5bae19..b533a3aa480bf 100644 --- a/scripts/legend-list-maintain-scroll-at-end.test.ts +++ b/scripts/legend-list-maintain-scroll-at-end.test.ts @@ -17,12 +17,17 @@ function createList(bundle: string) { const frames: Array<() => void> = []; let tasks: Array<{ at: number; run: () => void }> = []; let now = 0; - const scrollToEnd = vi.fn(); + let within = true; + let layoutLeavesWithinThreshold = true; + let nativeScroll = 2000; + const scrollToEnd = vi.fn(() => { + nativeScroll = 2200; + }); const state = { props: { maintainScrollAtEnd: { animated: true, onLayout: true, onDataChange: true } }, didContainersLayout: true, pendingNativeMVCPAdjust: undefined, - refScroller: { current: { scrollToEnd } }, + refScroller: { current: { scrollToEnd, getCurrentScrollOffset: () => nativeScroll } }, scroll: 2000, scrollLength: 800, otherAxisSize: 600, @@ -34,14 +39,20 @@ function createList(bundle: string) { { requestAnimationFrame: (callback: () => void) => frames.push(callback), setTimeout: (run: () => void, delay: number) => tasks.push({ at: now + delay, run }), - peek$: (_ctx: unknown, key: string) => key === "isWithinMaintainScrollAtEndThreshold", + peek$: (_ctx: unknown, key: string) => + key === "isWithinMaintainScrollAtEndThreshold" && within, set$: vi.fn(), getContentSize: () => 3000, isHorizontalRTL: () => false, updateContentMetricsState: vi.fn(), doInitialAllocateContainers: vi.fn(), calculateItemsInView: vi.fn(), - checkThresholds: vi.fn(), + checkThresholds: () => { + within = layoutLeavesWithinThreshold; + }, + updateScroll: (_ctx: unknown, offset: number) => { + state.scroll = offset; + }, IS_DEV: false, }, ) as { @@ -55,7 +66,22 @@ function createList(bundle: string) { }; return { scrollToEnd, + state, dataChange: () => api.doMaintainScrollAtEnd(ctx), + /** The shrink leaves the end further away than the new, smaller viewport. */ + layoutLeavesEndBeyondThreshold() { + layoutLeavesWithinThreshold = false; + }, + /** A scroll event moves the list without a pin, such as a smooth scroll in flight. */ + scrolled(offset: number) { + nativeScroll = offset; + state.scroll = offset; + }, + /** A pin that lands at the end brings the threshold back. */ + reachEnd() { + within = true; + layoutLeavesWithinThreshold = true; + }, // Browser order per frame: rAF callbacks, then ResizeObserver layout, then queued tasks. frame(viewportHeight?: number) { now += FRAME_MS; @@ -88,4 +114,22 @@ describe.each(["react.mjs", "react.js"])("maintainScrollAtEnd while animated (%s ...Array.from({ length: 6 }, () => [{ animated: false }]), ]); }); + + it("pins a shrink that leaves the end beyond one viewport while a smooth scroll is in flight", () => { + const list = createList(bundle); + list.dataChange(); + list.frame(); + list.layoutLeavesEndBeyondThreshold(); + list.frame(468); + list.scrolled(2030); + list.frame(); + expect(list.scrollToEnd.mock.calls).toEqual([[{ animated: true }], [{ animated: false }]]); + // A later MVCP adjustment rebases on this offset, so it must be the pinned one. + expect(list.state.scroll).toBe(2200); + list.reachEnd(); + for (let index = 0; index < 32; index++) list.frame(); + list.dataChange(); + list.frame(); + expect(list.scrollToEnd.mock.calls.at(-1)).toEqual([{ animated: true }]); + }); }); From 885285da7c9c579b5cc5018b091a856dcbb51dd1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 8 Nov 2026 04:14:05 +0000 Subject: [PATCH 4/9] test(web): a queued resize pin is dropped once the reader scrolls away Co-Authored-By: Claude Opus 5.5 --- .../legend-list-maintain-scroll-at-end.test.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/scripts/legend-list-maintain-scroll-at-end.test.ts b/scripts/legend-list-maintain-scroll-at-end.test.ts index b533a3aa480bf..5bc02dd2c116e 100644 --- a/scripts/legend-list-maintain-scroll-at-end.test.ts +++ b/scripts/legend-list-maintain-scroll-at-end.test.ts @@ -24,7 +24,11 @@ function createList(bundle: string) { nativeScroll = 2200; }); const state = { - props: { maintainScrollAtEnd: { animated: true, onLayout: true, onDataChange: true } }, + props: { + maintainScrollAtEnd: { animated: true, onLayout: true, onDataChange: true } as + | { animated: boolean; onLayout: boolean; onDataChange: boolean } + | undefined, + }, didContainersLayout: true, pendingNativeMVCPAdjust: undefined, refScroller: { current: { scrollToEnd, getCurrentScrollOffset: () => nativeScroll } }, @@ -77,6 +81,10 @@ function createList(bundle: string) { nativeScroll = offset; state.scroll = offset; }, + /** The app passes maintainScrollAtEnd={false} once the reader scrolls away. */ + releaseFollow() { + state.props.maintainScrollAtEnd = undefined; + }, /** A pin that lands at the end brings the threshold back. */ reachEnd() { within = true; @@ -132,4 +140,12 @@ describe.each(["react.mjs", "react.js"])("maintainScrollAtEnd while animated (%s list.frame(); expect(list.scrollToEnd.mock.calls.at(-1)).toEqual([{ animated: true }]); }); + + it("drops a queued resize pin once the reader scrolls away", () => { + const list = createList(bundle); + list.frame(700); + list.releaseFollow(); + list.frame(); + expect(list.scrollToEnd).not.toHaveBeenCalled(); + }); }); From bc12eab90ed6159cefa45a6b9b345aab8987074d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 11:42:05 +0000 Subject: [PATCH 5/9] fix(web): opening the terminal mid-turn keeps the live end in view While a turn runs the timeline follows its end with an animated maintainScrollAtEnd, and Legend List animates every pin of such a list, including the one for a viewport resize, queued behind any running smooth pin. Opening the terminal drawer pushed the newest content out of view until that scroll caught up. The timeline's existing viewport resize observer now pins the end with an instant scrollToEnd when the viewport height changes while it follows the end with smooth pins. It runs before paint, pre-empts Legend's smooth resize pin, leaves content-growth pins smooth, and does nothing once the reader has scrolled away. Replaces the obsolete Legend List 3.3.5 VM test. Fixes pingdotgg/t3code#32778 Co-Authored-By: Claude Opus 5.5 --- .../chat/MessagesTimeline.browser.test.tsx | 60 +++++++ .../src/components/chat/MessagesTimeline.tsx | 39 +++-- ...legend-list-maintain-scroll-at-end.test.ts | 151 ------------------ 3 files changed, 85 insertions(+), 165 deletions(-) delete mode 100644 scripts/legend-list-maintain-scroll-at-end.test.ts diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx index dae83fd0275ba..f63be947ac977 100644 --- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx @@ -297,6 +297,66 @@ describe("MessagesTimeline work rows", () => { await vi.waitFor(() => expect(distanceFromEnd()).toBeLessThan(2)); }); + it("pins the live end at once when the viewport shrinks during a turn", async () => { + const props = { ...baseProps(), isWorking: true }; + const runId = RunId.make("live-run"); + const container = document.createElement("div"); + container.style.cssText = "height: 600px; width: 900px; display: flex; flex-direction: column;"; + const reply = (id: string) => + message("Streamed reply\n\n".repeat(40), { role: "assistant", runId }, id); + const entries = [ + ...Array.from({ length: 12 }, (_, index) => + message(`Earlier context ${index}`, {}, `context-${index}`), + ), + reply("progress"), + ]; + await using screen = await render(, { + container, + }); + const viewport = props.listRef.current!.getScrollableNode()!; + const distanceFromEnd = () => + viewport.scrollHeight - viewport.clientHeight - viewport.scrollTop; + await vi.waitFor(() => expect(distanceFromEnd()).toBeLessThan(2)); + using scrollTo = vi.spyOn(viewport, "scrollTo"); + const pinnedEnd = (behavior: ScrollBehavior) => + vi.waitFor(() => { + expect(scrollTo).toHaveBeenLastCalledWith(expect.objectContaining({ behavior })); + expect(distanceFromEnd()).toBeLessThan(2); + }); + + await screen.rerender( + , + ); + await vi.waitFor(() => + expect(scrollTo).toHaveBeenCalledWith(expect.objectContaining({ behavior: "smooth" })), + ); + // Shrinks by more than the new viewport while that smooth pin runs. + container.style.height = "200px"; + await pinnedEnd("instant"); + + await screen.rerender( + , + ); + await pinnedEnd("smooth"); + + viewport.scrollTop = 0; + await vi.waitFor(() => expect(props.listRef.current!.getState().isFollowingEnd).toBe(false)); + scrollTo.mockClear(); + await new Promise((resolve) => { + const observer = new ResizeObserver(() => { + observer.disconnect(); + resolve(); + }); + observer.observe(viewport); + container.style.height = "500px"; + }); + expect(scrollTo).not.toHaveBeenCalled(); + expect(viewport.scrollTop).toBe(0); + }); + it.each(["delegated", "legacy", "missing"] as const)( "uses the reported task for %s completion notifications", async (source) => { diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index 09f8155c4f9bf..0f4bd6d45e032 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -967,12 +967,33 @@ const ConversationTimeline = memo(function ConversationTimeline({ if (viewportElement) measureTimelineGutter(viewportElement); }); }; + const maintainScrollAtEnd = + citationPositioning || + (restoringThreadPosition && rememberedPosition?.atEnd === false) || + anchoredEndSpace || + !liveFollowEnabled || + fullscreenAppRowId !== null + ? false + : isWorking && !prefersReducedMotion && settlingListIdentity === null + ? TIMELINE_MAINTAIN_SCROLL_AT_END_SMOOTH + : TIMELINE_MAINTAIN_SCROLL_AT_END; + // Legend animates every end pin of an animated list and queues a resize pin behind a running + // one, so a viewport resize (terminal drawer) would push the live end out of view for a beat. + const pinEndOnViewportResize = useStableCallback(() => { + const list = listRef.current; + if (maintainScrollAtEnd !== TIMELINE_MAINTAIN_SCROLL_AT_END_SMOOTH) return; + if (list?.getState().isFollowingEnd) void list.scrollToEnd({ animated: false }); + }); const attachTimelineViewportElement = (viewportElement: HTMLDivElement | null) => { setTimelineViewportElement(viewportElement); if (!viewportElement) return; - const stopObserving = observeResize(viewportElement, () => - measureTimelineGutter(viewportElement), - ); + let viewportHeight = viewportElement.clientHeight; + const stopObserving = observeResize(viewportElement, () => { + measureTimelineGutter(viewportElement); + if (viewportElement.clientHeight === viewportHeight) return; + viewportHeight = viewportElement.clientHeight; + pinEndOnViewportResize(); + }); return () => { stopObserving(); setTimelineViewportElement(null); @@ -1302,17 +1323,7 @@ const ConversationTimeline = memo(function ConversationTimeline({ onLoad={handleListLoad} {...(anchoredEndSpace ? { anchoredEndSpace } : {})} contentInsetEndAdjustment={anchoredEndSpace ? contentInsetEndAdjustment : 0} - maintainScrollAtEnd={ - citationPositioning || - (restoringThreadPosition && rememberedPosition?.atEnd === false) || - anchoredEndSpace || - !liveFollowEnabled || - fullscreenAppRowId !== null - ? false - : isWorking && !prefersReducedMotion && settlingListIdentity === null - ? TIMELINE_MAINTAIN_SCROLL_AT_END_SMOOTH - : TIMELINE_MAINTAIN_SCROLL_AT_END - } + maintainScrollAtEnd={maintainScrollAtEnd} maintainVisibleContentPosition={ findActive || citationPositioning || diff --git a/scripts/legend-list-maintain-scroll-at-end.test.ts b/scripts/legend-list-maintain-scroll-at-end.test.ts deleted file mode 100644 index 5bc02dd2c116e..0000000000000 --- a/scripts/legend-list-maintain-scroll-at-end.test.ts +++ /dev/null @@ -1,151 +0,0 @@ -// @effect-diagnostics nodeBuiltinImport:off - reads the installed web JS bundle for synchronous VM tests. -import * as NodeFS from "node:fs"; -import * as NodeVM from "node:vm"; -import { describe, expect, it, vi } from "vite-plus/test"; - -const FRAME_MS = 16; - -function createList(bundle: string) { - const source = NodeFS.readFileSync( - new URL(`../apps/web/node_modules/@legendapp/list/${bundle}`, import.meta.url), - "utf8", - ); - const slice = (start: string, end: string) => - source.slice(source.indexOf(start), source.indexOf(end)); - const maintain = slice("// src/core/doMaintainScrollAtEnd.ts", "// src/utils/requestAdjust.ts"); - const layout = slice("// src/core/handleLayout.ts", "// src/core/onScroll.ts"); - const frames: Array<() => void> = []; - let tasks: Array<{ at: number; run: () => void }> = []; - let now = 0; - let within = true; - let layoutLeavesWithinThreshold = true; - let nativeScroll = 2000; - const scrollToEnd = vi.fn(() => { - nativeScroll = 2200; - }); - const state = { - props: { - maintainScrollAtEnd: { animated: true, onLayout: true, onDataChange: true } as - | { animated: boolean; onLayout: boolean; onDataChange: boolean } - | undefined, - }, - didContainersLayout: true, - pendingNativeMVCPAdjust: undefined, - refScroller: { current: { scrollToEnd, getCurrentScrollOffset: () => nativeScroll } }, - scroll: 2000, - scrollLength: 800, - otherAxisSize: 600, - lastLayout: { x: 0, y: 0, width: 600, height: 800 }, - }; - const ctx = { state }; - const api = NodeVM.runInNewContext( - `${maintain}\n${layout}\n({ doMaintainScrollAtEnd, handleLayout })`, - { - requestAnimationFrame: (callback: () => void) => frames.push(callback), - setTimeout: (run: () => void, delay: number) => tasks.push({ at: now + delay, run }), - peek$: (_ctx: unknown, key: string) => - key === "isWithinMaintainScrollAtEndThreshold" && within, - set$: vi.fn(), - getContentSize: () => 3000, - isHorizontalRTL: () => false, - updateContentMetricsState: vi.fn(), - doInitialAllocateContainers: vi.fn(), - calculateItemsInView: vi.fn(), - checkThresholds: () => { - within = layoutLeavesWithinThreshold; - }, - updateScroll: (_ctx: unknown, offset: number) => { - state.scroll = offset; - }, - IS_DEV: false, - }, - ) as { - doMaintainScrollAtEnd: (context: typeof ctx) => boolean; - handleLayout: (context: typeof ctx, layout: object, setCanRender: () => void) => void; - }; - const runDueTasks = () => { - const due = tasks.filter((task) => task.at <= now); - tasks = tasks.filter((task) => task.at > now); - for (const task of due) task.run(); - }; - return { - scrollToEnd, - state, - dataChange: () => api.doMaintainScrollAtEnd(ctx), - /** The shrink leaves the end further away than the new, smaller viewport. */ - layoutLeavesEndBeyondThreshold() { - layoutLeavesWithinThreshold = false; - }, - /** A scroll event moves the list without a pin, such as a smooth scroll in flight. */ - scrolled(offset: number) { - nativeScroll = offset; - state.scroll = offset; - }, - /** The app passes maintainScrollAtEnd={false} once the reader scrolls away. */ - releaseFollow() { - state.props.maintainScrollAtEnd = undefined; - }, - /** A pin that lands at the end brings the threshold back. */ - reachEnd() { - within = true; - layoutLeavesWithinThreshold = true; - }, - // Browser order per frame: rAF callbacks, then ResizeObserver layout, then queued tasks. - frame(viewportHeight?: number) { - now += FRAME_MS; - for (const callback of frames.splice(0)) callback(); - if (viewportHeight !== undefined) { - api.handleLayout(ctx, { x: 0, y: 0, width: 600, height: viewportHeight }, () => {}); - } - runDueTasks(); - }, - }; -} - -describe.each(["react.mjs", "react.js"])("maintainScrollAtEnd while animated (%s)", (bundle) => { - it("pins every frame of a continuous viewport resize instantly", () => { - const list = createList(bundle); - for (let index = 1; index <= 12; index++) list.frame(800 - index * 20); - list.frame(); - expect(list.scrollToEnd.mock.calls).toEqual( - Array.from({ length: 12 }, () => [{ animated: false }]), - ); - }); - - it("keeps a content-growth pin smooth and pins the following resize instantly", () => { - const list = createList(bundle); - list.dataChange(); - for (let index = 1; index <= 6; index++) list.frame(800 - index * 20); - list.frame(); - expect(list.scrollToEnd.mock.calls).toEqual([ - [{ animated: true }], - ...Array.from({ length: 6 }, () => [{ animated: false }]), - ]); - }); - - it("pins a shrink that leaves the end beyond one viewport while a smooth scroll is in flight", () => { - const list = createList(bundle); - list.dataChange(); - list.frame(); - list.layoutLeavesEndBeyondThreshold(); - list.frame(468); - list.scrolled(2030); - list.frame(); - expect(list.scrollToEnd.mock.calls).toEqual([[{ animated: true }], [{ animated: false }]]); - // A later MVCP adjustment rebases on this offset, so it must be the pinned one. - expect(list.state.scroll).toBe(2200); - list.reachEnd(); - for (let index = 0; index < 32; index++) list.frame(); - list.dataChange(); - list.frame(); - expect(list.scrollToEnd.mock.calls.at(-1)).toEqual([{ animated: true }]); - }); - - it("drops a queued resize pin once the reader scrolls away", () => { - const list = createList(bundle); - list.frame(700); - list.releaseFollow(); - list.frame(); - expect(list.scrollToEnd).not.toHaveBeenCalled(); - }); -}); From c6bff8b1dde6be47a1b3358de1376281ef005ea8 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 11:55:51 +0000 Subject: [PATCH 6/9] fix(web): pin the end from Legend's onLayout so it sees the new viewport The shared resize observer can fire before Legend measures the resized viewport, so the instant pin aimed at the old end for one frame. Legend's onLayout runs after its own measurement. Co-Authored-By: Claude Opus 5.5 --- .../chat/MessagesTimeline.browser.test.tsx | 6 ++++ .../src/components/chat/MessagesTimeline.tsx | 29 +++++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx index f63be947ac977..65bcdfac01ced 100644 --- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx @@ -35,6 +35,7 @@ import { environmentThreadDetails } from "../../state/threads"; import { act, fakeTimers, render, stubGlobal } from "../../../test/render"; import { ChangedFilesCard } from "./ChangedFilesTree"; import { MessagesTimeline } from "./MessagesTimeline"; +import { observeResize } from "~/lib/observeResize"; import { makeStreamingTimelineFixture } from "../../test-fixtures"; import { type PendingApproval, @@ -310,6 +311,11 @@ describe("MessagesTimeline work rows", () => { ), reply("progress"), ]; + // The app shell subscribes to the shared resize observer before any list mounts, so it is + // notified before Legend measures the new viewport. + const shell = document.body.appendChild(document.createElement("div")); + onTestFinished(() => shell.remove()); + onTestFinished(observeResize(shell, () => {})); await using screen = await render(, { container, }); diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index 0f4bd6d45e032..b927e170dfcf1 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -979,21 +979,25 @@ const ConversationTimeline = memo(function ConversationTimeline({ : TIMELINE_MAINTAIN_SCROLL_AT_END; // Legend animates every end pin of an animated list and queues a resize pin behind a running // one, so a viewport resize (terminal drawer) would push the live end out of view for a beat. - const pinEndOnViewportResize = useStableCallback(() => { - const list = listRef.current; - if (maintainScrollAtEnd !== TIMELINE_MAINTAIN_SCROLL_AT_END_SMOOTH) return; - if (list?.getState().isFollowingEnd) void list.scrollToEnd({ animated: false }); - }); + // Runs from Legend's onLayout, after it has measured the new viewport length. + const listViewportHeightRef = useRef(null); + const pinEndOnViewportResize = useStableCallback( + (event: { nativeEvent: { layout: { height: number } } }) => { + const { height } = event.nativeEvent.layout; + const previousHeight = listViewportHeightRef.current; + listViewportHeightRef.current = height; + if (previousHeight === null || previousHeight === height) return; + const list = listRef.current; + if (maintainScrollAtEnd !== TIMELINE_MAINTAIN_SCROLL_AT_END_SMOOTH) return; + if (list?.getState().isFollowingEnd) void list.scrollToEnd({ animated: false }); + }, + ); const attachTimelineViewportElement = (viewportElement: HTMLDivElement | null) => { setTimelineViewportElement(viewportElement); if (!viewportElement) return; - let viewportHeight = viewportElement.clientHeight; - const stopObserving = observeResize(viewportElement, () => { - measureTimelineGutter(viewportElement); - if (viewportElement.clientHeight === viewportHeight) return; - viewportHeight = viewportElement.clientHeight; - pinEndOnViewportResize(); - }); + const stopObserving = observeResize(viewportElement, () => + measureTimelineGutter(viewportElement), + ); return () => { stopObserving(); setTimelineViewportElement(null); @@ -1324,6 +1328,7 @@ const ConversationTimeline = memo(function ConversationTimeline({ {...(anchoredEndSpace ? { anchoredEndSpace } : {})} contentInsetEndAdjustment={anchoredEndSpace ? contentInsetEndAdjustment : 0} maintainScrollAtEnd={maintainScrollAtEnd} + onLayout={pinEndOnViewportResize} maintainVisibleContentPosition={ findActive || citationPositioning || From 11f04dee6e2e5f442a54869a689c03e532c3031a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 13:17:26 +0000 Subject: [PATCH 7/9] fix(web): commit the resize pin before the frame paints scrollToEnd commits through a React update, so from Legend's resize observer it could land a frame after the resized layout painted. Flush it. Co-Authored-By: Claude Opus 5.5 --- .../chat/MessagesTimeline.browser.test.tsx | 13 +++++++++++-- apps/web/src/components/chat/MessagesTimeline.tsx | 9 ++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx index 65bcdfac01ced..875790c955cc6 100644 --- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx @@ -336,8 +336,17 @@ describe("MessagesTimeline work rows", () => { await vi.waitFor(() => expect(scrollTo).toHaveBeenCalledWith(expect.objectContaining({ behavior: "smooth" })), ); - // Shrinks by more than the new viewport while that smooth pin runs. - container.style.height = "200px"; + // Shrinks by more than the new viewport while that smooth pin runs. Observers run in creation + // order before paint, so this one reads the position the resized frame paints with. + const distanceWhenPainted = new Promise((resolve) => { + const observer = new ResizeObserver(() => { + observer.disconnect(); + resolve(distanceFromEnd()); + }); + observer.observe(viewport); + container.style.height = "200px"; + }); + expect(await distanceWhenPainted).toBeLessThan(2); await pinnedEnd("instant"); await screen.rerender( diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index b927e170dfcf1..a40899d6a9310 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -22,6 +22,7 @@ import { import { parseScopedThreadKey } from "@t3tools/client-runtime/environment"; import { resolveWorkGroupScrollAnchor } from "@t3tools/client-runtime/work-log/scroll-anchor"; import type { WorkflowOpening } from "@t3tools/client-runtime/state/subagentRuntime"; +import { flushSync } from "react-dom"; import { observeResize } from "~/lib/observeResize"; import { useStableCallback } from "~/hooks/useStableCallback"; @@ -979,7 +980,8 @@ const ConversationTimeline = memo(function ConversationTimeline({ : TIMELINE_MAINTAIN_SCROLL_AT_END; // Legend animates every end pin of an animated list and queues a resize pin behind a running // one, so a viewport resize (terminal drawer) would push the live end out of view for a beat. - // Runs from Legend's onLayout, after it has measured the new viewport length. + // Runs from Legend's onLayout, after it has measured the new viewport length. scrollToEnd + // commits through a React update, so flush it to scroll before this frame paints. const listViewportHeightRef = useRef(null); const pinEndOnViewportResize = useStableCallback( (event: { nativeEvent: { layout: { height: number } } }) => { @@ -987,9 +989,10 @@ const ConversationTimeline = memo(function ConversationTimeline({ const previousHeight = listViewportHeightRef.current; listViewportHeightRef.current = height; if (previousHeight === null || previousHeight === height) return; - const list = listRef.current; if (maintainScrollAtEnd !== TIMELINE_MAINTAIN_SCROLL_AT_END_SMOOTH) return; - if (list?.getState().isFollowingEnd) void list.scrollToEnd({ animated: false }); + const list = listRef.current; + if (!list?.getState().isFollowingEnd) return; + flushSync(() => void list.scrollToEnd({ animated: false })); }, ); const attachTimelineViewportElement = (viewportElement: HTMLDivElement | null) => { From c6b46f306c2a5c6e7d0b4eb9d83b275d658256cf Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 13:29:38 +0000 Subject: [PATCH 8/9] test(web): drop a before-paint check the test runtime cannot fail Co-Authored-By: Claude Opus 5.5 --- .../chat/MessagesTimeline.browser.test.tsx | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx index 875790c955cc6..33352e2336c20 100644 --- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx @@ -33,9 +33,9 @@ import "~/index.css"; import { environmentThreadDetails } from "../../state/threads"; import { act, fakeTimers, render, stubGlobal } from "../../../test/render"; +import { observeResize } from "~/lib/observeResize"; import { ChangedFilesCard } from "./ChangedFilesTree"; import { MessagesTimeline } from "./MessagesTimeline"; -import { observeResize } from "~/lib/observeResize"; import { makeStreamingTimelineFixture } from "../../test-fixtures"; import { type PendingApproval, @@ -336,17 +336,8 @@ describe("MessagesTimeline work rows", () => { await vi.waitFor(() => expect(scrollTo).toHaveBeenCalledWith(expect.objectContaining({ behavior: "smooth" })), ); - // Shrinks by more than the new viewport while that smooth pin runs. Observers run in creation - // order before paint, so this one reads the position the resized frame paints with. - const distanceWhenPainted = new Promise((resolve) => { - const observer = new ResizeObserver(() => { - observer.disconnect(); - resolve(distanceFromEnd()); - }); - observer.observe(viewport); - container.style.height = "200px"; - }); - expect(await distanceWhenPainted).toBeLessThan(2); + // Shrinks by more than the new viewport while that smooth pin runs. + container.style.height = "200px"; await pinnedEnd("instant"); await screen.rerender( From f57ff765d849ae3b730e7ccdc5bbd86a0219c97a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 16:03:28 +0000 Subject: [PATCH 9/9] fix(web): resize pin covers same-frame growth and the scroll-to-end settle Legend batches item remeasurements in a resize delivery and flushes them after the viewport's onLayout, so the instant pin landed on the pre-growth end and Legend pinned the growth smoothly a frame later. The pin now waits for that flush in a microtask, still before paint. Any Legend scroll request clears isFollowingEnd while it runs, so the scroll-to-end settle's scrollToOffset made the handler skip a resize. The handler now also pins when the reader sat inside the app's at-end band at the old viewport height; a reader who left the end is still not pulled back. AI-assisted; verified with the tests and tools listed above. --- .../chat/MessagesTimeline.browser.test.tsx | 37 ++++++++++++++----- .../src/components/chat/MessagesTimeline.tsx | 16 ++++++-- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx index c8dff877c9c8f..a4f48f35d7192 100644 --- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx @@ -348,19 +348,38 @@ describe("MessagesTimeline work rows", () => { ); await pinnedEnd("smooth"); + // Read after Legend's resize delivery and its microtasks, before the frame paints. + const distanceAfterResize = (height: string, change: () => void) => + new Promise((resolve) => { + const observer = new ResizeObserver(() => { + observer.disconnect(); + resolve(distanceFromEnd()); + }); + observer.observe(viewport); + change(); + container.style.height = height; + }); + // The newest row grows in the same resize delivery. + const newestParagraph = screen.getByText("Streamed reply").last().element() as HTMLElement; + expect( + await distanceAfterResize("300px", () => (newestParagraph.style.paddingBottom = "191px")), + ).toBeLessThan(2); + // The scroll-to-end settle's scrollToOffset clears Legend's follow while it runs. + expect( + await distanceAfterResize("250px", () => { + void props.listRef.current!.scrollToOffset({ + offset: viewport.scrollHeight - viewport.clientHeight, + animated: false, + }); + expect(props.listRef.current!.getState().isFollowingEnd).toBe(false); + }), + ).toBeLessThan(2); + viewport.scrollTop = 0; await vi.waitFor(() => expect(props.listRef.current!.getState().isFollowingEnd).toBe(false)); scrollTo.mockClear(); - await new Promise((resolve) => { - const observer = new ResizeObserver(() => { - observer.disconnect(); - resolve(); - }); - observer.observe(viewport); - container.style.height = "500px"; - }); + expect(await distanceAfterResize("500px", () => {})).toBeGreaterThan(500); expect(scrollTo).not.toHaveBeenCalled(); - expect(viewport.scrollTop).toBe(0); }); it.each(["delegated", "legacy", "missing"] as const)( diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index e53aa4680beec..821859c71544a 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -991,8 +991,8 @@ const ConversationTimeline = memo(function ConversationTimeline({ : TIMELINE_MAINTAIN_SCROLL_AT_END; // Legend animates every end pin of an animated list and queues a resize pin behind a running // one, so a viewport resize (terminal drawer) would push the live end out of view for a beat. - // Runs from Legend's onLayout, after it has measured the new viewport length. scrollToEnd - // commits through a React update, so flush it to scroll before this frame paints. + // Runs from Legend's onLayout, inside the resize batch that flushes item growth after it, so + // the pin waits for that flush. scrollToEnd commits through a React update; flushSync paints it. const listViewportHeightRef = useRef(null); const pinEndOnViewportResize = useStableCallback( (event: { nativeEvent: { layout: { height: number } } }) => { @@ -1002,8 +1002,16 @@ const ConversationTimeline = memo(function ConversationTimeline({ if (previousHeight === null || previousHeight === height) return; if (maintainScrollAtEnd !== TIMELINE_MAINTAIN_SCROLL_AT_END_SMOOTH) return; const list = listRef.current; - if (!list?.getState().isFollowingEnd) return; - flushSync(() => void list.scrollToEnd({ animated: false })); + if (!list) return; + const state = list.getState(); + // Any scroll request clears Legend's follow while it runs, so the scroll-to-end settle + // reads as leaving the end. A reader who left the end band is still never pulled back. + if ( + !state.isFollowingEnd && + !resolveTimelineIsAtEnd({ ...state, scrollLength: previousHeight }) + ) + return; + queueMicrotask(() => flushSync(() => void list.scrollToEnd({ animated: false }))); }, ); const attachTimelineViewportElement = (viewportElement: HTMLDivElement | null) => {