chaincss 1.13.2 → 2.0.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.
Files changed (101) hide show
  1. package/CHANGELOG.md +81 -0
  2. package/LICENSE +2 -3
  3. package/README.md +239 -114
  4. package/dist/cli/commands/build.d.ts +3 -0
  5. package/dist/cli/commands/build.d.ts.map +1 -0
  6. package/dist/cli/commands/compile.d.ts +3 -0
  7. package/dist/cli/commands/compile.d.ts.map +1 -0
  8. package/dist/cli/commands/init.d.ts +5 -0
  9. package/dist/cli/commands/init.d.ts.map +1 -0
  10. package/dist/cli/commands/timeline.d.ts +2 -0
  11. package/dist/cli/commands/timeline.d.ts.map +1 -0
  12. package/dist/cli/commands/watch.d.ts +6 -0
  13. package/dist/cli/commands/watch.d.ts.map +1 -0
  14. package/dist/cli/index.d.ts +2 -0
  15. package/dist/cli/index.d.ts.map +1 -0
  16. package/dist/cli/index.js +5960 -0
  17. package/dist/cli/types.d.ts +51 -0
  18. package/dist/cli/types.d.ts.map +1 -0
  19. package/dist/cli/utils/config-loader.d.ts +8 -0
  20. package/dist/cli/utils/config-loader.d.ts.map +1 -0
  21. package/dist/cli/utils/file-utils.d.ts +9 -0
  22. package/dist/cli/utils/file-utils.d.ts.map +1 -0
  23. package/dist/cli/utils/logger.d.ts +17 -0
  24. package/dist/cli/utils/logger.d.ts.map +1 -0
  25. package/dist/compiler/atomic-optimizer.d.ts +76 -0
  26. package/dist/compiler/atomic-optimizer.d.ts.map +1 -0
  27. package/dist/compiler/btt.d.ts +138 -0
  28. package/dist/compiler/btt.d.ts.map +1 -0
  29. package/dist/compiler/cache-manager.d.ts +20 -0
  30. package/dist/compiler/cache-manager.d.ts.map +1 -0
  31. package/dist/compiler/commonProps.d.ts +2 -0
  32. package/dist/compiler/commonProps.d.ts.map +1 -0
  33. package/dist/compiler/index.d.ts +12 -0
  34. package/dist/compiler/index.d.ts.map +1 -0
  35. package/dist/compiler/index.js +5177 -0
  36. package/dist/compiler/prefixer.d.ts +42 -0
  37. package/dist/compiler/prefixer.d.ts.map +1 -0
  38. package/dist/compiler/theme-contract.d.ts +61 -0
  39. package/dist/compiler/theme-contract.d.ts.map +1 -0
  40. package/dist/compiler/tokens.d.ts +52 -0
  41. package/dist/compiler/tokens.d.ts.map +1 -0
  42. package/dist/compiler/types.d.ts +57 -0
  43. package/dist/compiler/types.d.ts.map +1 -0
  44. package/dist/core/compiler.d.ts +32 -0
  45. package/dist/core/compiler.d.ts.map +1 -0
  46. package/dist/core/constants.d.ts +129 -0
  47. package/dist/core/constants.d.ts.map +1 -0
  48. package/dist/core/index.d.ts +4 -0
  49. package/dist/core/index.d.ts.map +1 -0
  50. package/dist/core/types.d.ts +88 -0
  51. package/dist/core/types.d.ts.map +1 -0
  52. package/dist/core/utils.d.ts +37 -0
  53. package/dist/core/utils.d.ts.map +1 -0
  54. package/dist/index.d.ts +13 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +5667 -0
  57. package/dist/plugins/vite.d.ts +11 -0
  58. package/dist/plugins/vite.d.ts.map +1 -0
  59. package/dist/plugins/vite.js +25839 -0
  60. package/dist/plugins/webpack.d.ts +45 -0
  61. package/dist/plugins/webpack.d.ts.map +1 -0
  62. package/dist/plugins/webpack.js +107 -0
  63. package/dist/runtime/hmr.d.ts +3 -0
  64. package/dist/runtime/hmr.d.ts.map +1 -0
  65. package/dist/runtime/index.d.ts +15 -0
  66. package/dist/runtime/index.d.ts.map +1 -0
  67. package/dist/runtime/index.js +552 -0
  68. package/dist/runtime/injector.d.ts +85 -0
  69. package/dist/runtime/injector.d.ts.map +1 -0
  70. package/dist/runtime/react.d.ts +54 -0
  71. package/dist/runtime/react.d.ts.map +1 -0
  72. package/dist/runtime/react.js +270 -0
  73. package/dist/runtime/types.d.ts +45 -0
  74. package/dist/runtime/types.d.ts.map +1 -0
  75. package/dist/runtime/utils.d.ts +62 -0
  76. package/dist/runtime/utils.d.ts.map +1 -0
  77. package/dist/runtime/vue.d.ts +52 -0
  78. package/dist/runtime/vue.d.ts.map +1 -0
  79. package/dist/runtime/vue.js +232 -0
  80. package/package.json +90 -109
  81. package/browser/commonProps.js +0 -14
  82. package/browser/index.js +0 -3
  83. package/browser/react-hooks.js +0 -162
  84. package/browser/rtt.js +0 -370
  85. package/node/atomic-optimizer.js +0 -391
  86. package/node/btt.js +0 -962
  87. package/node/cache-manager.js +0 -56
  88. package/node/chaincss.js +0 -489
  89. package/node/css-properties.json +0 -633
  90. package/node/index.js +0 -2
  91. package/node/loaders/chaincss-loader.js +0 -62
  92. package/node/plugins/next-plugin.js +0 -120
  93. package/node/plugins/vite-plugin.js +0 -383
  94. package/node/plugins/webpack-plugin.js +0 -41
  95. package/node/prefixer.js +0 -237
  96. package/node/strVal.js +0 -106
  97. package/node/theme-validator.js +0 -32
  98. package/shared/theme-contract.js +0 -98
  99. package/shared/tokens.cjs +0 -256
  100. package/shared/tokens.mjs +0 -320
  101. package/types.d.ts +0 -277
package/node/btt.js DELETED
@@ -1,962 +0,0 @@
1
- const path = require('path');
2
- const fs = require('fs');
3
- const https = require('https');
4
- const { tokens, createTokens, responsive } = require('../shared/tokens.cjs');
5
- const { AtomicOptimizer } = require('./atomic-optimizer');
6
-
7
- const atomicOptimizer = new AtomicOptimizer({
8
- enabled: false,
9
- alwaysAtomic: [],
10
- neverAtomic: ['content', 'animation']
11
- });
12
-
13
- function configureAtomic(opts) {
14
- Object.assign(atomicOptimizer.options, opts);
15
- }
16
-
17
- const chain = {
18
- cssOutput: undefined,
19
- catcher: {},
20
- cachedValidProperties: [],
21
- classMap: {},
22
- atomicStats: null,
23
-
24
- initializeProperties() {
25
- try {
26
- const jsonPath = path.join(__dirname, 'css-properties.json');
27
- if (fs.existsSync(jsonPath)) {
28
- const data = fs.readFileSync(jsonPath, 'utf8');
29
- this.cachedValidProperties = JSON.parse(data);
30
- } else {
31
- console.log('⚠️ CSS properties not cached, will load on first use');
32
- }
33
- } catch (error) {
34
- console.error('Error loading CSS properties:', error.message);
35
- }
36
- },
37
-
38
- fetchWithHttps(url) {
39
- return new Promise((resolve, reject) => {
40
- https.get(url, (response) => {
41
- let data = '';
42
- response.on('data', (chunk) => data += chunk);
43
- response.on('end', () => {
44
- try {
45
- resolve(JSON.parse(data));
46
- } catch (error) {
47
- reject(error);
48
- }
49
- });
50
- }).on('error', reject);
51
- });
52
- },
53
-
54
- async getCSSProperties() {
55
- try {
56
- const jsonPath = path.join(__dirname, 'css-properties.json');
57
- try {
58
- await fs.promises.access(jsonPath);
59
- const existingData = await fs.promises.readFile(jsonPath, 'utf8');
60
- const objProp = JSON.parse(existingData);
61
- this.cachedValidProperties = objProp;
62
- return objProp;
63
- } catch {
64
- const url = 'https://raw.githubusercontent.com/mdn/data/main/css/properties.json';
65
- const data = await this.fetchWithHttps(url);
66
- const allProperties = Object.keys(data);
67
- const baseProperties = new Set();
68
- allProperties.forEach(prop => {
69
- const baseProp = prop.replace(/^-(webkit|moz|ms|o)-/, '');
70
- baseProperties.add(baseProp);
71
- });
72
- const cleanProperties = Array.from(baseProperties).sort();
73
- await fs.promises.writeFile(jsonPath, JSON.stringify(cleanProperties, null, 2));
74
- this.cachedValidProperties = cleanProperties;
75
- return cleanProperties;
76
- }
77
- } catch (error) {
78
- console.error('Error loading CSS properties:', error.message);
79
- return [];
80
- }
81
- },
82
-
83
- getCachedProperties() {
84
- return this.cachedValidProperties;
85
- }
86
- };
87
-
88
- chain.initializeProperties();
89
-
90
- const resolveToken = (value, useTokens) => {
91
- if (!useTokens || typeof value !== 'string' || !value.startsWith('$')) {
92
- return value;
93
- }
94
- const tokenPath = value.slice(1);
95
- const tokenValue = tokens.get(tokenPath);
96
- return tokenValue || value;
97
- };
98
-
99
- function $(useTokens = true) {
100
- const catcher = {};
101
- const validProperties = chain.cachedValidProperties;
102
-
103
- const handler = {
104
- get: (target, prop) => {
105
- // Handle .block()
106
- if (prop === 'block') {
107
- return function(...args) {
108
- if (args.length === 0) {
109
- const result = { ...catcher };
110
- Object.keys(catcher).forEach(key => delete catcher[key]);
111
- return result;
112
- }
113
- const result = {
114
- selectors: args,
115
- ...catcher
116
- };
117
- Object.keys(catcher).forEach(key => delete catcher[key]);
118
- return result;
119
- };
120
- }
121
-
122
- // Handle .hover()
123
- if (prop === 'hover') {
124
- return () => {
125
- const hoverCatcher = {};
126
- const hoverHandler = {
127
- get: (hoverTarget, hoverProp) => {
128
- if (hoverProp === 'end') {
129
- return () => {
130
- catcher.hover = { ...hoverCatcher };
131
- Object.keys(hoverCatcher).forEach(key => delete hoverCatcher[key]);
132
- return proxy;
133
- };
134
- }
135
- const cssProperty = hoverProp.replace(/([A-Z])/g, '-$1').toLowerCase();
136
- if (validProperties && validProperties.length > 0 && !validProperties.includes(cssProperty)) {
137
- console.warn(`Warning: '${cssProperty}' may not be a valid CSS property`);
138
- }
139
- return (value) => {
140
- hoverCatcher[hoverProp] = resolveToken(value, useTokens);
141
- return hoverProxy;
142
- };
143
- }
144
- };
145
- const hoverProxy = new Proxy({}, hoverHandler);
146
- return hoverProxy;
147
- };
148
- }
149
-
150
- // Handle .end()
151
- if (prop === 'end') {
152
- return () => {
153
- return proxy;
154
- };
155
- }
156
-
157
- // Handle .select() - nested selectors
158
- if (prop === 'select') {
159
- return function(selector) {
160
- const nestedStyles = {};
161
- const nestedHandler = {
162
- get: (nestedTarget, nestedProp) => {
163
- if (nestedProp === 'block') {
164
- return () => {
165
- if (!catcher.nestedRules) catcher.nestedRules = [];
166
- catcher.nestedRules.push({
167
- selector: selector,
168
- styles: { ...nestedStyles }
169
- });
170
- return proxy;
171
- };
172
- }
173
- return (value) => {
174
- nestedStyles[nestedProp] = resolveToken(value, useTokens);
175
- return nestedProxy;
176
- };
177
- }
178
- };
179
- const nestedProxy = new Proxy({}, nestedHandler);
180
- return nestedProxy;
181
- };
182
- }
183
-
184
- // ========== AT-RULES ==========
185
-
186
- // @media
187
- if (prop === 'media') {
188
- return function(query, callback) {
189
- const subChain = $(useTokens);
190
- const result = callback(subChain);
191
- if (!catcher.atRules) catcher.atRules = [];
192
- catcher.atRules.push({
193
- type: 'media',
194
- query: query,
195
- styles: result
196
- });
197
- return proxy;
198
- };
199
- }
200
-
201
- // @keyframes
202
- if (prop === 'keyframes') {
203
- return function(name, callback) {
204
- const keyframeContext = { _keyframeSteps: {} };
205
- const keyframeProxy = new Proxy(keyframeContext, {
206
- get: (target, stepProp) => {
207
- if (stepProp === 'from' || stepProp === 'to') {
208
- return function(stepCallback) {
209
- const subChain = $(useTokens);
210
- const properties = stepCallback(subChain).block();
211
- target._keyframeSteps[stepProp] = properties;
212
- return keyframeProxy;
213
- };
214
- }
215
- if (stepProp === 'percent') {
216
- return function(value, stepCallback) {
217
- const subChain = $(useTokens);
218
- const properties = stepCallback(subChain).block();
219
- target._keyframeSteps[`${value}%`] = properties;
220
- return keyframeProxy;
221
- };
222
- }
223
- return undefined;
224
- }
225
- });
226
- callback(keyframeProxy);
227
- if (!catcher.atRules) catcher.atRules = [];
228
- catcher.atRules.push({
229
- type: 'keyframes',
230
- name: name,
231
- steps: keyframeContext._keyframeSteps
232
- });
233
- return proxy;
234
- };
235
- }
236
-
237
- // @font-face
238
- if (prop === 'fontFace') {
239
- return function(callback) {
240
- const fontProps = {};
241
- const fontHandler = {
242
- get: (target, fontProp) => {
243
- return (value) => {
244
- fontProps[fontProp] = resolveToken(value, useTokens);
245
- return fontProxy;
246
- };
247
- }
248
- };
249
- const fontProxy = new Proxy({}, fontHandler);
250
- callback(fontProxy);
251
-
252
- if (!catcher.atRules) catcher.atRules = [];
253
- catcher.atRules.push({
254
- type: 'font-face',
255
- properties: fontProps
256
- });
257
- return proxy;
258
- };
259
- }
260
-
261
- // @supports
262
- if (prop === 'supports') {
263
- return function(condition, callback) {
264
- const subChain = $(useTokens);
265
- const result = callback(subChain);
266
- if (!catcher.atRules) catcher.atRules = [];
267
- catcher.atRules.push({
268
- type: 'supports',
269
- condition: condition,
270
- styles: result
271
- });
272
- return proxy;
273
- };
274
- }
275
-
276
- // @container
277
- if (prop === 'container') {
278
- return function(condition, callback) {
279
- const subChain = $(useTokens);
280
- const result = callback(subChain);
281
- if (!catcher.atRules) catcher.atRules = [];
282
- catcher.atRules.push({
283
- type: 'container',
284
- condition: condition,
285
- styles: result
286
- });
287
- return proxy;
288
- };
289
- }
290
-
291
- // @layer
292
- if (prop === 'layer') {
293
- return function(name, callback) {
294
- const subChain = $(useTokens);
295
- const result = callback(subChain);
296
- if (!catcher.atRules) catcher.atRules = [];
297
- catcher.atRules.push({
298
- type: 'layer',
299
- name: name,
300
- styles: result
301
- });
302
- return proxy;
303
- };
304
- }
305
-
306
- // @counter-style
307
- if (prop === 'counterStyle') {
308
- return function(name, callback) {
309
- const counterProps = {};
310
- const counterHandler = {
311
- get: (target, counterProp) => {
312
- return (value) => {
313
- counterProps[counterProp] = resolveToken(value, useTokens);
314
- return counterProxy;
315
- };
316
- }
317
- };
318
- const counterProxy = new Proxy({}, counterHandler);
319
- callback(counterProxy);
320
-
321
- if (!catcher.atRules) catcher.atRules = [];
322
- catcher.atRules.push({
323
- type: 'counter-style',
324
- name: name,
325
- properties: counterProps
326
- });
327
- return proxy;
328
- };
329
- }
330
-
331
- // @property
332
- if (prop === 'property') {
333
- return function(name, callback) {
334
- const propertyDescs = {};
335
- const propertyHandler = {
336
- get: (target, descProp) => {
337
- return (value) => {
338
- propertyDescs[descProp] = resolveToken(value, useTokens);
339
- return propertyProxy;
340
- };
341
- }
342
- };
343
- const propertyProxy = new Proxy({}, propertyHandler);
344
- callback(propertyProxy);
345
-
346
- if (!catcher.atRules) catcher.atRules = [];
347
- catcher.atRules.push({
348
- type: 'property',
349
- name: name,
350
- descriptors: propertyDescs
351
- });
352
- return proxy;
353
- };
354
- }
355
-
356
- // theme method
357
- if (prop === 'theme') {
358
- return function(themeTokens, callback) {
359
- // Store original tokens to restore later
360
- const originalTokens = tokens;
361
-
362
- // Create a temporary token store for this theme
363
- const themeTokenStore = {
364
- get: (path) => {
365
- // Try to get from theme tokens first, fallback to original
366
- const themeValue = themeTokens.get ? themeTokens.get(path) : null;
367
- if (themeValue !== null && themeValue !== undefined) {
368
- return themeValue;
369
- }
370
- return originalTokens.get(path);
371
- },
372
- // Make it look like the original tokens object
373
- ...themeTokens
374
- };
375
-
376
- // Create a proxy to intercept token resolution
377
- const tokenProxy = new Proxy(themeTokenStore, {
378
- get: (target, prop) => {
379
- if (prop === 'get') {
380
- return target.get.bind(target);
381
- }
382
- return target[prop];
383
- }
384
- });
385
-
386
- // Temporarily override the global tokens
387
- const originalTokensRef = globalThis.__CHAINCSS_TOKENS__ || tokens;
388
- const tempTokens = themeTokenStore;
389
-
390
- // Create a new $ function that uses the theme tokens
391
- const themed$ = (useTokens = true) => {
392
- // Store original resolver
393
- const originalResolver = resolveToken;
394
-
395
- // Create temporary token resolver
396
- const themeResolver = (value, useTokensFlag) => {
397
- if (!useTokensFlag || typeof value !== 'string' || !value.startsWith('$')) {
398
- return value;
399
- }
400
- const tokenPath = value.slice(1);
401
- const tokenValue = tempTokens.get(tokenPath);
402
- return tokenValue || value;
403
- };
404
-
405
- // Temporarily replace resolveToken
406
- globalThis.__CHAINCSS_TEMP_RESOLVER__ = themeResolver;
407
-
408
- const result = $(useTokens);
409
-
410
- // Restore original resolver
411
- delete globalThis.__CHAINCSS_TEMP_RESOLVER__;
412
-
413
- return result;
414
- };
415
-
416
- // Execute callback with themed chain
417
- const result = callback(themed$);
418
-
419
- // Store theme data for CSS generation
420
- if (!catcher.themes) catcher.themes = [];
421
- catcher.themes.push({
422
- name: `theme-${Date.now()}`,
423
- styles: result,
424
- tokens: themeTokens,
425
- fallback: originalTokens
426
- });
427
-
428
- return proxy;
429
- };
430
- }
431
-
432
- // Regular CSS properties
433
- const cssProperty = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
434
- if (validProperties && validProperties.length > 0 && !validProperties.includes(cssProperty)) {
435
- console.warn(`Warning: '${cssProperty}' may not be a valid CSS property`);
436
- }
437
-
438
- return function(value) {
439
- catcher[prop] = resolveToken(value, useTokens);
440
- return proxy;
441
- };
442
- }
443
- };
444
-
445
- const proxy = new Proxy({}, handler);
446
-
447
- if (chain.cachedValidProperties.length === 0) {
448
- chain.getCSSProperties().catch(err => {
449
- console.error('Failed to load CSS properties:', err.message);
450
- });
451
- }
452
-
453
- return proxy;
454
- }
455
-
456
- // Process at-rules for CSS generation
457
- function processAtRule(rule, parentSelectors = null) {
458
- let output = '';
459
-
460
- switch(rule.type) {
461
- case 'media':
462
- if (parentSelectors) {
463
- let mediaBody = '';
464
- if (rule.styles && typeof rule.styles === 'object') {
465
- for (let prop in rule.styles) {
466
- if (prop !== 'selectors' && rule.styles.hasOwnProperty(prop)) {
467
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
468
- mediaBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
469
- }
470
- }
471
- }
472
- if (mediaBody.trim()) {
473
- output = `@media ${rule.query} {\n ${parentSelectors.join(', ')} {\n${mediaBody} }\n}\n`;
474
- }
475
- } else {
476
- output = `@media ${rule.query} {\n`;
477
- if (rule.styles && rule.styles.selectors) {
478
- let ruleBody = '';
479
- for (let prop in rule.styles) {
480
- if (prop !== 'selectors' && rule.styles.hasOwnProperty(prop)) {
481
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
482
- ruleBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
483
- }
484
- }
485
- if (ruleBody.trim()) {
486
- output += ` ${rule.styles.selectors.join(', ')} {\n${ruleBody} }\n`;
487
- }
488
- }
489
- output += '}\n';
490
- }
491
- break;
492
-
493
- case 'keyframes':
494
- output = `@keyframes ${rule.name} {\n`;
495
- for (let step in rule.steps) {
496
- output += ` ${step} {\n`;
497
- for (let prop in rule.steps[step]) {
498
- if (prop !== 'selectors') {
499
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
500
- output += ` ${kebabKey}: ${rule.steps[step][prop]};\n`;
501
- }
502
- }
503
- output += ' }\n';
504
- }
505
- output += '}\n';
506
- break;
507
-
508
- case 'font-face':
509
- output = '@font-face {\n';
510
- for (let prop in rule.properties) {
511
- if (prop !== 'selectors') {
512
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
513
- output += ` ${kebabKey}: ${rule.properties[prop]};\n`;
514
- }
515
- }
516
- output += '}\n';
517
- break;
518
-
519
- case 'supports':
520
- output = `@supports ${rule.condition} {\n`;
521
- if (rule.styles && rule.styles.selectors) {
522
- let ruleBody = '';
523
- for (let prop in rule.styles) {
524
- if (prop !== 'selectors' && rule.styles.hasOwnProperty(prop)) {
525
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
526
- ruleBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
527
- }
528
- }
529
- if (ruleBody.trim()) {
530
- output += ` ${rule.styles.selectors.join(', ')} {\n${ruleBody} }\n`;
531
- }
532
- }
533
- output += '}\n';
534
- break;
535
-
536
- case 'container':
537
- output = `@container ${rule.condition} {\n`;
538
- if (rule.styles && rule.styles.selectors) {
539
- let ruleBody = '';
540
- for (let prop in rule.styles) {
541
- if (prop !== 'selectors' && rule.styles.hasOwnProperty(prop)) {
542
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
543
- ruleBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
544
- }
545
- }
546
- if (ruleBody.trim()) {
547
- output += ` ${rule.styles.selectors.join(', ')} {\n${ruleBody} }\n`;
548
- }
549
- }
550
- output += '}\n';
551
- break;
552
-
553
- case 'layer':
554
- output = `@layer ${rule.name} {\n`;
555
- if (rule.styles && rule.styles.selectors) {
556
- let ruleBody = '';
557
- for (let prop in rule.styles) {
558
- if (prop !== 'selectors' && rule.styles.hasOwnProperty(prop)) {
559
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
560
- ruleBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
561
- }
562
- }
563
- if (ruleBody.trim()) {
564
- output += ` ${rule.styles.selectors.join(', ')} {\n${ruleBody} }\n`;
565
- }
566
- }
567
- output += '}\n';
568
- break;
569
-
570
- case 'counter-style':
571
- output = `@counter-style ${rule.name} {\n`;
572
- for (let prop in rule.properties) {
573
- if (prop !== 'selectors') {
574
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
575
- output += ` ${kebabKey}: ${rule.properties[prop]};\n`;
576
- }
577
- }
578
- output += '}\n';
579
- break;
580
-
581
- case 'property':
582
- output = `@property ${rule.name} {\n`;
583
- for (let desc in rule.descriptors) {
584
- if (desc !== 'selectors') {
585
- const kebabKey = desc.replace(/([A-Z])/g, '-$1').toLowerCase();
586
- output += ` ${kebabKey}: ${rule.descriptors[desc]};\n`;
587
- }
588
- }
589
- output += '}\n';
590
- break;
591
- }
592
-
593
- return output;
594
- }
595
-
596
- function processStandaloneAtRule(rule) {
597
- let output = '';
598
-
599
- switch(rule.type) {
600
- case 'font-face':
601
- output = '@font-face {\n';
602
- for (let prop in rule.properties) {
603
- if (prop !== 'selectors') {
604
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
605
- output += ` ${kebabKey}: ${rule.properties[prop]};\n`;
606
- }
607
- }
608
- output += '}\n';
609
- break;
610
-
611
- case 'keyframes':
612
- output = `@keyframes ${rule.name} {\n`;
613
- for (let step in rule.steps) {
614
- output += ` ${step} {\n`;
615
- for (let prop in rule.steps[step]) {
616
- if (prop !== 'selectors') {
617
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
618
- output += ` ${kebabKey}: ${rule.steps[step][prop]};\n`;
619
- }
620
- }
621
- output += ' }\n';
622
- }
623
- output += '}\n';
624
- break;
625
-
626
- case 'counter-style':
627
- output = `@counter-style ${rule.name} {\n`;
628
- for (let prop in rule.properties) {
629
- if (prop !== 'selectors') {
630
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
631
- output += ` ${kebabKey}: ${rule.properties[prop]};\n`;
632
- }
633
- }
634
- output += '}\n';
635
- break;
636
-
637
- case 'property':
638
- output = `@property ${rule.name} {\n`;
639
- for (let desc in rule.descriptors) {
640
- if (desc !== 'selectors') {
641
- const kebabKey = desc.replace(/([A-Z])/g, '-$1').toLowerCase();
642
- output += ` ${kebabKey}: ${rule.descriptors[desc]};\n`;
643
- }
644
- }
645
- output += '}\n';
646
- break;
647
- }
648
-
649
- return output;
650
- }
651
-
652
- const run = (...args) => {
653
- let cssOutput = '';
654
- const styleObjs = [];
655
-
656
- args.forEach((value) => {
657
- if (!value) return;
658
- styleObjs.push(value);
659
-
660
- if (value.selectors) {
661
- let mainRuleBody = '';
662
- let atRulesOutput = '';
663
-
664
- for (let key in value) {
665
- if (key === 'selectors' || !value.hasOwnProperty(key)) continue;
666
-
667
- if (key === 'atRules' && Array.isArray(value[key])) {
668
- value[key].forEach(rule => {
669
- atRulesOutput += processAtRule(rule, value.selectors);
670
- });
671
- continue;
672
- }
673
-
674
- if (key === 'nestedRules' && Array.isArray(value[key])) {
675
- value[key].forEach(rule => {
676
- let nestedBody = '';
677
- for (let prop in rule.styles) {
678
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
679
- nestedBody += ` ${kebabKey}: ${rule.styles[prop]};\n`;
680
- }
681
- if (nestedBody) {
682
- atRulesOutput += `${value.selectors.join(', ')} ${rule.selector} {\n${nestedBody} }\n`;
683
- }
684
- });
685
- continue;
686
- }
687
-
688
- if (key === 'hover' && typeof value[key] === 'object') {
689
- let hoverBody = '';
690
- for (let hoverKey in value[key]) {
691
- const kebabKey = hoverKey.replace(/([A-Z])/g, '-$1').toLowerCase();
692
- hoverBody += ` ${kebabKey}: ${value[key][hoverKey]};\n`;
693
- }
694
- if (hoverBody) {
695
- cssOutput += `${value.selectors.join(', ')}:hover {\n${hoverBody}}\n`;
696
- }
697
- continue;
698
- }
699
-
700
- const kebabKey = key.replace(/([A-Z])/g, '-$1').toLowerCase();
701
- mainRuleBody += ` ${kebabKey}: ${value[key]};\n`;
702
- }
703
-
704
- if (mainRuleBody.trim()) {
705
- cssOutput += `${value.selectors.join(', ')} {\n${mainRuleBody}}\n`;
706
- }
707
- cssOutput += atRulesOutput;
708
-
709
- } else if (value.type) {
710
- cssOutput += processStandaloneAtRule(value);
711
- }
712
- });
713
-
714
- cssOutput = cssOutput.replace(/\n{3,}/g, '\n\n').trim();
715
- chain.cssOutput = cssOutput;
716
-
717
- if (atomicOptimizer.options.enabled) {
718
- const result = atomicOptimizer.optimize(styleObjs);
719
- chain.cssOutput = result.css;
720
- chain.classMap = result.map;
721
- chain.atomicStats = result.stats;
722
- return result.css;
723
- }
724
-
725
- return cssOutput;
726
- };
727
-
728
- const compile = (obj) => {
729
- let cssString = '';
730
- const collected = [];
731
-
732
- for (const key in obj) {
733
- if (!obj.hasOwnProperty(key)) continue;
734
- const element = obj[key];
735
-
736
- // Handle themes
737
- if (element.themes && Array.isArray(element.themes)) {
738
- element.themes.forEach(theme => {
739
- // Generate CSS for each theme variant
740
- if (theme.styles && theme.styles.selectors) {
741
- let themeCSS = '';
742
- let themeSelectors = theme.styles.selectors || [];
743
-
744
- for (let prop in theme.styles) {
745
- if (prop !== 'selectors' && theme.styles.hasOwnProperty(prop)) {
746
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
747
- themeCSS += ` ${kebabKey}: ${theme.styles[prop]};\n`;
748
- }
749
- }
750
-
751
- if (themeCSS) {
752
- cssString += `${themeSelectors.join(', ')} {\n${themeCSS}}\n`;
753
- }
754
- }
755
- });
756
- continue;
757
- }
758
-
759
- if (element.atRules && Array.isArray(element.atRules)) {
760
- element.atRules.forEach(rule => {
761
- cssString += processAtRule(rule, null);
762
- });
763
- continue;
764
- }
765
-
766
- if (element.selectors) {
767
- collected.push(element);
768
- let elementCSS = '';
769
- let atRulesCSS = '';
770
-
771
- for (let prop in element) {
772
- if (prop === 'selectors' || !element.hasOwnProperty(prop)) continue;
773
-
774
- if (prop === 'atRules' && Array.isArray(element[prop])) {
775
- element[prop].forEach(rule => {
776
- atRulesCSS += processAtRule(rule, element.selectors);
777
- });
778
- } else if (prop === 'themes' && Array.isArray(element[prop])) {
779
- // Process themes (already handled above)
780
- continue;
781
- } else if (prop === 'hover' && typeof element[prop] === 'object') {
782
- let hoverBody = '';
783
- for (let hoverKey in element[prop]) {
784
- const kebabKey = hoverKey.replace(/([A-Z])/g, '-$1').toLowerCase();
785
- hoverBody += ` ${kebabKey}: ${element[prop][hoverKey]};\n`;
786
- }
787
- if (hoverBody) {
788
- cssString += `${element.selectors.join(', ')}:hover {\n${hoverBody}}\n`;
789
- }
790
- } else {
791
- const kebabKey = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
792
- elementCSS += ` ${kebabKey}: ${element[prop]};\n`;
793
- }
794
- }
795
-
796
- if (elementCSS.trim()) {
797
- cssString += `${element.selectors.join(', ')} {\n${elementCSS}}\n`;
798
- }
799
- cssString += atRulesCSS;
800
- }
801
- }
802
-
803
- chain.cssOutput = cssString.trim();
804
-
805
- if (atomicOptimizer.options.enabled) {
806
- const result = atomicOptimizer.optimize(collected);
807
- chain.cssOutput = result.css;
808
- chain.classMap = result.map;
809
- chain.atomicStats = result.stats;
810
- return result.css;
811
- }
812
-
813
- return chain.cssOutput;
814
- };
815
-
816
- function recipe(options) {
817
- const {
818
- base,
819
- variants = {},
820
- defaultVariants = {},
821
- compoundVariants = []
822
- } = options;
823
-
824
- const baseStyle = typeof base === 'function' ? base() : base;
825
- const variantStyles = {};
826
-
827
- for (const [variantName, variantMap] of Object.entries(variants)) {
828
- variantStyles[variantName] = {};
829
- for (const [variantKey, variantStyle] of Object.entries(variantMap)) {
830
- variantStyles[variantName][variantKey] = typeof variantStyle === 'function'
831
- ? variantStyle()
832
- : variantStyle;
833
- }
834
- }
835
-
836
- const compoundStyles = compoundVariants.map(cv => ({
837
- condition: cv.variants || cv,
838
- style: typeof cv.style === 'function' ? cv.style() : cv.style
839
- }));
840
-
841
- function mergeStyles(...styles) {
842
- const merged = {};
843
- for (const style of styles) {
844
- if (!style) continue;
845
- for (const [key, value] of Object.entries(style)) {
846
- if (key === 'selectors') {
847
- merged.selectors = merged.selectors || [];
848
- merged.selectors.push(...(Array.isArray(value) ? value : [value]));
849
- } else if (key === 'hover' && typeof value === 'object') {
850
- if (!merged.hover) merged.hover = {};
851
- Object.assign(merged.hover, value);
852
- } else if (key !== 'selectors') {
853
- merged[key] = value;
854
- }
855
- }
856
- }
857
- return merged;
858
- }
859
-
860
- function pick(variantSelection = {}) {
861
- const selected = { ...defaultVariants, ...variantSelection };
862
- const stylesToMerge = [];
863
-
864
- if (baseStyle) stylesToMerge.push(baseStyle);
865
- for (const [variantName, variantValue] of Object.entries(selected)) {
866
- const variantStyle = variantStyles[variantName]?.[variantValue];
867
- if (variantStyle) stylesToMerge.push(variantStyle);
868
- }
869
- for (const cv of compoundStyles) {
870
- const matches = Object.entries(cv.condition).every(
871
- ([key, value]) => selected[key] === value
872
- );
873
- if (matches && cv.style) stylesToMerge.push(cv.style);
874
- }
875
-
876
- const merged = mergeStyles(...stylesToMerge);
877
-
878
- const styleBuilder = $(true);
879
- for (const [prop, value] of Object.entries(merged)) {
880
- if (prop === 'selectors' || prop === 'hover') continue;
881
- if (styleBuilder[prop]) styleBuilder[prop](value);
882
- }
883
-
884
- if (merged.hover) {
885
- styleBuilder.hover();
886
- for (const [hoverProp, hoverValue] of Object.entries(merged.hover)) {
887
- if (styleBuilder[hoverProp]) styleBuilder[hoverProp](hoverValue);
888
- }
889
- styleBuilder.end();
890
- }
891
-
892
- const selectors = merged.selectors || [];
893
- return styleBuilder.block(...selectors);
894
- }
895
-
896
- pick.variants = variants;
897
- pick.defaultVariants = defaultVariants;
898
- pick.base = baseStyle;
899
-
900
- pick.getAllVariants = () => {
901
- const result = [];
902
- const variantKeys = Object.keys(variants);
903
-
904
- function generate(current, index) {
905
- if (index === variantKeys.length) {
906
- result.push({ ...current });
907
- return;
908
- }
909
- const variantName = variantKeys[index];
910
- for (const variantValue of Object.keys(variants[variantName])) {
911
- current[variantName] = variantValue;
912
- generate(current, index + 1);
913
- }
914
- }
915
-
916
- generate({}, 0);
917
- return result;
918
- };
919
-
920
- pick.compileAll = () => {
921
- const allVariants = pick.getAllVariants();
922
- const styles = [];
923
-
924
- if (baseStyle) styles.push(baseStyle);
925
- for (const variantMap of Object.values(variants)) {
926
- for (const variantStyle of Object.values(variantMap)) {
927
- if (variantStyle) styles.push(variantStyle);
928
- }
929
- }
930
- for (const cv of compoundStyles) {
931
- if (cv.style) styles.push(cv.style);
932
- }
933
-
934
- if (atomicOptimizer.options.enabled) {
935
- const styleObj = {};
936
- styles.forEach((style, i) => {
937
- const selectors = style.selectors || [`variant-${i}`];
938
- styleObj[selectors[0].replace(/^\./, '')] = style;
939
- });
940
- const result = atomicOptimizer.optimize(styleObj);
941
- chain.cssOutput = (chain.cssOutput || '') + result.css;
942
- chain.classMap = { ...chain.classMap, ...result.map };
943
- return result.css;
944
- }
945
-
946
- return run(...styles);
947
- };
948
-
949
- return pick;
950
- }
951
-
952
- module.exports = {
953
- chain,
954
- $,
955
- run,
956
- compile,
957
- createTokens,
958
- responsive,
959
- configureAtomic,
960
- atomicOptimizer,
961
- recipe
962
- };