@crossworks/share-ui 0.232.65 → 0.232.66

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crossworks/share-ui",
3
- "version": "0.232.65",
3
+ "version": "0.232.66",
4
4
  "description": "The server-rendered share surface — the /s/<token> presenters, view-payload contract, mini-app sandbox, and the few primitives they need. Lives in MANTLE (the server renders these), published for jackdaw to consume; may depend only on @mantle/{client-types,content-core} (the jackdaw-repo-split boundary).",
5
5
  "exports": {
6
6
  "./app-presenter": "./src/app-presenter.tsx",
@@ -25,6 +25,9 @@
25
25
  "./nav-items": "./src/nav-items.ts",
26
26
  "./help-topics": "./src/help-topics.ts",
27
27
  "./appearance": "./src/appearance.ts",
28
+ "./neat-background": "./src/neat-background.ts",
29
+ "./neat-mount": "./src/neat-mount.ts",
30
+ "./share-mode": "./src/share-mode.ts",
28
31
  "./backgrounds": "./src/backgrounds.ts",
29
32
  "./avatar": "./src/avatar.ts",
30
33
  "./lib/themes": "./src/lib/themes.ts",
@@ -32,8 +35,9 @@
32
35
  "./styles/app.css": "./styles/app.css"
33
36
  },
34
37
  "dependencies": {
35
- "@mantle/client-types": "npm:@crossworks/client-types@0.232.65",
36
- "@mantle/content-core": "npm:@crossworks/content-core@0.232.65",
38
+ "@firecms/neat": "1.0.2",
39
+ "@mantle/client-types": "npm:@crossworks/client-types@0.232.66",
40
+ "@mantle/content-core": "npm:@crossworks/content-core@0.232.66",
37
41
  "@radix-ui/react-label": "^2.1.12",
38
42
  "@radix-ui/react-slot": "^1.3.0",
39
43
  "class-variance-authority": "^0.7.1",
package/src/appearance.ts CHANGED
@@ -61,6 +61,18 @@ export type BrainAppearance = {
61
61
  * Optional for the published-contract reason below: readers treat absence
62
62
  * as "not set" and paint the plain themed fill. */
63
63
  neatBackground?: string | null;
64
+ /** The brain's default light/dark mode for surfaces where the visitor has
65
+ * not chosen one (the public /s share reader): 'light' | 'dark' | 'system'.
66
+ * NOT stamped onto `<html>` by resolveAppearanceAttrs — the share renderer
67
+ * applies it as the `.dark` class itself, and the client app ignores it
68
+ * (in-app mode stays the visitor's own next-themes choice). Optional for
69
+ * the published-contract reason below; absence reads as 'light', the share
70
+ * surface's historical rendering. */
71
+ defaultMode?: string | null;
72
+ /** Whether shared surfaces (/s, the team workspace) paint the saved Neat
73
+ * gradient at all — false is the printable plain fallback. NOT stamped onto
74
+ * `<html>`; readers treat absence as true (the default). */
75
+ shareNeat?: boolean | null;
64
76
  /**
65
77
  * WHO this brain is — its own name, the name of the box, and whether an
66
78
  * uploaded logo exists. Unlike everything above these are NOT stamped onto
@@ -0,0 +1,216 @@
1
+ import type { NeatConfig } from '@firecms/neat';
2
+
3
+ /**
4
+ * The Neat background — an animated WebGL gradient behind a whole surface,
5
+ * generated in Settings → Appearance and stored on the brain's preferences
6
+ * row. This module is the spec contract and the parameter derivation; it lives
7
+ * HERE (published as @crossworks/share-ui) because two renderers consume it —
8
+ * the jackdaw owner app (login screen, content area) and this repo's
9
+ * server-rendered /s share surface — and the two must never disagree about
10
+ * what a saved spec looks like.
11
+ *
12
+ * We store a SPEC, never colours: `{ v, seed, tone, speed }`. Every colour is
13
+ * derived from the LIVE theme tokens at paint time, so one saved background
14
+ * follows all ~40 colour themes and light/dark mode by construction — a
15
+ * stored hex would break silently on most themes, exactly like a hardcoded
16
+ * class would. The seed drives a deterministic PRNG for every other Neat
17
+ * parameter, so "the background I saved" is reproducible from four numbers,
18
+ * and "Generate" is just a re-roll of the seed.
19
+ *
20
+ * The derived colours are washes: each brand colour is pulled most of the way
21
+ * into `--background` before it reaches the shader. That is the legibility
22
+ * guarantee — the gradient reads as a tint of the page surface, never a
23
+ * poster fighting the text sitting on it.
24
+ */
25
+
26
+ export type NeatTone = 'auto' | 'darker' | 'lighter';
27
+
28
+ export type NeatBackgroundSpec = {
29
+ v: 1;
30
+ /** Seeds the PRNG that derives every Neat parameter. */
31
+ seed: number;
32
+ /** Push the wash below the page surface ('darker'), above it ('lighter'),
33
+ * or follow the mode — dark mode darkens, light mode lightens ('auto'). */
34
+ tone: NeatTone;
35
+ /** Animation speed, 0 (still) to {@link NEAT_SPEED_MAX}. */
36
+ speed: number;
37
+ };
38
+
39
+ export const NEAT_SPEED_MAX = 6;
40
+ export const NEAT_DEFAULT_SPEED = 2;
41
+
42
+ /** Storage cap, mirrored by the server route — the canonical encoding of a
43
+ * valid spec is ~60 chars, so this is a garbage guard, not a limit. */
44
+ export const NEAT_BACKGROUND_MAX = 200;
45
+
46
+ const TONES: readonly NeatTone[] = ['auto', 'darker', 'lighter'];
47
+
48
+ /** Canonical wire encoding — fixed key order so equal specs compare equal as
49
+ * strings (the UI's dirty check and the renderer's effect key rely on it). */
50
+ export function encodeNeatSpec(spec: NeatBackgroundSpec): string {
51
+ return JSON.stringify({ v: 1, seed: spec.seed, tone: spec.tone, speed: spec.speed });
52
+ }
53
+
54
+ /** Parse + shape-check a stored value. Garbage ⇒ null, never a throw — the
55
+ * same lenient contract every appearance preference follows on read. */
56
+ export function decodeNeatSpec(raw: unknown): NeatBackgroundSpec | null {
57
+ if (typeof raw !== 'string' || raw.length === 0 || raw.length > NEAT_BACKGROUND_MAX) return null;
58
+ let parsed: unknown;
59
+ try {
60
+ parsed = JSON.parse(raw);
61
+ } catch {
62
+ return null;
63
+ }
64
+ if (!parsed || typeof parsed !== 'object') return null;
65
+ const o = parsed as Record<string, unknown>;
66
+ if (o.v !== 1) return null;
67
+ if (typeof o.seed !== 'number' || !Number.isInteger(o.seed) || o.seed < 0 || o.seed > 0xffffffff)
68
+ return null;
69
+ if (typeof o.tone !== 'string' || !(TONES as readonly string[]).includes(o.tone)) return null;
70
+ if (typeof o.speed !== 'number' || !Number.isFinite(o.speed) || o.speed < 0) return null;
71
+ return {
72
+ v: 1,
73
+ seed: o.seed,
74
+ tone: o.tone as NeatTone,
75
+ speed: Math.min(o.speed, NEAT_SPEED_MAX),
76
+ };
77
+ }
78
+
79
+ export function randomNeatSpec(
80
+ prev?: Pick<NeatBackgroundSpec, 'tone' | 'speed'>,
81
+ ): NeatBackgroundSpec {
82
+ return {
83
+ v: 1,
84
+ seed: Math.floor(Math.random() * 0xffffffff),
85
+ tone: prev?.tone ?? 'auto',
86
+ speed: prev?.speed ?? NEAT_DEFAULT_SPEED,
87
+ };
88
+ }
89
+
90
+ /** The tokens the shader needs, resolved to literal hex — WebGL cannot read
91
+ * `var()`, same constraint as the DiceBear ramp in theme-ramp.ts. */
92
+ export type NeatThemeTokens = {
93
+ background: string;
94
+ primary: string;
95
+ accent: string;
96
+ secondary: string;
97
+ };
98
+
99
+ /** Deterministic PRNG (mulberry32) — same seed, same background, forever. */
100
+ function mulberry32(seed: number): () => number {
101
+ let a = seed >>> 0;
102
+ return () => {
103
+ a |= 0;
104
+ a = (a + 0x6d2b79f5) | 0;
105
+ let t = Math.imul(a ^ (a >>> 15), 1 | a);
106
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
107
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
108
+ };
109
+ }
110
+
111
+ function hexToRgb(hex: string): [number, number, number] | null {
112
+ const digits = /^#?([0-9a-f]{6})$/i.exec(hex.trim())?.[1];
113
+ if (!digits) return null;
114
+ const n = parseInt(digits, 16);
115
+ return [(n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff];
116
+ }
117
+
118
+ function rgbToHex([r, g, b]: [number, number, number]): string {
119
+ const c = (v: number) =>
120
+ Math.max(0, Math.min(255, Math.round(v)))
121
+ .toString(16)
122
+ .padStart(2, '0');
123
+ return `#${c(r)}${c(g)}${c(b)}`;
124
+ }
125
+
126
+ /** `amount` of `into` mixed over `from` — 0 keeps `from`, 1 lands on `into`. */
127
+ function mixHex(from: string, into: string, amount: number): string {
128
+ const a = hexToRgb(from);
129
+ const b = hexToRgb(into);
130
+ if (!a || !b) return from;
131
+ return rgbToHex([
132
+ a[0] + (b[0] - a[0]) * amount,
133
+ a[1] + (b[1] - a[1]) * amount,
134
+ a[2] + (b[2] - a[2]) * amount,
135
+ ]);
136
+ }
137
+
138
+ /** Nudge toward black (factor < 1) or white (factor > 1). */
139
+ function shadeHex(hex: string, factor: number): string {
140
+ const rgb = hexToRgb(hex);
141
+ if (!rgb) return hex;
142
+ if (factor <= 1) return rgbToHex([rgb[0] * factor, rgb[1] * factor, rgb[2] * factor]);
143
+ const up = factor - 1;
144
+ return rgbToHex([
145
+ rgb[0] + (255 - rgb[0]) * up,
146
+ rgb[1] + (255 - rgb[1]) * up,
147
+ rgb[2] + (255 - rgb[2]) * up,
148
+ ]);
149
+ }
150
+
151
+ const round1 = (n: number) => Math.round(n * 10) / 10;
152
+ const round2 = (n: number) => Math.round(n * 100) / 100;
153
+
154
+ /**
155
+ * Spec + live tokens + mode → the full Neat config (minus `ref`/`resolution`,
156
+ * which are the renderer's business). Pure and deterministic, so the settings
157
+ * preview and the real surface can never disagree about what a saved spec
158
+ * looks like.
159
+ */
160
+ export function neatConfigFromSpec(
161
+ spec: NeatBackgroundSpec,
162
+ tokens: NeatThemeTokens,
163
+ mode: 'light' | 'dark',
164
+ ): NeatConfig {
165
+ const rnd = mulberry32(spec.seed);
166
+ const range = (lo: number, hi: number) => lo + rnd() * (hi - lo);
167
+ const darker = spec.tone === 'darker' || (spec.tone === 'auto' && mode === 'dark');
168
+
169
+ // A wash: pull the brand colour part-way into the page background, then
170
+ // nudge the result off the surface in the chosen direction. Enough colour
171
+ // to read as a real gradient, close enough to the surface that content
172
+ // sitting on it never fights it.
173
+ const wash = (hex: string, towardBg: number) =>
174
+ shadeHex(mixHex(hex, tokens.background, towardBg), darker ? 0.88 : 1.08);
175
+
176
+ return {
177
+ colors: [
178
+ { color: tokens.background, enabled: true, influence: round2(range(0.3, 0.6)) },
179
+ {
180
+ color: wash(tokens.primary, round2(range(0.25, 0.5))),
181
+ enabled: true,
182
+ influence: round2(range(0.5, 0.9)),
183
+ },
184
+ {
185
+ color: wash(tokens.accent, round2(range(0.25, 0.55))),
186
+ enabled: true,
187
+ influence: round2(range(0.45, 0.85)),
188
+ },
189
+ {
190
+ color: wash(tokens.secondary, round2(range(0.35, 0.6))),
191
+ enabled: true,
192
+ influence: round2(range(0.35, 0.75)),
193
+ },
194
+ { color: tokens.background, enabled: true, influence: round2(range(0.25, 0.55)) },
195
+ ],
196
+ speed: spec.speed,
197
+ horizontalPressure: round1(range(2, 5)),
198
+ verticalPressure: round1(range(2, 5)),
199
+ waveFrequencyX: round1(range(2, 6)),
200
+ waveFrequencyY: round1(range(2, 6)),
201
+ waveAmplitude: round1(range(3, 7)),
202
+ // Keep the shader's own light play gentle: strong shadows/highlights are
203
+ // exactly what would carve text-hostile contrast into the surface.
204
+ shadows: round1(range(0, darker ? 3 : 1.5)),
205
+ highlights: round1(range(0, darker ? 1.5 : 3)),
206
+ colorBrightness: darker ? 0.95 : 1,
207
+ colorSaturation: round1(range(-1, 2.5)),
208
+ colorBlending: round1(range(5, 9)),
209
+ grainScale: 2,
210
+ grainIntensity: round2(range(0.02, 0.1)),
211
+ grainSpeed: 0.3,
212
+ wireframe: false,
213
+ backgroundColor: tokens.background,
214
+ backgroundAlpha: 1,
215
+ };
216
+ }
@@ -0,0 +1,82 @@
1
+ import {
2
+ neatConfigFromSpec,
3
+ type NeatBackgroundSpec,
4
+ type NeatThemeTokens,
5
+ } from './neat-background';
6
+
7
+ /**
8
+ * The one true way to put a Neat spec onto a canvas — shared by every
9
+ * renderer (the /s reader's vanilla runtime here in mantle; jackdaw's React
10
+ * NeatBackdrop wraps it too), so the hard-won WebGL specifics live exactly
11
+ * once:
12
+ *
13
+ * - `@firecms/neat` is dynamically imported HERE, so every consumer's
14
+ * bundler splits the ~80KB WebGL chunk out and only fetches it on
15
+ * surfaces that actually paint a gradient.
16
+ * - Colours are read off the live document per call — shaders take
17
+ * literals, not `var()` — and an unresolvable theme mounts NOTHING
18
+ * rather than a wrong guess; the themed surface underneath is the
19
+ * designed fallback.
20
+ * - Neat's `seed` is NOT a randomness seed: it is the animation clock's
21
+ * starting value (u_time, fp32 on the GPU). A raw 32-bit seed exceeds
22
+ * fp32 precision and collapses the shader's small offsets into ONE FLAT
23
+ * COLOUR. It is modded into the library's own clock range here; the full
24
+ * seed still drives the parameter PRNG.
25
+ * - `prefers-reduced-motion` freezes the animation (speed 0); the wash
26
+ * still paints.
27
+ *
28
+ * TIMING is the caller's job: mount only after the document's theme
29
+ * class/attributes have settled (in practice, from a requestAnimationFrame
30
+ * callback), or the tokens read here belong to the OUTGOING theme. So is
31
+ * CANCELLATION: this resolves after an await, so a caller that has moved on
32
+ * must destroy the returned handle instead of keeping it.
33
+ */
34
+
35
+ export type NeatMountHandle = { destroy: () => void };
36
+
37
+ /** The four tokens the shader derives every colour from, resolved to literal
38
+ * values off the live document. Null when the theme is unresolvable (a test
39
+ * DOM, a broken stylesheet) — paint nothing rather than a wrong guess. */
40
+ export function readNeatThemeTokens(): NeatThemeTokens | null {
41
+ const cs = getComputedStyle(document.documentElement);
42
+ const read = (name: string) => cs.getPropertyValue(name).trim();
43
+ const tokens = {
44
+ background: read('--background'),
45
+ primary: read('--primary'),
46
+ accent: read('--accent'),
47
+ secondary: read('--secondary'),
48
+ };
49
+ return tokens.background && tokens.primary ? tokens : null;
50
+ }
51
+
52
+ /**
53
+ * Build the gradient onto `canvas`. Resolves to a handle to destroy, or null
54
+ * when nothing mounted (unresolvable theme, WebGL unavailable) — null is the
55
+ * designed fallback, never an error.
56
+ */
57
+ export async function mountNeat(
58
+ canvas: HTMLCanvasElement,
59
+ spec: NeatBackgroundSpec,
60
+ mode: 'light' | 'dark',
61
+ opts: { resolution?: number; licenseKey?: string } = {},
62
+ ): Promise<NeatMountHandle | null> {
63
+ const { NeatGradient } = await import('@firecms/neat');
64
+ const tokens = readNeatThemeTokens();
65
+ if (!tokens) return null;
66
+
67
+ const reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
68
+ const config = neatConfigFromSpec(spec, tokens, mode);
69
+ try {
70
+ return new NeatGradient({
71
+ ...config,
72
+ ref: canvas,
73
+ seed: spec.seed % 3600,
74
+ resolution: opts.resolution ?? 1,
75
+ speed: reduced ? 0 : config.speed,
76
+ ...(opts.licenseKey ? { licenseKey: opts.licenseKey } : {}),
77
+ });
78
+ } catch {
79
+ // WebGL unavailable (headless browser, exhausted contexts).
80
+ return null;
81
+ }
82
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The /s reader's visitor mode contract — deliberately its own tiny module:
3
+ * the share-page browser runtime imports it, and routing it through
4
+ * appearance.ts would drag the avatar/font/background registries into a
5
+ * bundle that exists to stay small.
6
+ */
7
+
8
+ /** The localStorage key holding a /s VISITOR's own light/dark choice — theirs
9
+ * alone, never the owner's. Two bundles must agree on it: the server's
10
+ * pre-paint inline script (template.ts) writes the reader, the share-page
11
+ * runtime writes the value. One constant, no mirrored literal. */
12
+ export const SHARE_MODE_STORAGE_KEY = 'mantle-share-mode';