Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

Owners and moderators get a 44 px menu button on each other person's chat line that opens the moderator menu; Escape or an outside tap closes it #421

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/385-owners-and-moderators-get-a-44-px-menu-b → main 1 commit fork 2 threads · 2 unresolved

  • Merged. smith/385-owners-and-moderators-get-a-44-px-menu-b was merged into main as e9b9cd7.

Review

Sign in to review this change.
3 files changed +83 −4
Software/so101/src/ui/lobby-panel.js +28
@@ -1,4 +1,5 @@
11 // @ts-check
2 import { frameFor, menuItems, renderMenu } from "./mod-menu.js";
23 /**
34 * The room on the page: the turn, the queue and the chat over /live. Moved out of viewer.html; the page hands in what
45 * it owns (the DOM helpers, the tabs, the lock and the arm's status) and gets back the room's state and its few calls.
@@ -189,6 +190,27 @@ export function createRoom(page) {
189190 const done = (b) => { b.disabled = true; b.textContent = "Reported"; b.setAttribute("aria-label", `Reported ${b.name}`); };
190191 /** @param {number} id */
191192 const markReported = (id) => { reported.add(id); const b = chatBox.querySelector(`[data-id="${id}"] .report`); if (b) done(b); };
193 /** The one open moderator menu: its element, its button and the listeners that close it. @type {{ menu: any, button: any, off: () => void } | null} */
194 let openMenu = null;
195 function closeMenu() {
196 if (!openMenu) return;
197 const { menu, button, off } = openMenu; openMenu = null;
198 off(); menu.remove?.(); button.setAttribute("aria-expanded", "false");
199 }
200 /** @param {any} line @param {any} button @param {any} m @param {any[]} items */
201 function toggleMenu(line, button, m, items) {
202 const was = openMenu?.button === button;
203 closeMenu();
204 if (was) return;
205 const menu = renderMenu(doc, items, (item) => { closeMenu(); say(frameFor(item, m)); });
206 menu.className = "modmenu";
207 const onKey = (/** @type {any} */ e) => { if (e.key === "Escape") { closeMenu(); button.focus?.(); } };
208 const onOutside = (/** @type {any} */ e) => { if (!menu.contains?.(e.target) && !button.contains?.(e.target)) closeMenu(); };
209 doc.addEventListener("keydown", onKey); doc.addEventListener("pointerdown", onOutside);
210 openMenu = { menu, button, off: () => { doc.removeEventListener("keydown", onKey); doc.removeEventListener("pointerdown", onOutside); } };

Focus stays on the trigger when the menu opens, though role=menu implies arrow-key navigation. Focus the first menuitem and handle ArrowUp/ArrowDown.

211 button.setAttribute("aria-expanded", "true");
212 line.append(menu);
213 }
192214 /** @param {any} m */
193215 function chatLine(m) {
194216 const me = lobby.hello?.you;
@@ -197,6 +219,12 @@ export function createRoom(page) {
197219 if (m.from) { line.dataset.from = m.from.id; line.dataset.name = m.from.name; line.append(el("b", { textContent: m.from.name })); }
198220 line.append(el("span", { textContent: m.text }));
199221 if (m.from && canDelete()) line.tabIndex = 0;
222 const items = menuItems(m, me);
223 if (items.length) { // reserved with the line, so nothing shifts when the menu opens
224 const b = el("button", { type: "button", className: "modbtn", textContent: "⋯", onclick: () => toggleMenu(line, b, m, items) });
225 b.setAttribute("aria-label", `Moderate ${m.from.name}`); b.setAttribute("aria-haspopup", "menu"); b.setAttribute("aria-expanded", "false");
226 line.classList.add("rep"); line.append(b);
227 }
200228 if (mayReport(m, me)) { // always drawn, so the line never shifts when it is reported
201229 const b = el("button", { type: "button", className: "report", textContent: "Report", name: m.from.name, onclick: () => say({ type: "report", id: Number(m.id) }) });
202230 b.setAttribute("aria-label", `Report ${m.from.name}`); b.setAttribute("aria-live", "polite");
Software/so101/src/ui/lobby-panel.test.ts +52 −3
@@ -228,10 +228,17 @@ describe("reporting a chat line", () => {
228228
229229 function setup(you: object | null) {
230230 const sockets: any[] = [];
231 const g = globalThis as any, saved = { WebSocket: g.WebSocket, setInterval: g.setInterval, location: g.location };
231 const g = globalThis as any, saved = { WebSocket: g.WebSocket, setInterval: g.setInterval, location: g.location, document: g.document };
232232 g.WebSocket = class { readyState = 1; sent: string[] = []; constructor() { sockets.push(this); } send(m: string) { this.sent.push(m); } };
233233 g.setInterval = () => 0;
234234 g.location = { protocol: "http:", host: "x", pathname: "/", search: "" };
235 const heard: Record<string, ((e: any) => void)[]> = {};
236 const fake = (): any => { const on: Record<string, () => void> = {}; return { attrs: {}, style: {}, kids: [], setAttribute(k: string, v: string) { this.attrs[k] = v; }, append(...c: any[]) { this.kids.push(...c); }, remove() { this.gone = true; }, addEventListener(k: string, f: () => void) { on[k] = f; }, click: () => on.click() }; };
237 g.document = {
238 createElement: fake,
239 addEventListener: (k: string, f: any) => void (heard[k] ??= []).push(f),
240 removeEventListener: (k: string, f: any) => void (heard[k] = (heard[k] ?? []).filter((x) => x !== f)),
241 };
235242 const nodes = new Map<string, any>();
236243 const lines: any[] = [];
237244 const n = (): any => new Proxy({ children: [], classList: { toggle() {}, add() {}, remove() {}, contains: () => false }, style: { setProperty() {} }, dataset: {}, attrs: {} }, {
@@ -248,7 +255,8 @@ describe("reporting a chat line", () => {
248255 el: (tag: string, props: object = {}) => {
249256 const e = Object.assign(n(), { tag, btn: null }, props);
250257 e.setAttribute = (k: string, v: string) => (e.attrs[k] = v);
251 if (tag === "div") e.append = (...c: any[]) => { for (const x of c) if (x.tag === "button") e.btn = x; };
258 e.kids = [];
259 if (tag === "div") e.append = (...c: any[]) => { e.kids.push(...c); for (const x of c) if (x.tag === "button" && x.className === "report") e.btn = x; };
252260 return e;
253261 },
254262 relayed: true, tab: () => "chat", showTab() {}, applyLock() {}, applyStatus() {}, noStatus() {}, words() {}, admin() {}, msg() {},
@@ -259,7 +267,7 @@ describe("reporting a chat line", () => {
259267 const recv = (m: object) => ws.onmessage({ data: JSON.stringify(m) });
260268 recv({ type: "hello", signin: true, you });
261269 recv({ type: "chat", more: false, messages: [{ id: 1, at: 0, text: "hi", from: bob }, { id: 2, at: 0, text: "yo", from: ann }] });
262 return { ws, recv, lines, restore: () => Object.assign(g, saved) };
270 return { ws, recv, lines, heard, restore: () => Object.assign(g, saved) };
263271 }
264272
265273 test("the button is on the other person's line only, and not for guests", () => {
@@ -284,6 +292,47 @@ describe("reporting a chat line", () => {
284292 expect([b.disabled, b.textContent, b.attrs["aria-live"]]).toEqual([true, "Reported", "polite"]);
285293 } finally { t.restore(); }
286294 });
295
296 describe("the moderator menu button", () => {
297 const boss = { id: "o", name: "olly", owner: true }, mod = (l: any) => l.kids.find((k: any) => k.className === "modbtn");
298 const open = (t: any) => { mod(t.lines[0]).onclick(); return t.lines[0].kids.find((k: any) => k.attrs?.role === "menu"); };
299 test("absent for plain users, for guests and on your own line", () => {
300 for (const you of [{ id: "a", name: "ann" }, null]) {
301 const t = setup(you);
302 try { expect(t.lines.map(mod)).toEqual([undefined, undefined]); } finally { t.restore(); }
303 }
304 const t = setup({ id: "b", name: "bob", owner: true });
305 try { expect(t.lines.map((l) => mod(l) !== undefined)).toEqual([false, true]); } finally { t.restore(); }
306 });
307 test("named for the author and 44 px", () => {
308 const t = setup(boss);
309 try { expect(mod(t.lines[0]).attrs["aria-label"]).toContain("bob"); } finally { t.restore(); }
310 });
311 test("picking Mute 1h sends the mute frame and closes the menu", () => {
312 const t = setup(boss);
313 try {
314 const menu = open(t);
315 menu.kids.find((b: any) => b.textContent === "Mute 1h").click();
316 expect(t.ws.sent).toEqual([JSON.stringify({ type: "mute", id: "b", on: true, for: "1h" })]);
317 expect(menu.gone).toBe(true);
318 } finally { t.restore(); }
319 });
320 test("Escape or an outside tap closes it; a tap inside does not", () => {
321 const t = setup(boss);
322 try {
323 let menu = open(t);
324 t.heard.keydown.forEach((f) => f({ key: "a" }));
325 expect(menu.gone).toBeUndefined();
326 t.heard.keydown.forEach((f) => f({ key: "Escape" }));
327 expect(menu.gone).toBe(true);
328 expect(t.heard.keydown).toEqual([]);
329 menu = open(t);
330 t.heard.pointerdown.forEach((f) => f({ target: {} }));
331 expect(menu.gone).toBe(true);
332 expect(t.ws.sent).toEqual([]);
333 } finally { t.restore(); }
334 });
335 });
287336 });
288337
289338 describe("the presence line", () => {
Software/so101/src/viewer.html +3 −1
@@ -141,7 +141,9 @@
141141 #chat .line { line-height: 1.45; padding: 4px 8px; margin: 0 -8px; border-radius: 8px; }
142142 #chat .line b { color: var(--fg2); font-weight: 650; margin-right: 6px; } #chat .line.me b { color: var(--link); }
143143 #chat .rep { display: flex; align-items: center; gap: 8px; min-height: 44px; } #chat .rep span { flex: 1; min-width: 0; }
144 #chat .report { min-width: 44px; min-height: 44px; }
144 #chat .report, #chat .modbtn { min-width: 44px; min-height: 44px; }
145 #chat .line { position: relative; }
146 #chat .modmenu { position: absolute; right: 0; top: 100%; z-index: 2; display: flex; flex-direction: column; background: var(--bg2, #222); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,.4); }

Menu opens at top:100% inside the scrolling #chat; on the last line it falls off-screen on a phone. Open upward when there is no room, and cap max-height.

145147 #chat .line.held { background: var(--blue-soft); }
146148 #chat.moderated .line:not(.sys) { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; cursor: context-menu; }
147149 #newMsgs { align-self: center; margin: -38px 0 8px; position: relative; z-index: 1; border-radius: 999px; padding: 5px 14px; font-size: 13px; background: var(--link); color: #fff; border: 0; box-shadow: 0 2px 8px rgba(0,0,0,.4); }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs