@mccaskie-labs/workbench-ui 0.8.0 → 0.9.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,12 @@
1
1
  # @mccaskie-labs/workbench-ui
2
2
 
3
+ ## 0.9.0
4
+
5
+ ### Minor Changes
6
+
7
+ - A page part for "Are you sure?": `confirm({ endpoint, fields, ask, question, consequence?, yes, no, reason?, type?, extra?, open? })` draws a button (`ask`, with the `wb-ui-button` look) that only opens a question naming the thing, what happens next and whether it can be undone; its "Yes, …" button posts `fields` with `confirm=yes`. It is a `<details>`, so it works with no script. A page's handler checks `confirmed(body)` (or `confirmed(body, word)` where a word must be typed) and refuses, changing nothing, when it is false. `renderConfirm`, `confirmed` and `ConfirmOptions` (the shared block every Workbench tool asks with) and `ConfirmPartOptions` are exported.
8
+ - New classes `wb-ui-confirm`, `-confirm-open`, `-confirm-ask`, `-confirm-keep`, `-confirm-form`, `-confirm-question`, `-confirm-consequence`, `-confirm-field`, `-confirm-label`, `-confirm-input` and `-confirm-yes`, in `UI_STYLES` and so in a project's generated `workbench.css`. They read the existing variables (the box is `--wb-ui-background`, the yes button `--wb-ui-bad` with `--wb-ui-accent-text`) and one new one, `--wb-ui-corner` (6px). `wb-ui-button` on a `<summary>` sits inline, and `wb-ui-actions` no longer makes the question's own form inline (its rule is now ` form:not(.wb-ui-confirm-form)`).
9
+
3
10
  ## 0.8.0
4
11
 
5
12
  ### Minor Changes
package/README.md CHANGED
@@ -46,6 +46,7 @@ What goes inside a card. Each draws a `wb-ui-*` class with a default look, so a
46
46
  | `small(html)` | Small print (`wb-ui-small`) |
47
47
  | `button(text, { kind?, type?, attrs? })` | A button: `primary`, `secondary` or `link` (`wb-ui-button`) |
48
48
  | `actions(html)` | A row of buttons and their forms (`wb-ui-actions`) |
49
+ | `confirm({ endpoint, fields, ask, question, consequence?, yes, no, reason?, type?, extra?, open? })` | "Are you sure?" before something that removes, cancels or can't be undone: the `ask` button only opens the question, and its `yes` button posts `fields` with `confirm=yes` (`wb-ui-confirm`). The page's handler refuses a post where `confirmed(body)` is false, changing nothing. `reason: { label }` adds a reason box; `type: { label, word }` makes the person type a word first (check it with `confirmed(body, word)`) |
49
50
  | `input(attrs)`, `select(attrs, options, selected?)`, `textarea(attrs, text?)` | A control (`wb-ui-input`). Attributes are escaped; `class` and `on…` are refused |
50
51
  | `field(label, id, control, { hint? })` | A label over its control, then a hint with the id `<id>-hint` (`wb-ui-field`) |
51
52
  | `check(label, attrs)` | A tick box with its words (`wb-ui-check`) |
@@ -54,6 +55,23 @@ What goes inside a card. Each draws a `wb-ui-*` class with a default look, so a
54
55
  | `more(label, html)` | More detail, folded until asked for (`wb-ui-more`) |
55
56
  | `code(text)` | Exact text kept as it is (`wb-ui-code`) |
56
57
 
58
+ ```ts
59
+ import { confirm, confirmed } from '@mccaskie-labs/workbench-ui'
60
+
61
+ confirm({
62
+ endpoint: '/admin/admins',
63
+ fields: { action: 'remove_admin', email: admin.email },
64
+ ask: 'Remove',
65
+ question: `Remove ${admin.email} as an admin?`,
66
+ consequence: 'They can no longer open the admin page. You can add them again later.',
67
+ yes: 'Yes, remove this admin',
68
+ no: 'No, keep them',
69
+ })
70
+ // In the handler: if (!confirmed(body)) refuse with "Please confirm first. Nothing has changed."
71
+ ```
72
+
73
+ `renderConfirm`, `confirmed` and `ConfirmOptions` are the shared block every Workbench tool asks with; `confirm` is it with workbench-ui's own classes.
74
+
57
75
  For a form whose answers are checked, use workbench-forms' `renderForm` instead of `field` and `input`.
58
76
 
59
77
  Pages are always dark, whatever the computer is set to. A page with its own rules adds them after the shared ones and uses the colour variables (`--wb-ui-surface`, `--wb-ui-line`, `--wb-ui-pass` and so on), never its own colours.
@@ -0,0 +1,60 @@
1
+ export interface ConfirmOptions {
2
+ /** The tool's class prefix, without the trailing dash: "wb-booking". */
3
+ prefix: string;
4
+ /** Where the form posts. */
5
+ endpoint: string;
6
+ /** The hidden fields that say what to do: action, the thing's id or token. */
7
+ fields: Readonly<Record<string, string>>;
8
+ /** The first button's words: "Cancel". */
9
+ ask: string;
10
+ /** The first button's class, so it keeps the look it has in the tool's styles. */
11
+ askClass: string;
12
+ /** The question, naming the thing: "Cancel Sam's booking for Yoga on Monday 7 December at 6:00 pm?" */
13
+ question: string;
14
+ /** What happens next and who is told: "We'll email Sam to say it's cancelled." */
15
+ consequence?: string;
16
+ /** The button that does it, in its own words: "Yes, cancel this booking". */
17
+ yes: string;
18
+ /** The words that close the question again: "No, keep it". */
19
+ no: string;
20
+ /** A reason to send with it, when the action takes one. */
21
+ reason?: {
22
+ label: string;
23
+ name?: string;
24
+ maxlength?: number;
25
+ required?: boolean;
26
+ };
27
+ /** For deleting a lot at once: a word the person must type before the button works. */
28
+ type?: {
29
+ label: string;
30
+ word: string;
31
+ };
32
+ /** More fields for the question's form, as HTML the tool has already escaped (a second box, a choice). Drawn above the button. */
33
+ extra?: string;
34
+ /** Draw the question already showing: the page was drawn again to ask it. */
35
+ open?: boolean;
36
+ }
37
+ /** The button and its question. */
38
+ export declare function renderConfirm(o: ConfirmOptions): string;
39
+ /** Whether a post answered the question: confirm=yes and, where a word had to be typed, that word. */
40
+ export declare function confirmed(body: Readonly<Record<string, unknown>>, word?: string): boolean;
41
+ interface ConfirmStyle {
42
+ name: string;
43
+ element: string;
44
+ about: string;
45
+ css: string;
46
+ states?: readonly {
47
+ selector: string;
48
+ about: string;
49
+ css: string;
50
+ }[];
51
+ }
52
+ /**
53
+ * The question's classes with their default rules, for the tool's styles.
54
+ * `v` reads one of the tool's own variables (text, muted, line, sunk, surface,
55
+ * radius, danger, on-danger, focus).
56
+ */
57
+ export declare function confirmStyles(prefix: string, v: (name: string) => string, o?: {
58
+ reason?: boolean;
59
+ }): ConfirmStyle[];
60
+ export {};
@@ -0,0 +1,85 @@
1
+ // "Are you sure?" for the actions that cancel, decline or delete
2
+ // (docs/critical-actions-audit.md, "The standard"). The button a person presses first only opens
3
+ // the question: what is about to happen, named in full, and who will be told.
4
+ // The second button does it. It is a <details>, so it works with no script,
5
+ // and the post carries confirm=yes, which the action helpers insist on: a page
6
+ // that draws its own one-click button is refused with `confirm_needed`.
7
+ //
8
+ // This file is the same in every tool that asks (only the prefix differs), so
9
+ // a project restyles each tool's question by that tool's own class names.
10
+ const esc = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
11
+ const pattern = (word) => word.replace(/[.*+?^${}()|[\]\\\/-]/g, "\\$&");
12
+ /** The button and its question. */
13
+ export function renderConfirm(o) {
14
+ const p = o.prefix;
15
+ const hidden = Object.entries({ ...o.fields, confirm: "yes" })
16
+ .map(([k, v]) => `<input type="hidden" name="${esc(k)}" value="${esc(v)}">`)
17
+ .join("");
18
+ const reason = o.reason
19
+ ? `<label class="${p}-confirm-field"><span class="${p}-confirm-label">${esc(o.reason.label)}</span><input class="${p}-confirm-input" type="text" name="${esc(o.reason.name ?? "reason")}" maxlength="${o.reason.maxlength ?? 200}"${o.reason.required ? " required" : ""}></label>`
20
+ : "";
21
+ const type = o.type
22
+ ? `<label class="${p}-confirm-field"><span class="${p}-confirm-label">${esc(o.type.label)}</span><input class="${p}-confirm-input" type="text" name="confirm_word" required pattern="${esc(pattern(o.type.word))}" autocomplete="off" autocapitalize="off" spellcheck="false"></label>`
23
+ : "";
24
+ return `<details class="${p}-confirm"${o.open ? " open" : ""}><summary class="${p}-confirm-open ${esc(o.askClass)}"><span class="${p}-confirm-ask">${esc(o.ask)}</span><span class="${p}-confirm-keep">${esc(o.no)}</span></summary><form class="${p}-confirm-form" method="post" action="${esc(o.endpoint)}">${hidden}<p class="${p}-confirm-question">${esc(o.question)}</p>${o.consequence ? `<p class="${p}-confirm-consequence">${esc(o.consequence)}</p>` : ""}${reason}${o.extra ?? ""}${type}<button class="${p}-confirm-yes" type="submit">${esc(o.yes)}</button></form></details>`;
25
+ }
26
+ /** Whether a post answered the question: confirm=yes and, where a word had to be typed, that word. */
27
+ export function confirmed(body, word) {
28
+ if (body.confirm !== "yes" && body.confirm !== true)
29
+ return false;
30
+ return word === undefined || (typeof body.confirm_word === "string" && body.confirm_word.trim() === word);
31
+ }
32
+ /**
33
+ * The question's classes with their default rules, for the tool's styles.
34
+ * `v` reads one of the tool's own variables (text, muted, line, sunk, surface,
35
+ * radius, danger, on-danger, focus).
36
+ */
37
+ export function confirmStyles(prefix, v, o = {}) {
38
+ const focus = { selector: ":focus-visible", about: "Has focus", css: `outline:none;box-shadow:0 0 0 3px ${v("focus")}` };
39
+ return [
40
+ {
41
+ name: `${prefix}-confirm`,
42
+ element: "details",
43
+ about: "A button that asks \"Are you sure?\" before it cancels, declines or deletes",
44
+ css: "display:block",
45
+ states: [
46
+ { selector: `[open]`, about: "The question is showing", css: `display:flex;flex-direction:column;align-items:flex-start;gap:.75rem;box-sizing:border-box;width:100%;padding:1rem;background:${v("sunk")};border:1px solid ${v("line")};border-radius:${v("radius")}` },
47
+ { selector: `[open] > summary`, about: "\"No, keep it\" sits under the button that does it", css: "order:2" },
48
+ { selector: `[open] .${prefix}-confirm-ask`, about: "The first button's words, hidden while the question shows", css: "display:none" },
49
+ { selector: `[open] .${prefix}-confirm-keep`, about: "\"No, keep it\", shown while the question shows", css: "display:inline" },
50
+ ],
51
+ },
52
+ {
53
+ name: `${prefix}-confirm-open`,
54
+ element: "summary",
55
+ about: "The first button: it only opens the question",
56
+ css: "list-style:none",
57
+ states: [{ selector: "::-webkit-details-marker", about: "No arrow beside the button", css: "display:none" }],
58
+ },
59
+ { name: `${prefix}-confirm-ask`, element: "span", about: "The first button's words", css: "display:inline" },
60
+ { name: `${prefix}-confirm-keep`, element: "span", about: "\"No, keep it\": closes the question", css: `display:none;color:${v("text")}` },
61
+ { name: `${prefix}-confirm-form`, element: "form", about: "The question and the button that does it", css: "display:grid;gap:.75rem;justify-items:start;width:100%" },
62
+ { name: `${prefix}-confirm-question`, element: "p", about: "What is about to happen, naming the thing", css: `margin:0;font-weight:600;color:${v("text")}` },
63
+ { name: `${prefix}-confirm-consequence`, element: "p", about: "What happens next and who is told", css: `margin:0;color:${v("muted")}` },
64
+ ...(o.reason
65
+ ? [
66
+ { name: `${prefix}-confirm-field`, element: "label", about: "The reason, or the word to type", css: "display:grid;gap:.25rem;width:100%" },
67
+ { name: `${prefix}-confirm-label`, element: "span", about: "What to type there", css: `color:${v("muted")}` },
68
+ {
69
+ name: `${prefix}-confirm-input`,
70
+ element: "input",
71
+ about: "Where it is typed",
72
+ css: `box-sizing:border-box;width:100%;min-height:2.75rem;padding:.5rem .75rem;font:inherit;color:${v("text")};background:${v("surface")};border:1px solid ${v("line")};border-radius:${v("radius")}`,
73
+ states: [focus],
74
+ },
75
+ ]
76
+ : []),
77
+ {
78
+ name: `${prefix}-confirm-yes`,
79
+ element: "button",
80
+ about: "The button that does it",
81
+ css: `display:inline-flex;align-items:center;justify-content:center;min-height:2.75rem;padding:0 1rem;font:inherit;font-weight:600;border-radius:${v("radius")};cursor:pointer;color:${v("on-danger")};background:${v("danger")};border:1px solid ${v("danger")}`,
82
+ states: [focus],
83
+ },
84
+ ];
85
+ }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { type ConfirmOptions } from "./confirm.ts";
2
+ export { confirmed, renderConfirm, type ConfirmOptions } from "./confirm.ts";
1
3
  export { BASE_CSS, CSS, STYLE, UI_CSS } from "./style.ts";
2
4
  export { UI_STYLES } from "./ui-styles.ts";
3
5
  export { FORM_SCRIPT } from "./form-script.ts";
@@ -94,6 +96,16 @@ export interface ButtonOptions {
94
96
  export declare function button(text: string, opts?: ButtonOptions): string;
95
97
  /** A row of buttons, and of the small forms around them. `inner` is HTML. */
96
98
  export declare function actions(inner: string): string;
99
+ /** What `confirm` takes: renderConfirm's options without the prefix and the first button's class, which are workbench-ui's own. */
100
+ export type ConfirmPartOptions = Omit<ConfirmOptions, "prefix" | "askClass">;
101
+ /**
102
+ * "Are you sure?" before something that removes, cancels or can't be undone.
103
+ * The first button (`ask`, drawn like `button`) only opens the question; the
104
+ * second (`yes`) posts `fields` with confirm=yes to `endpoint`, so the page's
105
+ * handler checks `confirmed(body)` before it acts. A `<details>`, so it works
106
+ * with no script. Every text option is plain text; `extra` is HTML.
107
+ */
108
+ export declare function confirm(opts: ConfirmPartOptions): string;
97
109
  /** A one-line control. `a` holds its attributes (id, name, type, value, required and so on). */
98
110
  export declare function input(a: Attrs): string;
99
111
  /** A text area holding `text`, which is plain text. */
package/dist/index.js CHANGED
@@ -2,7 +2,9 @@
2
2
  // (Control, the Gateway, the probe console and each project's starter page).
3
3
  // Everything here returns HTML text; every value given as plain text is
4
4
  // escaped here, and arguments documented as HTML are trusted as they are.
5
+ import { renderConfirm } from "./confirm.js";
5
6
  import { STYLE } from "./style.js";
7
+ export { confirmed, renderConfirm } from "./confirm.js";
6
8
  export { BASE_CSS, CSS, STYLE, UI_CSS } from "./style.js";
7
9
  export { UI_STYLES } from "./ui-styles.js";
8
10
  export { FORM_SCRIPT } from "./form-script.js";
@@ -115,6 +117,16 @@ export function button(text, opts = {}) {
115
117
  export function actions(inner) {
116
118
  return `<div class="wb-ui-actions">${inner}</div>`;
117
119
  }
120
+ /**
121
+ * "Are you sure?" before something that removes, cancels or can't be undone.
122
+ * The first button (`ask`, drawn like `button`) only opens the question; the
123
+ * second (`yes`) posts `fields` with confirm=yes to `endpoint`, so the page's
124
+ * handler checks `confirmed(body)` before it acts. A `<details>`, so it works
125
+ * with no script. Every text option is plain text; `extra` is HTML.
126
+ */
127
+ export function confirm(opts) {
128
+ return renderConfirm({ ...opts, prefix: "wb-ui", askClass: "wb-ui-button" });
129
+ }
118
130
  /** A one-line control. `a` holds its attributes (id, name, type, value, required and so on). */
119
131
  export function input(a) {
120
132
  return `<input class="wb-ui-input"${attrs(a)}>`;
package/dist/ui-styles.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // The look of what workbench-ui's helpers draw: shell, topBar, card, pill,
2
2
  // chip and jumpNav, and the page parts (body, note, message, small, button,
3
- // actions, field, input, check, table, facts, more and code), so a page needs
3
+ // actions, confirm, field, input, check, table, facts, more and code), so a page needs
4
4
  // no markup of its own to look finished (docs/capabilities-manifest.md §10). Every element they put
5
5
  // on a page carries one of these classes with a default rule, and the colours
6
6
  // and sizes are `--wb-ui-*` variables, so a project restyles a Workbench page
@@ -8,6 +8,7 @@
8
8
  // a breaking change. The older page conventions for markup a page writes
9
9
  // itself (.note, .body, bare tables and buttons) stay in CSS (style.ts) for
10
10
  // Workbench's own pages; a project's generated workbench.css leaves them out.
11
+ import { confirmStyles } from "./confirm.js";
11
12
  const v = (name) => `var(--wb-ui-${name})`;
12
13
  const TONES = ["pass", "refused", "fail", "skip", "running"];
13
14
  const RUN = (t) => (t === "running" ? "run" : t);
@@ -17,6 +18,8 @@ const SAYS = [
17
18
  ["warn", "warn"],
18
19
  ["fail", "fail"],
19
20
  ];
21
+ /** The variables the shared "Are you sure?" rules read, as workbench-ui's own. */
22
+ const CONFIRM_VARS = { radius: "corner", sunk: "background", danger: "bad", "on-danger": "accent-text" };
20
23
  const FOCUS = `outline:none;box-shadow:0 0 0 3px ${v("focus")}`;
21
24
  /** The style hooks of a Workbench page. */
22
25
  export const UI_STYLES = {
@@ -55,6 +58,7 @@ export const UI_STYLES = {
55
58
  { name: "--wb-ui-mono", value: "var(--wb-font-mono, ui-monospace, monospace)", about: "Code's font" },
56
59
  { name: "--wb-ui-width", value: "max(64rem,80vw)", about: "Widest the page's column grows" },
57
60
  { name: "--wb-ui-input-width", value: "24rem", about: "Widest a one-line control grows" },
61
+ { name: "--wb-ui-corner", value: "6px", about: "Corners of the \"Are you sure?\" box, its typed box and its button" },
58
62
  ],
59
63
  classes: [
60
64
  { name: "wb-ui", element: "html", about: "The whole page, in the project's look (Workbench's own pages are always dark)", css: `background:${v("background")};color:${v("text")};font:15px/1.55 ${v("font")}` },
@@ -216,6 +220,7 @@ export const UI_STYLES = {
216
220
  { selector: "[data-kind=link]:hover", about: "A link button under the pointer", css: `background:none;color:${v("accent-hover")}` },
217
221
  { selector: ":disabled", about: "A button that can't be pressed yet", css: "opacity:.45;cursor:not-allowed" },
218
222
  { selector: ":focus-visible", about: "With the keyboard", css: FOCUS },
223
+ { selector: ":where(summary)", about: "The button that opens \"Are you sure?\", from confirm()", css: "display:inline-flex;align-items:center;width:max-content;max-width:100%" },
219
224
  ],
220
225
  },
221
226
  {
@@ -223,7 +228,7 @@ export const UI_STYLES = {
223
228
  element: "div",
224
229
  about: "A row of buttons and the forms around them, from actions()",
225
230
  css: "display:flex;flex-wrap:wrap;align-items:center;gap:.6rem;margin:1rem 0",
226
- states: [{ selector: " form", about: "A form in the row sits inline", css: "display:inline;margin:0" }],
231
+ states: [{ selector: " form:not(.wb-ui-confirm-form)", about: "A form in the row sits inline (not the form inside \"Are you sure?\")", css: "display:inline;margin:0" }],
227
232
  },
228
233
  { name: "wb-ui-option", element: "option", about: "One choice in a drop-down list", css: `background:${v("surface")};color:${v("text")}` },
229
234
  { name: "wb-ui-field", element: "p", about: "One labelled control, from field(): the label over the control, then any hint", css: "display:flex;flex-direction:column;align-items:flex-start;gap:.3rem;margin:0 0 1rem" },
@@ -293,5 +298,6 @@ export const UI_STYLES = {
293
298
  about: "Exact text kept as it is, such as a reply or a log line, from code()",
294
299
  css: `font:.85rem/1.5 ${v("mono")};white-space:pre-wrap;overflow-wrap:anywhere;padding:.75rem 1rem;margin:.75rem 0;background:${v("surface")};border:1px solid ${v("line")};border-radius:8px;max-height:28rem;overflow:auto`,
295
300
  },
301
+ ...confirmStyles("wb-ui", (n) => v(CONFIRM_VARS[n] ?? n), { reason: true }),
296
302
  ],
297
303
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mccaskie-labs/workbench-ui",
3
- "version": "0.8.0",
3
+ "version": "0.9.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",