@mccaskie-labs/workbench-ui 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/dist/form-script.d.ts +24 -0
- package/dist/form-script.js +210 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/style.js +16 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @mccaskie-labs/workbench-ui
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
- `FORM_SCRIPT`: the screen behaviour for forms checked by `workbench-forms` (Robert's form-validation standard §7 and §8): a problem shows when the person leaves the field and clears as they fix it, a failed submit focuses the first bad field and announces the count, an optional summary links each problem to its field, a date, amount or phone number that was read differently is shown back before it is stored, and reset clears everything. Styles for field errors, the summary, counters, text areas and the input width tokens (`wb-w-xs` to `wb-w-full`).
|
|
6
|
+
|
|
7
|
+
## 0.2.2
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- No code change. The first version published by the publish workflow, which confirms the package's npm trusted publisher.
|
|
12
|
+
|
|
3
13
|
## 0.2.1
|
|
4
14
|
|
|
5
15
|
- A folding card can carry a `summary` chip beside its heading, shown only while the card is folded ("All running" with a tick, "1 waiting"), so a folded card still says whether it needs anything.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Screen behaviour for forms checked by workbench-forms (Robert's
|
|
3
|
+
* form-validation standard §7 and §8). Put it on the page, under the page's
|
|
4
|
+
* nonce, after workbench-forms' FORMS_SCRIPT; each `<form data-wb-form="<name>">`
|
|
5
|
+
* reads the config block workbench-forms' `embedConfig` wrote for it.
|
|
6
|
+
*
|
|
7
|
+
* - A field's problem first shows when the person leaves the field; a valid
|
|
8
|
+
* field's tidied value is written back then, so they see what was read.
|
|
9
|
+
* - Once shown, the field is checked again on every keystroke, and that only
|
|
10
|
+
* ever clears the message.
|
|
11
|
+
* - A failed submit is never silent: every problem shows, focus moves to the
|
|
12
|
+
* first bad field and scrolls it into view, and the count is announced.
|
|
13
|
+
* A form with `data-wb-summary` also lists the problems at its top, each a
|
|
14
|
+
* link to its field.
|
|
15
|
+
* - When a date, an amount or a phone number was read as something other than
|
|
16
|
+
* what was typed, the first submit writes it back and stops, so the person
|
|
17
|
+
* sees it before it is stored.
|
|
18
|
+
* - Reset clears every message, the summary and the counters.
|
|
19
|
+
*
|
|
20
|
+
* Messages come only from the config (already in the visitor's language) and
|
|
21
|
+
* are written as text, never as HTML. Without WorkbenchForms of the same
|
|
22
|
+
* version on the page it does nothing: the endpoint still checks everything.
|
|
23
|
+
*/
|
|
24
|
+
export declare const FORM_SCRIPT: string;
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Screen behaviour for forms checked by workbench-forms (Robert's
|
|
3
|
+
* form-validation standard §7 and §8). Put it on the page, under the page's
|
|
4
|
+
* nonce, after workbench-forms' FORMS_SCRIPT; each `<form data-wb-form="<name>">`
|
|
5
|
+
* reads the config block workbench-forms' `embedConfig` wrote for it.
|
|
6
|
+
*
|
|
7
|
+
* - A field's problem first shows when the person leaves the field; a valid
|
|
8
|
+
* field's tidied value is written back then, so they see what was read.
|
|
9
|
+
* - Once shown, the field is checked again on every keystroke, and that only
|
|
10
|
+
* ever clears the message.
|
|
11
|
+
* - A failed submit is never silent: every problem shows, focus moves to the
|
|
12
|
+
* first bad field and scrolls it into view, and the count is announced.
|
|
13
|
+
* A form with `data-wb-summary` also lists the problems at its top, each a
|
|
14
|
+
* link to its field.
|
|
15
|
+
* - When a date, an amount or a phone number was read as something other than
|
|
16
|
+
* what was typed, the first submit writes it back and stops, so the person
|
|
17
|
+
* sees it before it is stored.
|
|
18
|
+
* - Reset clears every message, the summary and the counters.
|
|
19
|
+
*
|
|
20
|
+
* Messages come only from the config (already in the visitor's language) and
|
|
21
|
+
* are written as text, never as HTML. Without WorkbenchForms of the same
|
|
22
|
+
* version on the page it does nothing: the endpoint still checks everything.
|
|
23
|
+
*/
|
|
24
|
+
export const FORM_SCRIPT = `(() => {
|
|
25
|
+
const F = globalThis.WorkbenchForms;
|
|
26
|
+
const configs = {};
|
|
27
|
+
for (const s of document.querySelectorAll('script[type="application/json"][data-wb-form-config]')) {
|
|
28
|
+
try { configs[s.getAttribute("data-wb-form-config")] = JSON.parse(s.textContent); } catch (e) { console.error("workbench-ui: a form config isn't valid JSON"); }
|
|
29
|
+
}
|
|
30
|
+
const plural = (text, params) => text.replace(/\\{(\\w+), plural, one \\{([^{}]*)\\} other \\{([^{}]*)\\}\\}/g, (m, k, one, other) => {
|
|
31
|
+
const n = Number(params[k]);
|
|
32
|
+
let kind = n === 1 ? "one" : "other";
|
|
33
|
+
try { kind = new Intl.PluralRules(document.documentElement.lang || "en").select(n) === "one" ? "one" : "other"; } catch (e) {}
|
|
34
|
+
return (kind === "one" ? one : other).replace(/#/g, String(params[k]));
|
|
35
|
+
});
|
|
36
|
+
const fill = (text, params) => plural(text, params || {}).replace(/\\{(\\w+)\\}/g, (m, k) => (params && Object.prototype.hasOwnProperty.call(params, k) ? String(params[k]) : m));
|
|
37
|
+
for (const form of document.querySelectorAll("form[data-wb-form]")) {
|
|
38
|
+
const name = form.getAttribute("data-wb-form");
|
|
39
|
+
const cfg = Object.prototype.hasOwnProperty.call(configs, name) ? configs[name] : null;
|
|
40
|
+
if (!cfg) continue;
|
|
41
|
+
if (!F || F.version !== cfg.version) { console.warn("workbench-ui: form " + name + " needs FORMS_SCRIPT " + cfg.version + " on the page"); continue; }
|
|
42
|
+
const fields = cfg.form.fields;
|
|
43
|
+
const names = Object.keys(fields);
|
|
44
|
+
const messages = cfg.messages || {};
|
|
45
|
+
let held = false;
|
|
46
|
+
const controls = (n) => Array.prototype.filter.call(form.elements, (el) => el.name === n);
|
|
47
|
+
const read = (n) => {
|
|
48
|
+
const def = fields[n];
|
|
49
|
+
const els = controls(n);
|
|
50
|
+
if (!els.length) return undefined;
|
|
51
|
+
if (def.kind === "choice" && def.multiple) {
|
|
52
|
+
const picked = [];
|
|
53
|
+
for (const el of els) {
|
|
54
|
+
if (el.tagName === "SELECT") for (const o of el.selectedOptions) picked.push(o.value);
|
|
55
|
+
else if (el.checked) picked.push(el.value);
|
|
56
|
+
}
|
|
57
|
+
if (picked.length) return picked;
|
|
58
|
+
return form.elements.namedItem(n + "__none") ? [] : null;
|
|
59
|
+
}
|
|
60
|
+
const radios = els.filter((el) => el.type === "radio");
|
|
61
|
+
if (radios.length) { const on = radios.find((el) => el.checked); return on ? on.value : undefined; }
|
|
62
|
+
return els[0].value;
|
|
63
|
+
};
|
|
64
|
+
const writable = (n) => {
|
|
65
|
+
const def = fields[n];
|
|
66
|
+
const els = controls(n);
|
|
67
|
+
return !def.generic && def.kind !== "choice" && els.length === 1 && (els[0].tagName === "TEXTAREA" || (els[0].tagName === "INPUT" && !/^(checkbox|radio|file|hidden)$/.test(els[0].type)));
|
|
68
|
+
};
|
|
69
|
+
const writeBack = (n, value) => { if (writable(n)) { const el = controls(n)[0]; if (el.value !== value) el.value = value; } };
|
|
70
|
+
const idFor = (n) => { const el = controls(n)[0]; if (el && !el.id) el.id = "wb-" + name + "-" + n; return el ? el.id : ""; };
|
|
71
|
+
const errorFor = (n) => {
|
|
72
|
+
const id = "wb-err-" + name + "-" + n;
|
|
73
|
+
let p = document.getElementById(id);
|
|
74
|
+
if (!p) {
|
|
75
|
+
const els = controls(n);
|
|
76
|
+
if (!els.length) return null;
|
|
77
|
+
p = document.createElement("p");
|
|
78
|
+
p.id = id;
|
|
79
|
+
p.className = "field-error";
|
|
80
|
+
const last = els[els.length - 1];
|
|
81
|
+
const anchor = last.closest("fieldset") && els.length > 1 ? last.closest("fieldset") : last;
|
|
82
|
+
anchor.insertAdjacentElement("afterend", p);
|
|
83
|
+
for (const el of els) {
|
|
84
|
+
const by = (el.getAttribute("aria-describedby") || "").split(" ").filter(Boolean);
|
|
85
|
+
if (!by.includes(id)) el.setAttribute("aria-describedby", by.concat(id).join(" "));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return p;
|
|
89
|
+
};
|
|
90
|
+
const words = (p) => {
|
|
91
|
+
const keys = ["validation." + name + "." + p.field + "." + p.code, "validation." + p.code];
|
|
92
|
+
for (const k of keys) if (Object.prototype.hasOwnProperty.call(messages, k)) return fill(messages[k], p.params);
|
|
93
|
+
return p.code;
|
|
94
|
+
};
|
|
95
|
+
const show = (p) => {
|
|
96
|
+
const el = errorFor(p.field);
|
|
97
|
+
if (el) el.textContent = words(p);
|
|
98
|
+
for (const c of controls(p.field)) c.setAttribute("aria-invalid", "true");
|
|
99
|
+
};
|
|
100
|
+
const clear = (n) => {
|
|
101
|
+
const el = document.getElementById("wb-err-" + name + "-" + n);
|
|
102
|
+
if (el) el.textContent = "";
|
|
103
|
+
for (const c of controls(n)) c.removeAttribute("aria-invalid");
|
|
104
|
+
};
|
|
105
|
+
const showing = (n) => controls(n).some((c) => c.getAttribute("aria-invalid") === "true");
|
|
106
|
+
const check = () => {
|
|
107
|
+
const input = {};
|
|
108
|
+
for (const n of names) input[n] = read(n);
|
|
109
|
+
try { return F.validate(cfg.form, input, cfg.context); } catch (e) { console.error("workbench-ui: form " + name + ": " + e.message); return null; }
|
|
110
|
+
};
|
|
111
|
+
let live = form.querySelector(".wb-live");
|
|
112
|
+
if (!live) { live = document.createElement("p"); live.className = "wb-live"; live.setAttribute("role", "status"); live.setAttribute("aria-live", "polite"); form.prepend(live); }
|
|
113
|
+
const summary = form.hasAttribute("data-wb-summary") ? (form.querySelector(".wb-summary") || form.insertBefore(Object.assign(document.createElement("div"), { className: "wb-summary", tabIndex: -1 }), form.firstChild)) : null;
|
|
114
|
+
const say = (key, count) => { const text = Object.prototype.hasOwnProperty.call(messages, key) ? fill(messages[key], { count }) : ""; live.textContent = ""; setTimeout(() => { live.textContent = text; }, 50); return text; };
|
|
115
|
+
const counters = () => {
|
|
116
|
+
for (const el of form.querySelectorAll("[data-wb-max]")) {
|
|
117
|
+
const id = "wb-count-" + name + "-" + el.name;
|
|
118
|
+
let c = document.getElementById(id);
|
|
119
|
+
if (!c) { c = document.createElement("span"); c.id = id; c.className = "wb-counter"; c.setAttribute("aria-hidden", "true"); el.insertAdjacentElement("afterend", c); }
|
|
120
|
+
const def = fields[el.name];
|
|
121
|
+
const tidied = def && def.kind === "long_text" ? F.tidyText(el.value) : F.tidyLine(el.value);
|
|
122
|
+
const n = Array.from(tidied).length;
|
|
123
|
+
c.textContent = n + " / " + el.getAttribute("data-wb-max");
|
|
124
|
+
c.classList.toggle("over", n > Number(el.getAttribute("data-wb-max")));
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
counters();
|
|
128
|
+
// Pressing a submit button leaves the field too, but the submit shows every problem itself: a message
|
|
129
|
+
// appearing on the way would move the button out from under the pointer, and the press would be lost.
|
|
130
|
+
let pressing = false;
|
|
131
|
+
const isSubmit = (el) => el && el.form === form && (el.type === "submit" || (el.tagName === "BUTTON" && !el.getAttribute("type")));
|
|
132
|
+
form.addEventListener("pointerdown", (e) => {
|
|
133
|
+
const b = e.target && e.target.closest ? e.target.closest("button, input") : null;
|
|
134
|
+
if (isSubmit(b)) { pressing = true; setTimeout(() => { pressing = false; }, 1000); }
|
|
135
|
+
});
|
|
136
|
+
form.addEventListener("focusout", (e) => {
|
|
137
|
+
const n = e.target && e.target.name;
|
|
138
|
+
if (!n || !names.includes(n)) return;
|
|
139
|
+
// Leaving one box of a group for another of the same field isn't leaving the field.
|
|
140
|
+
if (e.relatedTarget && e.relatedTarget.name === n) return;
|
|
141
|
+
if (pressing || isSubmit(e.relatedTarget)) return;
|
|
142
|
+
const r = check();
|
|
143
|
+
if (!r) return;
|
|
144
|
+
const p = r.problems.find((x) => x.field === n);
|
|
145
|
+
if (p) show(p);
|
|
146
|
+
else { clear(n); writeBack(n, r.shown[n]); counters(); }
|
|
147
|
+
});
|
|
148
|
+
const edited = (e) => {
|
|
149
|
+
held = false;
|
|
150
|
+
counters();
|
|
151
|
+
const n = e.target && e.target.name;
|
|
152
|
+
if (!n || !names.includes(n)) return;
|
|
153
|
+
// Keystrokes only ever turn messages off: this field's, and any a condition on it no longer asks for.
|
|
154
|
+
if (!names.some(showing)) return;
|
|
155
|
+
const r = check();
|
|
156
|
+
if (!r) return;
|
|
157
|
+
for (const m of names) if (showing(m) && !r.problems.some((x) => x.field === m)) clear(m);
|
|
158
|
+
};
|
|
159
|
+
form.addEventListener("input", edited);
|
|
160
|
+
form.addEventListener("change", edited);
|
|
161
|
+
form.addEventListener("submit", (e) => {
|
|
162
|
+
pressing = false;
|
|
163
|
+
const r = check();
|
|
164
|
+
if (!r) return;
|
|
165
|
+
for (const n of names) if (!r.problems.some((x) => x.field === n)) writeBack(n, r.shown[n]);
|
|
166
|
+
counters();
|
|
167
|
+
if (r.problems.length) {
|
|
168
|
+
e.preventDefault();
|
|
169
|
+
held = false;
|
|
170
|
+
for (const n of names) if (!r.problems.some((x) => x.field === n)) clear(n);
|
|
171
|
+
for (const p of r.problems) show(p);
|
|
172
|
+
const text = say("validation.summary", r.problems.length);
|
|
173
|
+
if (summary) {
|
|
174
|
+
summary.textContent = "";
|
|
175
|
+
const head = document.createElement("p");
|
|
176
|
+
head.textContent = text;
|
|
177
|
+
const list = document.createElement("ul");
|
|
178
|
+
for (const p of r.problems) {
|
|
179
|
+
const li = document.createElement("li");
|
|
180
|
+
const a = document.createElement("a");
|
|
181
|
+
a.href = "#" + idFor(p.field);
|
|
182
|
+
a.textContent = words(p);
|
|
183
|
+
li.append(a);
|
|
184
|
+
list.append(li);
|
|
185
|
+
}
|
|
186
|
+
summary.append(head, list);
|
|
187
|
+
summary.hidden = false;
|
|
188
|
+
}
|
|
189
|
+
const first = controls(r.problems[0].field)[0];
|
|
190
|
+
if (first) { first.focus({ preventScroll: true }); first.scrollIntoView({ block: "center" }); }
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (summary) { summary.textContent = ""; summary.hidden = true; }
|
|
194
|
+
if (r.changed.length && !held) {
|
|
195
|
+
e.preventDefault();
|
|
196
|
+
held = true;
|
|
197
|
+
say("validation.changed", r.changed.length);
|
|
198
|
+
const first = controls(r.changed[0])[0];
|
|
199
|
+
if (first) { first.focus({ preventScroll: true }); first.scrollIntoView({ block: "center" }); }
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
form.addEventListener("reset", () => {
|
|
203
|
+
held = false;
|
|
204
|
+
for (const n of names) clear(n);
|
|
205
|
+
if (summary) { summary.textContent = ""; summary.hidden = true; }
|
|
206
|
+
live.textContent = "";
|
|
207
|
+
setTimeout(counters, 0);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
})();`;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { CSS, STYLE } from "./style.ts";
|
|
2
|
+
export { FORM_SCRIPT } from "./form-script.ts";
|
|
2
3
|
/** Plain text made safe to put in HTML, in text or in a quoted attribute. */
|
|
3
4
|
export declare const escape: (value: string) => string;
|
|
4
5
|
/** The outcome colours: a pill's class and, prefixed with o-, a table row's left edge. */
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// escaped here, and arguments documented as HTML are trusted as they are.
|
|
5
5
|
import { STYLE } from "./style.js";
|
|
6
6
|
export { CSS, STYLE } from "./style.js";
|
|
7
|
+
export { FORM_SCRIPT } from "./form-script.js";
|
|
7
8
|
/** Plain text made safe to put in HTML, in text or in a quoted attribute. */
|
|
8
9
|
export const escape = (value) => value.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
9
10
|
/** A coloured outcome pill. `text` is plain text. */
|
package/dist/style.js
CHANGED
|
@@ -151,6 +151,22 @@ dl.plain dl.plain>dd{margin-bottom:.5rem}dl.plain dl.plain>dd:last-child{margin-
|
|
|
151
151
|
.bool{font-size:.8rem;font-weight:600;padding:0 .4rem;border:1px solid var(--line);border-radius:4px;background:var(--raised)}
|
|
152
152
|
@media (max-width:640px){dl.plain{grid-template-columns:1fr;gap:.1rem}dl.plain dd{margin-bottom:.5rem}}
|
|
153
153
|
@media (prefers-reduced-motion:reduce){*{animation:none!important;transition:none!important}}
|
|
154
|
+
textarea,select{font:inherit;color:var(--text);background:var(--surface);border:1px solid var(--line);border-radius:6px;padding:.45rem .6rem;min-height:2.75rem}
|
|
155
|
+
textarea{width:100%;min-height:6rem;max-height:var(--wb-textarea-cap);field-sizing:content;resize:vertical}
|
|
156
|
+
textarea:focus-visible,select:focus-visible{outline:none;box-shadow:0 0 0 3px var(--focus)}
|
|
157
|
+
:root{--wb-w-xs:6ch;--wb-w-s:12ch;--wb-w-m:24ch;--wb-w-l:40ch;--wb-textarea-cap:20rem}
|
|
158
|
+
.wb-w-xs{width:calc(var(--wb-w-xs) + 1.4rem)}.wb-w-s{width:calc(var(--wb-w-s) + 1.4rem)}.wb-w-m{width:calc(var(--wb-w-m) + 1.4rem)}
|
|
159
|
+
.wb-w-l{width:min(100%,calc(var(--wb-w-l) + 1.4rem))}.wb-w-full{width:100%}
|
|
160
|
+
input:not([type=hidden]):not([type=checkbox]):not([type=radio]){min-height:2.75rem}
|
|
161
|
+
[aria-invalid=true]{border-color:var(--fail)}
|
|
162
|
+
.field-error{margin:.3rem 0 0;color:var(--fail);font-size:.875rem}
|
|
163
|
+
.field-error:empty{display:none}
|
|
164
|
+
.wb-counter{display:block;margin-top:.2rem;color:var(--muted);font-size:.78rem;font-variant-numeric:tabular-nums}
|
|
165
|
+
.wb-counter.over{color:var(--fail)}
|
|
166
|
+
.wb-summary{margin:0 0 1rem;padding:.6rem .75rem;border-left:3px solid var(--fail);background:var(--fail-bg);border-radius:6px}
|
|
167
|
+
.wb-summary[hidden],.wb-summary:empty{display:none}
|
|
168
|
+
.wb-summary p{margin:0 0 .3rem;font-weight:600}.wb-summary ul{margin:0;padding-left:1.1rem}
|
|
169
|
+
.wb-live{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}
|
|
154
170
|
@media (max-width:640px){nav.links{margin-left:0}th,td{padding:.55rem .7rem}table.kv th{width:auto}}
|
|
155
171
|
`;
|
|
156
172
|
/** The shared rules as a <style> tag. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mccaskie-labs/workbench-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Workbench pages: one dark look, a top bar with the environment tag, cards, outcome pills, and one format for times, money and durations.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|