Public: anyone can read this; the crew can change it.
Dark mode and a keyboard help overlay #4
Check · Review · Land · Live done — Landed and live in production.
scout/3-dark-mode-and-a-keyboard-help-overlay → main 1 commit
5 files
changed +247
−9
src/drag.test.ts +1 −1
| @@ -44,7 +44,7 @@ function fireDrag(window: Window, target: Element, type: string, dt: unknown, cl | ||
| 44 | 44 | test("dragging a card into another column moves it there, server included", async () => { |
| 45 | 45 | const { id: boardId, columns } = await boardWithTwoCards(); |
| 46 | 46 | const html = await (await fetch(`${base}/b/${boardId}`)).text(); |
| 47 | const [, scriptSrc] = html.match(/<script src="(\/static\/[^"]+)"/)!; | |
| 47 | const [, scriptSrc] = html.match(/<script src="(\/static\/board[^"]+)"/)!; | |
| 48 | 48 | const script = await (await fetch(base + scriptSrc)).text(); |
| 49 | 49 | |
| 50 | 50 | const window = new Window({ url: `${base}/b/${boardId}` }); |
src/pages/board.ts +58 −7
| @@ -12,24 +12,33 @@ export const escape = (s: string): string => | ||
| 12 | 12 | s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c] ?? c); |
| 13 | 13 | |
| 14 | 14 | const styles = ` |
| 15 | :root { color-scheme: light dark; font: 16px/1.5 system-ui, sans-serif; } | |
| 15 | :root { | |
| 16 | color-scheme: light dark; font: 16px/1.5 system-ui, sans-serif; | |
| 17 | --accent: #2563eb; --accent-fg: #fff; --btn-bg: #111; --btn-fg: #fff; | |
| 18 | --danger: #b91c1c; --danger-fg: #fff; | |
| 19 | } | |
| 20 | @media (prefers-color-scheme: dark) { :root { --btn-bg: #e5e7eb; --btn-fg: #111; } } | |
| 21 | :root[data-theme="light"] { color-scheme: light; --btn-bg: #111; --btn-fg: #fff; } | |
| 22 | :root[data-theme="dark"] { color-scheme: dark; --btn-bg: #e5e7eb; --btn-fg: #111; } | |
| 16 | 23 | * { box-sizing: border-box; } |
| 17 | 24 | body { margin: 0; padding: 1rem 1.5rem 3rem; } |
| 18 | 25 | a { color: inherit; } |
| 19 | 26 | header.top { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; margin-bottom: .75rem; } |
| 20 | 27 | header.top h1 { font-size: 1.1rem; margin: 0; } |
| 21 | 28 | .env { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .7rem; |
| 22 | background: #2563eb; color: white; } | |
| 29 | background: var(--accent); color: var(--accent-fg); } | |
| 23 | 30 | .hint { font-size: .8rem; opacity: .7; margin: 0 0 1rem; } |
| 24 | 31 | .toolbar { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: 1rem; align-items: center; } |
| 25 | 32 | .toolbar form { display: flex; gap: .35rem; } |
| 26 | 33 | input, select, button { font: inherit; } |
| 27 | 34 | input, select { padding: .35rem .5rem; border: 1px solid #8884; border-radius: .4rem; background: transparent; color: inherit; } |
| 28 | button { padding: .35rem .7rem; border: 0; border-radius: .4rem; background: #111; color: white; cursor: pointer; } | |
| 35 | button { padding: .35rem .7rem; border: 0; border-radius: .4rem; background: var(--btn-bg); color: var(--btn-fg); cursor: pointer; } | |
| 29 | 36 | button.ghost { background: transparent; color: inherit; border: 1px solid #8884; } |
| 37 | .tools { position: fixed; top: .75rem; right: .75rem; display: flex; gap: .4rem; z-index: 10; } | |
| 38 | .tools button { padding: .3rem .55rem; font-size: 1rem; line-height: 1; } | |
| 30 | 39 | .board { display: flex; gap: 1rem; align-items: flex-start; overflow-x: auto; padding-bottom: 1rem; } |
| 31 | 40 | .column { background: #8881; border-radius: .6rem; padding: .6rem; width: 16rem; flex: none; } |
| 32 | .column.drop-target { outline: 2px dashed #2563eb; } | |
| 41 | .column.drop-target { outline: 2px dashed var(--accent); } | |
| 33 | 42 | .column header { display: flex; align-items: center; gap: .3rem; margin-bottom: .5rem; } |
| 34 | 43 | .column header form.rename { flex: 1; } |
| 35 | 44 | .column header form.rename input { width: 100%; border: 0; background: transparent; font-weight: 600; padding: .2rem; } |
| @@ -37,7 +46,7 @@ const styles = ` | ||
| 37 | 46 | .count { font-size: .7rem; opacity: .6; margin-left: .2rem; } |
| 38 | 47 | ul.cards { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .5rem; min-height: .5rem; } |
| 39 | 48 | .card { background: canvas; border: 1px solid #8884; border-radius: .5rem; padding: .4rem; cursor: grab; } |
| 40 | .card:focus-visible { outline: 2px solid #2563eb; } | |
| 49 | .card:focus-visible { outline: 2px solid var(--accent); } | |
| 41 | 50 | .card.dragging { opacity: .4; } |
| 42 | 51 | .card .edit input { width: 100%; border: 0; background: transparent; padding: .2rem; } |
| 43 | 52 | .card .row { display: flex; gap: .25rem; margin-top: .3rem; align-items: center; } |
| @@ -47,18 +56,51 @@ const styles = ` | ||
| 47 | 56 | .add-card input { flex: 1; } |
| 48 | 57 | .new-column { flex: none; width: 14rem; } |
| 49 | 58 | .new-column form { display: flex; gap: .3rem; } |
| 50 | #toast { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); background: #b91c1c; color: white; | |
| 59 | #toast { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); background: var(--danger); color: var(--danger-fg); | |
| 51 | 60 | padding: .5rem 1rem; border-radius: .5rem; display: none; } |
| 52 | 61 | #toast.show { display: block; } |
| 62 | dialog#shortcuts { border: 1px solid #8884; border-radius: .6rem; padding: 1rem 1.25rem; max-width: 24rem; | |
| 63 | background: canvas; color: canvastext; } | |
| 64 | dialog#shortcuts::backdrop { background: rgba(0, 0, 0, .4); } | |
| 65 | dialog#shortcuts h2 { margin: 0 0 .6rem; font-size: 1rem; } | |
| 66 | dialog#shortcuts dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: .35rem .75rem; align-items: baseline; } | |
| 67 | dialog#shortcuts dt, dialog#shortcuts dd { margin: 0; } | |
| 68 | dialog#shortcuts dd { opacity: .8; font-size: .9rem; } | |
| 69 | dialog#shortcuts kbd { font: inherit; font-size: .8rem; background: #8882; border: 1px solid #8884; border-radius: .3rem; padding: .05rem .4rem; } | |
| 70 | dialog#shortcuts form { margin-top: .85rem; text-align: right; } | |
| 53 | 71 | `; |
| 54 | 72 | |
| 55 | const shell = (title: string, body: string): string => `<!doctype html> | |
| 73 | // Every shortcut applies everywhere; a page can add its own on top. | |
| 74 | const baseShortcuts: [string, string][] = [ | |
| 75 | ["?", "Show or hide this help"], | |
| 76 | ["Esc", "Close this help"], | |
| 77 | ]; | |
| 78 | ||
| 79 | const shortcutsDialog = (extra: [string, string][]): string => { | |
| 80 | const row = ([key, desc]: [string, string]) => `<dt><kbd>${escape(key)}</kbd></dt><dd>${escape(desc)}</dd>`; | |
| 81 | return `<dialog id="shortcuts" aria-labelledby="shortcuts-title"> | |
| 82 | <h2 id="shortcuts-title">Keyboard shortcuts</h2> | |
| 83 | <dl>${[...extra, ...baseShortcuts].map(row).join("")}</dl> | |
| 84 | <form method="dialog"><button class="ghost">Close</button></form> | |
| 85 | </dialog>`; | |
| 86 | }; | |
| 87 | ||
| 88 | // static/theme.js runs before the stylesheet below is parsed, so a stored | |
| 89 | // preference applies to the first paint — no flash of the other theme. | |
| 90 | const shell = (title: string, body: string, shortcuts: [string, string][] = []): string => `<!doctype html> | |
| 56 | 91 | <meta charset="utf-8"> |
| 92 | <script src="/static/${THEME_JS}"></script> | |
| 57 | 93 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 58 | 94 | <title>${escape(title)}</title> |
| 59 | 95 | <style>${styles}</style> |
| 96 | <div class="tools"> | |
| 97 | <button id="theme-toggle" class="ghost" type="button" title="Toggle dark mode" aria-label="Toggle dark mode">🌙</button> | |
| 98 | <button id="shortcuts-toggle" class="ghost" type="button" title="Keyboard shortcuts (?)" aria-label="Keyboard shortcuts" aria-haspopup="dialog">?</button> | |
| 99 | </div> | |
| 60 | 100 | ${body} |
| 101 | ${shortcutsDialog(shortcuts)} | |
| 61 | 102 | <div id="toast" role="status" aria-live="polite"></div> |
| 103 | <script src="/static/${THEME_JS}" defer></script> | |
| 62 | 104 | `; |
| 63 | 105 | |
| 64 | 106 | export function landingPage(env: string): string { |
| @@ -162,9 +204,18 @@ export function boardPage({ board, columns, cards, query }: BoardData): string { | ||
| 162 | 204 | </div> |
| 163 | 205 | </div> |
| 164 | 206 | <script src="/static/${BOARD_JS}" defer></script>`, |
| 207 | boardShortcuts, | |
| 165 | 208 | ); |
| 166 | 209 | } |
| 167 | 210 | |
| 211 | const boardShortcuts: [string, string][] = [ | |
| 212 | ["Tab", "Move focus between cards and controls"], | |
| 213 | ["↑ / ↓", "Move the focused card up or down its column"], | |
| 214 | ["← / →", "Move the focused card to the previous or next column"], | |
| 215 | ["Enter", "Save a title, or submit the focused form"], | |
| 216 | ]; | |
| 217 | ||
| 168 | 218 | // Named by content — a change to static/board.js gets a new hash here, |
| 169 | 219 | // so the year-long cache on /static/ never serves a stale script. |
| 170 | 220 | export const BOARD_JS = "board.2992576250.js"; |
| 221 | export const THEME_JS = "theme.1025564933.js"; |
src/server.test.ts +1 −1
| @@ -199,7 +199,7 @@ test("serves the board script with a year-long cache", async () => { | ||
| 199 | 199 | // static assets aren't linked from the landing page; fetch a board instead |
| 200 | 200 | const id = await newBoard(); |
| 201 | 201 | const boardPage = await (await fetch(base + `/b/${id}`)).text(); |
| 202 | const [, src] = boardPage.match(/<script src="(\/static\/[^"]+)"/)!; | |
| 202 | const [, src] = boardPage.match(/<script src="(\/static\/board[^"]+)"/)!; | |
| 203 | 203 | const response = await fetch(base + src); |
| 204 | 204 | expect(response.status).toBe(200); |
| 205 | 205 | expect(response.headers.get("cache-control")).toContain("immutable"); |
src/theme.test.ts added +131
| @@ -0,0 +1,131 @@ | ||
| 1 | /** | |
| 2 | * static/theme.js runs on every page. These tests load the real HTML and | |
| 3 | * script from the running server into happy-dom (the same approach as | |
| 4 | * drag.test.ts) so the toggle and the "?" dialog run against real DOM | |
| 5 | * events, not a mock of them. | |
| 6 | */ | |
| 7 | import { expect, test } from "bun:test"; | |
| 8 | import { Window } from "happy-dom"; | |
| 9 | import { mkdtempSync } from "node:fs"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | ||
| 13 | process.env.FORGE_APP_DATA = mkdtempSync(join(tmpdir(), "app-")); | |
| 14 | process.env.PORT = "0"; | |
| 15 | const { server } = await import("./server.ts"); | |
| 16 | const base = `http://${server.hostname}:${server.port}`; | |
| 17 | ||
| 18 | test("the stylesheet follows the system theme and lets an explicit choice override it", async () => { | |
| 19 | const page = await (await fetch(base + "/")).text(); | |
| 20 | expect(page).toContain("prefers-color-scheme: dark"); | |
| 21 | expect(page).toContain('[data-theme="dark"]'); | |
| 22 | expect(page).toContain('[data-theme="light"]'); | |
| 23 | }); | |
| 24 | ||
| 25 | test("serves the theme script with a year-long cache", async () => { | |
| 26 | const page = await (await fetch(base + "/")).text(); | |
| 27 | const [, src] = page.match(/<script src="(\/static\/theme[^"]+)"/)!; | |
| 28 | const response = await fetch(base + src); | |
| 29 | expect(response.status).toBe(200); | |
| 30 | expect(response.headers.get("cache-control")).toContain("immutable"); | |
| 31 | }); | |
| 32 | ||
| 33 | test("the landing page and a board both carry the theme toggle and the shortcuts dialog", async () => { | |
| 34 | const landing = await (await fetch(base + "/")).text(); | |
| 35 | expect(landing).toContain('id="theme-toggle"'); | |
| 36 | expect(landing).toContain('id="shortcuts"'); | |
| 37 | expect(landing).toContain("Show or hide this help"); | |
| 38 | ||
| 39 | const created = await fetch(base + "/boards", { method: "POST", body: new URLSearchParams({ name: "Board" }) }); | |
| 40 | const boardPage = await (await fetch(created.url)).text(); | |
| 41 | expect(boardPage).toContain('id="theme-toggle"'); | |
| 42 | expect(boardPage).toContain("Move the focused card"); | |
| 43 | }); | |
| 44 | ||
| 45 | async function loadBoardIntoWindow(): Promise<{ window: Window; boardId: string }> { | |
| 46 | const created = await fetch(base + "/boards", { method: "POST", body: new URLSearchParams({ name: "Board" }) }); | |
| 47 | const boardId = new URL(created.url).pathname.split("/")[2]; | |
| 48 | const html = await (await fetch(base + `/b/${boardId}`)).text(); | |
| 49 | ||
| 50 | const window = new Window({ url: `${base}/b/${boardId}` }); | |
| 51 | const document = window.document; | |
| 52 | document.open(); | |
| 53 | document.write(html); | |
| 54 | document.close(); | |
| 55 | ||
| 56 | for (const script of [...document.querySelectorAll("script[src]")]) { | |
| 57 | const src = (script as unknown as HTMLScriptElement).getAttribute("src")!; | |
| 58 | const code = await (await fetch(base + src)).text(); | |
| 59 | // theme.js is a bare IIFE reading document/localStorage/matchMedia as | |
| 60 | // globals; run it against happy-dom's versions instead of the real ones. | |
| 61 | new Function("document", "localStorage", "matchMedia", code)(document, window.localStorage, window.matchMedia.bind(window)); | |
| 62 | } | |
| 63 | ||
| 64 | return { window, boardId }; | |
| 65 | } | |
| 66 | ||
| 67 | test("the toggle flips the theme and remembers it", async () => { | |
| 68 | const { window } = await loadBoardIntoWindow(); | |
| 69 | const document = window.document; | |
| 70 | const root = document.documentElement as unknown as HTMLElement & { dataset: DOMStringMap }; | |
| 71 | const toggle = document.getElementById("theme-toggle") as unknown as HTMLElement; | |
| 72 | ||
| 73 | expect(root.dataset.theme).toBeUndefined(); | |
| 74 | toggle.click(); | |
| 75 | expect(root.dataset.theme).toBe("dark"); | |
| 76 | expect(window.localStorage.getItem("tempo-theme")).toBe("dark"); | |
| 77 | ||
| 78 | toggle.click(); | |
| 79 | expect(root.dataset.theme).toBe("light"); | |
| 80 | expect(window.localStorage.getItem("tempo-theme")).toBe("light"); | |
| 81 | ||
| 82 | await window.close(); | |
| 83 | }); | |
| 84 | ||
| 85 | test("a stored theme applies again on the next load, before any click", async () => { | |
| 86 | const { window: first, boardId } = await loadBoardIntoWindow(); | |
| 87 | (first.document.getElementById("theme-toggle") as unknown as HTMLElement).click(); | |
| 88 | const stored = first.localStorage.getItem("tempo-theme"); | |
| 89 | expect(stored).toBe("dark"); | |
| 90 | await first.close(); | |
| 91 | ||
| 92 | const html = await (await fetch(base + `/b/${boardId}`)).text(); | |
| 93 | const second = new Window({ url: `${base}/b/${boardId}` }); | |
| 94 | second.localStorage.setItem("tempo-theme", stored!); | |
| 95 | const document = second.document; | |
| 96 | document.open(); | |
| 97 | document.write(html); | |
| 98 | document.close(); | |
| 99 | for (const script of [...document.querySelectorAll("script[src]")]) { | |
| 100 | const src = (script as unknown as HTMLScriptElement).getAttribute("src")!; | |
| 101 | const code = await (await fetch(base + src)).text(); | |
| 102 | new Function("document", "localStorage", "matchMedia", code)(document, second.localStorage, second.matchMedia.bind(second)); | |
| 103 | } | |
| 104 | const root = document.documentElement as unknown as HTMLElement & { dataset: DOMStringMap }; | |
| 105 | expect(root.dataset.theme).toBe("dark"); | |
| 106 | await second.close(); | |
| 107 | }); | |
| 108 | ||
| 109 | test("the \"?\" key opens the shortcuts dialog, and it does not fire while typing", async () => { | |
| 110 | const { window } = await loadBoardIntoWindow(); | |
| 111 | const document = window.document; | |
| 112 | const dialog = document.getElementById("shortcuts") as unknown as HTMLDialogElement; | |
| 113 | expect(dialog.open).toBe(false); | |
| 114 | ||
| 115 | const press = (key: string, target: Element) => { | |
| 116 | const event = new window.KeyboardEvent("keydown", { key, bubbles: true, cancelable: true }); | |
| 117 | target.dispatchEvent(event); | |
| 118 | }; | |
| 119 | ||
| 120 | press("?", document.body as unknown as Element); | |
| 121 | expect(dialog.open).toBe(true); | |
| 122 | ||
| 123 | press("?", document.body as unknown as Element); | |
| 124 | expect(dialog.open).toBe(false); | |
| 125 | ||
| 126 | const filter = document.getElementById("filter") as unknown as Element; | |
| 127 | press("?", filter); | |
| 128 | expect(dialog.open).toBe(false); | |
| 129 | ||
| 130 | await window.close(); | |
| 131 | }); |
static/theme.1025564933.js added +56
| @@ -0,0 +1,56 @@ | ||
| 1 | // Runs on every page, twice: once blocking (before the stylesheet below it | |
| 2 | // is parsed) so a stored theme applies to the very first paint with no | |
| 3 | // flash, and once deferred (after the tools and dialog exist) to wire the | |
| 4 | // toggle button and the "?" shortcuts dialog. The first run finds no | |
| 5 | // buttons yet and only sets the theme; the second finds them and wires up. | |
| 6 | (() => { | |
| 7 | "use strict"; | |
| 8 | const KEY = "tempo-theme"; | |
| 9 | const root = document.documentElement; | |
| 10 | ||
| 11 | function getStored() { | |
| 12 | try { | |
| 13 | return localStorage.getItem(KEY); | |
| 14 | } catch { | |
| 15 | return null; | |
| 16 | } | |
| 17 | } | |
| 18 | const stored = getStored(); | |
| 19 | if (stored === "light" || stored === "dark") root.dataset.theme = stored; | |
| 20 | ||
| 21 | function effectiveTheme() { | |
| 22 | return root.dataset.theme || (matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"); | |
| 23 | } | |
| 24 | ||
| 25 | // The script tag appears twice (see above): guard wiring so a second run | |
| 26 | // that finds the same elements never binds a second set of listeners. | |
| 27 | const toggle = document.getElementById("theme-toggle"); | |
| 28 | if (toggle) toggle.textContent = effectiveTheme() === "dark" ? "☀️" : "🌙"; | |
| 29 | if (toggle && !toggle.dataset.wired) { | |
| 30 | toggle.dataset.wired = "1"; | |
| 31 | toggle.addEventListener("click", () => { | |
| 32 | const theme = effectiveTheme() === "dark" ? "light" : "dark"; | |
| 33 | root.dataset.theme = theme; | |
| 34 | try { | |
| 35 | localStorage.setItem(KEY, theme); | |
| 36 | } catch { | |
| 37 | // A private-browsing tab may refuse storage; the choice just won't | |
| 38 | // survive a reload, which is a fine fallback for a preference. | |
| 39 | } | |
| 40 | toggle.textContent = theme === "dark" ? "☀️" : "🌙"; | |
| 41 | }); | |
| 42 | } | |
| 43 | ||
| 44 | const dialog = document.getElementById("shortcuts"); | |
| 45 | const help = document.getElementById("shortcuts-toggle"); | |
| 46 | if (dialog && help && !help.dataset.wired) { | |
| 47 | help.dataset.wired = "1"; | |
| 48 | const toggleDialog = () => (dialog.open ? dialog.close() : dialog.showModal()); | |
| 49 | help.addEventListener("click", toggleDialog); | |
| 50 | document.addEventListener("keydown", (e) => { | |
| 51 | if (e.key !== "?" || e.target.closest("input, textarea, select")) return; | |
| 52 | e.preventDefault(); | |
| 53 | toggleDialog(); | |
| 54 | }); | |
| 55 | } | |
| 56 | })(); |
new above
offline