@poolse/theme 0.0.0-stage → 3.4.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/dist/index.js ADDED
@@ -0,0 +1,723 @@
1
+ // src/themes.ts
2
+ var warmLight = {
3
+ background: "#fbf7f4",
4
+ surface: "#ffffff",
5
+ surfaceAlt: "#f4eee9",
6
+ text: "#1c1a19",
7
+ textMuted: "#6b6560",
8
+ textSubtle: "#9a938c",
9
+ border: "#eae3dc"
10
+ };
11
+ var warmDark = {
12
+ background: "#0e0d10",
13
+ surface: "#16141a",
14
+ surfaceAlt: "#201d25",
15
+ text: "#f6f2ee",
16
+ textMuted: "#a8a29b",
17
+ textSubtle: "#766f68",
18
+ border: "#2a2630"
19
+ };
20
+ var coolLight = {
21
+ background: "#f6f8fb",
22
+ surface: "#ffffff",
23
+ surfaceAlt: "#eef2f7",
24
+ text: "#111827",
25
+ textMuted: "#4b5563",
26
+ textSubtle: "#8a94a3",
27
+ border: "#e2e8f0"
28
+ };
29
+ var coolDark = {
30
+ background: "#0b0f17",
31
+ surface: "#121826",
32
+ surfaceAlt: "#1b2232",
33
+ text: "#e8edf5",
34
+ textMuted: "#a0aabb",
35
+ textSubtle: "#6b7588",
36
+ border: "#263044"
37
+ };
38
+ var greenLight = {
39
+ background: "#f6f8f5",
40
+ surface: "#ffffff",
41
+ surfaceAlt: "#edf2ec",
42
+ text: "#142016",
43
+ textMuted: "#4d5b4f",
44
+ textSubtle: "#8b978c",
45
+ border: "#dfe7dd"
46
+ };
47
+ var greenDark = {
48
+ background: "#0c110d",
49
+ surface: "#131a14",
50
+ surfaceAlt: "#1b241c",
51
+ text: "#e9f1ea",
52
+ textMuted: "#a2b0a4",
53
+ textSubtle: "#6e7c70",
54
+ border: "#263228"
55
+ };
56
+ var greyLight = {
57
+ background: "#f7f7f7",
58
+ surface: "#ffffff",
59
+ surfaceAlt: "#efefef",
60
+ text: "#111111",
61
+ textMuted: "#555555",
62
+ textSubtle: "#8e8e8e",
63
+ border: "#e2e2e2"
64
+ };
65
+ var greyDark = {
66
+ background: "#0d0d0d",
67
+ surface: "#161616",
68
+ surfaceAlt: "#202020",
69
+ text: "#f2f2f2",
70
+ textMuted: "#a6a6a6",
71
+ textSubtle: "#707070",
72
+ border: "#2c2c2c"
73
+ };
74
+ var semantic = {
75
+ light: {
76
+ danger: "#e5484d",
77
+ warning: "#e08c16",
78
+ success: "#16a34a",
79
+ info: "#3b82f6",
80
+ presence: "#16c172",
81
+ overlay: "#110f0e8c"
82
+ },
83
+ dark: {
84
+ danger: "#f87171",
85
+ warning: "#fbbf24",
86
+ success: "#34d399",
87
+ info: "#60a5fa",
88
+ presence: "#22d37a",
89
+ overlay: "#000000b3"
90
+ }
91
+ };
92
+ var fonts = {
93
+ body: "'Hanken Grotesk', system-ui, -apple-system, sans-serif",
94
+ display: "'Bricolage Grotesque', Georgia, serif",
95
+ mono: "'JetBrains Mono', ui-monospace, SFMono-Regular, monospace",
96
+ sizeXs: 11,
97
+ sizeSm: 13,
98
+ sizeMd: 15,
99
+ sizeLg: 17,
100
+ sizeXl: 20,
101
+ lineHeight: 1.5
102
+ };
103
+ var roundRadii = { sm: 8, md: 12, lg: 16, xl: 24, pill: 999, bubble: 18 };
104
+ var squareRadii = { sm: 4, md: 6, lg: 8, xl: 12, pill: 999, bubble: 8 };
105
+ var spacing = { xs: 4, sm: 8, md: 12, lg: 16, xl: 24 };
106
+ function shadows(color, dark) {
107
+ const a = dark ? 2.5 : 1;
108
+ return {
109
+ sm: {
110
+ layers: [{ x: 0, y: 1, blur: 2, spread: 0, color, opacity: Math.min(1, 0.08 * a) }],
111
+ elevation: 1
112
+ },
113
+ md: {
114
+ layers: [
115
+ { x: 0, y: 1, blur: 2, spread: 0, color, opacity: Math.min(1, 0.06 * a) },
116
+ { x: 0, y: 10, blur: 30, spread: -12, color, opacity: Math.min(1, 0.18 * a) }
117
+ ],
118
+ elevation: 6
119
+ },
120
+ lg: {
121
+ layers: [
122
+ { x: 0, y: 1, blur: 2, spread: 0, color, opacity: Math.min(1, 0.06 * a) },
123
+ { x: 0, y: 24, blur: 60, spread: -18, color, opacity: Math.min(1, 0.28 * a) }
124
+ ],
125
+ elevation: 12
126
+ }
127
+ };
128
+ }
129
+ function tokens(scheme, n, a, radii, shadowColor) {
130
+ const colors = {
131
+ ...n,
132
+ ...a,
133
+ link: a.accentStrong,
134
+ sidebar: scheme === "light" ? n.surfaceAlt : n.surface,
135
+ sidebarText: n.text,
136
+ sidebarActive: a.accentSoft,
137
+ sidebarActiveText: scheme === "light" ? a.accentStrong : n.text,
138
+ badge: a.accent,
139
+ badgeText: a.accentText,
140
+ mention: a.accentSoft,
141
+ mentionText: a.accentStrong,
142
+ ...semantic[scheme],
143
+ ownBubble: a.accent,
144
+ ownBubbleText: a.accentText,
145
+ otherBubble: n.surface,
146
+ otherBubbleText: n.text
147
+ };
148
+ return {
149
+ colors,
150
+ fonts,
151
+ radii,
152
+ spacing,
153
+ shadows: shadows(scheme === "dark" ? "#000000" : shadowColor, scheme === "dark")
154
+ };
155
+ }
156
+ function theme(id, name, neutrals, accents, radii = roundRadii) {
157
+ return {
158
+ id,
159
+ name,
160
+ extends: id,
161
+ light: tokens("light", neutrals[0], accents[0], radii, neutrals[0].text),
162
+ dark: tokens("dark", neutrals[1], accents[1], radii, neutrals[1].text)
163
+ };
164
+ }
165
+ var WHITE = "#ffffff";
166
+ var BUILT_IN_THEMES = {
167
+ coral: theme(
168
+ "coral",
169
+ "Coral",
170
+ [warmLight, warmDark],
171
+ [
172
+ { accent: "#d9381a", accentText: WHITE, accentSoft: "#ffe9e2", accentStrong: "#b52d10" },
173
+ { accent: "#d9381a", accentText: WHITE, accentSoft: "#2c1712", accentStrong: "#ff8a70" }
174
+ ]
175
+ ),
176
+ ocean: theme(
177
+ "ocean",
178
+ "Ocean",
179
+ [coolLight, coolDark],
180
+ [
181
+ { accent: "#0b63c4", accentText: WHITE, accentSoft: "#e3effc", accentStrong: "#0a56a8" },
182
+ { accent: "#1d6fd6", accentText: WHITE, accentSoft: "#10233d", accentStrong: "#7cb8ff" }
183
+ ]
184
+ ),
185
+ forest: theme(
186
+ "forest",
187
+ "Forest",
188
+ [greenLight, greenDark],
189
+ [
190
+ { accent: "#1f7a3f", accentText: WHITE, accentSoft: "#e2f2e6", accentStrong: "#1a6a36" },
191
+ { accent: "#1f7a3f", accentText: WHITE, accentSoft: "#12291a", accentStrong: "#6fd08f" }
192
+ ]
193
+ ),
194
+ grape: theme(
195
+ "grape",
196
+ "Grape",
197
+ [coolLight, coolDark],
198
+ [
199
+ { accent: "#7c3aed", accentText: WHITE, accentSoft: "#efe7fd", accentStrong: "#6d28d9" },
200
+ { accent: "#7c3aed", accentText: WHITE, accentSoft: "#241640", accentStrong: "#b99bff" }
201
+ ]
202
+ ),
203
+ slate: theme(
204
+ "slate",
205
+ "Slate",
206
+ [coolLight, coolDark],
207
+ [
208
+ { accent: "#475569", accentText: WHITE, accentSoft: "#e7ebf0", accentStrong: "#334155" },
209
+ { accent: "#4b5d78", accentText: WHITE, accentSoft: "#1c2533", accentStrong: "#a9b8cf" }
210
+ ],
211
+ squareRadii
212
+ ),
213
+ sunset: theme(
214
+ "sunset",
215
+ "Sunset",
216
+ [warmLight, warmDark],
217
+ [
218
+ { accent: "#c2410c", accentText: WHITE, accentSoft: "#ffedd5", accentStrong: "#9a3412" },
219
+ { accent: "#c2410c", accentText: WHITE, accentSoft: "#2e1608", accentStrong: "#ff9f5a" }
220
+ ]
221
+ ),
222
+ rose: theme(
223
+ "rose",
224
+ "Rose",
225
+ [warmLight, warmDark],
226
+ [
227
+ { accent: "#c81e5b", accentText: WHITE, accentSoft: "#fde6ee", accentStrong: "#a3164a" },
228
+ { accent: "#c81e5b", accentText: WHITE, accentSoft: "#2e0f1b", accentStrong: "#ff86b0" }
229
+ ]
230
+ ),
231
+ mono: theme(
232
+ "mono",
233
+ "Mono",
234
+ [greyLight, greyDark],
235
+ [
236
+ { accent: "#1a1a1a", accentText: WHITE, accentSoft: "#ebebeb", accentStrong: "#1a1a1a" },
237
+ { accent: "#f2f2f2", accentText: "#111111", accentSoft: "#2a2a2a", accentStrong: "#f2f2f2" }
238
+ ],
239
+ squareRadii
240
+ )
241
+ };
242
+ var BUILT_IN_THEME_IDS = [
243
+ "coral",
244
+ "ocean",
245
+ "forest",
246
+ "grape",
247
+ "slate",
248
+ "sunset",
249
+ "rose",
250
+ "mono"
251
+ ];
252
+ var DEFAULT_THEME_ID = "coral";
253
+ function isBuiltInThemeId(value) {
254
+ return typeof value === "string" && Object.prototype.hasOwnProperty.call(BUILT_IN_THEMES, value);
255
+ }
256
+
257
+ // src/resolve.ts
258
+ function getTheme(input) {
259
+ if (input && typeof input === "object") return input;
260
+ if (isBuiltInThemeId(input)) return BUILT_IN_THEMES[input];
261
+ return BUILT_IN_THEMES[DEFAULT_THEME_ID];
262
+ }
263
+ function resolveTheme(input, scheme) {
264
+ const theme2 = getTheme(input);
265
+ return { id: theme2.id, name: theme2.name, scheme, ...theme2[scheme] };
266
+ }
267
+ function mergeTokens(base, overrides) {
268
+ if (!overrides) return base;
269
+ return {
270
+ colors: { ...base.colors, ...overrides.colors },
271
+ fonts: { ...base.fonts, ...overrides.fonts },
272
+ radii: { ...base.radii, ...overrides.radii },
273
+ spacing: { ...base.spacing, ...overrides.spacing },
274
+ shadows: { ...base.shadows, ...overrides.shadows }
275
+ };
276
+ }
277
+ function createTheme(options) {
278
+ const baseId = options.extends ?? DEFAULT_THEME_ID;
279
+ const base = BUILT_IN_THEMES[baseId];
280
+ return {
281
+ id: options.id,
282
+ name: options.name,
283
+ extends: baseId,
284
+ light: mergeTokens(mergeTokens(base.light, options.tokens), options.light),
285
+ dark: mergeTokens(mergeTokens(base.dark, options.tokens), options.dark)
286
+ };
287
+ }
288
+
289
+ // src/contrast.ts
290
+ function parseHex(hex) {
291
+ const m = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(hex.trim());
292
+ if (!m) return null;
293
+ let h = m[1];
294
+ if (h.length === 3) h = h.replace(/./g, (c) => c + c);
295
+ return {
296
+ r: parseInt(h.slice(0, 2), 16),
297
+ g: parseInt(h.slice(2, 4), 16),
298
+ b: parseInt(h.slice(4, 6), 16),
299
+ a: h.length === 8 ? parseInt(h.slice(6, 8), 16) / 255 : 1
300
+ };
301
+ }
302
+ function relativeLuminance(hex) {
303
+ const c = parseHex(hex);
304
+ if (!c) throw new Error(`not a hex colour: ${hex}`);
305
+ const lin = (v) => {
306
+ const s = v / 255;
307
+ return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
308
+ };
309
+ return 0.2126 * lin(c.r) + 0.7152 * lin(c.g) + 0.0722 * lin(c.b);
310
+ }
311
+ function contrastRatio(a, b) {
312
+ const la = relativeLuminance(a);
313
+ const lb = relativeLuminance(b);
314
+ return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
315
+ }
316
+ var CONTRAST_PAIRS = [
317
+ ["text", "background"],
318
+ ["text", "surface"],
319
+ ["textMuted", "surface"],
320
+ ["accentText", "accent"],
321
+ ["accentStrong", "background"],
322
+ ["link", "otherBubble"],
323
+ ["ownBubbleText", "ownBubble"],
324
+ ["otherBubbleText", "otherBubble"],
325
+ ["sidebarText", "sidebar"],
326
+ ["sidebarActiveText", "sidebarActive"],
327
+ ["mentionText", "mention"],
328
+ ["badgeText", "badge"]
329
+ ];
330
+ function contrastIssues(theme2, min = 4.5) {
331
+ const issues = [];
332
+ for (const scheme of ["light", "dark"]) {
333
+ const colors = theme2[scheme].colors;
334
+ for (const [fg, bg] of CONTRAST_PAIRS) {
335
+ const ratio = contrastRatio(colors[fg], colors[bg]);
336
+ if (ratio < min) issues.push({ scheme, foreground: fg, background: bg, ratio });
337
+ }
338
+ }
339
+ return issues;
340
+ }
341
+
342
+ // src/css.ts
343
+ var kebab = (s) => s.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
344
+ function rgba(hex, opacity) {
345
+ const c = parseHex(hex);
346
+ if (!c) return hex;
347
+ const a = Math.round(c.a * opacity * 1e3) / 1e3;
348
+ return `rgba(${c.r}, ${c.g}, ${c.b}, ${a})`;
349
+ }
350
+ function cssShadow(token) {
351
+ return token.layers.map((l) => `${l.x}px ${l.y}px ${l.blur}px ${l.spread}px ${rgba(l.color, l.opacity)}`).join(", ");
352
+ }
353
+ function tokensToCssVariables(t, options = {}) {
354
+ const vars = {};
355
+ for (const [k, v] of Object.entries(t.colors)) vars[`--poolse-${kebab(k)}`] = v;
356
+ vars["--poolse-font-body"] = t.fonts.body;
357
+ vars["--poolse-font-display"] = t.fonts.display;
358
+ vars["--poolse-font-mono"] = t.fonts.mono;
359
+ for (const size of ["Xs", "Sm", "Md", "Lg", "Xl"]) {
360
+ vars[`--poolse-font-size-${size.toLowerCase()}`] = `${t.fonts[`size${size}`]}px`;
361
+ }
362
+ vars["--poolse-line-height"] = String(t.fonts.lineHeight);
363
+ for (const [k, v] of Object.entries(t.radii)) vars[`--poolse-radius-${k}`] = `${v}px`;
364
+ for (const [k, v] of Object.entries(t.spacing)) vars[`--poolse-space-${k}`] = `${v}px`;
365
+ for (const [k, v] of Object.entries(t.shadows)) vars[`--poolse-shadow-${k}`] = cssShadow(v);
366
+ if (options.legacy !== false) {
367
+ const c = t.colors;
368
+ Object.assign(vars, {
369
+ "--poolse-brand": c.accent,
370
+ "--poolse-brand-strong": c.accentStrong,
371
+ "--poolse-brand-soft": c.accentSoft,
372
+ "--poolse-on-brand": c.accentText,
373
+ "--poolse-paper": c.background,
374
+ "--poolse-surface-2": c.surfaceAlt,
375
+ "--poolse-ink": c.text,
376
+ "--poolse-ink-2": c.textMuted,
377
+ "--poolse-ink-3": c.textSubtle,
378
+ "--poolse-error": c.danger,
379
+ "--poolse-color-bg": c.background,
380
+ "--poolse-color-surface": c.surface,
381
+ "--poolse-color-border": c.border,
382
+ "--poolse-color-text": c.text,
383
+ "--poolse-color-muted": c.textMuted,
384
+ "--poolse-color-primary": c.accent,
385
+ "--poolse-color-primary-text": c.accentText,
386
+ "--poolse-color-self-bubble": c.ownBubble,
387
+ "--poolse-color-self-bubble-text": c.ownBubbleText,
388
+ "--poolse-color-other-bubble": c.otherBubble,
389
+ "--poolse-color-other-bubble-text": c.otherBubbleText,
390
+ "--poolse-font-family": t.fonts.body,
391
+ "--poolse-font-size": `${t.fonts.sizeMd}px`,
392
+ "--poolse-radius": `${t.radii.lg}px`,
393
+ "--poolse-spacing-xs": `${t.spacing.xs}px`,
394
+ "--poolse-spacing-sm": `${t.spacing.sm}px`,
395
+ "--poolse-spacing-md": `${t.spacing.md}px`,
396
+ "--poolse-spacing-lg": `${t.spacing.lg}px`
397
+ });
398
+ }
399
+ return vars;
400
+ }
401
+ function toCssVariables(theme2, scheme, options) {
402
+ return tokensToCssVariables(resolveTheme(theme2, scheme), options);
403
+ }
404
+ function toCssText(theme2, scheme, selector = ":root", options) {
405
+ const body = Object.entries(toCssVariables(theme2, scheme, options)).map(([k, v]) => ` ${k}: ${v};`).join("\n");
406
+ return `${selector} {
407
+ ${body}
408
+ }`;
409
+ }
410
+
411
+ // src/native.ts
412
+ function nativeShadow(token) {
413
+ const layer = token.layers[token.layers.length - 1];
414
+ if (!layer) {
415
+ return {
416
+ shadowColor: "#000000",
417
+ shadowOpacity: 0,
418
+ shadowOffset: { width: 0, height: 0 },
419
+ shadowRadius: 0,
420
+ elevation: 0
421
+ };
422
+ }
423
+ return {
424
+ shadowColor: layer.color,
425
+ shadowOpacity: layer.opacity,
426
+ shadowOffset: { width: layer.x, height: layer.y },
427
+ // RN's radius is roughly half the CSS blur.
428
+ shadowRadius: layer.blur / 2,
429
+ elevation: token.elevation
430
+ };
431
+ }
432
+ function tokensToNative(t, identity, options = {}) {
433
+ return {
434
+ ...identity,
435
+ colors: { ...t.colors },
436
+ fonts: {
437
+ body: options.fonts?.body ?? "System",
438
+ display: options.fonts?.display ?? "System",
439
+ mono: options.fonts?.mono ?? "Menlo",
440
+ sizeXs: t.fonts.sizeXs,
441
+ sizeSm: t.fonts.sizeSm,
442
+ sizeMd: t.fonts.sizeMd,
443
+ sizeLg: t.fonts.sizeLg,
444
+ sizeXl: t.fonts.sizeXl,
445
+ lineHeight: Math.round(t.fonts.sizeMd * t.fonts.lineHeight)
446
+ },
447
+ radii: { ...t.radii },
448
+ spacing: { ...t.spacing },
449
+ shadows: {
450
+ sm: nativeShadow(t.shadows.sm),
451
+ md: nativeShadow(t.shadows.md),
452
+ lg: nativeShadow(t.shadows.lg)
453
+ }
454
+ };
455
+ }
456
+ function toNativeTheme(theme2, scheme, options) {
457
+ const r = resolveTheme(theme2, scheme);
458
+ return tokensToNative(r, { id: r.id, name: r.name, scheme }, options);
459
+ }
460
+
461
+ // src/code.ts
462
+ var VERSION = 1;
463
+ var MAX_CODE_LENGTH = 8192;
464
+ var ThemeCodeError = class extends Error {
465
+ constructor(message) {
466
+ super(message);
467
+ this.name = "ThemeCodeError";
468
+ }
469
+ };
470
+ var GROUPS = [
471
+ ["c", "colors"],
472
+ ["f", "fonts"],
473
+ ["r", "radii"],
474
+ ["s", "spacing"],
475
+ ["h", "shadows"]
476
+ ];
477
+ var ID_RE = /^[a-z0-9][a-z0-9-]{0,39}$/;
478
+ var FONT_RE = /^[\p{L}\p{N} ,'_-]{1,200}$/u;
479
+ function normalizeHex(hex, where) {
480
+ const c = parseHex(hex);
481
+ if (!c) throw new ThemeCodeError(`${where}: not a hex colour: ${hex}`);
482
+ const h2 = (v) => v.toString(16).padStart(2, "0");
483
+ const alpha = c.a < 1 ? h2(Math.round(c.a * 255)) : "";
484
+ return `${h2(c.r)}${h2(c.g)}${h2(c.b)}${alpha}`;
485
+ }
486
+ function diffTokens(t, base, scheme) {
487
+ const out = {};
488
+ for (const [short, group] of GROUPS) {
489
+ const changed = {};
490
+ const values = t[group];
491
+ const baseValues = base[group];
492
+ for (const key of Object.keys(baseValues)) {
493
+ const v = values[key];
494
+ if (group === "colors") {
495
+ const hex = normalizeHex(String(v), `${scheme}.colors.${key}`);
496
+ if (hex !== normalizeHex(String(baseValues[key]), "base")) changed[key] = hex;
497
+ } else if (JSON.stringify(v) !== JSON.stringify(baseValues[key])) {
498
+ changed[key] = v;
499
+ }
500
+ }
501
+ if (Object.keys(changed).length > 0) out[short] = changed;
502
+ }
503
+ return Object.keys(out).length > 0 ? out : void 0;
504
+ }
505
+ function splitShared(l, d) {
506
+ const shared = {};
507
+ const ld = l;
508
+ const dd = d;
509
+ for (const [short] of GROUPS) {
510
+ const lg = ld[short];
511
+ const dg = dd[short];
512
+ if (!lg || !dg) continue;
513
+ for (const key of Object.keys(lg)) {
514
+ if (!(key in dg) || JSON.stringify(lg[key]) !== JSON.stringify(dg[key])) continue;
515
+ (shared[short] ??= {})[key] = lg[key];
516
+ delete lg[key];
517
+ delete dg[key];
518
+ }
519
+ if (Object.keys(lg).length === 0) delete ld[short];
520
+ if (Object.keys(dg).length === 0) delete dd[short];
521
+ }
522
+ return Object.keys(shared).length > 0 ? shared : void 0;
523
+ }
524
+ function encodeTheme(theme2) {
525
+ if (!ID_RE.test(theme2.id)) throw new ThemeCodeError(`invalid theme id: ${theme2.id}`);
526
+ const b = isBuiltInThemeId(theme2.extends) ? theme2.extends : DEFAULT_THEME_ID;
527
+ const base = BUILT_IN_THEMES[b];
528
+ const payload = { i: theme2.id, n: theme2.name, b };
529
+ const l = diffTokens(theme2.light, base.light, "light") ?? {};
530
+ const d = diffTokens(theme2.dark, base.dark, "dark") ?? {};
531
+ const a = splitShared(l, d);
532
+ if (a) payload.a = a;
533
+ if (Object.keys(l).length > 0) payload.l = l;
534
+ if (Object.keys(d).length > 0) payload.d = d;
535
+ validateName(payload.n);
536
+ return `pt${VERSION}.${base64UrlEncode(utf8Encode(JSON.stringify(payload)))}`;
537
+ }
538
+ function isRecord(v) {
539
+ return typeof v === "object" && v !== null && !Array.isArray(v);
540
+ }
541
+ function validateName(name) {
542
+ if (typeof name !== "string" || name.trim().length === 0 || name.length > 60 || // eslint-disable-next-line no-control-regex
543
+ /[\u0000-\u001f\u007f]/.test(name)) {
544
+ throw new ThemeCodeError("invalid theme name");
545
+ }
546
+ }
547
+ function num(v, min, max, where) {
548
+ if (typeof v !== "number" || !Number.isFinite(v) || v < min || v > max) {
549
+ throw new ThemeCodeError(`${where}: expected a number from ${min} to ${max}`);
550
+ }
551
+ return v;
552
+ }
553
+ function onlyKeys(v, allowed, where) {
554
+ for (const k of Object.keys(v)) {
555
+ if (!allowed.includes(k)) throw new ThemeCodeError(`${where}: unknown field "${k}"`);
556
+ }
557
+ }
558
+ function shadowToken(v, where) {
559
+ if (!isRecord(v)) throw new ThemeCodeError(`${where}: expected a shadow`);
560
+ onlyKeys(v, ["layers", "elevation"], where);
561
+ if (!Array.isArray(v.layers) || v.layers.length > 4) {
562
+ throw new ThemeCodeError(`${where}.layers: expected up to 4 layers`);
563
+ }
564
+ return {
565
+ layers: v.layers.map((l, i) => {
566
+ const at = `${where}.layers[${i}]`;
567
+ if (!isRecord(l)) throw new ThemeCodeError(`${at}: expected a layer`);
568
+ onlyKeys(l, ["x", "y", "blur", "spread", "color", "opacity"], at);
569
+ return {
570
+ x: num(l.x, -100, 100, `${at}.x`),
571
+ y: num(l.y, -100, 100, `${at}.y`),
572
+ blur: num(l.blur, 0, 200, `${at}.blur`),
573
+ spread: num(l.spread, -100, 100, `${at}.spread`),
574
+ color: `#${hexValue(l.color, `${at}.color`)}`,
575
+ opacity: num(l.opacity, 0, 1, `${at}.opacity`)
576
+ };
577
+ }),
578
+ elevation: num(v.elevation, 0, 24, `${where}.elevation`)
579
+ };
580
+ }
581
+ function hexValue(v, where) {
582
+ if (typeof v !== "string" || !/^[0-9a-f]{6}([0-9a-f]{2})?$/i.test(v)) {
583
+ throw new ThemeCodeError(`${where}: expected a hex colour`);
584
+ }
585
+ return v.toLowerCase();
586
+ }
587
+ function applyDiff(base, diff, scheme) {
588
+ if (diff === void 0) return base;
589
+ if (!isRecord(diff)) throw new ThemeCodeError(`${scheme}: expected an object`);
590
+ onlyKeys(
591
+ diff,
592
+ GROUPS.map(([s]) => s),
593
+ scheme
594
+ );
595
+ const out = {
596
+ colors: { ...base.colors },
597
+ fonts: { ...base.fonts },
598
+ radii: { ...base.radii },
599
+ spacing: { ...base.spacing },
600
+ shadows: { ...base.shadows }
601
+ };
602
+ for (const [short, group] of GROUPS) {
603
+ const values = diff[short];
604
+ if (values === void 0) continue;
605
+ const where = `${scheme}.${group}`;
606
+ if (!isRecord(values)) throw new ThemeCodeError(`${where}: expected an object`);
607
+ onlyKeys(values, Object.keys(base[group]), where);
608
+ const target = out[group];
609
+ for (const [k, v] of Object.entries(values)) {
610
+ const at = `${where}.${k}`;
611
+ if (group === "colors") target[k] = `#${hexValue(v, at)}`;
612
+ else if (group === "fonts") {
613
+ if (k === "body" || k === "display" || k === "mono") {
614
+ if (typeof v !== "string" || !FONT_RE.test(v)) {
615
+ throw new ThemeCodeError(`${at}: expected a font-family list`);
616
+ }
617
+ target[k] = v;
618
+ } else if (k === "lineHeight") target[k] = num(v, 1, 3, at);
619
+ else target[k] = num(v, 8, 48, at);
620
+ } else if (group === "radii") target[k] = num(v, 0, 999, at);
621
+ else if (group === "spacing") target[k] = num(v, 0, 64, at);
622
+ else target[k] = shadowToken(v, at);
623
+ }
624
+ }
625
+ return out;
626
+ }
627
+ function decodeTheme(code) {
628
+ if (typeof code !== "string") throw new ThemeCodeError("expected a string");
629
+ const trimmed = code.trim();
630
+ if (trimmed.length > MAX_CODE_LENGTH) throw new ThemeCodeError("code is too long");
631
+ const m = /^pt(\d{1,3})\.([A-Za-z0-9_-]+)$/.exec(trimmed);
632
+ if (!m) throw new ThemeCodeError("not a poolse theme code");
633
+ const version = Number(m[1]);
634
+ if (version !== VERSION) {
635
+ throw new ThemeCodeError(`unsupported theme code version ${version}; update @poolse/theme`);
636
+ }
637
+ let payload;
638
+ try {
639
+ payload = JSON.parse(utf8Decode(base64UrlDecode(m[2])));
640
+ } catch {
641
+ throw new ThemeCodeError("the code is damaged");
642
+ }
643
+ if (!isRecord(payload)) throw new ThemeCodeError("the code is damaged");
644
+ onlyKeys(payload, ["i", "n", "b", "a", "l", "d"], "theme");
645
+ if (typeof payload.i !== "string" || !ID_RE.test(payload.i)) {
646
+ throw new ThemeCodeError("invalid theme id");
647
+ }
648
+ validateName(payload.n);
649
+ if (!isBuiltInThemeId(payload.b)) throw new ThemeCodeError(`unknown base theme`);
650
+ const base = BUILT_IN_THEMES[payload.b];
651
+ return {
652
+ id: payload.i,
653
+ name: payload.n,
654
+ extends: payload.b,
655
+ light: applyDiff(applyDiff(base.light, payload.a, "all"), payload.l, "light"),
656
+ dark: applyDiff(applyDiff(base.dark, payload.a, "all"), payload.d, "dark")
657
+ };
658
+ }
659
+ function utf8Encode(s) {
660
+ const out = [];
661
+ for (const ch of s) {
662
+ const cp = ch.codePointAt(0);
663
+ if (cp < 128) out.push(cp);
664
+ else if (cp < 2048) out.push(192 | cp >> 6, 128 | cp & 63);
665
+ else if (cp < 65536) out.push(224 | cp >> 12, 128 | cp >> 6 & 63, 128 | cp & 63);
666
+ else
667
+ out.push(
668
+ 240 | cp >> 18,
669
+ 128 | cp >> 12 & 63,
670
+ 128 | cp >> 6 & 63,
671
+ 128 | cp & 63
672
+ );
673
+ }
674
+ return out;
675
+ }
676
+ function utf8Decode(bytes) {
677
+ let s = "";
678
+ for (let i = 0; i < bytes.length; ) {
679
+ const b = bytes[i];
680
+ const n = b < 128 ? 0 : b >= 240 ? 3 : b >= 224 ? 2 : b >= 192 ? 1 : -1;
681
+ if (n < 0 || i + n >= bytes.length) throw new Error("bad utf-8");
682
+ let cp = n === 0 ? b : b & 63 >> n;
683
+ for (let k = 1; k <= n; k++) {
684
+ const c = bytes[i + k];
685
+ if (c === void 0 || (c & 192) !== 128) throw new Error("bad utf-8");
686
+ cp = cp << 6 | c & 63;
687
+ }
688
+ s += String.fromCodePoint(cp);
689
+ i += n + 1;
690
+ }
691
+ return s;
692
+ }
693
+ var B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
694
+ function base64UrlEncode(bytes) {
695
+ let out = "";
696
+ for (let i = 0; i < bytes.length; i += 3) {
697
+ const n = bytes[i] << 16 | (bytes[i + 1] ?? 0) << 8 | (bytes[i + 2] ?? 0);
698
+ const chars = i + 2 < bytes.length ? 4 : i + 1 < bytes.length ? 3 : 2;
699
+ for (let k = 0; k < chars; k++) out += B64[n >> 18 - 6 * k & 63];
700
+ }
701
+ return out;
702
+ }
703
+ function base64UrlDecode(s) {
704
+ if (s.length % 4 === 1) throw new Error("bad base64");
705
+ const out = [];
706
+ for (let i = 0; i < s.length; i += 4) {
707
+ const chunk = s.slice(i, i + 4);
708
+ let n = 0;
709
+ for (let k = 0; k < 4; k++) {
710
+ const v = k < chunk.length ? B64.indexOf(chunk[k]) : 0;
711
+ if (v < 0) throw new Error("bad base64");
712
+ n = n << 6 | v;
713
+ }
714
+ out.push(n >> 16 & 255);
715
+ if (chunk.length > 2) out.push(n >> 8 & 255);
716
+ if (chunk.length > 3) out.push(n & 255);
717
+ }
718
+ return out;
719
+ }
720
+
721
+ export { BUILT_IN_THEMES, BUILT_IN_THEME_IDS, CONTRAST_PAIRS, DEFAULT_THEME_ID, ThemeCodeError, contrastIssues, contrastRatio, createTheme, cssShadow, decodeTheme, encodeTheme, getTheme, isBuiltInThemeId, mergeTokens, parseHex, relativeLuminance, resolveTheme, toCssText, toCssVariables, toNativeTheme, tokensToCssVariables, tokensToNative };
722
+ //# sourceMappingURL=index.js.map
723
+ //# sourceMappingURL=index.js.map