Skip to content

Commit 66cecf1

Browse files
fix(web): preserve scroll opt-out across iframe events and anchors
1 parent 09b1837 commit 66cecf1

3 files changed

Lines changed: 49 additions & 63 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4315,9 +4315,20 @@ export default function ChatView(props: ChatViewProps) {
43154315
});
43164316
const handleScroll = () => {
43174317
if (
4318-
scrolledUp({ top: scrollNode.scrollTop, height: scrollNode.scrollHeight }) &&
4318+
scrolledUp({
4319+
top: scrollNode.scrollTop,
4320+
height: scrollNode.scrollHeight,
4321+
programmatic:
4322+
positionedTimelineAnchorRef.current !== null &&
4323+
positionedTimelineAnchorRef.current !== settledTimelineAnchorRef.current,
4324+
}) &&
43194325
timelineScrollModeRef.current !== "free-scrolling" &&
4320-
viewportIsAwayFromEnd()
4326+
// LegendList updates its scroll state on the next animation frame.
4327+
resolveTimelineIsAtEnd({
4328+
contentLength: scrollNode.scrollHeight,
4329+
scroll: scrollNode.scrollTop,
4330+
scrollLength: scrollNode.clientHeight,
4331+
}) === false
43214332
) {
43224333
handleManualNavigation();
43234334
}
@@ -4380,30 +4391,22 @@ export default function ChatView(props: ChatViewProps) {
43804391
}
43814392
return;
43824393
}
4383-
const scrollNode = list.getScrollableNode();
4384-
let finished = false;
4385-
const finishAnimatedPositioning = () => {
4386-
if (finished) {
4387-
return;
4388-
}
4389-
finished = true;
4390-
window.clearTimeout(fallbackTimer);
4391-
scrollNode.removeEventListener("scrollend", finishAnimatedPositioning);
4392-
if (positionedTimelineAnchorRef.current !== messageId) {
4393-
return;
4394-
}
4395-
const scrollOffset = list.getState().scroll;
4396-
void list.scrollToOffset({ offset: scrollOffset, animated: false });
4397-
settledTimelineAnchorRef.current = messageId;
4398-
};
4399-
const fallbackTimer = window.setTimeout(finishAnimatedPositioning, 750);
4400-
scrollNode.addEventListener("scrollend", finishAnimatedPositioning, { once: true });
4401-
void list.scrollToIndex({
4402-
index: anchorIndex,
4403-
animated: true,
4404-
viewPosition: 0,
4405-
viewOffset: CHAT_LIST_ANCHOR_OFFSET,
4406-
});
4394+
// LegendList waits for queued layout and the animation before resolving.
4395+
void list
4396+
.scrollToIndex({
4397+
index: anchorIndex,
4398+
animated: true,
4399+
viewPosition: 0,
4400+
viewOffset: CHAT_LIST_ANCHOR_OFFSET,
4401+
})
4402+
.then(() => {
4403+
if (
4404+
legendListRef.current === list &&
4405+
positionedTimelineAnchorRef.current === messageId
4406+
) {
4407+
settledTimelineAnchorRef.current = messageId;
4408+
}
4409+
});
44074410
});
44084411
};
44094412
requestAnimationFrame(() => positionAnchor(12));

‎apps/web/src/components/chat/timelineScrollTarget.test.ts‎

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -141,37 +141,27 @@ describe("timeline scroll targets", () => {
141141
});
142142
});
143143

144-
describe("createUpwardScrollDetector", () => {
145-
it("reports a reader scrolling up, such as a scroll chained out of an embedded frame", () => {
146-
const scrolledUp = createUpwardScrollDetector({ top: 6621, height: 7421 });
147-
expect(scrolledUp({ top: 6471, height: 7421 })).toBe(true);
148-
expect(scrolledUp({ top: 6321, height: 7421 })).toBe(true);
149-
});
144+
describe("timeline scroll follow opt-out", () => {
145+
it("detects iframe-only upward scrolling after following streaming content", () => {
146+
const scrolledUp = createUpwardScrollDetector({ top: 1000, height: 1500 });
147+
expect(scrolledUp({ top: 1000, height: 1800 })).toBe(false);
148+
expect(scrolledUp({ top: 1300, height: 1800 })).toBe(false);
149+
expect(scrolledUp({ top: 1200, height: 1700 })).toBe(false);
150150

151-
it("ignores moves toward the end and content growing while it follows", () => {
152-
const scrolledUp = createUpwardScrollDetector({ top: 1000, height: 2000 });
153-
expect(scrolledUp({ top: 1000, height: 2400 })).toBe(false);
154-
expect(scrolledUp({ top: 1400, height: 2400 })).toBe(false);
151+
// Sandboxed frames deliver no wheel or touch event to the parent timeline.
152+
expect(scrolledUp({ top: 1199.5, height: 1700 })).toBe(false);
153+
expect(scrolledUp({ top: 1199, height: 1700 })).toBe(false);
154+
expect(scrolledUp({ top: 1198.5, height: 1700 })).toBe(true);
155155
});
156156

157-
it("ignores the offset clamping up when content above the end shrinks", () => {
157+
it("preserves upward new-turn positioning, then detects the reader leaving that anchor", () => {
158158
const scrolledUp = createUpwardScrollDetector({ top: 1400, height: 2400 });
159-
expect(scrolledUp({ top: 1100, height: 2100 })).toBe(false);
160-
// The next upward move is measured from the clamped position.
161-
expect(scrolledUp({ top: 900, height: 2100 })).toBe(true);
162-
});
163-
164-
it("ignores subpixel jitter", () => {
165-
const scrolledUp = createUpwardScrollDetector({ top: 500.5, height: 2000 });
166-
expect(scrolledUp({ top: 500, height: 2000 })).toBe(false);
167-
expect(scrolledUp({ top: 500.5, height: 2000 })).toBe(false);
168-
expect(scrolledUp({ top: 500, height: 2000 })).toBe(false);
169-
});
159+
// The caller must distinguish its animated positioning from iframe scroll chaining.
160+
const positioning = { top: 1200, height: 2400, programmatic: true };
161+
expect(scrolledUp(positioning)).toBe(false);
162+
expect(scrolledUp({ ...positioning, top: 1100 })).toBe(false);
170163

171-
it("adds up a slow scroll that moves under a pixel per event", () => {
172-
const scrolledUp = createUpwardScrollDetector({ top: 500, height: 2000 });
173-
expect(scrolledUp({ top: 499.5, height: 2000 })).toBe(false);
174-
expect(scrolledUp({ top: 499, height: 2000 })).toBe(false);
175-
expect(scrolledUp({ top: 498.5, height: 2000 })).toBe(true);
164+
expect(scrolledUp({ top: 1100, height: 2400 })).toBe(false);
165+
expect(scrolledUp({ top: 950, height: 2400 })).toBe(true);
176166
});
177167
});

‎apps/web/src/components/chat/timelineScrollTarget.ts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,11 @@ export function isTimelineScrollTarget(
2828
return true;
2929
}
3030

31-
/**
32-
* Reports, for each timeline scroll, whether the reader moved it up. A scroll
33-
* that chains out of an embedded frame (an HTML render or MCP app) reaches the
34-
* timeline with no wheel, touch, or pointer event it can see. Following only
35-
* moves toward the end, and shrinking content can only pull the offset up by
36-
* clamping, so an upward move while the content kept its height is the reader.
37-
*/
31+
// Sandboxed iframe gestures are invisible to the parent; only their chained scroll arrives.
3832
export function createUpwardScrollDetector(initial: { top: number; height: number }) {
3933
let baseline = initial;
40-
return (next: { top: number; height: number }) => {
41-
// Shrinking content or a move toward the end starts a new measurement.
42-
if (next.height < baseline.height || next.top > baseline.top) {
34+
return (next: { top: number; height: number; programmatic?: boolean }) => {
35+
if (next.programmatic || next.height < baseline.height || next.top > baseline.top) {
4336
baseline = next;
4437
return false;
4538
}

0 commit comments

Comments
 (0)