Skip to content

Commit 7e10144

Browse files
fix(web): scrolling over an inline HTML render no longer snaps back to the bottom (pingdotgg#17065)
Co-authored-by: Julius Marminge <[email protected]>
1 parent acf7043 commit 7e10144

3 files changed

Lines changed: 89 additions & 26 deletions

File tree

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

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ import {
397397
import { environmentShell, useEnvironmentShellHasSnapshot } from "../state/shell";
398398
import { ChatComposer, type ChatComposerHandle } from "./chat/ChatComposer";
399399
import { createPageScrollController, type PageScrollKey } from "./chat/pageScrollController";
400-
import { isTimelineScrollTarget } from "./chat/timelineScrollTarget";
400+
import { createUpwardScrollDetector, isTimelineScrollTarget } from "./chat/timelineScrollTarget";
401401
import { DraftHeroHeadline } from "./chat/DraftHeroHeadline";
402402
import { ExpandedImageDialogHost, openExpandedImagePreview } from "./chat/ExpandedImageDialog";
403403
import {
@@ -4295,6 +4295,33 @@ export default function ChatView(props: ChatViewProps) {
42954295
break;
42964296
}
42974297
};
4298+
// Input inside an embedded frame reaches none of the listeners above,
4299+
// but its scroll still chains out to the timeline (#17056).
4300+
const scrolledUp = createUpwardScrollDetector({
4301+
top: scrollNode.scrollTop,
4302+
height: scrollNode.scrollHeight,
4303+
});
4304+
const handleScroll = () => {
4305+
if (
4306+
scrolledUp({
4307+
top: scrollNode.scrollTop,
4308+
height: scrollNode.scrollHeight,
4309+
programmatic:
4310+
positionedTimelineAnchorRef.current !== null &&
4311+
positionedTimelineAnchorRef.current !== settledTimelineAnchorRef.current,
4312+
}) &&
4313+
timelineScrollModeRef.current !== "free-scrolling" &&
4314+
// LegendList updates its scroll state on the next animation frame.
4315+
resolveTimelineIsAtEnd({
4316+
contentLength: scrollNode.scrollHeight,
4317+
scroll: scrollNode.scrollTop,
4318+
scrollLength: scrollNode.clientHeight,
4319+
}) === false
4320+
) {
4321+
handleManualNavigation();
4322+
}
4323+
};
4324+
scrollNode.addEventListener("scroll", handleScroll, { passive: true });
42984325
scrollNode.addEventListener("wheel", handleWheel, {
42994326
passive: true,
43004327
});
@@ -4306,6 +4333,7 @@ export default function ChatView(props: ChatViewProps) {
43064333
});
43074334
document.addEventListener("keydown", handleKeyDown);
43084335
removeListeners = () => {
4336+
scrollNode.removeEventListener("scroll", handleScroll);
43094337
scrollNode.removeEventListener("wheel", handleWheel);
43104338
scrollNode.removeEventListener("touchmove", handleTouchMove);
43114339
scrollNode.removeEventListener("pointerdown", handlePointerDown);
@@ -4351,30 +4379,22 @@ export default function ChatView(props: ChatViewProps) {
43514379
}
43524380
return;
43534381
}
4354-
const scrollNode = list.getScrollableNode();
4355-
let finished = false;
4356-
const finishAnimatedPositioning = () => {
4357-
if (finished) {
4358-
return;
4359-
}
4360-
finished = true;
4361-
window.clearTimeout(fallbackTimer);
4362-
scrollNode.removeEventListener("scrollend", finishAnimatedPositioning);
4363-
if (positionedTimelineAnchorRef.current !== messageId) {
4364-
return;
4365-
}
4366-
const scrollOffset = list.getState().scroll;
4367-
void list.scrollToOffset({ offset: scrollOffset, animated: false });
4368-
settledTimelineAnchorRef.current = messageId;
4369-
};
4370-
const fallbackTimer = window.setTimeout(finishAnimatedPositioning, 750);
4371-
scrollNode.addEventListener("scrollend", finishAnimatedPositioning, { once: true });
4372-
void list.scrollToIndex({
4373-
index: anchorIndex,
4374-
animated: !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
4375-
viewPosition: 0,
4376-
viewOffset: CHAT_LIST_ANCHOR_OFFSET,
4377-
});
4382+
// LegendList waits for queued layout and the animation before resolving.
4383+
void list
4384+
.scrollToIndex({
4385+
index: anchorIndex,
4386+
animated: !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
4387+
viewPosition: 0,
4388+
viewOffset: CHAT_LIST_ANCHOR_OFFSET,
4389+
})
4390+
.then(() => {
4391+
if (
4392+
legendListRef.current === list &&
4393+
positionedTimelineAnchorRef.current === messageId
4394+
) {
4395+
settledTimelineAnchorRef.current = messageId;
4396+
}
4397+
});
43784398
});
43794399
};
43804400
requestAnimationFrame(() => positionAnchor(12));

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
createComposerScrollGestureState,
55
recordComposerScrollGestureEvent,
66
} from "./composerScrollGesture";
7-
import { isTimelineScrollTarget } from "./timelineScrollTarget";
7+
import { createUpwardScrollDetector, isTimelineScrollTarget } from "./timelineScrollTarget";
88

99
class ScrollElement extends EventTarget {
1010
scrollTop = 0;
@@ -140,3 +140,28 @@ describe("timeline scroll targets", () => {
140140
expect(record(content, 60, 14)).toBe(true);
141141
});
142142
});
143+
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);
150+
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);
155+
});
156+
157+
it("preserves upward new-turn positioning, then detects the reader leaving that anchor", () => {
158+
const scrolledUp = createUpwardScrollDetector({ top: 1400, height: 2400 });
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);
163+
164+
expect(scrolledUp({ top: 1100, height: 2400 })).toBe(false);
165+
expect(scrolledUp({ top: 950, height: 2400 })).toBe(true);
166+
});
167+
});

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,21 @@ export function isTimelineScrollTarget(
2727
}
2828
return true;
2929
}
30+
31+
// Sandboxed iframe gestures are invisible to the parent; only their chained scroll arrives.
32+
export function createUpwardScrollDetector(initial: { top: number; height: number }) {
33+
let baseline = initial;
34+
return (next: { top: number; height: number; programmatic?: boolean }) => {
35+
if (next.programmatic || next.height < baseline.height || next.top > baseline.top) {
36+
baseline = next;
37+
return false;
38+
}
39+
if (next.top < baseline.top - 1) {
40+
baseline = next;
41+
return true;
42+
}
43+
// A slow scroll can move under a pixel per event; let those moves add up.
44+
baseline = { top: baseline.top, height: next.height };
45+
return false;
46+
};
47+
}

0 commit comments

Comments
 (0)