@7365admin1/layer-common 3.2.2-staging.80 → 3.2.2-staging.81

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@7365admin1/layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "3.2.2-staging.80",
5
+ "version": "3.2.2-staging.81",
6
6
  "author": "7365admin1",
7
7
  "main": "./nuxt.config.ts",
8
8
  "publishConfig": {
@@ -14,7 +14,8 @@
14
14
  "build": "nuxt build .playground",
15
15
  "generate": "nuxt generate .playground",
16
16
  "preview": "nuxt preview .playground",
17
- "test": "esbuild composables/useVisitorSocket.ts --format=esm --outfile=test/.build/useVisitorSocket.mjs --log-level=error && node --test \"test/*.test.mjs\"",
17
+ "test": "esbuild composables/useVisitorSocket.ts --format=esm --outfile=test/.build/useVisitorSocket.mjs --log-level=error && node --test \"test/*.test.mjs\" && yarn test:units",
18
+ "test:units": "node --experimental-strip-types --test \"utils/*.test.ts\"",
18
19
  "release": "yarn run build && changeset publish"
19
20
  },
20
21
  "devDependencies": {
@@ -43,8 +43,37 @@ import {
43
43
  import "vuetify-pro-tiptap/style.css";
44
44
 
45
45
  import { LIGHT_THEME, DARK_THEME } from "../utils/theme";
46
+ import { THEME_COOKIE } from "../composables/useThemePreference";
47
+ import type { CookieOptions } from "#app";
48
+
49
+ /**
50
+ * The design tokens. Loaded HERE rather than in `nuxt.config.ts` so they land
51
+ * after `vuetify/styles` in the bundle - `tokens.css` aliases `--v-theme-*`,
52
+ * which Vuetify has to have emitted first.
53
+ */
54
+ import "../assets/css/tokens.css";
55
+ import "../assets/css/shell.css";
46
56
 
47
57
  export default defineNuxtPlugin((app) => {
58
+ /**
59
+ * BOOT ON THE REMEMBERED THEME, DON'T CORRECT IT AFTERWARDS.
60
+ *
61
+ * Reading the cookie here, before `createVuetify`, is what makes the choice
62
+ * survive a reload with no flash: Vuetify's first paint is already the right
63
+ * theme, so there is no moment where the wrong one is on screen. Setting it
64
+ * from a component's `onMounted` would repaint the whole application one
65
+ * frame in, which is the flash this is written to avoid.
66
+ *
67
+ * `useThemePreference()` writes this cookie; nothing else does.
68
+ */
69
+ const stored = useCookie<string | null>(
70
+ THEME_COOKIE,
71
+ // typed `unknown` in runtimeConfig; same object `sid` and `user` use
72
+ useRuntimeConfig().public.cookieConfig as CookieOptions<string | null> & {
73
+ readonly?: false;
74
+ }
75
+ ).value;
76
+
48
77
  const vuetify = createVuetify({
49
78
  defaults: {
50
79
  VTextField: {
@@ -97,7 +126,7 @@ export default defineNuxtPlugin((app) => {
97
126
  * `utils/theme.ts`, which `utils/theme.test.ts` measures on every run.
98
127
  */
99
128
  theme: {
100
- defaultTheme: "light",
129
+ defaultTheme: stored === "dark" ? "dark" : "light",
101
130
  themes: {
102
131
  light: LIGHT_THEME,
103
132
  dark: DARK_THEME,
@@ -0,0 +1,41 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+
4
+ import { statusTone, statusToneClass } from "./status.ts";
5
+
6
+ test("the handoff's mapping, word for word", () => {
7
+ for (const s of ["Completed", "Paid", "Available", "Open"]) {
8
+ assert.equal(statusTone(s), "ok", s);
9
+ }
10
+
11
+ assert.equal(statusTone("Pending"), "warn");
12
+
13
+ for (const s of ["In Use", "Closed", "Checkout"]) {
14
+ assert.equal(statusTone(s), "err", s);
15
+ }
16
+
17
+ assert.equal(statusTone("Role"), "info");
18
+ });
19
+
20
+ /** The same status arrives spelled three ways from three endpoints. */
21
+ test("casing, spacing and underscores do not change a status's colour", () => {
22
+ for (const s of ["in use", "IN USE", "In_Use", "in-use", " In Use "]) {
23
+ assert.equal(statusTone(s), "err", s);
24
+ }
25
+ });
26
+
27
+ /**
28
+ * The important one. A status the design never named must NOT be guessed into
29
+ * a colour - a wrong green on a failed job reads as success. Neutral is
30
+ * visible, honest, and gets it noticed.
31
+ */
32
+ test("an unmapped status comes out neutral rather than guessed", () => {
33
+ for (const s of ["Cancelled", "Overdue", "Draft", "", null, undefined]) {
34
+ assert.equal(statusTone(s), "neutral", String(s));
35
+ }
36
+ });
37
+
38
+ test("the class name matches what tokens.css actually defines", () => {
39
+ assert.equal(statusToneClass("Paid"), "tone-ok");
40
+ assert.equal(statusToneClass("Whatever"), "tone-neutral");
41
+ });
@@ -0,0 +1,54 @@
1
+ /**
2
+ * THE SEMANTIC STATUS MAPPING, IN ONE PLACE.
3
+ *
4
+ * The design assigns a colour to a status WORD rather than to a module, so the
5
+ * same word has to look the same wherever it appears - "Open" on a Daily
6
+ * Occurrence Book and "Open" on a work order are the same green. Keeping the
7
+ * list here rather than in each screen is what makes that true, and is what
8
+ * Phases 3 and 4 read when they restyle the chips.
9
+ *
10
+ * Straight from the handoff:
11
+ * Completed / Paid / Available / Open -> ok
12
+ * Pending -> warn
13
+ * In Use / Closed / Checkout -> err
14
+ * Role and info tags -> info
15
+ *
16
+ * Anything not listed is `neutral` on purpose. A status this product has and
17
+ * the design did not name must NOT be guessed into a colour - a wrong green on
18
+ * a failed job is worse than a grey one - so it comes out neutral and visible,
19
+ * which is how it gets noticed and added.
20
+ */
21
+ export type TStatusTone = "ok" | "warn" | "err" | "info" | "neutral";
22
+
23
+ const TONES: Record<string, TStatusTone> = {
24
+ completed: "ok",
25
+ paid: "ok",
26
+ available: "ok",
27
+ open: "ok",
28
+
29
+ pending: "warn",
30
+
31
+ "in use": "err",
32
+ closed: "err",
33
+ checkout: "err",
34
+
35
+ role: "info",
36
+ info: "info",
37
+ };
38
+
39
+ /**
40
+ * The tone for a status word. Case- and space-insensitive, because the same
41
+ * status arrives as "In Use", "in_use" and "IN USE" from different endpoints.
42
+ */
43
+ export function statusTone(status: string | null | undefined): TStatusTone {
44
+ if (!status) return "neutral";
45
+
46
+ const key = String(status).trim().toLowerCase().replace(/[_-]+/g, " ");
47
+
48
+ return TONES[key] ?? "neutral";
49
+ }
50
+
51
+ /** The class `assets/css/tokens.css` paints that tone with. */
52
+ export function statusToneClass(status: string | null | undefined): string {
53
+ return `tone-${statusTone(status)}`;
54
+ }
@@ -1,7 +1,7 @@
1
1
  import assert from "node:assert/strict";
2
2
  import { test } from "node:test";
3
3
 
4
- import { DARK_THEME, LIGHT_THEME } from "./theme.ts";
4
+ import { DARK_THEME, LIGHT_THEME, PALETTE, type TPalette } from "./theme.ts";
5
5
 
6
6
  /**
7
7
  * The dark theme was shipped once with `primary` chosen for the one place it
@@ -199,18 +199,336 @@ test("a filled primary surface carries a readable label in both themes", () => {
199
199
  * Both themes correct the same two Vuetify defaults, and a value that drifts
200
200
  * apart between them is a bug nobody sees until they flip the switch.
201
201
  */
202
- test("disabled text clears 4.5:1 on the surface each theme puts it on", () => {
203
- assert.equal(LIGHT_THEME.variables["disabled-opacity"], 0.55);
204
- assert.equal(DARK_THEME.variables["disabled-opacity"], 0.55);
205
- assert.ok(contrast(over("#000000", WHITE, 0.55), WHITE) >= 4.5);
202
+ /**
203
+ * Both opacities are applied to `on-surface`, which is now the design's ink
204
+ * rather than pure black - so an opacity that was right for black is not right
205
+ * for this. These hold the corrected values in place, measured against the ink
206
+ * they are actually applied to.
207
+ */
208
+ test("disabled text is no paler than it was before the palette changed", () => {
209
+ assert.equal(LIGHT_THEME.variables["disabled-opacity"], 0.62);
210
+ assert.equal(DARK_THEME.variables["disabled-opacity"], 0.62);
211
+
212
+ // The bar is the previous rendering, not 4.5: Vuetify multiplies its own
213
+ // field alpha on top of this, which is Phase 3's problem to unpick.
214
+ const wasLight = contrast(over("#000000", WHITE, 0.55), WHITE);
215
+ const nowLight = contrast(over(PALETTE.light.text, WHITE, 0.62), WHITE);
206
216
  assert.ok(
207
- contrast(over(WHITE, dark.background, 0.55), dark.background) >= 4.5
217
+ nowLight >= wasLight,
218
+ `disabled light went backwards: ${wasLight.toFixed(2)} -> ${nowLight.toFixed(2)}`
208
219
  );
220
+
221
+ const nowDark = contrast(
222
+ over(PALETTE.dark.text, PALETTE.dark.card, 0.62),
223
+ PALETTE.dark.card
224
+ );
225
+ assert.ok(nowDark >= 4.5, `disabled dark: ${nowDark.toFixed(2)}`);
226
+ });
227
+
228
+ /**
229
+ * THE REGRESSION THIS CAUGHT. `text-medium-emphasis` is `on-surface` at this
230
+ * opacity, and it is roughly two hundred lines of secondary text across eleven
231
+ * applications. At Vuetify's 0.6 the new ink lands at 4.41:1 on the page.
232
+ */
233
+ test("medium-emphasis text clears AA on every surface, in both themes", () => {
234
+ for (const [name, p] of [
235
+ ["light", PALETTE.light],
236
+ ["dark", PALETTE.dark],
237
+ ] as const) {
238
+ const alpha = (name === "light" ? LIGHT_THEME : DARK_THEME).variables[
239
+ "medium-emphasis-opacity"
240
+ ];
241
+
242
+ for (const bg of [p.card, p.bg, p.sidebar]) {
243
+ const ratio = contrast(over(p.text, bg, alpha), bg);
244
+ assert.ok(
245
+ ratio >= 4.5,
246
+ `${name} medium emphasis on ${bg}: ${ratio.toFixed(2)} at alpha ${alpha}`
247
+ );
248
+ }
249
+ }
209
250
  });
210
251
 
211
252
  test("the dark divider clears the 3:1 a boundary wants", () => {
212
- const border = "#62666a"; // white at 0.35 over the dark page
253
+ // Derived, not hardcoded: this used to be a literal "#62666a" with a comment
254
+ // saying it was white at 0.35 over the dark page, and it stopped being that
255
+ // the moment the page colour moved.
256
+ const border = over(WHITE, dark.background, 0.35);
213
257
 
214
258
  assert.equal(DARK_THEME.variables["border-opacity"], 0.35);
215
259
  assert.ok(contrast(border, dark.background) >= 3);
216
260
  });
261
+
262
+ /* ==================================================================== */
263
+ /* THE REDESIGN PALETTE. */
264
+ /* */
265
+ /* Measured against the design as handed over, the light theme failed */
266
+ /* AA on 27 text pairs and the dark theme on 6. The owner's instruction */
267
+ /* was "minimal darkening, keep the look", so what these tests hold in */
268
+ /* place is BOTH halves of that: every pair a person reads clears AA, */
269
+ /* AND the values that were not the problem are still exactly the ones */
270
+ /* the design specifies. A later phase that "tidies" a colour back to */
271
+ /* its handoff value, or drifts one away from it, fails here. */
272
+ /* ==================================================================== */
273
+
274
+ const L = PALETTE.light;
275
+ const D = PALETTE.dark;
276
+
277
+ /** The soft chip backgrounds, as the design states them - literally. */
278
+ const SOFT = {
279
+ light: {
280
+ ok: ["#0f8a62", 0.1],
281
+ warn: ["#be8c19", 0.13],
282
+ err: ["#cf4b4b", 0.1],
283
+ info: ["#3b6fd4", 0.1],
284
+ },
285
+ dark: {
286
+ ok: ["#41c795", 0.12],
287
+ warn: ["#dcaf4e", 0.13],
288
+ err: ["#e57373", 0.12],
289
+ info: ["#7da6ec", 0.12],
290
+ },
291
+ } as const;
292
+
293
+ /** `--thead` is a 2% ink wash over the card, not a colour of its own. */
294
+ const thead = (p: TPalette) =>
295
+ over(p === L ? "#14161a" : WHITE, p.card, 0.02);
296
+
297
+ const soft = (p: TPalette, key: "ok" | "warn" | "err" | "info", on: string) => {
298
+ const [hex, alpha] = SOFT[p === L ? "light" : "dark"][key];
299
+ return over(hex, on, alpha);
300
+ };
301
+
302
+ const accentSoft = (p: TPalette, on: string) => over(p.accent, on, 0.12);
303
+
304
+ for (const [name, p] of [
305
+ ["light", L],
306
+ ["dark", D],
307
+ ] as const) {
308
+ /**
309
+ * `--muted` is the table header of every table in the product, the
310
+ * breadcrumb, and every KPI sub-line. It failed on all four of its surfaces
311
+ * in the light theme at 2.99-3.24:1, which is the single most-read text in
312
+ * the design.
313
+ */
314
+ test(`${name}: muted text is readable on every surface it is used on`, () => {
315
+ for (const [where, bg] of [
316
+ ["a card", p.card],
317
+ ["the page", p.bg],
318
+ ["a table header", thead(p)],
319
+ ["the sidebar", p.sidebar],
320
+ ] as const) {
321
+ assert.ok(
322
+ contrast(p.muted, bg) >= 4.5,
323
+ `${name} muted on ${where}: ${contrast(p.muted, bg).toFixed(2)}`
324
+ );
325
+ }
326
+ });
327
+
328
+ test(`${name}: primary and secondary text clear AA on every surface`, () => {
329
+ for (const fg of [p.text, p.text2]) {
330
+ for (const bg of [p.card, p.bg, p.sidebar]) {
331
+ assert.ok(
332
+ contrast(fg, bg) >= 4.5,
333
+ `${name} ${fg} on ${bg}: ${contrast(fg, bg).toFixed(2)}`
334
+ );
335
+ }
336
+ }
337
+ });
338
+
339
+ /**
340
+ * A status chip is its own soft background, so the label is NOT read on the
341
+ * card - measuring it against white flatters it by about half a point.
342
+ */
343
+ test(`${name}: every status chip's label is readable on its own chip`, () => {
344
+ for (const [key, fg] of [
345
+ ["ok", p.ok],
346
+ ["warn", p.warn],
347
+ ["err", p.err],
348
+ ["info", p.info],
349
+ ] as const) {
350
+ const bg = soft(p, key, p.card);
351
+ assert.ok(
352
+ contrast(fg, bg) >= 4.5,
353
+ `${name} ${key} chip: ${contrast(fg, bg).toFixed(2)}`
354
+ );
355
+ }
356
+ });
357
+
358
+ /** The same four also appear as bare text: status dots, chart labels, the
359
+ * red Logout row in the profile dropdown. */
360
+ test(`${name}: the status colours are readable as plain text on a card`, () => {
361
+ for (const fg of [p.ok, p.warn, p.err, p.info]) {
362
+ assert.ok(
363
+ contrast(fg, p.card) >= 4.5,
364
+ `${name} ${fg} on a card: ${contrast(fg, p.card).toFixed(2)}`
365
+ );
366
+ }
367
+ });
368
+
369
+ /**
370
+ * The active sidebar item is `--accent-text` on `--accent-soft`, and
371
+ * `--accent-soft` is transparent - so it takes the colour of whatever is
372
+ * behind it. Behind it is the SIDEBAR, which is not the card. Solving this
373
+ * against the card gives 4.50:1 and ships a 4.35:1 sidebar.
374
+ */
375
+ test(`${name}: accent text is readable on accent-soft over BOTH surfaces`, () => {
376
+ for (const [where, under] of [
377
+ ["the sidebar", p.sidebar],
378
+ ["a card", p.card],
379
+ ] as const) {
380
+ const bg = accentSoft(p, under);
381
+ assert.ok(
382
+ contrast(p.accentText, bg) >= 4.5,
383
+ `${name} accent-text on accent-soft over ${where}: ${contrast(
384
+ p.accentText,
385
+ bg
386
+ ).toFixed(2)}`
387
+ );
388
+ }
389
+
390
+ assert.ok(contrast(p.accentText, p.card) >= 4.5, "as a plain link");
391
+ });
392
+
393
+ /**
394
+ * THE REASON `--accent-strong` EXISTS. If someone deletes it and points the
395
+ * primary button back at `--accent`, the second assertion is what fails, and
396
+ * the message says why rather than just reporting a number.
397
+ */
398
+ test(`${name}: a white label sits on accent-strong, never on accent`, () => {
399
+ assert.ok(
400
+ contrast(WHITE, p.accentStrong) >= 4.5,
401
+ `white on accent-strong: ${contrast(WHITE, p.accentStrong).toFixed(2)}`
402
+ );
403
+ assert.ok(
404
+ contrast(WHITE, p.accent) < 4.5,
405
+ "white on --accent passes now? then --accent-strong is redundant - " +
406
+ "re-measure before deleting it"
407
+ );
408
+ // The fill still has to be findable against the card it sits on.
409
+ assert.ok(
410
+ contrast(p.accentStrong, p.card) >= 3,
411
+ `accent-strong against a card: ${contrast(p.accentStrong, p.card).toFixed(2)}`
412
+ );
413
+ });
414
+
415
+ /** Non-text uses of the accent only owe 3:1, and they keep the design value. */
416
+ test(`${name}: the accent clears 3:1 where it is a line rather than a word`, () => {
417
+ assert.ok(contrast(p.accent, p.card) >= 3, "chart line / tab underline");
418
+ assert.ok(contrast(WHITE, p.accent) >= 3, "the toggle knob on its on state");
419
+ });
420
+
421
+ /**
422
+ * Vuetify DERIVES the label on a filled colour and its derivation picks
423
+ * white, which on the dark palette is 2.04:1 to 2.99:1. Every one is set
424
+ * explicitly in `theme.ts`; this is what proves they were all set.
425
+ */
426
+ test(`${name}: every filled semantic colour carries a readable label`, () => {
427
+ const t = p === L ? LIGHT_THEME : DARK_THEME;
428
+
429
+ for (const key of ["primary", "success", "warning", "error", "info"]) {
430
+ const on = (t.colors as Record<string, string>)[`on-${key}`];
431
+ const fill = (t.colors as Record<string, string>)[key];
432
+
433
+ assert.ok(on, `on-${key} is not set - Vuetify would guess, and it guesses white`);
434
+ assert.ok(
435
+ contrast(on, fill) >= 4.5,
436
+ `${name} on-${key}: ${contrast(on, fill).toFixed(2)}`
437
+ );
438
+ }
439
+
440
+ assert.ok(
441
+ contrast(t.colors["on-primary-button"], t.colors["primary-button"]) >= 4.5
442
+ );
443
+ });
444
+ }
445
+
446
+ /**
447
+ * "KEEP THE LOOK" IS HALF THE INSTRUCTION, AND IT IS THE HALF A CONTRAST TEST
448
+ * CANNOT CATCH ON ITS OWN - darkening everything until it passes would satisfy
449
+ * every test above and would not be the design. These are the values that were
450
+ * NOT the problem, pinned to what the handoff says.
451
+ */
452
+ test("the design's own values are unchanged wherever they already passed", () => {
453
+ assert.equal(L.accent, "#5b8def", "the brand accent is the design's, exactly");
454
+ assert.equal(D.accent, "#5b8def");
455
+
456
+ assert.equal(L.bg, "#f6f6f2");
457
+ assert.equal(L.sidebar, "#fbfbf8");
458
+ assert.equal(L.card, "#ffffff");
459
+ assert.equal(L.border, "#e8e8e1");
460
+ assert.equal(L.text, "#191b1f");
461
+ assert.equal(L.text2, "#41454d");
462
+
463
+ // The dark theme passed AA as designed and therefore moves nowhere at all.
464
+ assert.deepEqual(
465
+ [D.bg, D.sidebar, D.card, D.border, D.text, D.text2, D.muted],
466
+ ["#131418", "#17181d", "#1c1e24", "#2a2d35", "#eef0f3", "#c6cad2", "#868c98"],
467
+ "the dark palette needed no darkening - it should still be the handoff's"
468
+ );
469
+ assert.deepEqual(
470
+ [D.ok, D.warn, D.err, D.info, D.accentText],
471
+ ["#41c795", "#dcaf4e", "#e57373", "#7da6ec", "#78a2f4"]
472
+ );
473
+ });
474
+
475
+ /**
476
+ * Minimal darkening means MINIMAL. Each light value that moved is checked to
477
+ * still be recognisably the colour it was - if a later pass "fixes" contrast
478
+ * by reaching for a different green, this is what objects.
479
+ */
480
+ test("the light values that moved are still the same colours", () => {
481
+ const moved: Array<[string, string, string]> = [
482
+ ["muted", "#8b8f98", L.muted],
483
+ ["ok", "#0f8a62", L.ok],
484
+ ["warn", "#a97a14", L.warn],
485
+ ["err", "#cf4b4b", L.err],
486
+ ["info", "#3b6fd4", L.info],
487
+ ["accent-strong vs accent", "#5b8def", L.accentStrong],
488
+ ];
489
+
490
+ for (const [name, before, after] of moved) {
491
+ const shift = contrast(before, after);
492
+ assert.ok(
493
+ shift < 1.9,
494
+ `${name} moved from ${before} to ${after}, a ${shift.toFixed(
495
+ 2
496
+ )}:1 jump - that is a different colour, not a darkened one`
497
+ );
498
+ assert.ok(shift > 1, `${name} did not move at all`);
499
+ }
500
+ });
501
+
502
+ /**
503
+ * THE BRIDGE. Every design token in `assets/css/tokens.css` is an alias onto a
504
+ * `--v-theme-*`, so a token whose Vuetify colour does not exist resolves to
505
+ * nothing and paints transparent - silently, in eleven applications. This is
506
+ * the list that CSS file reads.
507
+ */
508
+ test("every design token has a Vuetify colour behind it, in both themes", () => {
509
+ const required = [
510
+ "background", "surface", "sidebar", "border",
511
+ "text-primary", "text2", "muted",
512
+ "success", "warning", "error", "info",
513
+ "accent", "accent-strong", "accent-text",
514
+ "primary", "primary-button", "brand-surface",
515
+ ];
516
+
517
+ for (const [name, t] of [["light", LIGHT_THEME], ["dark", DARK_THEME]] as const) {
518
+ for (const key of required) {
519
+ const value = (t.colors as Record<string, string>)[key];
520
+ assert.match(
521
+ value ?? "",
522
+ /^#[0-9a-f]{6}$/i,
523
+ `${name} is missing a usable colour for "${key}" - tokens.css aliases it`
524
+ );
525
+ }
526
+ }
527
+
528
+ assert.deepEqual(
529
+ Object.keys(LIGHT_THEME.colors),
530
+ Object.keys(DARK_THEME.colors),
531
+ "a colour wired into one theme and forgotten in the other is how this " +
532
+ "file broke before"
533
+ );
534
+ });