Skip to content

Commit b2d72ca

Browse files
committed
fix(web): count slow sub-pixel scrolls when leaving live-follow
1 parent d60783e commit b2d72ca

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,14 @@ describe("createUpwardScrollDetector", () => {
164164
it("ignores subpixel jitter", () => {
165165
const scrolledUp = createUpwardScrollDetector({ top: 500.5, height: 2000 });
166166
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+
});
170+
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);
167176
});
168177
});

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,19 @@ export function isTimelineScrollTarget(
3838
* clamping, so an upward move while the content kept its height is the reader.
3939
*/
4040
export function createUpwardScrollDetector(initial: { top: number; height: number }) {
41-
let previous = initial;
41+
let baseline = initial;
4242
return (next: { top: number; height: number }) => {
43-
const movedUp = next.top < previous.top - 1 && next.height >= previous.height;
44-
previous = next;
45-
return movedUp;
43+
// Shrinking content or a move toward the end starts a new measurement.
44+
if (next.height < baseline.height || next.top > baseline.top) {
45+
baseline = next;
46+
return false;
47+
}
48+
if (next.top < baseline.top - 1) {
49+
baseline = next;
50+
return true;
51+
}
52+
// A slow scroll can move under a pixel per event; let those moves add up.
53+
baseline = { top: baseline.top, height: next.height };
54+
return false;
4655
};
4756
}

0 commit comments

Comments
 (0)