Files
CatoandClaude b53f8e615a fix(thread): following never scrolls the page up, and --ck-follow-gap (#88)
On bitbaum /hire/ at 390x844 the contact box (a ChatThread, a receipt link
and the Composer) sits mid-page and the document owns the scroll. The
reader's first message pinned the end of the thread to the bottom of the
viewport, which scrolled the page UP by about 300px and pushed the composer
they were typing in below the fold.

What a consumer gets:
- When the document owns the scroll, following only moves the page down:
  with the end of the thread already on screen, nothing moves. A thread or
  panel that scrolls itself is unchanged, and the jump button still goes to
  the latest turn in either direction, because the reader asked.
- --ck-follow-gap (a length, default 0) on the thread's wrapper or any
  ancestor leaves that much room under the latest turn when the page
  follows, so a composer under the thread stays in view. The browser
  resolves it (rem, em, px, calc); unset, negative or invalid is 0.
- While the reader has sent nothing on a page, nothing moves, as before.

How it is checked: test/browser-scroll.mjs gains a mid-page fixture
(demo/scroll.html?owner=page&greeting&below) at 390 and 1280px. With the
composer mid-screen the first message does not scroll the page up and the
composer stays on screen; as the thread grows past the foot of the screen
the page follows and leaves the end at 0px, 10rem, 6em, 90px and (invalid)
0px above the bottom of the viewport, within 2px. Both checks fail on the
previous code; every existing scroll case passes unchanged.

Claude-Session: https://claude.ai/code/session_019MDtmsSWTTpVpe4wh3v1sb

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 10:30:35 +01:00

120 lines
4.1 KiB
TypeScript

// Synthetic page-scroll and nested-scroll fixtures; no customer conversation.
import { useEffect, useRef, useState } from "react";
import { createRoot } from "react-dom/client";
import { Composer, ChatThread, type ChatMessageData } from "../src/react/index.js";
function ScrollFixture() {
const [tick, setTick] = useState(0);
const [sending, setSending] = useState(false);
const complete = useRef<(() => void) | null>(null);
// `?greeting`: a contact box at the foot of a long page. One greeting from
// the assistant, nothing from the reader yet.
const greeting = new URLSearchParams(location.search).has("greeting");
// `&below`: more page after the composer, so the box sits mid-page as on a
// real site (bitbaum /hire/: a receipt, then the composer, then the footer).
const below = new URLSearchParams(location.search).has("below");
const [messages, setMessages] = useState<ChatMessageData[]>(
greeting
? [{ id: "hello", role: "assistant", content: "Hello. What would you like done?" }]
: [
{ id: "question", role: "user", content: "Explain the synthetic example." },
{
id: "answer",
role: "assistant",
content: "A synthetic paragraph for scroll testing.\n\n".repeat(90),
},
],
);
useEffect(() => {
const timer = setInterval(() => setTick((n) => n + 1), 150);
return () => clearInterval(timer);
}, []);
const mode = new URLSearchParams(location.search).get("owner") ?? "page";
return (
<>
<div id="fixture-controls" style={{ position: "fixed", top: 0, left: 0, zIndex: 20 }}>
<button
id="assistant-turn"
onClick={() =>
setMessages((m) => [
...m,
{ id: `a${m.length}`, role: "assistant", content: "A completed answer." },
])
}
>
Complete answer
</button>
<button
id="user-turn"
onClick={() =>
setMessages((m) => [
...m,
{ id: `u${m.length}`, role: "user", content: "Another question." },
])
}
>
Send question
</button>
<button id="finish-send" onClick={() => complete.current?.()}>
Finish pending send
</button>
<output id="fixture-sending">{String(sending)}</output>
<output id="fixture-tick">{tick}</output>
</div>
{greeting && (
<div id="fixture-page" style={{ height: "3000px" }}>
The page above the contact box.
</div>
)}
<div
id="fixture-owner"
style={mode === "ancestor" ? { height: "90vh", overflowY: "auto" } : {}}
>
<div
style={
mode === "panel" ? { height: "90vh", display: "flex", flexDirection: "column" } : {}
}
>
<ChatThread
messages={messages}
live={greeting ? null : { status: `Checking details ${tick}` }}
/>
<Composer
placeholder="Synthetic message"
voice={false}
attach={false}
sending={sending}
onSend={(text) => {
setSending(true);
setMessages((m) => [...m, { id: `u${m.length}`, role: "user", content: text }]);
return new Promise<boolean>((resolve) => {
complete.current = () => {
setMessages((m) => [
...m,
{
id: `a${m.length}`,
role: "assistant",
content: "Completed synthetic reply.",
},
]);
setSending(false);
resolve(true);
};
});
}}
/>
</div>
</div>
{below && (
<div id="fixture-below" style={{ height: "1500px" }}>
The page below the contact box.
</div>
)}
</>
);
}
export function mountScrollFixture() {
const root = document.getElementById("scroll-fixture");
if (root) createRoot(root).render(<ScrollFixture />);
}