xonecas.io
8058
Ir

irvc / commit 68ddbd2

Merge pull request #3 from sacenox/fix/scrolling-use-a-minimal-js-alternative

fix: scrolling left to CSS we only JS to pin the users scroll

xonecas2026-10-16 68ddbd29db7827fd192d99d81dea4155908f3cf8 parent 31f4c7cparent bea498b

4 files changed

web/src/components/MessageList.tsx+24−32
@@ -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];
web/src/components/Sidebar.tsx+1−2
@@ -15,6 +15,7 @@ import {
1515 import { cn } from "cn";
1616 import { IconButton, Menu, MenuItem, Dot, VoiceMarks } from "@/ui";
1717 import { api } from "../lib/api";
18+import { byName } from "../lib/channels";
1819 import type { Channel, UserSummary, VoiceOccupant } from "../lib/types";
1920 import type { Voice } from "../lib/voice";
2021 import VoiceBar from "./VoiceBar";
@@ -41,8 +42,6 @@ interface Props {
4142 onLogout: () => void;
4243 }
4344
44-const byName = (a: Channel, b: Channel) => a.name.localeCompare(b.name);
45-
4645 export default function Sidebar({
4746 channels,
4847 unread,
web/src/lib/channels.ts+8−0
@@ -0,0 +1,8 @@
1+import type { Channel } from "./types";
2+
3+export const byName = (a: Channel, b: Channel) => a.name.localeCompare(b.name);
4+
5+const rank = (c: Channel) => (c.type === "text" ? 0 : c.type === "voice" ? 1 : 2);
6+
7+export const firstChannel = (channels: Channel[]): Channel | null =>
8+ [...channels].sort((a, b) => rank(a) - rank(b) || byName(a, b))[0] ?? null;
web/src/views/Chat.tsx+5−5
@@ -3,6 +3,7 @@ import { AtSign, Hash, Menu, MoreHorizontal, PhoneOff, ScreenShare, Settings as
33 import { cn } from "cn";
44 import { Button, Centered, IconButton, Menu as MenuPrimitive, MenuItem } from "@/ui";
55 import { api } from "../lib/api";
6+import { firstChannel } from "../lib/channels";
67 import { usePresence } from "../lib/presence";
78 import { useSettings } from "../lib/settings";
89 import { useTheme } from "../lib/theme";
@@ -184,7 +185,7 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
184185
185186 useEffect(() => {
186187 if (selected && !channels.some((c) => c.id === selected.id)) {
187- setSelected(channels.find((c) => c.type === "text") ?? channels[0] ?? null);
188+ setSelected(firstChannel(channels));
188189 }
189190 }, [channels, selected]);
190191
@@ -195,7 +196,7 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
195196 setUnread(
196197 Object.fromEntries(cs.filter((c) => c.unread).map((c) => [c.id, c.unread as number])),
197198 );
198- setSelected((s) => s ?? cs.find((c) => c.type === "text") ?? cs[0] ?? null);
199+ setSelected((s) => s ?? firstChannel(cs));
199200 })
200201 .catch(() => {});
201202 api<UserSummary[]>("/users")
@@ -353,8 +354,7 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
353354 onSelectUser={selectUser}
354355 onCloseDM={(dm) => {
355356 setHiddenDms((ids) => (ids.includes(dm.id) ? ids : [...ids, dm.id]));
356- if (selected?.id === dm.id)
357- setSelected(channels.find((c) => c.type === "text") ?? null);
357+ if (selected?.id === dm.id) setSelected(firstChannel(channels));
358358 }}
359359 onCreateChannel={createChannel}
360360 onDeleteChannel={deleteChannel}
@@ -446,7 +446,7 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
446446 {inCall && <CallView voice={voice} className="flex-[3]" />}
447447 <div className={cn("flex min-h-0 flex-1 flex-col", inCall && "flex-[1] border-t border-line")}>
448448 <MessageList
449- channelId={selected.id}
449+ key={selected.id}
450450 messages={messages}
451451 username={user.username}
452452 collapsed={collapsed}