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

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

quill/pixel-plays:quill/98-the-picker-above-the-game-needs-to-be-be → main 6 commits fork

  • Merged. quill/98-the-picker-above-the-game-needs-to-be-be was merged into main as d0bf4f9.

Review

Sign in to review this change.
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`)
6464 ## Add a game
6565 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`.
6666
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.
6868 - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals.
6969 - A game supplies its own `groundColor`, `bake`, `update` and `draw`.
7070
shell.js +72 −11
@@ -52,7 +52,14 @@ window.PixelPlays = (() => {
5252 .pp-link { font: inherit; color: #8b877f; background: none; border: 0; padding: 10px; margin: 0; cursor: pointer; }
5353 .pp-link:hover, .pp-link:focus-visible, .pp-link[aria-expanded="true"] { color: #1c1b19; }
5454 #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; }
5663 #pp-list[hidden] { display: none; }
5764 #pp-list a, #pp-list span { display: flex; justify-content: space-between; gap: 16px; padding: 8px 12px; color: #1c1b19; text-decoration: none; }
5865 #pp-list a:hover, #pp-list a:focus-visible { background: #1c1b19; color: #fff; outline: none; }
@@ -91,25 +98,79 @@ window.PixelPlays = (() => {
9198 const list = document.createElement('ul');
9299 list.id = 'pp-list';
93100 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 => {
101139 const link = document.createElement('a');
102140 link.href = '../' + g.id + '/';
103141 link.textContent = g.title;
104142 if (g.id === id) link.setAttribute('aria-current', 'page');
105 item.append(link);
106 }
107 list.append(item);
143 return link;
144 });
108145 }
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(); });
109169 menu.append(toggle, list);
110170 const setOpen = open => {
111171 list.hidden = !open;
112172 toggle.setAttribute('aria-expanded', String(open));
173 if (open) list.querySelector('[aria-current]')?.scrollIntoView({ block: 'center' });
113174 };
114175 toggle.addEventListener('click', () => setOpen(list.hidden));
115176 document.addEventListener('pointerdown', e => { if (!menu.contains(e.target)) setOpen(false); });
shell.test.mjs +112 −7
@@ -1,16 +1,31 @@
11 import { test, expect } from 'bun:test';
22
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() {},
722 });
823 const SAVED = ['window', 'document', 'Audio', 'PixelPlays'];
924 const realFetch = globalThis.fetch;
10 let audios, byId, sent, listeners;
25 let audios, byId, sent, listeners, scrolled;
1126 // stubs live only while a test runs, so they never leak into the server tests in the same process
1227 function stub() {
13 audios = []; byId = {}; sent = []; listeners = {};
28 scrolled = []; audios = []; byId = {}; sent = []; listeners = {};
1429 globalThis.fetch = (url, init) => { sent.push([url, JSON.parse(init.body)]); return Promise.resolve(); };
1530 globalThis.window = globalThis;
1631 window.addEventListener = (k, f) => { (window.addEventListener.calls[k] ??= []).push(f); };
@@ -18,7 +33,7 @@ function stub() {
1833 globalThis.Audio = class { constructor(src) { this.src = src; audios.push(src); } play() { return Promise.resolve(); } pause() {} };
1934 globalThis.document = {
2035 head: element(), body: element(), hidden: false, addEventListener(k, f) { (listeners[k] ??= []).push(f); },
21 createElement: () => element(),
36 createElement: tag => element(tag),
2237 getElementById: id => (byId[id] ??= element()),
2338 };
2439 }
@@ -95,3 +110,93 @@ test('time is sent when the page is hidden after a play, resumes on return, clam
95110 expect(times()).toEqual([12, 3600, 3]);
96111 } finally { Date.now = realNow; }
97112 }));
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 }));

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs