Skip to content
TypeScript

fix(web): scrolling over an inline HTML render no longer snaps back to the bottom (#17065)

Joseph Vidal7e101444a1fe
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index 3c811d2cb51..51c468261b0 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -397,7 +397,7 @@ import {
import { environmentShell, useEnvironmentShellHasSnapshot } from "../state/shell";
import { ChatComposer, type ChatComposerHandle } from "./chat/ChatComposer";
import { createPageScrollController, type PageScrollKey } from "./chat/pageScrollController";
-import { isTimelineScrollTarget } from "./chat/timelineScrollTarget";
+import { createUpwardScrollDetector, isTimelineScrollTarget } from "./chat/timelineScrollTarget";
import { DraftHeroHeadline } from "./chat/DraftHeroHeadline";
import { ExpandedImageDialogHost, openExpandedImagePreview } from "./chat/ExpandedImageDialog";
import {
@@ -4295,6 +4295,33 @@ export default function ChatView(props: ChatViewProps) {
break;
}
};
+ // Input inside an embedded frame reaches none of the listeners above,
+ // but its scroll still chains out to the timeline (#17056).
+ const scrolledUp = createUpwardScrollDetector({
+ top: scrollNode.scrollTop,
+ height: scrollNode.scrollHeight,
+ });
+ const handleScroll = () => {
+ if (
+ scrolledUp({
+ top: scrollNode.scrollTop,
+ height: scrollNode.scrollHeight,
+ programmatic:
+ positionedTimelineAnchorRef.current !== null &&
+ positionedTimelineAnchorRef.current !== settledTimelineAnchorRef.current,
+ }) &&
+ timelineScrollModeRef.current !== "free-scrolling" &&
+ // LegendList updates its scroll state on the next animation frame.
+ resolveTimelineIsAtEnd({
+ contentLength: scrollNode.scrollHeight,
+ scroll: scrollNode.scrollTop,
+ scrollLength: scrollNode.clientHeight,
+ }) === false
+ ) {
+ handleManualNavigation();
+ }
+ };
+ scrollNode.addEventListener("scroll", handleScroll, { passive: true });
scrollNode.addEventListener("wheel", handleWheel, {
passive: true,
});
@@ -4306,6 +4333,7 @@ export default function ChatView(props: ChatViewProps) {
});
document.addEventListener("keydown", handleKeyDown);
removeListeners = () => {
+ scrollNode.removeEventListener("scroll", handleScroll);
scrollNode.removeEventListener("wheel", handleWheel);
scrollNode.removeEventListener("touchmove", handleTouchMove);
scrollNode.removeEventListener("pointerdown", handlePointerDown);
@@ -4351,30 +4379,22 @@ export default function ChatView(props: ChatViewProps) {
}
return;
}
- const scrollNode = list.getScrollableNode();
- let finished = false;
- const finishAnimatedPositioning = () => {
- if (finished) {
- return;
- }
- finished = true;
- window.clearTimeout(fallbackTimer);
- scrollNode.removeEventListener("scrollend", finishAnimatedPositioning);
- if (positionedTimelineAnchorRef.current !== messageId) {
- return;
- }
- const scrollOffset = list.getState().scroll;
- void list.scrollToOffset({ offset: scrollOffset, animated: false });
- settledTimelineAnchorRef.current = messageId;
- };
- const fallbackTimer = window.setTimeout(finishAnimatedPositioning, 750);
- scrollNode.addEventListener("scrollend", finishAnimatedPositioning, { once: true });
- void list.scrollToIndex({
- index: anchorIndex,
- animated: !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
- viewPosition: 0,
- viewOffset: CHAT_LIST_ANCHOR_OFFSET,
- });
+ // LegendList waits for queued layout and the animation before resolving.
+ void list
+ .scrollToIndex({
+ index: anchorIndex,
+ animated: !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
+ viewPosition: 0,
+ viewOffset: CHAT_LIST_ANCHOR_OFFSET,
+ })
+ .then(() => {
+ if (
+ legendListRef.current === list &&
+ positionedTimelineAnchorRef.current === messageId
+ ) {
+ settledTimelineAnchorRef.current = messageId;
+ }
+ });
});
};
requestAnimationFrame(() => positionAnchor(12));
diff --git a/apps/web/src/components/chat/timelineScrollTarget.test.ts b/apps/web/src/components/chat/timelineScrollTarget.test.ts
index 051f7785f5a..410f3474171 100644
--- a/apps/web/src/components/chat/timelineScrollTarget.test.ts
+++ b/apps/web/src/components/chat/timelineScrollTarget.test.ts
@@ -4,7 +4,7 @@ import {
createComposerScrollGestureState,
recordComposerScrollGestureEvent,
} from "./composerScrollGesture";
-import { isTimelineScrollTarget } from "./timelineScrollTarget";
+import { createUpwardScrollDetector, isTimelineScrollTarget } from "./timelineScrollTarget";
class ScrollElement extends EventTarget {
scrollTop = 0;
@@ -140,3 +140,28 @@ describe("timeline scroll targets", () => {
expect(record(content, 60, 14)).toBe(true);
});
});
+
+describe("timeline scroll follow opt-out", () => {
+ it("detects iframe-only upward scrolling after following streaming content", () => {
+ const scrolledUp = createUpwardScrollDetector({ top: 1000, height: 1500 });
+ expect(scrolledUp({ top: 1000, height: 1800 })).toBe(false);
+ expect(scrolledUp({ top: 1300, height: 1800 })).toBe(false);
+ expect(scrolledUp({ top: 1200, height: 1700 })).toBe(false);
+
+ // Sandboxed frames deliver no wheel or touch event to the parent timeline.
+ expect(scrolledUp({ top: 1199.5, height: 1700 })).toBe(false);
+ expect(scrolledUp({ top: 1199, height: 1700 })).toBe(false);
+ expect(scrolledUp({ top: 1198.5, height: 1700 })).toBe(true);
+ });
+
+ it("preserves upward new-turn positioning, then detects the reader leaving that anchor", () => {
+ const scrolledUp = createUpwardScrollDetector({ top: 1400, height: 2400 });
+ // The caller must distinguish its animated positioning from iframe scroll chaining.
+ const positioning = { top: 1200, height: 2400, programmatic: true };
+ expect(scrolledUp(positioning)).toBe(false);
+ expect(scrolledUp({ ...positioning, top: 1100 })).toBe(false);
+
+ expect(scrolledUp({ top: 1100, height: 2400 })).toBe(false);
+ expect(scrolledUp({ top: 950, height: 2400 })).toBe(true);
+ });
+});
diff --git a/apps/web/src/components/chat/timelineScrollTarget.ts b/apps/web/src/components/chat/timelineScrollTarget.ts
index 9434f079d27..49314f006f5 100644
--- a/apps/web/src/components/chat/timelineScrollTarget.ts
+++ b/apps/web/src/components/chat/timelineScrollTarget.ts
@@ -27,3 +27,21 @@ export function isTimelineScrollTarget(
}
return true;
}
+
+// Sandboxed iframe gestures are invisible to the parent; only their chained scroll arrives.
+export function createUpwardScrollDetector(initial: { top: number; height: number }) {
+ let baseline = initial;
+ return (next: { top: number; height: number; programmatic?: boolean }) => {
+ if (next.programmatic || next.height < baseline.height || next.top > baseline.top) {
+ baseline = next;
+ return false;
+ }
+ if (next.top < baseline.top - 1) {
+ baseline = next;
+ return true;
+ }
+ // A slow scroll can move under a pixel per event; let those moves add up.
+ baseline = { top: baseline.top, height: next.height };
+ return false;
+ };
+}