@mccaskie-labs/workbench-ui 0.10.0 → 0.11.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @mccaskie-labs/workbench-ui
2
2
 
3
+ ## 0.11.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Form fields are sized from what they hold, and text areas grow as people type (Robert, 24 November 2026). workbench-forms: renderForm gives every one-line input a width worked out from the field when the limits register gives none: a number under 10,000 is about six characters wide, a 4-digit postcode five, a phone number sixteen, a date fourteen, an email thirty, and text with a maximum of 40 or fewer characters about that many; longer text takes the full width. The widths are new `wb-form-size-<n>` classes (n in characters: 5, 6, 8, 10, 12, 14, 16, 20, 24, 30, 40), each capped at the form's width, so a phone or a narrow layout column still fits. The register's `width` still wins. A long answer's text area grows with its text up to `--wb-form-textarea-cap` and only then scrolls; `data-wb-grow="off"` keeps one fixed. workbench-ui: `input()` sizes a control the same way from its type, maxlength, min and max (`wb-ui-size-<n>`, which any tool's own control can also carry, weighted to win over that tool's single-class rule); every text area on a page built with shell grows with its text (BASE_CSS); and FORM_SCRIPT sets the height itself in browsers without CSS `field-sizing` (Firefox), with no animation and without moving the page.
8
+
3
9
  ## 0.10.0
4
10
 
5
11
  ### Minor Changes
@@ -20,6 +20,10 @@
20
20
  * control itself) gets \`hidden\`, its controls are disabled so they aren't
21
21
  * sent, and its message clears. This updates as the person types.
22
22
  * - Reset clears every message, the summary and the counters.
23
+ * - Every text area on the page grows with what is typed up to its CSS
24
+ * max-height, then scrolls (Robert, 24 November 2026). Browsers with CSS
25
+ * \`field-sizing\` do this themselves; for the rest the script sets the
26
+ * height. \`data-wb-grow="off"\` on a text area keeps it fixed.
23
27
  *
24
28
  * Every element it adds carries a \`wb-form-*\` class (workbench-forms'
25
29
  * FORM_STYLES), the same names renderForm uses.
@@ -20,6 +20,10 @@
20
20
  * control itself) gets \`hidden\`, its controls are disabled so they aren't
21
21
  * sent, and its message clears. This updates as the person types.
22
22
  * - Reset clears every message, the summary and the counters.
23
+ * - Every text area on the page grows with what is typed up to its CSS
24
+ * max-height, then scrolls (Robert, 24 November 2026). Browsers with CSS
25
+ * \`field-sizing\` do this themselves; for the rest the script sets the
26
+ * height. \`data-wb-grow="off"\` on a text area keeps it fixed.
23
27
  *
24
28
  * Every element it adds carries a \`wb-form-*\` class (workbench-forms'
25
29
  * FORM_STYLES), the same names renderForm uses.
@@ -29,6 +33,32 @@
29
33
  * version on the page it does nothing: the endpoint still checks everything.
30
34
  */
31
35
  export const FORM_SCRIPT = `(() => {
36
+ // Text areas grow with what is typed, up to their max-height, and only then scroll. CSS field-sizing does it
37
+ // where the browser has it; elsewhere (Firefox) this sets the height. No animation, and the page never jumps.
38
+ if (typeof window !== "undefined" && typeof document.addEventListener === "function" && !globalThis.__wbGrow && !(globalThis.CSS && CSS.supports && CSS.supports("field-sizing", "content"))) {
39
+ globalThis.__wbGrow = true;
40
+ const grows = (el) => el && el.tagName === "TEXTAREA" && el.getAttribute("data-wb-grow") !== "off";
41
+ const grow = (el) => {
42
+ if (!grows(el) || !el.isConnected || el.offsetParent === null) return;
43
+ const cs = getComputedStyle(el);
44
+ const cap = cs.maxHeight === "none" ? 320 : parseFloat(cs.maxHeight);
45
+ const edges = cs.boxSizing === "border-box" ? parseFloat(cs.borderTopWidth) + parseFloat(cs.borderBottomWidth) : -(parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom));
46
+ const x = window.scrollX, y = window.scrollY;
47
+ el.style.height = "auto";
48
+ const want = el.scrollHeight + edges;
49
+ el.style.height = Math.min(cap, want) + "px";
50
+ el.style.overflowY = want > cap + 1 ? "auto" : "hidden";
51
+ if (window.scrollY !== y) window.scrollTo(x, y);
52
+ };
53
+ const all = () => { for (const el of document.querySelectorAll("textarea")) grow(el); };
54
+ document.addEventListener("input", (e) => grow(e.target));
55
+ // A value the page writes back (a tidied answer, a reset) fires no input event.
56
+ document.addEventListener("focusout", (e) => grow(e.target));
57
+ document.addEventListener("reset", () => setTimeout(all, 0));
58
+ let frame = 0;
59
+ window.addEventListener("resize", () => { if (!frame) frame = requestAnimationFrame(() => { frame = 0; all(); }); });
60
+ if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", all); else all();
61
+ }
32
62
  const F = globalThis.WorkbenchForms;
33
63
  const configs = {};
34
64
  for (const s of document.querySelectorAll('script[type="application/json"][data-wb-form-config]')) {
package/dist/index.d.ts CHANGED
@@ -107,7 +107,7 @@ export type ConfirmPartOptions = Omit<ConfirmOptions, "prefix" | "askClass">;
107
107
  * with no script. Every text option is plain text; `extra` is HTML.
108
108
  */
109
109
  export declare function confirm(opts: ConfirmPartOptions): string;
110
- /** A one-line control. `a` holds its attributes (id, name, type, value, required and so on). */
110
+ /** A one-line control. `a` holds its attributes (id, name, type, value, required and so on); its width follows from them (type, maxlength, min and max). */
111
111
  export declare function input(a: Attrs): string;
112
112
  /** A text area holding `text`, which is plain text. */
113
113
  export declare function textarea(a: Attrs, text?: string): string;
package/dist/index.js CHANGED
@@ -128,9 +128,52 @@ export function actions(inner) {
128
128
  export function confirm(opts) {
129
129
  return renderConfirm({ ...opts, prefix: "wb-ui", askClass: "wb-ui-button" });
130
130
  }
131
- /** A one-line control. `a` holds its attributes (id, name, type, value, required and so on). */
131
+ /** The widths a one-line control can take, in characters of its own font (UI_STYLES' `wb-ui-size-<n>`), each capped at the width of what holds it. */
132
+ const SIZE_STEPS = Object.freeze([5, 6, 8, 10, 12, 14, 16, 20, 24, 30, 40]);
133
+ /**
134
+ * About how many characters a control's answer takes, from its attributes
135
+ * (Robert, 24 November 2026): a number up to 9999 needs about five, a phone
136
+ * number sixteen, text with maxlength 4 four. Undefined leaves the control at
137
+ * the usual width.
138
+ */
139
+ function controlChars(a) {
140
+ const type = String(a.type ?? "text").toLowerCase();
141
+ const num = (k) => (a[k] === undefined || a[k] === null || a[k] === "" || Number.isNaN(Number(a[k])) ? undefined : Number(a[k]));
142
+ const max = num("maxlength");
143
+ switch (type) {
144
+ case "number": {
145
+ const hi = num("max");
146
+ if (hi === undefined)
147
+ return undefined;
148
+ const lo = num("min") ?? 0;
149
+ const step = String(a.step ?? "");
150
+ const places = step.includes(".") ? step.split(".")[1].length : 0;
151
+ // Digits, a minus sign, the decimals and the browser's up and down arrows.
152
+ return Math.max(String(Math.floor(Math.abs(hi))).length, String(Math.floor(Math.abs(lo))).length) + (lo < 0 ? 1 : 0) + (places ? places + 1 : 0) + 3;
153
+ }
154
+ case "tel":
155
+ return 15;
156
+ case "date":
157
+ return 13;
158
+ case "time":
159
+ return 9;
160
+ case "month":
161
+ return 14;
162
+ case "email":
163
+ return Math.min(29, max ?? 29);
164
+ case "text":
165
+ case "password":
166
+ case "search":
167
+ return max !== undefined && max <= 40 ? max : undefined;
168
+ default:
169
+ return undefined;
170
+ }
171
+ }
172
+ /** A one-line control. `a` holds its attributes (id, name, type, value, required and so on); its width follows from them (type, maxlength, min and max). */
132
173
  export function input(a) {
133
- return `<input class="wb-ui-input"${attrs(a)}>`;
174
+ const chars = controlChars(a);
175
+ const step = chars === undefined ? undefined : SIZE_STEPS.find((s) => s >= chars + 1);
176
+ return `<input class="wb-ui-input${step ? ` wb-ui-size-${step}` : ""}"${attrs(a)}>`;
134
177
  }
135
178
  /** A text area holding `text`, which is plain text. */
136
179
  export function textarea(a, text = "") {
package/dist/style.d.ts CHANGED
@@ -3,7 +3,8 @@ export declare const CSS: string;
3
3
  /**
4
4
  * What a project's workbench.css carries besides the classes: the few rules
5
5
  * every Workbench page needs that no class can carry (the spinner's keyframes,
6
- * links and code inside a page, reduced motion). Each is scoped to a page built
6
+ * links and code inside a page, every text area growing with its text, reduced
7
+ * motion). Each is scoped to a page built
7
8
  * with shell (`<html class="wb-ui">`) and wrapped in :where(), so it adds no
8
9
  * weight and never touches a page of the project's own.
9
10
  */
package/dist/style.js CHANGED
@@ -111,7 +111,8 @@ input:not([type=hidden]):not([type=checkbox]):not([type=radio]){min-height:2.75r
111
111
  /**
112
112
  * What a project's workbench.css carries besides the classes: the few rules
113
113
  * every Workbench page needs that no class can carry (the spinner's keyframes,
114
- * links and code inside a page, reduced motion). Each is scoped to a page built
114
+ * links and code inside a page, every text area growing with its text, reduced
115
+ * motion). Each is scoped to a page built
115
116
  * with shell (`<html class="wb-ui">`) and wrapped in :where(), so it adds no
116
117
  * weight and never touches a page of the project's own.
117
118
  */
@@ -122,6 +123,7 @@ export const BASE_CSS = `:where(.wb-ui),:where(.wb-ui) *,:where(.wb-ui) ::before
122
123
  :where(.wb-ui) a:hover{color:var(--wb-ui-accent-hover)}
123
124
  :where(.wb-ui) a:focus-visible{outline:none;box-shadow:0 0 0 3px var(--wb-ui-focus)}
124
125
  :where(.wb-ui) code{font:.85em/1.4 var(--wb-ui-mono)}
126
+ :where(.wb-ui) :where(textarea:not([data-wb-grow=off])){field-sizing:content;width:100%;max-width:100%;max-height:20rem}
125
127
  @keyframes wb-ui-spin{to{transform:rotate(360deg)}}
126
128
  @media (prefers-reduced-motion:reduce){:where(.wb-ui) *{animation:none!important;transition:none!important}}
127
129
  `;
package/dist/ui-styles.js CHANGED
@@ -243,10 +243,24 @@ export const UI_STYLES = {
243
243
  { selector: ":focus-visible", about: "With the keyboard", css: FOCUS },
244
244
  { selector: "[aria-invalid=true]", about: "A control with a problem", css: `border-color:${v("fail")}` },
245
245
  { selector: ":where(textarea)", about: "A text area grows with what is typed", css: "min-height:6rem;max-height:20rem;field-sizing:content;resize:vertical" },
246
+ { selector: ":where(textarea[data-wb-grow=off])", about: "A text area that keeps its size", css: "field-sizing:fixed" },
246
247
  { selector: ":where([type=color])", about: "A colour picker", css: "width:4rem;padding:.2rem" },
247
248
  { selector: ":where([type=number])", about: "A number", css: "width:7.5rem;font-variant-numeric:tabular-nums" },
248
249
  ],
249
250
  },
251
+ // Widths input() works out from a control's type, maxlength, min and max; any tool's own control can carry one too, so the width
252
+ // sits on [class] to outweigh that tool's single-class rule. Each is capped at the width of what holds it.
253
+ { name: "wb-ui-size-5", element: "input", about: "Width for an answer of up to 4 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(5ch + 1.4rem))" }] },
254
+ { name: "wb-ui-size-6", element: "input", about: "Width for an answer of up to 5 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(6ch + 1.4rem))" }] },
255
+ { name: "wb-ui-size-8", element: "input", about: "Width for an answer of up to 7 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(8ch + 1.4rem))" }] },
256
+ { name: "wb-ui-size-10", element: "input", about: "Width for an answer of up to 9 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(10ch + 1.4rem))" }] },
257
+ { name: "wb-ui-size-12", element: "input", about: "Width for an answer of up to 11 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(12ch + 1.4rem))" }] },
258
+ { name: "wb-ui-size-14", element: "input", about: "Width for an answer of up to 13 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(14ch + 1.4rem))" }] },
259
+ { name: "wb-ui-size-16", element: "input", about: "Width for an answer of up to 15 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(16ch + 1.4rem))" }] },
260
+ { name: "wb-ui-size-20", element: "input", about: "Width for an answer of up to 19 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(20ch + 1.4rem))" }] },
261
+ { name: "wb-ui-size-24", element: "input", about: "Width for an answer of up to 23 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(24ch + 1.4rem))" }] },
262
+ { name: "wb-ui-size-30", element: "input", about: "Width for an answer of up to 29 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(30ch + 1.4rem))" }] },
263
+ { name: "wb-ui-size-40", element: "input", about: "Width for an answer of up to 39 characters, from input() or any tool's control", css: "max-width:100%", states: [{ selector: "[class]", about: "The width, weighted to win over a tool's own control rule", css: "width:min(100%,calc(40ch + 1.4rem))" }] },
250
264
  {
251
265
  name: "wb-ui-check",
252
266
  element: "label",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mccaskie-labs/workbench-ui",
3
- "version": "0.10.0",
3
+ "version": "0.11.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",