Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Trim comments and pin scrollMargin in the near-screen test

AdityaUmale98865953f6c6
diff --git a/src/components/ChatMarkdown.test.ts b/src/components/ChatMarkdown.test.ts
index 2739009dd1..1c5a87ab89 100644
--- a/src/components/ChatMarkdown.test.ts
+++ b/src/components/ChatMarkdown.test.ts
@@ -59,7 +59,7 @@ vi.mock("shiki/engine/javascript", () => ({
createJavaScriptRegexEngine: () => ({}),
}));
// These tests pin how a block is coloured, not when: without an observer a block counts as near the screen at once.
-// (Removed outright rather than stubbed, since tests below undo every stubbed global.)
+// Tests below undo every stubbed global, so the observer is deleted instead.
delete (globalThis as { IntersectionObserver?: unknown }).IntersectionObserver;
describe("soft line breaks", () => {
diff --git a/src/lib/on-screen.test.ts b/src/lib/on-screen.test.ts
index f4586fdcc9..0dfda0954c 100644
--- a/src/lib/on-screen.test.ts
+++ b/src/lib/on-screen.test.ts
@@ -7,7 +7,6 @@ import { useNearScreen } from "./on-screen";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
-// The module makes its observer once, on first use, so only the first test that watches sees this fake's reports.
class FakeObserver {
static last: FakeObserver | undefined;
watched = new Set<Element>();
@@ -49,7 +48,7 @@ describe("useNearScreen", () => {
const watcher = FakeObserver.last!;
const element = host.firstElementChild!;
expect([...watcher.watched]).toEqual([element]);
- expect(watcher.options).toMatchObject({ rootMargin: "800px 0px" });
+ expect(watcher.options).toMatchObject({ rootMargin: "800px 0px", scrollMargin: "800px 0px" });
act(() => watcher.report([{ target: element, isIntersecting: false }]));
expect(near()).toBe("false");
act(() => watcher.report([{ target: element, isIntersecting: true }]));
diff --git a/src/lib/on-screen.ts b/src/lib/on-screen.ts
index b3ede64bb7..c8478064a0 100644
--- a/src/lib/on-screen.ts
+++ b/src/lib/on-screen.ts
@@ -38,11 +38,9 @@ export function useOnScreen(ref: RefObject<Element | null>): boolean {
return onScreen;
}
-/** How far from the screen, in pixels, an element counts as near it. */
const NEAR_SCREEN_MARGIN = 800;
-/** Whether the element has come within NEAR_SCREEN_MARGIN of the screen yet. It stays true once it has, and is true at
- * once where the browser cannot tell. */
+/** Stays true once the element has come near the screen; true at once where the browser has no IntersectionObserver. */
export function useNearScreen(ref: RefObject<Element | null>): boolean {
const [near, setNear] = useState(() => typeof IntersectionObserver === "undefined");
useEffect(() => {