Skip to content

Commit 68607c5

Browse files
authored
fix(web): preserve nested scroll behavior in chat timeline (#13167)
1 parent f223312 commit 68607c5

6 files changed

Lines changed: 194 additions & 155 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 1 addition & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
TurnId,
1313
type WorktreeSetupSnapshot,
1414
} from "@t3tools/contracts";
15-
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
15+
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
1616
import { Atom, AsyncResult } from "effect/unstable/reactivity";
1717
import { appAtomRegistry } from "../rpc/atomRegistry";
1818
import { environmentThreadDetails } from "../state/threads";
@@ -86,7 +86,6 @@ import {
8686
shouldShowBranchMismatchBanner,
8787
shouldShowPlanFollowUpPrompt,
8888
shouldWriteThreadErrorToCurrentServerThread,
89-
toolGroupConsumesUpwardNavigation,
9089
waitForRevertedMessage,
9190
prepareRevertedMessageAttachments,
9291
} from "./ChatView.logic";
@@ -446,134 +445,6 @@ describe("proactive panels", () => {
446445
});
447446
});
448447

449-
describe("toolGroupConsumesUpwardNavigation", () => {
450-
class ScrollElement extends EventTarget {
451-
scrollTop = 0;
452-
scrollHeight = 100;
453-
clientHeight = 100;
454-
overflowY = "visible";
455-
456-
constructor(
457-
readonly parentElement: ScrollElement | null = null,
458-
readonly isToolGroup = false,
459-
) {
460-
super();
461-
}
462-
463-
closest(selector: string): ScrollElement | null {
464-
if (selector !== "[data-tool-group-scroll]") return null;
465-
return this.isToolGroup ? this : (this.parentElement?.closest(selector) ?? null);
466-
}
467-
}
468-
469-
beforeEach(() => {
470-
vi.stubGlobal("Element", ScrollElement);
471-
vi.stubGlobal("getComputedStyle", (element: ScrollElement) => ({
472-
overflowY: element.overflowY,
473-
}));
474-
});
475-
afterEach(() => vi.unstubAllGlobals());
476-
477-
it("releases upward navigation when an overflowing group is at the top", () => {
478-
const group = Object.assign(new ScrollElement(null, true), {
479-
overflowY: "auto",
480-
scrollHeight: 300,
481-
});
482-
483-
expect(toolGroupConsumesUpwardNavigation(new ScrollElement(group))).toBe(false);
484-
});
485-
486-
it.each([
487-
{ overflowY: "auto", scrollTop: 1 },
488-
{ overflowY: "auto", scrollTop: 0.25 },
489-
{ overflowY: "scroll", scrollTop: 80 },
490-
])("consumes upward navigation within a scrolled group: %j", (scroll) => {
491-
const group = Object.assign(new ScrollElement(null, true), {
492-
scrollHeight: 300,
493-
...scroll,
494-
});
495-
496-
expect(toolGroupConsumesUpwardNavigation(group)).toBe(true);
497-
});
498-
499-
it.each([100, 300])(
500-
"consumes scrolling in a nested result with a group content height of %i",
501-
(scrollHeight) => {
502-
const group = Object.assign(new ScrollElement(null, true), {
503-
overflowY: "auto",
504-
scrollHeight,
505-
});
506-
const result = Object.assign(new ScrollElement(group), {
507-
overflowY: "auto",
508-
scrollHeight: 300,
509-
scrollTop: 0.25,
510-
});
511-
512-
expect(toolGroupConsumesUpwardNavigation(new ScrollElement(result))).toBe(true);
513-
},
514-
);
515-
516-
it("releases upward navigation when the group and nested result are both at the top", () => {
517-
const group = Object.assign(new ScrollElement(null, true), {
518-
overflowY: "auto",
519-
scrollHeight: 300,
520-
});
521-
const result = Object.assign(new ScrollElement(group), {
522-
overflowY: "scroll",
523-
scrollHeight: 300,
524-
});
525-
526-
expect(toolGroupConsumesUpwardNavigation(new ScrollElement(result))).toBe(false);
527-
});
528-
529-
it("ignores targets outside a tool group and non-element targets", () => {
530-
const outside = Object.assign(new ScrollElement(), {
531-
overflowY: "auto",
532-
scrollHeight: 300,
533-
scrollTop: 40,
534-
});
535-
536-
expect(toolGroupConsumesUpwardNavigation(outside)).toBe(false);
537-
expect(toolGroupConsumesUpwardNavigation(new EventTarget())).toBe(false);
538-
expect(toolGroupConsumesUpwardNavigation(null)).toBe(false);
539-
});
540-
541-
it("does not consume scrolling from an ancestor beyond the tool group", () => {
542-
const timeline = Object.assign(new ScrollElement(), {
543-
overflowY: "auto",
544-
scrollHeight: 300,
545-
scrollTop: 40,
546-
});
547-
const group = new ScrollElement(timeline, true);
548-
549-
expect(toolGroupConsumesUpwardNavigation(new ScrollElement(group))).toBe(false);
550-
});
551-
552-
it.each(["hidden", "clip", "visible"])(
553-
"ignores a non-scrollable child with overflow-y %s",
554-
(overflowY) => {
555-
const group = new ScrollElement(null, true);
556-
const result = Object.assign(new ScrollElement(group), {
557-
overflowY,
558-
scrollHeight: 300,
559-
scrollTop: 40,
560-
});
561-
562-
expect(toolGroupConsumesUpwardNavigation(new ScrollElement(result))).toBe(false);
563-
},
564-
);
565-
566-
it("does not consume programmatic scrolling on an overflow-hidden group", () => {
567-
const group = Object.assign(new ScrollElement(null, true), {
568-
overflowY: "hidden",
569-
scrollHeight: 300,
570-
scrollTop: 40,
571-
});
572-
573-
expect(toolGroupConsumesUpwardNavigation(group)).toBe(false);
574-
});
575-
});
576-
577448
const environmentId = EnvironmentId.make("environment-local");
578449
const projectId = ProjectId.make("project-1");
579450
const threadId = ThreadId.make("thread-1");

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,6 @@ export function shouldReleaseTimelineAnchorForToolActivity(input: {
239239
});
240240
}
241241

242-
export function toolGroupConsumesUpwardNavigation(target: EventTarget | null): boolean {
243-
const elementTarget = target instanceof Element ? target : null;
244-
const group = elementTarget?.closest<HTMLElement>("[data-tool-group-scroll]");
245-
if (!group) return false;
246-
247-
// A nested result or the group itself can consume an upward scroll.
248-
for (let element = elementTarget; element; element = element.parentElement) {
249-
if (element.scrollTop > 0) {
250-
const overflowY = getComputedStyle(element).overflowY;
251-
if (overflowY === "auto" || overflowY === "scroll") return true;
252-
}
253-
if (element === group) break;
254-
}
255-
return false;
256-
}
257-
258242
export {
259243
findRecordedWorktreeSetup,
260244
resolveVisibleWorktreeSetup,

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ import {
365365
import { environmentShell } from "../state/shell";
366366
import { ChatComposer, type ChatComposerHandle } from "./chat/ChatComposer";
367367
import { createPageScrollController, type PageScrollKey } from "./chat/pageScrollController";
368+
import { isTimelineScrollTarget } from "./chat/timelineScrollTarget";
368369
import { DraftHeroHeadline } from "./chat/DraftHeroHeadline";
369370
import { ExpandedImageDialog } from "./chat/ExpandedImageDialog";
370371
import { PullRequestThreadDialog } from "./PullRequestThreadDialog";
@@ -474,7 +475,6 @@ import {
474475
shouldWriteThreadErrorToCurrentServerThread,
475476
startNewThreadForProject,
476477
codexArtifactTemplatePromptToAppend,
477-
toolGroupConsumesUpwardNavigation,
478478
waitForStartedServerThread,
479479
shouldRefocusComposerOnWindowFocus,
480480
} from "./ChatView.logic";
@@ -5466,6 +5466,8 @@ export default function ChatView(props: ChatViewProps) {
54665466
// Only an upward wheel is a navigation intent; wheeling down while
54675467
// following either does nothing (at the end) or moves toward it.
54685468
const handleWheel = (event: WheelEvent) => {
5469+
if (event.ctrlKey || !isTimelineScrollTarget(event.target, scrollNode, event.deltaY))
5470+
return;
54695471
if (event.deltaY > 0) {
54705472
timelineScrollIntentRef.current = "toward-end";
54715473
if (isAtEndRef.current) {
@@ -5474,11 +5476,7 @@ export default function ChatView(props: ChatViewProps) {
54745476
} else if (event.deltaY < 0) {
54755477
timelineScrollIntentRef.current = "away-from-end";
54765478
}
5477-
if (
5478-
event.deltaY < 0 &&
5479-
contentScrollsUp() &&
5480-
!toolGroupConsumesUpwardNavigation(event.target)
5481-
) {
5479+
if (event.deltaY < 0 && contentScrollsUp()) {
54825480
handleManualNavigation();
54835481
}
54845482
};
@@ -5528,12 +5526,20 @@ export default function ChatView(props: ChatViewProps) {
55285526
) {
55295527
return;
55305528
}
5529+
if (!["PageUp", "Home", "ArrowUp", "PageDown", "End", "ArrowDown"].includes(event.key))
5530+
return;
5531+
const scrollDirection = ["PageUp", "Home", "ArrowUp"].includes(event.key) ? -1 : 1;
5532+
if (
5533+
scrollNode.contains(event.target) &&
5534+
!isTimelineScrollTarget(event.target, scrollNode, scrollDirection)
5535+
)
5536+
return;
55315537
switch (event.key) {
55325538
case "PageUp":
55335539
case "Home":
55345540
case "ArrowUp":
55355541
timelineScrollIntentRef.current = "away-from-end";
5536-
if (contentScrollsUp() && !toolGroupConsumesUpwardNavigation(event.target)) {
5542+
if (contentScrollsUp()) {
55375543
handleManualNavigation();
55385544
composerRef.current?.collapseForTimelineScrollKey(event.key);
55395545
}

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ import {
310310
import { ComposerPromptLengthValidation } from "./ComposerPromptLengthValidation";
311311
import { PierreEntryIcon } from "./PierreEntryIcon";
312312
import { pendingDraftWork } from "./pendingDraftWork";
313+
import { isTimelineScrollTarget } from "./timelineScrollTarget";
313314
import {
314315
createComposerScrollGestureState,
315316
recordComposerScrollGestureEvent,
@@ -4894,8 +4895,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
48944895

48954896
const scrollNode = getTimelineScrollableNode();
48964897
if (!scrollNode) return;
4897-
const targetsTimeline = scrollNode.contains(event.target);
4898-
if (!targetsTimeline && !composerScrollGestureRef.current.collapseSuppressed) return;
4898+
const targetsTimeline = isTimelineScrollTarget(event.target, scrollNode, event.deltaY);
4899+
if (
4900+
!scrollNode.contains(event.target) &&
4901+
!composerScrollGestureRef.current.collapseSuppressed
4902+
)
4903+
return;
48994904

49004905
if (composerScrollCollapseTimeoutRef.current !== null) {
49014906
window.clearTimeout(composerScrollCollapseTimeoutRef.current);

0 commit comments

Comments
 (0)