Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Chat scrolls itself only when you are at the bottom, otherwise a 'new messages' pill appears; system lines are muted #309
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/286-chat-scrolls-itself-only-when-you-are-at → main 1 commit fork 1 thread · 1 unresolved
3 files
changed +55
−2
Software/so101/src/ui/lobby-panel.js +7 −2
| @@ -199,13 +199,18 @@ export function createRoom(page) { | ||
| 199 | 199 | chatBox.scrollTop = chatBox.scrollHeight - below; |
| 200 | 200 | showEarlier(more); |
| 201 | 201 | } |
| 202 | const pill = $("newMsgs"); | |
| 203 | const toEnd = () => { chatBox.scrollTop = chatBox.scrollHeight; pill.hidden = true; }; | |
| 204 | pill.onclick = toEnd; | |
| 205 | chatBox.addEventListener("scroll", () => { if (chatBox.scrollHeight - chatBox.scrollTop - chatBox.clientHeight < 40) pill.hidden = true; }); | |
| 202 | 206 | /** @param {any[]} lines */ |
| 203 | 207 | function addChat(lines, replace = false) { |
| 204 | 208 | const atEnd = chatBox.scrollHeight - chatBox.scrollTop - chatBox.clientHeight < 40; |
| 205 | 209 | if (replace) chatBox.replaceChildren(); |
| 206 | 210 | chatBox.append(...lines.map(chatLine)); |
| 207 | 211 | if (!replace) while (chatBox.querySelectorAll(".line").length > 200) chatBox.querySelector(".line").remove(); |
| 208 | if (atEnd || replace) chatBox.scrollTop = chatBox.scrollHeight; | |
| 212 | if (atEnd || replace) toEnd(); | |
| 213 | else if (lines.length) pill.hidden = false; // the reader is up in the history: keep the place, offer the way down | |
| 209 | 214 | if (!replace && page.tab() !== "chat") $("unread").textContent = String(Math.min(99, Number($("unread").textContent || 0) + lines.length)); |
| 210 | 215 | } |
| 211 | 216 | // Moderation on a phone: long-press a line (or right-click it, or Enter on it) for delete (and mute and ban, for the owner). |
| @@ -288,7 +293,7 @@ export function createRoom(page) { | ||
| 288 | 293 | if (!text) return; |
| 289 | 294 | say({ type: "post", text }); |
| 290 | 295 | $("sayText").value = ""; |
| 291 | chatBox.scrollTop = chatBox.scrollHeight; | |
| 296 | toEnd(); | |
| 292 | 297 | }; |
| 293 | 298 | |
| 294 | 299 | return { lobby, mayDrive, isOwner, say, serverNow, lobbyConnect, chatBox, renderTurn, button, tickClock }; |
Software/so101/src/ui/lobby-panel.test.ts +46
| @@ -147,3 +147,49 @@ describe("a deliberate reopen", () => { | ||
| 147 | 147 | test("the retry delays", () => { |
| 148 | 148 | expect([1, 2, 3, 4, 5, 50].map(backoff)).toEqual([250, 500, 1000, 2000, 2000, 2000]); |
| 149 | 149 | }); |
| 150 | ||
| 151 | describe("the chat's scroll", () => { | |
| 152 | function setup() { | |
| 153 | const sockets: any[] = []; | |
| 154 | const g = globalThis as any, saved = { WebSocket: g.WebSocket, setInterval: g.setInterval, location: g.location }; | |
| 155 | g.WebSocket = class { readyState = 1; constructor() { sockets.push(this); } send() {} }; | |
| 156 | g.setInterval = () => 0; | |
| 157 | g.location = { protocol: "http:", host: "x", pathname: "/", search: "" }; | |
| 158 | const nodes = new Map<string, any>(); | |
| 159 | const n = (): any => new Proxy({ children: [], classList: { toggle() {}, add() {}, remove() {}, contains: () => false }, style: { setProperty() {} }, dataset: {} }, { | |
| 160 | get: (t: any, k) => (k in t ? t[k] : (() => n())), set: (t: any, k, v) => ((t[k] = v), true), | |
| 161 | }); | |
| 162 | const page = { | |
| 163 | $: (id: string) => nodes.get(id) ?? (nodes.set(id, n()), nodes.get(id)), | |
| 164 | el: (tag: string, props: object = {}) => Object.assign(n(), { tag }, props), | |
| 165 | relayed: true, tab: () => "chat", showTab() {}, applyLock() {}, applyStatus() {}, noStatus() {}, words() {}, admin() {}, msg() {}, | |
| 166 | }; | |
| 167 | const room = createRoom(page); | |
| 168 | room.lobbyConnect(); | |
| 169 | const said = (id: number) => sockets[0].onmessage({ data: JSON.stringify({ type: "said", message: { id, at: 0, text: "hi", from: { id: "a", name: "ann" } } }) }); | |
| 170 | return { box: page.$("chat"), pill: page.$("newMsgs"), said, restore: () => Object.assign(g, saved) }; | |
| 171 | } | |
| 172 | ||
| 173 | test("scrolled up, a new line shows the pill and keeps the position", () => { | |
| 174 | const t = setup(); | |
| 175 | try { | |
| 176 | Object.assign(t.box, { scrollHeight: 1000, clientHeight: 300, scrollTop: 100 }); | |
| 177 | t.said(1); | |
| 178 | expect(t.pill.hidden).toBe(false); | |
| 179 | expect(t.box.scrollTop).toBe(100); | |
| 180 | t.pill.onclick(); // tapping it jumps to the bottom | |
| 181 | expect(t.box.scrollTop).toBe(1000); | |
| 182 | expect(t.pill.hidden).toBe(true); | |
| 183 | } finally { t.restore(); } | |
| 184 | }); | |
| 185 | ||
| 186 | test("at the bottom, a new line scrolls and no pill shows", () => { | |
| 187 | const t = setup(); | |
| 188 | try { | |
| 189 | Object.assign(t.box, { scrollHeight: 1000, clientHeight: 300, scrollTop: 690 }); | |
| 190 | t.said(1); | |
| 191 | expect(t.pill.hidden).toBe(true); | |
| 192 | expect(t.box.scrollTop).toBe(1000); | |
| 193 | } finally { t.restore(); } | |
| 194 | }); | |
| 195 | }); |
Software/so101/src/viewer.html +2
| @@ -139,6 +139,7 @@ | ||
| 139 | 139 | #chat .line b { color: var(); font-weight: 650; margin-right: 6px; } #chat .line.me b { color: var(); } |
| 140 | 140 | #chat .line.held { background: var(); } |
| 141 | 141 | #chat.moderated .line:not(.sys) { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; cursor: context-menu; } |
| 142 | #newMsgs { align-self: center; margin: -38px 0 8px; position: relative; z-index: 1; border-radius: 999px; padding: 5px 14px; font-size: 13px; background: var(); color: #fff; border: 0; box-shadow: 0 2px 8px rgba(0,0,0,.4); } | |
| 142 | 143 | #chat .sys { color: var(); font-style: italic; } |
| 143 | 144 | #say { display: flex; gap: 8px; padding: 8px calc(12px + var()) 8px calc(12px + var()); border-top: 1px solid var(); } |
| 144 | 145 | #say input { flex: 1; min-width: 0; } |
| @@ -320,6 +321,7 @@ | ||
| 320 | 321 | <div id="notice" role="status" hidden></div> |
| 321 | 322 | <div class="tab" id="tab-chat" role="tabpanel" aria-labelledby="t-chat"> |
| 322 | 323 | <div id="chat" role="log" aria-label="Chat"></div> |
| 324 | <button type="button" id="newMsgs" hidden>New messages ↓</button> | |
| 323 | 325 | <form id="say" hidden><input id="sayText" type="text" maxlength="500" autocomplete="off" enterkeyhint="send" placeholder="Say something" aria-label="Message"><button class="go">Send</button></form> |
| 324 | 326 | <div id="sayNote"></div> |
| 325 | 327 | </div> |
new above
offline
About 28px tall is small for a thumb. Add min-height and a :focus-visible style.