@pure-ds/storybook 0.1.10 → 0.1.12
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/.storybook/preview.js +14 -20
- package/package.json +2 -2
- package/src/js/pds-core/pds-generator.js +32 -1
- package/src/js/pds-core/pds-registry.js +7 -57
- package/src/js/pds.js +22 -13
package/.storybook/preview.js
CHANGED
|
@@ -42,7 +42,7 @@ console.log('🎨 Starting PDS initialization with preset:', initialPreset);
|
|
|
42
42
|
|
|
43
43
|
// Wrap top-level await in IIFE for production build compatibility
|
|
44
44
|
(async () => {
|
|
45
|
-
|
|
45
|
+
PDS.initializing = true;
|
|
46
46
|
const pdsOptions = {
|
|
47
47
|
mode: 'live',
|
|
48
48
|
preset: initialPreset,
|
|
@@ -70,14 +70,13 @@ console.log('🎨 Starting PDS initialization with preset:', initialPreset);
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
await PDS.start(pdsOptions);
|
|
73
|
-
|
|
73
|
+
PDS.initializing = false;
|
|
74
74
|
|
|
75
75
|
console.log('✨ PDS initialized in live mode for Storybook');
|
|
76
76
|
console.log('📦 AutoDefiner active at:', PDS.autoDefiner?.config?.baseURL);
|
|
77
77
|
|
|
78
78
|
// Store PDS designer globally for reuse
|
|
79
|
-
|
|
80
|
-
window.__pdsCurrentPreset = initialPreset;
|
|
79
|
+
PDS.currentPreset = initialPreset;
|
|
81
80
|
})();
|
|
82
81
|
|
|
83
82
|
// Set up persistent style protection - monitor and restore PDS sheets if cleared
|
|
@@ -86,9 +85,9 @@ function ensurePDSStyles() {
|
|
|
86
85
|
const sheets = document.adoptedStyleSheets || [];
|
|
87
86
|
const hasPDS = sheets.some(s => s._pds === true);
|
|
88
87
|
|
|
89
|
-
if (!hasPDS &&
|
|
88
|
+
if (!hasPDS && PDS.Generator.instance) {
|
|
90
89
|
console.log('🛡️ PDS sheets missing - restoring...');
|
|
91
|
-
PDS.Generator.applyStyles(
|
|
90
|
+
PDS.Generator.applyStyles(PDS.Generator.instance);
|
|
92
91
|
}
|
|
93
92
|
}
|
|
94
93
|
|
|
@@ -107,14 +106,14 @@ const withPDS = (story, context) => {
|
|
|
107
106
|
console.log('📋 Current adoptedStyleSheets:', currentSheets.length, 'PDS sheets:', pdsSheets.length);
|
|
108
107
|
|
|
109
108
|
// ALWAYS reapply PDS styles before each story render
|
|
110
|
-
const designer =
|
|
109
|
+
const designer = PDS.Generator.instance;
|
|
111
110
|
if (designer) {
|
|
112
111
|
PDS.Generator.applyStyles(designer);
|
|
113
112
|
|
|
114
113
|
// Check again after applying
|
|
115
114
|
const afterSheets = document.adoptedStyleSheets || [];
|
|
116
115
|
const afterPdsSheets = afterSheets.filter(s => s._pds === true);
|
|
117
|
-
} else if (!
|
|
116
|
+
} else if (!PDS.initializing && PDS.isLiveMode()) {
|
|
118
117
|
console.warn('⚠️ No designer found!');
|
|
119
118
|
}
|
|
120
119
|
|
|
@@ -236,13 +235,13 @@ const withGlobalsHandler = (story, context) => {
|
|
|
236
235
|
const { globals } = context;
|
|
237
236
|
|
|
238
237
|
// Handle preset changes via decorator (has access to globals)
|
|
239
|
-
if (globals?.preset && globals.preset !==
|
|
240
|
-
console.log('🔄 Decorator detected preset change:',
|
|
238
|
+
if (globals?.preset && globals.preset !== PDS.currentPreset) {
|
|
239
|
+
console.log('🔄 Decorator detected preset change:', PDS.currentPreset, '→', globals.preset);
|
|
241
240
|
|
|
242
241
|
// Apply preset asynchronously
|
|
243
242
|
(async () => {
|
|
244
243
|
try {
|
|
245
|
-
|
|
244
|
+
PDS.currentPreset = globals.preset;
|
|
246
245
|
|
|
247
246
|
// Store for persistence
|
|
248
247
|
try {
|
|
@@ -267,9 +266,6 @@ const withGlobalsHandler = (story, context) => {
|
|
|
267
266
|
const newDesigner = new PDS.Generator(generatorOptions);
|
|
268
267
|
await PDS.Generator.applyStyles(newDesigner);
|
|
269
268
|
|
|
270
|
-
PDS.registry._designer = newDesigner;
|
|
271
|
-
window.__pdsDesigner = newDesigner;
|
|
272
|
-
|
|
273
269
|
console.log(`✅ Preset applied via decorator: ${globals.preset}`);
|
|
274
270
|
}
|
|
275
271
|
} catch (err) {
|
|
@@ -1397,10 +1393,10 @@ if (typeof window !== 'undefined') {
|
|
|
1397
1393
|
|
|
1398
1394
|
if (globals?.preset) {
|
|
1399
1395
|
console.log('🔔 SET_GLOBALS message received with preset:', globals.preset);
|
|
1400
|
-
console.log('📦 Current stored preset:',
|
|
1396
|
+
console.log('📦 Current stored preset:', PDS.currentPreset);
|
|
1401
1397
|
|
|
1402
1398
|
// Skip if already on this preset
|
|
1403
|
-
if (globals.preset ===
|
|
1399
|
+
if (globals.preset === PDS.currentPreset) {
|
|
1404
1400
|
console.log('⏭️ Preset unchanged, skipping');
|
|
1405
1401
|
return;
|
|
1406
1402
|
}
|
|
@@ -1453,10 +1449,8 @@ if (typeof window !== 'undefined') {
|
|
|
1453
1449
|
await PDS.Generator.applyStyles(newDesigner);
|
|
1454
1450
|
console.log('✅ Styles applied to document');
|
|
1455
1451
|
|
|
1456
|
-
// Update
|
|
1457
|
-
PDS.
|
|
1458
|
-
window.__pdsDesigner = newDesigner;
|
|
1459
|
-
window.__pdsCurrentPreset = presetId;
|
|
1452
|
+
// Update global reference
|
|
1453
|
+
PDS.currentPreset = presetId;
|
|
1460
1454
|
|
|
1461
1455
|
console.log(`✅✅✅ Preset applied successfully: ${presetConfig.name || presetId}`);
|
|
1462
1456
|
} else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pure-ds/storybook",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "Storybook showcase for Pure Design System with live configuration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"storybook:build": "npm run build-storybook"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@pure-ds/core": "^0.3.
|
|
35
|
+
"@pure-ds/core": "^0.3.3"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@custom-elements-manifest/analyzer": "^0.11.0",
|
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
import { registry as pdsRegistry } from "./pds-registry.js";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Generator - A JS-config-first design system
|
|
3
5
|
* Generates comprehensive CSS variables and styles from a minimal configuration
|
|
4
6
|
*/
|
|
5
7
|
export class Generator {
|
|
8
|
+
// Singleton instance
|
|
9
|
+
static #instance;
|
|
10
|
+
|
|
11
|
+
static get instance() {
|
|
12
|
+
return this.#instance;
|
|
13
|
+
}
|
|
14
|
+
|
|
6
15
|
// Private internal fields
|
|
7
16
|
#layers;
|
|
8
17
|
#stylesheets;
|
|
@@ -22,6 +31,10 @@ export class Generator {
|
|
|
22
31
|
if (this.options.debug) {
|
|
23
32
|
this.options.log?.("debug", "Generator options:", this.options);
|
|
24
33
|
}
|
|
34
|
+
|
|
35
|
+
// Set singleton instance
|
|
36
|
+
Generator.#instance = this;
|
|
37
|
+
|
|
25
38
|
this.tokens = this.generateTokens();
|
|
26
39
|
if (this.options.debug) {
|
|
27
40
|
this.options.log?.("debug", "Generated tokens:", this.tokens);
|
|
@@ -5266,7 +5279,25 @@ export async function adoptLayers(
|
|
|
5266
5279
|
try {
|
|
5267
5280
|
// Get all requested stylesheets
|
|
5268
5281
|
const stylesheets = await Promise.all(
|
|
5269
|
-
layers.map((layer) =>
|
|
5282
|
+
layers.map(async (layer) => {
|
|
5283
|
+
// In live mode, get stylesheets directly from the Generator singleton
|
|
5284
|
+
if (Generator.instance) {
|
|
5285
|
+
switch (layer) {
|
|
5286
|
+
case "tokens":
|
|
5287
|
+
return Generator.instance.tokensStylesheet;
|
|
5288
|
+
case "primitives":
|
|
5289
|
+
return Generator.instance.primitivesStylesheet;
|
|
5290
|
+
case "components":
|
|
5291
|
+
return Generator.instance.componentsStylesheet;
|
|
5292
|
+
case "utilities":
|
|
5293
|
+
return Generator.instance.utilitiesStylesheet;
|
|
5294
|
+
default:
|
|
5295
|
+
// Fall through to registry for unknown layers or static fallback
|
|
5296
|
+
break;
|
|
5297
|
+
}
|
|
5298
|
+
}
|
|
5299
|
+
return pdsRegistry.getStylesheet(layer);
|
|
5300
|
+
})
|
|
5270
5301
|
);
|
|
5271
5302
|
|
|
5272
5303
|
// Filter out any null results
|
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
class PDSRegistry {
|
|
7
7
|
constructor() {
|
|
8
8
|
this._mode = "static"; // Default to static mode
|
|
9
|
-
this._designer = null;
|
|
10
9
|
this._staticPaths = {
|
|
11
10
|
tokens: "/assets/pds/styles/pds-tokens.css.js",
|
|
12
11
|
primitives: "/assets/pds/styles/pds-primitives.css.js",
|
|
@@ -17,17 +16,10 @@ class PDSRegistry {
|
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
/**
|
|
20
|
-
*
|
|
19
|
+
* Switch to live mode
|
|
21
20
|
*/
|
|
22
|
-
|
|
23
|
-
this._designer = designer;
|
|
21
|
+
setLiveMode() {
|
|
24
22
|
this._mode = "live";
|
|
25
|
-
const presetName = meta?.presetName;
|
|
26
|
-
if (presetName) {
|
|
27
|
-
designer?.options?.log?.("log", `PDS live with preset "${presetName}"`);
|
|
28
|
-
} else {
|
|
29
|
-
designer?.options?.log?.("log", "PDS live with custom config");
|
|
30
|
-
}
|
|
31
23
|
}
|
|
32
24
|
|
|
33
25
|
/**
|
|
@@ -46,21 +38,10 @@ class PDSRegistry {
|
|
|
46
38
|
* Returns CSSStyleSheet object (constructable stylesheet)
|
|
47
39
|
*/
|
|
48
40
|
async getStylesheet(layer) {
|
|
49
|
-
if (this._mode === "live"
|
|
50
|
-
//
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return this._designer.tokensStylesheet;
|
|
54
|
-
case "primitives":
|
|
55
|
-
return this._designer.primitivesStylesheet;
|
|
56
|
-
case "components":
|
|
57
|
-
return this._designer.componentsStylesheet;
|
|
58
|
-
case "utilities":
|
|
59
|
-
return this._designer.utilitiesStylesheet;
|
|
60
|
-
default:
|
|
61
|
-
this._designer?.options?.log?.("warn", `[PDS Registry] Unknown layer: ${layer}`);
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
41
|
+
if (this._mode === "live") {
|
|
42
|
+
// In live mode, stylesheets should be retrieved from Generator.instance
|
|
43
|
+
// If we are here, it means adoptLayers fell back or something is wrong
|
|
44
|
+
return null;
|
|
64
45
|
} else {
|
|
65
46
|
// Import from static path
|
|
66
47
|
try {
|
|
@@ -79,30 +60,6 @@ class PDSRegistry {
|
|
|
79
60
|
}
|
|
80
61
|
}
|
|
81
62
|
|
|
82
|
-
// /**
|
|
83
|
-
// * Get BLOB URL for a layer (live mode only)
|
|
84
|
-
// * Used for @import statements in CSS
|
|
85
|
-
// */
|
|
86
|
-
// getBlobURL(layer) {
|
|
87
|
-
// if (this._mode === "live" && this._designer) {
|
|
88
|
-
// switch (layer) {
|
|
89
|
-
// case "tokens":
|
|
90
|
-
// return this._designer.tokensBlobURL;
|
|
91
|
-
// case "primitives":
|
|
92
|
-
// return this._designer.primitivesBlobURL;
|
|
93
|
-
// case "components":
|
|
94
|
-
// return this._designer.componentsBlobURL;
|
|
95
|
-
// case "utilities":
|
|
96
|
-
// return this._designer.utilitiesBlobURL;
|
|
97
|
-
// case "styles":
|
|
98
|
-
// return this._designer.stylesBlobURL;
|
|
99
|
-
// default:
|
|
100
|
-
// return null;
|
|
101
|
-
// }
|
|
102
|
-
// }
|
|
103
|
-
// return null;
|
|
104
|
-
// }
|
|
105
|
-
|
|
106
63
|
/**
|
|
107
64
|
* Get current mode
|
|
108
65
|
*/
|
|
@@ -114,14 +71,7 @@ class PDSRegistry {
|
|
|
114
71
|
* Check if in live mode
|
|
115
72
|
*/
|
|
116
73
|
get isLive() {
|
|
117
|
-
return this._mode === "live"
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Check if designer is available
|
|
122
|
-
*/
|
|
123
|
-
get hasDesigner() {
|
|
124
|
-
return this._designer !== null;
|
|
74
|
+
return this._mode === "live";
|
|
125
75
|
}
|
|
126
76
|
}
|
|
127
77
|
|
package/src/js/pds.js
CHANGED
|
@@ -40,6 +40,10 @@ class PDSBase extends EventTarget {}
|
|
|
40
40
|
/** @type {PDSAPI & PDSBase} */
|
|
41
41
|
const PDS = new PDSBase();
|
|
42
42
|
|
|
43
|
+
// State properties
|
|
44
|
+
PDS.initializing = false;
|
|
45
|
+
PDS.currentPreset = null;
|
|
46
|
+
|
|
43
47
|
import {
|
|
44
48
|
Generator,
|
|
45
49
|
adoptLayers,
|
|
@@ -64,7 +68,7 @@ import { loadTypographyFonts } from "./common/font-loader.js";
|
|
|
64
68
|
/** Generator class — use to programmatically create design system assets from a config */
|
|
65
69
|
PDS.Generator = Generator;
|
|
66
70
|
|
|
67
|
-
/** Singleton runtime registry. Use `registry.
|
|
71
|
+
/** Singleton runtime registry. Use `registry.setLiveMode()` to enable live mode or `registry.setStaticMode()` for static assets */
|
|
68
72
|
PDS.registry = registry;
|
|
69
73
|
|
|
70
74
|
/** Ontology and metadata about components and tokens */
|
|
@@ -163,8 +167,8 @@ Object.defineProperty(PDS, "currentConfig", {
|
|
|
163
167
|
Object.defineProperty(PDS, "compiled", {
|
|
164
168
|
get() {
|
|
165
169
|
// Only available in live mode when we have a generator
|
|
166
|
-
if (PDS.registry?.isLive &&
|
|
167
|
-
return
|
|
170
|
+
if (PDS.registry?.isLive && Generator.instance) {
|
|
171
|
+
return Generator.instance.compiled;
|
|
168
172
|
}
|
|
169
173
|
return null;
|
|
170
174
|
},
|
|
@@ -1150,10 +1154,15 @@ async function live(config) {
|
|
|
1150
1154
|
}
|
|
1151
1155
|
}
|
|
1152
1156
|
|
|
1153
|
-
// Set the registry to
|
|
1154
|
-
PDS.registry.
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
+
// Set the registry to live mode
|
|
1158
|
+
PDS.registry.setLiveMode();
|
|
1159
|
+
|
|
1160
|
+
// Log preset info if available
|
|
1161
|
+
if (normalized.presetInfo?.name) {
|
|
1162
|
+
generatorConfig?.log?.("log", `PDS live with preset "${normalized.presetInfo.name}"`);
|
|
1163
|
+
} else {
|
|
1164
|
+
generatorConfig?.log?.("log", "PDS live with custom config");
|
|
1165
|
+
}
|
|
1157
1166
|
|
|
1158
1167
|
// Apply styles globally if requested (default behavior)
|
|
1159
1168
|
if (applyGlobalStyles) {
|
|
@@ -1459,13 +1468,13 @@ async function setTheme(theme, options = {}) {
|
|
|
1459
1468
|
}
|
|
1460
1469
|
|
|
1461
1470
|
// If we're in live mode, regenerate styles with new theme
|
|
1462
|
-
if (PDS.registry.isLive &&
|
|
1471
|
+
if (PDS.registry.isLive && Generator.instance) {
|
|
1463
1472
|
try {
|
|
1464
|
-
const
|
|
1465
|
-
if (
|
|
1466
|
-
// Update the
|
|
1467
|
-
const newConfig = { ...
|
|
1468
|
-
|
|
1473
|
+
const currentGenerator = Generator.instance;
|
|
1474
|
+
if (currentGenerator && currentGenerator.configure) {
|
|
1475
|
+
// Update the generator's config with new theme
|
|
1476
|
+
const newConfig = { ...currentGenerator.config, theme: resolvedTheme };
|
|
1477
|
+
currentGenerator.configure(newConfig);
|
|
1469
1478
|
|
|
1470
1479
|
// Reapply styles
|
|
1471
1480
|
await PDS.Generator.applyStyles(currentDesigner);
|