Skip to content

Commit 3960c19

Browse files
author
Star Lord
committed
Merge main into composer validation repairs
Signed-off-by: Star Lord <b89298dbe87c6b3fd8a425b535d9d161f23c88d555a47925ae0b04dfe02b201e@buzz.block.builderlab.xyz>
2 parents 335c66d + 0124f3f commit 3960c19

4 files changed

Lines changed: 136 additions & 2 deletions

File tree

‎src/features/messages/Messages.module.css‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ html[data-keyboard-navigation] .attachmentImage:focus-visible {
579579
overflow: auto;
580580
padding: 0 var(--space-panel-inset);
581581
padding-bottom: var(--space-panel-inset);
582+
/* Older-history corrections hide overflow for a task (scroll-correction.ts). */
583+
scrollbar-gutter: stable;
582584
scrollbar-width: thin;
583585
scrollbar-color: color-mix(in srgb, var(--text) 20%, transparent) transparent;
584586
}

‎src/features/messages/ThreadPanel.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import type { PageNavigation } from "../navigation/service";
3636
import { messageViewKey } from "./view-key";
3737
import { useKnownAgentPubkeys } from "../agents/use-known";
3838
import { JumpToLatestButton } from "./JumpToLatestButton";
39+
import { correctScrollTop } from "./scroll-correction";
3940

4041
export type ThreadPanelProps = {
4142
extensions?: ConversationExtensions | undefined;
@@ -584,14 +585,17 @@ function ThreadMessages({
584585
...element.querySelectorAll<HTMLElement>("[data-message-id]"),
585586
].find((row) => row.dataset.messageId === anchor.id);
586587
if (row)
587-
element.scrollTop += row.getBoundingClientRect().top - anchor.top;
588+
correctScrollTop(
589+
element,
590+
row.getBoundingClientRect().top - anchor.top,
591+
);
588592
if (snapshot.status !== "loading") olderAnchor.current = undefined;
589593
}
590594
}
591595
if (targetAnchor.current !== undefined) {
592596
const offset = selectedOffset();
593597
if (offset !== undefined) {
594-
element.scrollTop += offset - targetAnchor.current;
598+
correctScrollTop(element, offset - targetAnchor.current);
595599
targetAnchor.current = offset;
596600
follow.current = false;
597601
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// @vitest-environment jsdom
2+
import { afterEach, beforeEach, expect, it, vi } from "vitest";
3+
import { correctScrollTop } from "./scroll-correction";
4+
5+
const stubNavigator = (platform: string, vendor: string, maxTouchPoints = 0) =>
6+
vi.stubGlobal("navigator", { platform, vendor, maxTouchPoints });
7+
8+
function scroller() {
9+
const element = document.createElement("section");
10+
let top = 100;
11+
Object.defineProperty(element, "scrollTop", {
12+
get: () => top,
13+
set: (value: number) => {
14+
top = value;
15+
},
16+
});
17+
return element;
18+
}
19+
20+
const overflow = (element: HTMLElement) => [
21+
element.style.getPropertyValue("overflow-y"),
22+
element.style.getPropertyPriority("overflow-y"),
23+
];
24+
25+
beforeEach(() => {
26+
vi.useFakeTimers();
27+
stubNavigator("MacIntel", "Apple Computer, Inc.");
28+
});
29+
30+
afterEach(() => {
31+
vi.useRealTimers();
32+
vi.unstubAllGlobals();
33+
});
34+
35+
it("interrupts Mac WebKit momentum for one task around the correction", () => {
36+
const element = scroller();
37+
element.style.setProperty("overflow-y", "scroll", "important");
38+
39+
correctScrollTop(element, 40);
40+
41+
expect(element.scrollTop).toBe(140);
42+
expect(overflow(element)).toEqual(["hidden", "important"]);
43+
vi.runAllTimers();
44+
expect(overflow(element)).toEqual(["scroll", "important"]);
45+
});
46+
47+
it("restores the original declaration across overlapping corrections", () => {
48+
const element = scroller();
49+
50+
correctScrollTop(element, 40);
51+
correctScrollTop(element, -10);
52+
53+
expect(element.scrollTop).toBe(130);
54+
expect(overflow(element)).toEqual(["hidden", "important"]);
55+
vi.runAllTimers();
56+
expect(element.getAttribute("style")).toBe("");
57+
});
58+
59+
it("keeps a declaration written after the correction", () => {
60+
const element = scroller();
61+
62+
correctScrollTop(element, 40);
63+
element.style.overflowY = "auto";
64+
vi.runAllTimers();
65+
66+
expect(element.style.getPropertyValue("overflow-y")).toBe("auto");
67+
});
68+
69+
it.each([
70+
["Chromium", "MacIntel", "Google Inc.", 0],
71+
["desktop-mode iPad", "MacIntel", "Apple Computer, Inc.", 5],
72+
["non-Mac WebKit", "Linux x86_64", "Apple Computer, Inc.", 0],
73+
])("leaves %s momentum alone", (_, platform, vendor, touchPoints) => {
74+
stubNavigator(platform, vendor, touchPoints);
75+
const element = scroller();
76+
77+
correctScrollTop(element, 40);
78+
79+
expect(element.scrollTop).toBe(140);
80+
expect(element.getAttribute("style")).toBeNull();
81+
expect(vi.getTimerCount()).toBe(0);
82+
});
83+
84+
it("ignores a zero correction", () => {
85+
const element = scroller();
86+
87+
correctScrollTop(element, 0);
88+
89+
expect(element.scrollTop).toBe(100);
90+
expect(element.getAttribute("style")).toBeNull();
91+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const restores = new WeakMap<HTMLElement, () => void>();
2+
const macWebKit = () =>
3+
navigator.platform === "MacIntel" &&
4+
!navigator.maxTouchPoints && // desktop-mode iPad keeps its momentum policy
5+
navigator.vendor === "Apple Computer, Inc.";
6+
7+
/**
8+
* Applies an automatic scroll correction. Mac WebKit can leave the corrected
9+
* viewport unpainted while native momentum continues, so stop the momentum the
10+
* same way the Virtua patch does (patches/README.md): hide vertical overflow for
11+
* one task. The scroller must keep its inline size (`scrollbar-gutter: stable`).
12+
*/
13+
export function correctScrollTop(element: HTMLElement, delta: number) {
14+
if (!delta) return;
15+
if (macWebKit()) {
16+
// Corrections in one task share the original declaration.
17+
restores.get(element)?.();
18+
const { style } = element;
19+
const value = style.getPropertyValue("overflow-y");
20+
const priority = style.getPropertyPriority("overflow-y");
21+
style.setProperty("overflow-y", "hidden", "important");
22+
const restore = () => {
23+
clearTimeout(timer);
24+
restores.delete(element);
25+
if (
26+
style.getPropertyValue("overflow-y") === "hidden" &&
27+
style.getPropertyPriority("overflow-y") === "important"
28+
)
29+
value
30+
? style.setProperty("overflow-y", value, priority)
31+
: style.removeProperty("overflow-y");
32+
};
33+
const timer = setTimeout(restore);
34+
restores.set(element, restore);
35+
}
36+
element.scrollTop += delta;
37+
}

0 commit comments

Comments
 (0)