Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(channels): stop iOS channel scroll jumping when momentum settles (#9232)
Peter Chinmane59a5e66d78c
diff --git a/apps/web/src/features/channel/Channel/ThreadList.tsx b/apps/web/src/features/channel/Channel/ThreadList.tsxindex a39ce31e10..b095d10da3 100644--- a/apps/web/src/features/channel/Channel/ThreadList.tsx+++ b/apps/web/src/features/channel/Channel/ThreadList.tsx@@ -146,6 +146,9 @@ export function ThreadList(props: ThreadListProps) {snapshot?.measurements?.map(({ key, size }) => [key, size]));let programmaticOffset: number | undefined;+ // A scroll event dispatched while our own scrollTo runs is ours as well.+ // Native scrolling activates gesture compensation, so it must not be mistaken.+ let writingScroll = false;let stateQueued = false;let nearTopKey: string | undefined;let nearBottomFired = false;@@ -174,7 +177,12 @@ export function ThreadList(props: ThreadListProps) {writeOffset: (offset) => {const element = scrollEl();if (!element) return;- element.scrollTo({ top: offset });+ writingScroll = true;+ try {+ element.scrollTo({ top: offset });+ } finally {+ writingScroll = false;+ }programmaticOffset = element.scrollTop;scheduleScrollState();},@@ -322,8 +330,9 @@ export function ThreadList(props: ThreadListProps) {const reportOffset = (offset: number, isScrolling: boolean) => {// An instant navigation/correction must not extend gesture compensation.const isOwnScroll =- programmaticOffset !== undefined &&- Math.abs(offset - programmaticOffset) < 1.5;+ writingScroll ||+ (programmaticOffset !== undefined &&+ Math.abs(offset - programmaticOffset) < 1.5);programmaticOffset = undefined;if (isScrolling && !isOwnScroll) scrollIntent.observeScroll();const logicalOffset =@@ -387,7 +396,12 @@ export function ThreadList(props: ThreadListProps) {) {return;}- elementScroll(offset, options, instance);+ writingScroll = true;+ try {+ elementScroll(offset, options, instance);+ } finally {+ writingScroll = false;+ }programmaticOffset =options.behavior === 'smooth'? undefineddiff --git a/apps/web/src/features/channel/Channel/create-scroll-compensation.ts b/apps/web/src/features/channel/Channel/create-scroll-compensation.tsindex f4429b4188..b8e904466d 100644--- a/apps/web/src/features/channel/Channel/create-scroll-compensation.ts+++ b/apps/web/src/features/channel/Channel/create-scroll-compensation.ts@@ -36,12 +36,17 @@ export function createScrollCompensation(options: ScrollCompensationOptions) {const scheduleFinish = () => {clearTimer();if (touchTargets.size > 0) return;+ const scheduledOffset = options.getElement()?.scrollTop;timer = setTimeout(() => {const element = options.getElement();- // Do not interrupt iOS rubber-banding. Retry even without another scroll event.+ // WebKit drops a scrollTop write while native momentum is still moving,+ // jumping the list by the whole adjustment, and a busy main thread can+ // delay scroll events past the idle period. Commit only once the offset+ // has also held still. Rubber-banding retries here without a scroll event.if (element &&- (element.scrollTop < 0 ||+ (element.scrollTop !== scheduledOffset ||+ element.scrollTop < 0 ||element.scrollTop > element.scrollHeight - element.clientHeight)) {scheduleFinish();@@ -86,24 +91,14 @@ export function createScrollCompensation(options: ScrollCompensationOptions) {scheduleFinish();},observeOffset: (offset: number, isScrolling: boolean) => {- const element = options.getElement();const logicalOffset = offset + adjustment;- if (- adjustment !== 0 &&- element &&- offset >= 0 &&- offset <= element.scrollHeight - element.clientHeight &&- (offset <= 0 ||- offset >= element.scrollHeight - element.clientHeight ||- logicalOffset <= 0 ||- logicalOffset >= element.scrollHeight - element.clientHeight)- ) {- // Release the shifted origin at either edge so all history remains reachable.- finish();- reportedOffset = element.scrollTop;- return reportedOffset;+ // iOS sends no touchstart for a fling that begins during momentum, so+ // native scrolling itself keeps the gesture active. Reaching an edge+ // also waits to settle: the boundary is where momentum bounces.+ if (isScrolling) {+ active = true;+ scheduleFinish();}- if (active && isScrolling) scheduleFinish();reportedOffset = logicalOffset;return logicalOffset;},diff --git a/apps/web/src/features/channel/Channel/tests/create-scroll-compensation.test.ts b/apps/web/src/features/channel/Channel/tests/create-scroll-compensation.test.tsindex d7e091290e..1d6ac93c82 100644--- a/apps/web/src/features/channel/Channel/tests/create-scroll-compensation.test.ts+++ b/apps/web/src/features/channel/Channel/tests/create-scroll-compensation.test.ts@@ -106,16 +106,58 @@ describe('gesture scroll compensation', () => {expect(f.writeOffset).toHaveBeenCalledExactlyOnceWith(1300);});- it('releases the shifted origin when native scrolling reaches the top', () => {+ it('releases the shifted origin once scrolling settles at the top', () => {const f = setup();f.wheel();f.compensation.defer(1200);f.element.scrollTop = 0;expect(f.compensation.observeOffset(0, true)).toBe(200);+ // Momentum bounces at the boundary, where WebKit would drop the write.+ expect(f.writeOffset).not.toHaveBeenCalled();+ vi.advanceTimersByTime(150);expect(f.shift()).toBe(0);expect(f.writeOffset).toHaveBeenCalledExactlyOnceWith(200);});+ it('waits while momentum moves between delayed scroll events', () => {+ const f = setup();+ f.element.addEventListener('touchstart', f.compensation.onTouchStart);+ f.element.dispatchEvent(new TouchEvent('touchstart'));+ f.compensation.defer(1200);+ f.element.dispatchEvent(new TouchEvent('touchend', { touches: [] }));+ // A busy main thread delays the next scroll event while momentum continues.+ f.element.scrollTop = 900;+ vi.advanceTimersByTime(150);+ expect(f.writeOffset).not.toHaveBeenCalled();+ vi.advanceTimersByTime(150);+ expect(f.writeOffset).toHaveBeenCalledExactlyOnceWith(1100);+ });++ it('defers corrections for a fling that starts during momentum', () => {+ const f = setup();+ // iOS sends no touchstart when a touch catches momentum and flings again.+ f.element.scrollTop = 950;+ expect(f.compensation.observeOffset(950, true)).toBe(950);+ expect(f.compensation.defer(1150)).toBe(true);+ expect(f.shift()).toBe(200);+ vi.advanceTimersByTime(150);+ expect(f.writeOffset).toHaveBeenCalledExactlyOnceWith(1150);+ });++ it('does not release at an edge while a finger holds the list', () => {+ const f = setup();+ f.element.addEventListener('touchstart', f.compensation.onTouchStart);+ f.element.dispatchEvent(new TouchEvent('touchstart'));+ f.compensation.defer(1200);+ f.element.scrollTop = 0;+ expect(f.compensation.observeOffset(0, true)).toBe(200);+ vi.advanceTimersByTime(1000);+ expect(f.writeOffset).not.toHaveBeenCalled();+ f.element.dispatchEvent(new TouchEvent('touchend', { touches: [] }));+ vi.advanceTimersByTime(150);+ expect(f.writeOffset).toHaveBeenCalledExactlyOnceWith(200);+ });+it('does not write after disposal or detachment', () => {const f = setup();f.wheel();@@ -128,12 +170,13 @@ describe('gesture scroll compensation', () => {expect(f.writeOffset).not.toHaveBeenCalled();});- it('releases negative compensation at the physical bottom', () => {+ it('releases negative compensation once settled at the physical bottom', () => {const f = setup();f.wheel();f.compensation.defer(800);f.element.scrollTop = 4500;expect(f.compensation.observeOffset(4500, true)).toBe(4300);+ vi.advanceTimersByTime(150);expect(f.shift()).toBe(0);expect(f.writeOffset).toHaveBeenCalledExactlyOnceWith(4300);});@@ -193,6 +236,8 @@ describe('gesture scroll compensation', () => {expect(f.writeOffset).not.toHaveBeenCalled();f.element.scrollTop = 0;expect(f.compensation.observeOffset(0, true)).toBe(200);+ expect(f.writeOffset).not.toHaveBeenCalled();+ vi.advanceTimersByTime(150);expect(f.writeOffset).toHaveBeenCalledExactlyOnceWith(200);});@@ -211,6 +256,9 @@ describe('gesture scroll compensation', () => {expect(f.shift()).toBe(target - 1000);f.element.scrollTop = settled;+ // Returning from overscroll is still native motion.+ vi.advanceTimersByTime(150);+ expect(f.writeOffset).not.toHaveBeenCalled();vi.advanceTimersByTime(150);expect(f.writeOffset).toHaveBeenCalledExactlyOnceWith(expected);expect(f.shift()).toBe(0);diff --git a/apps/web/src/features/channel/docs/sticky-scrolling.md b/apps/web/src/features/channel/docs/sticky-scrolling.mdindex 9d3013d618..0cc932e53a 100644--- a/apps/web/src/features/channel/docs/sticky-scrolling.md+++ b/apps/web/src/features/channel/docs/sticky-scrolling.md@@ -57,16 +57,23 @@ written, `writeScroll` synchronously commits the current total to the sizer;otherwise a growing last row can clamp the scroll against the old DOM extent.Viewport and inset changes explicitly scroll to the end only if previously pinned.The offset observer distinguishes instant programmatic scrolls from user scrolling,-so our own corrections do not prolong gesture compensation.+including scroll events dispatched while our own `scrollTo` runs, so our own+corrections neither start nor prolong gesture compensation.Safari wheel scrolling and iOS touch scrolling use a separate logical offset while a gesture isactive. Size and prepend corrections counter-shift the rows instead of writing-`scrollTop`, which interrupts native momentum. Scroll observations and saved-snapshots include that adjustment. After touch release and 150ms without input or scrolling,-the adjustment is removed and committed to the DOM offset in the same task.+`scrollTop`: WebKit drops a write that lands while native scrolling is still moving,+and the list jumps by the whole adjustment. Any native scroll event activates+compensation, because iOS sends no `touchstart` for a fling that begins during momentum.+Scroll observations and saved snapshots include that adjustment. After touch release,+once 150ms pass without input or scrolling and `scrollTop` has not moved in that time,+the adjustment is removed and committed to the DOM offset in the same task. A busy+main thread can delay scroll events beyond 150ms while momentum continues, so the+timer alone is not proof that scrolling stopped.The sizer retains its logical height and clips shifted overflow during this period.-Reaching either scroll boundary releases the shifted origin; explicit message,-reply, latest, and pinned viewport/inset navigation flush it before computing targets.+Scroll boundaries follow the same rule: momentum bounces there, and a finger can hold+the list against an edge. Explicit message, reply, latest, and pinned viewport/inset+navigation flush the adjustment before computing targets.Elastic overscroll must return in bounds before a correction is committed. Touch endand cancellation listeners stay on the original event target even if its row unmounts.The `@tanstack/virtual-core` patch adds `useIOSScrollDeferral`, enabled by default.diff --git a/docs/agent-guide/channels.md b/docs/agent-guide/channels.mdindex 28429245d9..5a38b3b225 100644--- a/docs/agent-guide/channels.md+++ b/docs/agent-guide/channels.md@@ -678,6 +678,9 @@ that buffer. Check that pagination retains the visible message, and that lateststays pinned when messages arrive, images load, or the composer resizes. Verifymessage/reply navigation, restoration, and the custom scrollbar after pagination.Very long flings or slow responses can still exhaust the available scroll range.+In the iOS app, fling repeatedly through a long channel, starting each fling before+the previous one's momentum ends, then let it settle. The messages must not jump when+scrolling comes to rest, nor when it bounces or is held against the top of loaded history.Inline document mentions should show their stored title before entering the viewportand while preview requests are pending. With a slow preview response, check that a