@@ -26,7 +26,6 @@ const clock = (ts: number) =>
2626 new Date(ts * 1000).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
2727
2828 export default function MessageList({
29- channelId,
3029 messages,
3130 username,
3231 collapsed,
@@ -38,7 +37,6 @@ export default function MessageList({
3837 onCancelEdit,
3938 onSaveEdit,
4039 }: {
41- channelId: number;
4240 messages: Message[];
4341 username: string;
4442 collapsed: Set<number>;
@@ -55,48 +53,42 @@ export default function MessageList({
5553 // The scrollTop we last set ourselves. Our scroll-to-bottom can queue a
5654 // scroll event that is handled only after a lazy image grows the list, so
5755 // the gap to the bottom reads large even though the reader never scrolled.
58- const pinnedTop = useRef<number | null>(null);
56+ const pinned = useRef(0);
5957
60- const scrollDown = () => {
58+ const pin = () => {
6159 const el = list.current;
62- if (!el) return;
60+ if (!el || !stick.current) return;
6361 el.scrollTop = el.scrollHeight;
64- pinnedTop.current = el.scrollTop;
62+ pinned.current = el.scrollTop;
6563 };
6664
67- // Jump to the newest message on channel switch, whatever the old position.
68- useLayoutEffect(() => {
69- stick.current = true;
70- scrollDown();
71- }, [channelId]);
72-
73- // Follow new messages only while the reader is pinned to the bottom.
74- useLayoutEffect(() => {
75- if (stick.current) scrollDown();
76- }, [messages]);
65+ // Follow new messages while the reader stays at the bottom. The channel is
66+ // remounted on switch, so this also lands on the newest message.
67+ useLayoutEffect(pin);
7768
78- // Images grow after the effects above, so keep following the bottom while
79- // the content resizes — unless the reader has scrolled up.
8069 useEffect(() => {
8170 const el = list.current;
8271 if (!el) return;
83- const ro = new ResizeObserver(() => stick.current && scrollDown());
72+ // Images load after the effect above, so follow the bottom on resize too.
73+ const ro = new ResizeObserver(pin);
8474 ro.observe(el.firstElementChild ?? el);
85- return () => ro.disconnect();
86- }, [channelId]);
87-
88- function onScroll() {
89- const el = list.current;
90- if (!el) return;
91- if (el.scrollHeight - el.scrollTop - el.clientHeight < 48) stick.current = true;
92- // A scroll event still sitting where we pinned is ours, not a reader
93- // scrolling up, so leave stick alone.
94- else if (pinnedTop.current === null || Math.abs(el.scrollTop - pinnedTop.current) >= 1)
95- stick.current = false;
96- }
75+ const onScroll = () => {
76+ // A scroll still sitting where we pinned is ours, so leave stick alone.
77+ if (el.scrollTop === pinned.current) return;
78+ stick.current = el.scrollTop + el.clientHeight >= el.scrollHeight - 2;
79+ };
80+ el.addEventListener("scroll", onScroll, { passive: true });
81+ return () => {
82+ ro.disconnect();
83+ el.removeEventListener("scroll", onScroll);
84+ };
85+ }, []);
9786
9887 return (
99- <div ref={list} onScroll={onScroll} className="min-h-0 flex-1 overflow-y-auto py-2">
88+ <div
89+ ref={list}
90+ className="min-h-0 flex-1 overflow-y-auto overscroll-contain py-2 [scrollbar-gutter:stable]"
91+ >
10092 <div>
10193 {messages.map((m, i) => {
10294 const prev = messages[i - 1];