@pure-ds/storybook 0.1.11 → 0.1.13
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.
|
@@ -85,7 +85,7 @@ async function loadConfigForm() {
|
|
|
85
85
|
// The event detail contains both config and designer from pds-config-form
|
|
86
86
|
if (e.detail.designer) {
|
|
87
87
|
// Apply the styles from the designer that was already created by pds-config-form
|
|
88
|
-
await PDS.Generator.applyStyles(
|
|
88
|
+
await PDS.Generator.applyStyles();
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
// Notify manager
|
package/.storybook/preview.js
CHANGED
|
@@ -87,7 +87,7 @@ function ensurePDSStyles() {
|
|
|
87
87
|
|
|
88
88
|
if (!hasPDS && PDS.Generator.instance) {
|
|
89
89
|
console.log('🛡️ PDS sheets missing - restoring...');
|
|
90
|
-
PDS.Generator.applyStyles(
|
|
90
|
+
PDS.Generator.applyStyles();
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
|
|
@@ -108,7 +108,7 @@ const withPDS = (story, context) => {
|
|
|
108
108
|
// ALWAYS reapply PDS styles before each story render
|
|
109
109
|
const designer = PDS.Generator.instance;
|
|
110
110
|
if (designer) {
|
|
111
|
-
PDS.Generator.applyStyles(
|
|
111
|
+
PDS.Generator.applyStyles();
|
|
112
112
|
|
|
113
113
|
// Check again after applying
|
|
114
114
|
const afterSheets = document.adoptedStyleSheets || [];
|
|
@@ -264,7 +264,7 @@ const withGlobalsHandler = (story, context) => {
|
|
|
264
264
|
if (PDS.theme) generatorOptions.theme = PDS.theme;
|
|
265
265
|
|
|
266
266
|
const newDesigner = new PDS.Generator(generatorOptions);
|
|
267
|
-
await PDS.Generator.applyStyles(
|
|
267
|
+
await PDS.Generator.applyStyles();
|
|
268
268
|
|
|
269
269
|
console.log(`✅ Preset applied via decorator: ${globals.preset}`);
|
|
270
270
|
}
|
|
@@ -1446,7 +1446,7 @@ if (typeof window !== 'undefined') {
|
|
|
1446
1446
|
console.log('✅ Generator created');
|
|
1447
1447
|
|
|
1448
1448
|
console.log('🎨 Applying styles to document...');
|
|
1449
|
-
await PDS.Generator.applyStyles(
|
|
1449
|
+
await PDS.Generator.applyStyles();
|
|
1450
1450
|
console.log('✅ Styles applied to document');
|
|
1451
1451
|
|
|
1452
1452
|
// Update global reference
|
package/package.json
CHANGED
|
@@ -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);
|
|
@@ -5113,23 +5126,23 @@ export const ${name}CSS = \`${escapedCSS}\`;
|
|
|
5113
5126
|
/**
|
|
5114
5127
|
* Static method to apply styles to document
|
|
5115
5128
|
* Creates a link element with BLOB URL
|
|
5116
|
-
* @param {Generator} generator -
|
|
5129
|
+
* @param {Generator} [generator] - Optional Generator instance (defaults to singleton)
|
|
5117
5130
|
*/
|
|
5118
5131
|
static applyStyles(generator) {
|
|
5132
|
+
// Use provided generator or singleton instance
|
|
5133
|
+
const target = generator || Generator.instance;
|
|
5134
|
+
|
|
5119
5135
|
// Validate parameter
|
|
5120
|
-
if (!
|
|
5121
|
-
generator
|
|
5122
|
-
"error",
|
|
5123
|
-
"[Generator] applyStyles requires a generator object"
|
|
5124
|
-
);
|
|
5136
|
+
if (!target || typeof target !== "object") {
|
|
5137
|
+
console.error("[Generator] applyStyles requires a generator object or active singleton");
|
|
5125
5138
|
return;
|
|
5126
5139
|
}
|
|
5127
5140
|
|
|
5128
5141
|
// Preferred: apply layered CSS so tokens + primitives + components + utilities
|
|
5129
5142
|
// are available in light DOM (ensures primitives like :where(button):active apply)
|
|
5130
|
-
const cssText =
|
|
5143
|
+
const cssText = target.layeredCSS || target.css || "";
|
|
5131
5144
|
if (!cssText) {
|
|
5132
|
-
|
|
5145
|
+
target.options?.log?.(
|
|
5133
5146
|
"warn",
|
|
5134
5147
|
"[Generator] No CSS available on designer to apply"
|
|
5135
5148
|
);
|
|
@@ -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
|
|
@@ -5311,7 +5342,7 @@ export function createStylesheet(css) {
|
|
|
5311
5342
|
* @returns {boolean}
|
|
5312
5343
|
*/
|
|
5313
5344
|
export function isLiveMode() {
|
|
5314
|
-
return
|
|
5345
|
+
return pdsRegistry.isLive;
|
|
5315
5346
|
}
|
|
5316
5347
|
import { enums } from "./pds-enums.js";
|
|
5317
5348
|
import { ontology } from "./pds-ontology.js";
|
|
@@ -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 {
|
|
@@ -90,14 +71,7 @@ class PDSRegistry {
|
|
|
90
71
|
* Check if in live mode
|
|
91
72
|
*/
|
|
92
73
|
get isLive() {
|
|
93
|
-
return this._mode === "live"
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Check if designer is available
|
|
98
|
-
*/
|
|
99
|
-
get hasDesigner() {
|
|
100
|
-
return this._designer !== null;
|
|
74
|
+
return this._mode === "live";
|
|
101
75
|
}
|
|
102
76
|
}
|
|
103
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,14 +1154,19 @@ 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) {
|
|
1160
|
-
await PDS.Generator.applyStyles(
|
|
1169
|
+
await PDS.Generator.applyStyles();
|
|
1161
1170
|
|
|
1162
1171
|
// Clean up critical styles after adoptedStyleSheets are applied
|
|
1163
1172
|
if (typeof window !== "undefined") {
|
|
@@ -1459,19 +1468,19 @@ 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
|
-
await PDS.Generator.applyStyles(
|
|
1480
|
+
await PDS.Generator.applyStyles();
|
|
1472
1481
|
}
|
|
1473
1482
|
} catch (error) {
|
|
1474
|
-
|
|
1483
|
+
console.warn("Failed to update styles for new theme:", error);
|
|
1475
1484
|
}
|
|
1476
1485
|
}
|
|
1477
1486
|
|