Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
the picker above the game needs to be better for all the games we have now #100
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/98-the-picker-above-the-game-needs-to-be-be → main 6 commits fork
3 files
changed +185
−19
README.md +1 −1
| @@ -64,7 +64,7 @@ Every page carries Open Graph and Twitter tags (between `og:begin` and `og:end`) | ||
| 64 | 64 | ## Add a game |
| 65 | 65 | Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules })`, set `window.PP_SCENE`, load `../engine.js`, then add the game to `GAMES` in `shell.js` with a one-line `blurb`; the home page lists every `GAMES` entry as a card with its `kind`, `input` and `accent`, searchable and filterable by kind (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too, with the same `data-kind`, `data-input`, `--accent` and tags line). Then run `bun scripts/og.mjs` to make its share card and tags. The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route, Harbour Crane, Pizza Toss, Sled Run, Pottery Wheel, Row Home, Orchard, Lighthouse, Market Scale, Window Washer, Domino Run, Campfire, Beekeeper, Bread Oven, Ice Fishing, Tightrope, Tram Line, Archery, Bell Tower, Espresso Bar, Goalkeeper, Wood Split, Signal Box, Soapbox Derby, Sheepdog and Firefly Jar play on their own sound effects). Rules that need testing go in a plain script with a `.test.mjs` beside it, as in `kite-hill/model.js`. |
| 66 | 66 | |
| 67 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. | |
| 67 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. The menu lists the games grouped under their kind (the not-yet-made ones last, under Soon) and scrolls within the screen; type in its find box to narrow it by a word of the title, kind, input or blurb (keys typed there never reach the game), and press Escape to close it. | |
| 68 | 68 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
| 69 | 69 | - A game supplies its own `groundColor`, `bake`, `update` and `draw`. |
| 70 | 70 |
shell.js +72 −11
| @@ -52,7 +52,14 @@ window.PixelPlays = (() => { | ||
| 52 | 52 | .pp-link { font: inherit; color: #8b877f; background: none; border: 0; padding: 10px; margin: 0; cursor: pointer; } |
| 53 | 53 | .pp-link:hover, .pp-link:focus-visible, .pp-link[aria-expanded="true"] { color: #1c1b19; } |
| 54 | 54 | #pp-menu { position: fixed; top: max(4px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); z-index: 5; text-align: center; } |
| 55 | #pp-list { list-style: none; margin: 0; padding: 4px 0; background: #fff; border: 1px solid #1c1b19; min-width: 150px; text-align: left; } | |
| 55 | #pp-list { list-style: none; margin: 0; padding: 4px 0; background: #fff; border: 1px solid #1c1b19; min-width: 200px; max-height: min(70vh, 480px); max-height: min(70dvh, 480px); overflow-y: auto; overscroll-behavior: contain; text-align: left; } | |
| 56 | #pp-list > li:first-child { position: sticky; top: 0; z-index: 1; background: #fff; } | |
| 57 | #pp-find { box-sizing: border-box; display: block; width: calc(100% - 16px); margin: 4px 8px 6px; padding: 8px; font: inherit; font-size: max(16px, 1em); color: #1c1b19; background: #fff; border: 1px solid #b3afa7; border-radius: 0; } | |
| 58 | #pp-find:focus-visible { outline: 2px solid #1c1b19; outline-offset: 1px; } | |
| 59 | #pp-list .pp-kind { padding: 8px 12px 2px; font-size: .75em; letter-spacing: .08em; text-transform: uppercase; color: #8b877f; } | |
| 60 | #pp-list .pp-none { padding: 8px 12px; color: #8b877f; } | |
| 61 | #pp-list li[hidden] { display: none; } | |
| 62 | #pp-list ul { list-style: none; margin: 0; padding: 0; } | |
| 56 | 63 | #pp-list[hidden] { display: none; } |
| 57 | 64 | #pp-list a, #pp-list span { display: flex; justify-content: space-between; gap: 16px; padding: 8px 12px; color: #1c1b19; text-decoration: none; } |
| 58 | 65 | #pp-list a:hover, #pp-list a:focus-visible { background: #1c1b19; color: #fff; outline: none; } |
| @@ -91,25 +98,79 @@ window.PixelPlays = (() => { | ||
| 91 | 98 | const list = document.createElement('ul'); |
| 92 | 99 | list.id = 'pp-list'; |
| 93 | 100 | list.hidden = true; |
| 94 | for (const g of GAMES) { | |
| 95 | const item = document.createElement('li'); | |
| 96 | if (g.soon) { | |
| 97 | const label = document.createElement('span'); | |
| 98 | label.innerHTML = '<span style="padding:0">' + g.title + '</span><span style="padding:0">soon</span>'; | |
| 99 | item.append(label); | |
| 100 | } else { | |
| 101 | const find = document.createElement('input'); | |
| 102 | find.type = 'search'; | |
| 103 | find.id = 'pp-find'; | |
| 104 | find.placeholder = 'Find a game'; | |
| 105 | find.setAttribute('aria-label', 'Find a game'); | |
| 106 | find.autocomplete = 'off'; | |
| 107 | const findRow = document.createElement('li'); | |
| 108 | findRow.append(find); | |
| 109 | list.append(findRow); | |
| 110 | const none = document.createElement('li'); | |
| 111 | none.className = 'pp-none'; | |
| 112 | none.textContent = 'No game matches'; | |
| 113 | none.hidden = true; | |
| 114 | const rows = []; | |
| 115 | // each kind is a labelled group (a nested list), so a screen reader hears "balance, group" rather than one flat list | |
| 116 | const addGroup = (kind, games, fill) => { | |
| 117 | const group = document.createElement('li'); | |
| 118 | const head = document.createElement('div'); | |
| 119 | head.className = 'pp-kind'; | |
| 120 | head.setAttribute('aria-hidden', 'true'); // the group's own label says it | |
| 121 | head.textContent = kind; | |
| 122 | const inner = document.createElement('ul'); | |
| 123 | inner.setAttribute('role', 'group'); | |
| 124 | inner.setAttribute('aria-label', kind); | |
| 125 | const items = []; | |
| 126 | for (const g of games) { | |
| 127 | const item = document.createElement('li'); | |
| 128 | item.append(fill(g)); | |
| 129 | inner.append(item); | |
| 130 | items.push({ item, game: g }); | |
| 131 | } | |
| 132 | group.append(head, inner); | |
| 133 | list.append(group); | |
| 134 | rows.push({ group, items }); | |
| 135 | }; | |
| 136 | // games sit under their kind, in the order the kinds first appear in GAMES; the ones not yet made come last | |
| 137 | for (const kind of new Set(GAMES.filter(g => !g.soon).map(g => g.kind))) { | |
| 138 | addGroup(kind, GAMES.filter(x => !x.soon && x.kind === kind), g => { | |
| 101 | 139 | const link = document.createElement('a'); |
| 102 | 140 | link.href = '../' + g.id + '/'; |
| 103 | 141 | link.textContent = g.title; |
| 104 | 142 | if (g.id === id) link.setAttribute('aria-current', 'page'); |
| 105 | item.append(link); | |
| 106 | } | |
| 107 | list.append(item); | |
| 143 | return link; | |
| 144 | }); | |
| 108 | 145 | } |
| 146 | const soon = GAMES.filter(g => g.soon); | |
| 147 | if (soon.length) { | |
| 148 | addGroup('Soon', soon, g => { | |
| 149 | const label = document.createElement('span'); | |
| 150 | label.textContent = g.title; | |
| 151 | return label; | |
| 152 | }); | |
| 153 | } | |
| 154 | list.append(none); | |
| 155 | find.addEventListener('input', () => { | |
| 156 | const shown = new Set(filterGames(GAMES, { query: find.value }).map(g => g.id)); | |
| 157 | let any = false; | |
| 158 | for (const { group, items } of rows) { | |
| 159 | let some = false; | |
| 160 | for (const { item, game: g } of items) { item.hidden = !shown.has(g.id); some ||= !item.hidden; } | |
| 161 | group.hidden = !some; | |
| 162 | any ||= some; | |
| 163 | } | |
| 164 | none.hidden = any; | |
| 165 | }); | |
| 166 | // keys typed to search stay in the box: the games listen on document and window, and would steer or act on them; | |
| 167 | // Escape still bubbles to close the menu | |
| 168 | for (const kind of ['keydown', 'keyup', 'keypress']) find.addEventListener(kind, e => { if (e.key !== 'Escape') e.stopPropagation(); }); | |
| 109 | 169 | menu.append(toggle, list); |
| 110 | 170 | const setOpen = open => { |
| 111 | 171 | list.hidden = !open; |
| 112 | 172 | toggle.setAttribute('aria-expanded', String(open)); |
| 173 | if (open) list.querySelector('[aria-current]')?.scrollIntoView({ block: 'center' }); | |
| 113 | 174 | }; |
| 114 | 175 | toggle.addEventListener('click', () => setOpen(list.hidden)); |
| 115 | 176 | document.addEventListener('pointerdown', e => { if (!menu.contains(e.target)) setOpen(false); }); |
shell.test.mjs +112 −7
| @@ -1,16 +1,31 @@ | ||
| 1 | 1 | import { test, expect } from 'bun:test'; |
| 2 | 2 | |
| 3 | // a stand-in element that accepts everything shell.js does to it | |
| 4 | const element = () => ({ | |
| 5 | children: [], style: {}, classList: { add() {}, remove() {}, toggle() {} }, | |
| 6 | append(...n) { this.children.push(...n); }, addEventListener() {}, setAttribute() {}, showModal() {}, close() {}, | |
| 3 | // a stand-in element that accepts everything shell.js does to it, and keeps enough of a tree to be searched | |
| 4 | const element = (tag = 'div') => ({ | |
| 5 | tag, children: [], style: {}, attrs: {}, handlers: {}, hidden: false, value: '', classList: { add() {}, remove() {}, toggle() {} }, | |
| 6 | append(...n) { for (const c of n) if (typeof c === 'object') c.parent = this; this.children.push(...n); }, | |
| 7 | addEventListener(k, f) { (this.handlers[k] ??= []).push(f); }, | |
| 8 | setAttribute(k, v) { this.attrs[k] = String(v); }, | |
| 9 | getAttribute(k) { return this.attrs[k] ?? null; }, | |
| 10 | contains(n) { return n === this || this.children.some(c => typeof c === 'object' && c.contains(n)); }, | |
| 11 | all() { return this.children.flatMap(c => typeof c === 'object' ? [c, ...c.all()] : []); }, | |
| 12 | querySelector(sel) { const k = sel.slice(1, -1); return this.all().find(e => k in e.attrs) ?? null; }, // '[attr]' only | |
| 13 | scrollIntoView(o) { scrolled.push([this, o]); }, | |
| 14 | dispatch(k, ev = {}) { for (const f of this.handlers[k] ?? []) f({ target: this, ...ev }); }, | |
| 15 | // an event that bubbles up the tree to the document, as a browser's does, until something stops it | |
| 16 | fire(k, ev = {}) { | |
| 17 | const e = { target: this, stopped: false, stopPropagation() { this.stopped = true; }, ...ev }; | |
| 18 | for (let n = this; n && !e.stopped; n = n.parent) for (const f of n.handlers[k] ?? []) f(e); | |
| 19 | if (!e.stopped) for (const f of listeners[k] ?? []) f(e); | |
| 20 | }, | |
| 21 | showModal() {}, close() {}, focus() {}, | |
| 7 | 22 | }); |
| 8 | 23 | const SAVED = ['window', 'document', 'Audio', 'PixelPlays']; |
| 9 | 24 | const realFetch = globalThis.fetch; |
| 10 | let audios, byId, sent, listeners; | |
| 25 | let audios, byId, sent, listeners, scrolled; | |
| 11 | 26 | // stubs live only while a test runs, so they never leak into the server tests in the same process |
| 12 | 27 | function stub() { |
| 13 | audios = []; byId = {}; sent = []; listeners = {}; | |
| 28 | scrolled = []; audios = []; byId = {}; sent = []; listeners = {}; | |
| 14 | 29 | globalThis.fetch = (url, init) => { sent.push([url, JSON.parse(init.body)]); return Promise.resolve(); }; |
| 15 | 30 | globalThis.window = globalThis; |
| 16 | 31 | window.addEventListener = (k, f) => { (window.addEventListener.calls[k] ??= []).push(f); }; |
| @@ -18,7 +33,7 @@ function stub() { | ||
| 18 | 33 | globalThis.Audio = class { constructor(src) { this.src = src; audios.push(src); } play() { return Promise.resolve(); } pause() {} }; |
| 19 | 34 | globalThis.document = { |
| 20 | 35 | head: element(), body: element(), hidden: false, addEventListener(k, f) { (listeners[k] ??= []).push(f); }, |
| 21 | createElement: () => element(), | |
| 36 | createElement: tag => element(tag), | |
| 22 | 37 | getElementById: id => (byId[id] ??= element()), |
| 23 | 38 | }; |
| 24 | 39 | } |
| @@ -95,3 +110,93 @@ test('time is sent when the page is hidden after a play, resumes on return, clam | ||
| 95 | 110 | expect(times()).toEqual([12, 3600, 3]); |
| 96 | 111 | } finally { Date.now = realNow; } |
| 97 | 112 | })); |
| 113 | ||
| 114 | // the open menu: [nav, toggle, list]; the list's rows are [find, group..., none] | |
| 115 | async function openMenu(id) { | |
| 116 | await mount(id); | |
| 117 | const nav = document.body.children[0], list = nav.children[1]; | |
| 118 | const find = list.all().find(e => e.id === 'pp-find'); | |
| 119 | const groups = list.children.filter(c => c.children[1]?.attrs.role === 'group'); | |
| 120 | const none = list.children.find(c => c.className === 'pp-none'); | |
| 121 | return { nav, toggle: nav.children[0], list, find, groups, none }; | |
| 122 | } | |
| 123 | const visible = groups => groups.flatMap(g => g.hidden ? [] : g.children[1].children.filter(i => !i.hidden).map(i => text(i).trim())); | |
| 124 | const type = (find, v) => { find.value = v; find.dispatch('input'); }; | |
| 125 | ||
| 126 | test('the menu groups games under their kind, each kind a labelled group, and lists each playable game once', guarded(async () => { | |
| 127 | const { list, find, groups } = await openMenu('balote'); | |
| 128 | expect(list.children[0].children[0]).toBe(find); | |
| 129 | const { GAMES, kindsOf } = window.PixelPlays; | |
| 130 | const labels = groups.map(g => g.children[1].attrs['aria-label']); | |
| 131 | expect(labels).toEqual([...kindsOf(GAMES.filter(g => !g.soon)), ...(GAMES.some(g => g.soon) ? ['Soon'] : [])]); | |
| 132 | for (const g of groups) expect(g.children[0].attrs['aria-hidden']).toBe('true'); // the group's label speaks, the heading does not repeat it | |
| 133 | for (const g of GAMES.filter(g => !g.soon)) expect(visible(groups).filter(t => t.trim() === g.title).length).toBe(1); | |
| 134 | })); | |
| 135 | ||
| 136 | test('typing in the find box hides what does not match and the kinds left empty', guarded(async () => { | |
| 137 | const { find, groups, none } = await openMenu('balote'); | |
| 138 | const all = visible(groups); | |
| 139 | const { GAMES } = window.PixelPlays; | |
| 140 | const g = GAMES.find(x => !x.soon); | |
| 141 | type(find, g.title); | |
| 142 | const some = visible(groups); | |
| 143 | expect(some).toContain(g.title); | |
| 144 | expect(some.length).toBeLessThan(all.length); | |
| 145 | expect(groups.filter(x => x.hidden).length).toBeGreaterThan(0); | |
| 146 | for (const x of groups) expect(x.hidden).toBe(!x.children[1].children.some(i => !i.hidden)); | |
| 147 | expect(none.hidden).toBe(true); | |
| 148 | })); | |
| 149 | ||
| 150 | test('a nonsense query says no game matches, and clearing it restores everything', guarded(async () => { | |
| 151 | const { find, groups, none } = await openMenu('balote'); | |
| 152 | const all = visible(groups); | |
| 153 | type(find, 'zzzqqq'); | |
| 154 | expect(visible(groups)).toEqual([]); | |
| 155 | expect(groups.every(g => g.hidden)).toBe(true); | |
| 156 | expect(none.hidden).toBe(false); | |
| 157 | expect(none.textContent).toBe('No game matches'); | |
| 158 | type(find, ''); | |
| 159 | expect(visible(groups)).toEqual(all); | |
| 160 | expect(groups.some(g => g.hidden)).toBe(false); | |
| 161 | expect(none.hidden).toBe(true); | |
| 162 | })); | |
| 163 | ||
| 164 | test('Soon games are labels, not links', guarded(async () => { | |
| 165 | const { groups } = await openMenu('balote'); | |
| 166 | const { GAMES } = window.PixelPlays; | |
| 167 | const soon = GAMES.filter(g => g.soon); | |
| 168 | if (!soon.length) return; // nothing is announced right now | |
| 169 | const last = groups.at(-1); | |
| 170 | expect(last.children[1].attrs['aria-label']).toBe('Soon'); | |
| 171 | for (const item of last.children[1].children) expect(item.children[0].tag).toBe('span'); | |
| 172 | expect(last.all().some(e => e.tag === 'a')).toBe(false); | |
| 173 | })); | |
| 174 | ||
| 175 | test('opening the menu marks the current game and scrolls it into view; typing does not close it', guarded(async () => { | |
| 176 | const { nav, toggle, list, find } = await openMenu('balote'); | |
| 177 | expect(list.hidden).toBe(true); | |
| 178 | toggle.dispatch('click'); | |
| 179 | expect(list.hidden).toBe(false); | |
| 180 | const current = list.querySelector('[aria-current]'); | |
| 181 | expect(current.attrs['aria-current']).toBe('page'); | |
| 182 | expect(current.textContent).toBe(window.PixelPlays.GAMES.find(g => g.id === 'balote').title); | |
| 183 | expect(scrolled.map(s => s[0])).toEqual([current]); | |
| 184 | // a pointer on the find box is inside the menu, so it does not close it; one elsewhere does | |
| 185 | for (const f of listeners.pointerdown) f({ target: find }); | |
| 186 | expect(list.hidden).toBe(false); | |
| 187 | for (const f of listeners.pointerdown) f({ target: document.body }); | |
| 188 | expect(list.hidden).toBe(true); | |
| 189 | })); | |
| 190 | ||
| 191 | test('keys typed in the find box do not reach the game, and Escape still closes the menu', guarded(async () => { | |
| 192 | const { toggle, list, find } = await openMenu('balote'); | |
| 193 | const seen = []; | |
| 194 | document.addEventListener('keydown', e => seen.push(e.key)); // a game listening on the document | |
| 195 | toggle.dispatch('click'); | |
| 196 | for (const key of ['s', ' ', 'ArrowLeft', 'Enter']) { find.fire('keydown', { key }); find.fire('keyup', { key }); } | |
| 197 | expect(seen).toEqual([]); | |
| 198 | expect(list.hidden).toBe(false); | |
| 199 | find.fire('keydown', { key: 'Escape' }); | |
| 200 | expect(seen).toEqual(['Escape']); | |
| 201 | expect(list.hidden).toBe(true); | |
| 202 | })); |
new above
offline