Public: anyone can read this; the crew can change it.
A form's button says it is working after a tap and refuses a second tap until the answer returns #13054
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/13031-a-form-s-button-says-it-is-working-after → main 9 commits fork 3 threads · 3 unresolved
4 files
changed +123
−1
docs/design/areas/forms.md +6
| @@ -10,3 +10,9 @@ A form's fields and its buttons sit in two rows, one for what you fill in and on | ||
| 10 | 10 | - `/signin` and `/signup` (#9812) speak the home page's language and add no class: `authForm` in `pages/site.ts` draws the page's one `<h1>` (`pageHead`), then the passkey door, the form and its `.form-actions` row straight on the page, no `.card`. `test/auth-language.test.ts` holds the shape. |
| 11 | 11 | |
| 12 | 12 | - `/new` (#9830) fits a 390 px phone with no new class: the page's wrapper carries the `data-new-repo` hook (not a class), which `style/content.ts` gives `overflow-wrap: anywhere` like the other long-text pages. `test/new-repo-fit.test.ts` holds it. |
| 13 | ||
| 14 | ## Known gap: `<input type=submit>` | |
| 15 | ||
| 16 | The busy helper (`src/web/form_busy.ts`) swaps a button's inner HTML, so a form | |
| 17 | submitted by `<input type=submit>` gets no "working" word and no second-tap | |
| 18 | refusal. Use `<button>` for a POST form until the helper learns `value`. |
src/web/client.ts +2 −1
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | import { GO_MATCH_JS, goPagesData } from "./go_pages.ts"; |
| 14 | 14 | import { keyDecisionJs } from "./key_decision.ts"; |
| 15 | 15 | import { actNowJs } from "./act_now.ts"; |
| 16 | import { FORM_BUSY_JS } from "./form_busy.ts"; | |
| 16 | 17 | import { pickJs } from "./pick.ts"; |
| 17 | 18 | import { imageViewJs } from "./render/image.ts"; |
| 18 | 19 | import { STAGE_BADGE_JS } from "./stage-row.ts"; |
| @@ -2217,7 +2218,7 @@ const shellCore = /* js */ ` | ||
| 2217 | 2218 | `; |
| 2218 | 2219 | |
| 2219 | 2220 | /** The shell and the one script that makes actions answer at once (`act_now.ts`). */ |
| 2220 | export const shellJs = `${shellCore}\n${actNowJs}\n${pickJs}`; | |
| 2221 | export const shellJs = `${shellCore}\n${actNowJs}\n${pickJs}\n${FORM_BUSY_JS}`; | |
| 2221 | 2222 | |
| 2222 | 2223 | /** |
| 2223 | 2224 | * Live regions. A page marks the parts of itself that change while it is |
src/web/form_busy.ts added +26
| @@ -0,0 +1,26 @@ | ||
| 1 | /** | |
| 2 | * The one client helper every form gets (appended to `shellJs`, `client.ts`): | |
| 3 | * a POST form that is submitting says its working word (`data-working` on the | |
| 4 | * button, else "Working…"), marks it aria-busy and aria-disabled (never | |
| 5 | * `disabled`: focus stays on the button) and refuses a second send until the | |
| 6 | * page changes or 20 s pass. A later handler that cancels the submit (a | |
| 7 | * fetch-handled form) restores the button at once. `<input type=submit>` has | |
| 8 | * no innerHTML to swap and is left without feedback (docs/design/areas/forms.md). | |
| 9 | * Without script the form posts once, as always. Forms with their own handler | |
| 10 | * (`data-now`, or any handler that prevents the default) and GET or new-tab forms are | |
| 11 | * left alone. | |
| 12 | * `FORM_BUSY_BUDGET` is its gzipped byte budget, held by `test/form-busy.test.ts`. | |
| 13 | */ | |
| 14 | export const FORM_BUSY_BUDGET = 400; | |
| 15 | ||
| 16 | export const FORM_BUSY_JS = /* js */ ` | |
| 17 | var fbr=[];addEventListener('pageshow',function(){while(fbr.length)fbr.pop()()}); | |
| 18 | addEventListener('submit',function(e){var f=e.target,b=e.submitter,h; | |
| 19 | if(!b||f.method!='post'||f.target||f.dataset.now!=null||e.defaultPrevented)return; | |
outdated
A new pageshow listener is added per submit and never removed. Register it once, outside the submit handler. |
||
| 20 | if(f.__w)return e.preventDefault(); | |
| 21 | f.__w=1;h=b.innerHTML; | |
| 22 | function u(){f.__w=0;b.removeAttribute('aria-busy');b.removeAttribute('aria-disabled');b.innerHTML=h} | |
| 23 | setTimeout(function(){if(e.defaultPrevented)return u(); | |
| 24 | b.innerHTML=b.dataset.working||'Working\\u2026';b.setAttribute('aria-busy','true');b.setAttribute('aria-disabled','true'); | |
| 25 | fbr.push(u);setTimeout(u,2e4)})}); | |
| 26 | `; |
test/form-busy.test.ts added +89
| @@ -0,0 +1,89 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { gzipSync } from "node:zlib"; | |
| 3 | import { FORM_BUSY_BUDGET, FORM_BUSY_JS } from "../src/web/form_busy.ts"; | |
| 4 | import { shellJs } from "../src/web/client.ts"; | |
| 5 | ||
| 6 | type Listener = (e: any) => void; | |
| 7 | ||
| 8 | /** Runs the helper against a fake form and button; returns the pieces. */ | |
| 9 | function run(over: Record<string, unknown> = {}) { | |
| 10 | let submit: Listener = () => {}; | |
| 11 | const timers: Array<() => void> = []; | |
| 12 | const pageshow: Listener[] = []; | |
| 13 | const button: any = { innerHTML: "Ship", dataset: { working: "Shipping…" }, disabled: 0, attrs: {} as Record<string, string>, | |
| 14 | setAttribute(k: string, v: string) { this.attrs[k] = v; }, removeAttribute(k: string) { delete this.attrs[k]; } }; | |
| 15 | const form: any = { method: "post", target: "", dataset: {}, querySelector: () => button, ...over }; | |
| 16 | const g = { | |
| 17 | addEventListener: (t: string, fn: Listener) => (t === "submit" ? (submit = fn) : pageshow.push(fn)), | |
| 18 | setTimeout: (fn: () => void) => timers.push(fn), | |
| 19 | }; | |
| 20 | new Function("addEventListener", "setTimeout", FORM_BUSY_JS)(g.addEventListener, g.setTimeout); | |
| 21 | const send = () => { | |
| 22 | const e = { target: form, submitter: button, defaultPrevented: false, prevented: false, preventDefault() { this.prevented = this.defaultPrevented = true; } }; | |
| 23 | submit(e); | |
| 24 | return e; | |
| 25 | }; | |
| 26 | return { form, button, timers, pageshow, send }; | |
| 27 | } | |
| 28 | ||
| 29 | test("a submitting form says its working word and disables the button", () => { | |
| 30 | const t = run(); | |
| 31 | expect(t.send().prevented).toBe(false); // the first send goes through | |
| 32 | t.timers[0]!(); | |
| 33 | expect(t.button.innerHTML).toBe("Shipping…"); | |
| 34 | expect(t.button.attrs["aria-busy"]).toBe("true"); | |
| 35 | expect(t.button.attrs["aria-disabled"]).toBe("true"); | |
| 36 | expect(t.button.disabled).toBe(0); // focus stays on the button | |
| 37 | }); | |
| 38 | ||
| 39 | test("a submit a later handler cancels restores the button and never says working", () => { | |
| 40 | const t = run(); | |
| 41 | const e = t.send(); | |
| 42 | e.preventDefault(); // a fetch-handled form's own handler, after ours | |
| 43 | t.timers[0]!(); | |
| 44 | expect(t.button.innerHTML).toBe("Ship"); | |
| 45 | expect(t.button.attrs).toEqual({}); | |
| 46 | expect(t.timers.length).toBe(1); // no 20 s timer armed | |
outdated
Add a test for restore on error (the form whose submit is cancelled by a later handler or fails without navigating); today only the pageshow and 20 s paths are pinned. |
||
| 47 | expect(t.send().prevented).toBe(false); // and the form can be sent again | |
| 48 | }); | |
| 49 | ||
| 50 | test("one pageshow listener serves every submit", () => { | |
| 51 | const t = run(); | |
| 52 | t.send(); | |
| 53 | t.timers[0]!(); | |
| 54 | t.send(); | |
| 55 | expect(t.pageshow.length).toBe(1); | |
| 56 | }); | |
| 57 | ||
| 58 | test("a second submit is refused until the page is restored", () => { | |
| 59 | const t = run(); | |
| 60 | t.send(); | |
| 61 | expect(t.send().prevented).toBe(true); | |
| 62 | t.timers[0]!(); | |
| 63 | t.pageshow[0]!({}); | |
| 64 | expect(t.button.innerHTML).toBe("Ship"); | |
| 65 | expect(t.button.attrs).toEqual({}); | |
| 66 | expect(t.send().prevented).toBe(false); | |
| 67 | }); | |
| 68 | ||
| 69 | test("the button comes back after the time limit", () => { | |
| 70 | const t = run(); | |
| 71 | t.send(); | |
| 72 | t.timers[0]!(); | |
| 73 | t.timers[1]!(); | |
| 74 | expect(t.button.innerHTML).toBe("Ship"); | |
| 75 | }); | |
| 76 | ||
| 77 | test("forms with their own handler, GET forms and new-tab forms are left alone", () => { | |
| 78 | for (const over of [{ dataset: { now: "Saving" } }, { method: "get" }, { target: "_blank" }]) { | |
| 79 | const t = run(over); | |
| 80 | t.send(); | |
| 81 | expect(t.timers.length).toBe(0); | |
| 82 | expect(t.send().prevented).toBe(false); | |
| 83 | } | |
| 84 | }); | |
| 85 | ||
| 86 | test("it adds under the byte budget gzipped and ships in the shell", () => { | |
| 87 | expect(gzipSync(FORM_BUSY_JS).length).toBeLessThan(FORM_BUSY_BUDGET); | |
| 88 | expect(shellJs).toContain(FORM_BUSY_JS); | |
| 89 | }); |
new above
offline
Setting disabled=1 on the submitter moves focus to <body> for keyboard and screen-reader users and announces nothing. Use aria-busy/aria-disabled and let the handler refuse the second send. Also there is no restore on error here, only the 20 s timer (box 1).