@master/css 2.0.0-rc.67 → 2.0.0-rc.69

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 (109) hide show
  1. package/dist/common.cjs +0 -2
  2. package/dist/common.d.ts +0 -1
  3. package/dist/common.mjs +1 -2
  4. package/dist/config/functions.cjs +9 -243
  5. package/dist/config/functions.d.ts +137 -2
  6. package/dist/config/functions.mjs +9 -243
  7. package/dist/config/index.cjs +2 -2
  8. package/dist/config/index.mjs +2 -2
  9. package/dist/config/modes.cjs +307 -3
  10. package/dist/config/modes.d.ts +306 -2
  11. package/dist/config/modes.mjs +307 -3
  12. package/dist/config/rules.cjs +740 -782
  13. package/dist/config/rules.d.ts +692 -683
  14. package/dist/config/rules.mjs +741 -783
  15. package/dist/config/utilities.cjs +328 -97
  16. package/dist/config/utilities.d.ts +247 -16
  17. package/dist/config/utilities.mjs +328 -97
  18. package/dist/config/variables.cjs +88 -460
  19. package/dist/config/variables.d.ts +358 -369
  20. package/dist/config/variables.mjs +88 -460
  21. package/dist/core.cjs +242 -260
  22. package/dist/core.d.ts +5 -3
  23. package/dist/core.mjs +242 -260
  24. package/dist/create.cjs +10 -0
  25. package/dist/create.d.ts +3 -0
  26. package/dist/create.mjs +8 -0
  27. package/dist/declarers/core.group.cjs +99 -0
  28. package/dist/declarers/core.group.d.ts +2 -0
  29. package/dist/declarers/core.group.mjs +97 -0
  30. package/dist/declarers/core.variable.cjs +9 -0
  31. package/dist/declarers/core.variable.d.ts +4 -0
  32. package/dist/declarers/core.variable.mjs +7 -0
  33. package/dist/declarers/index.cjs +13 -0
  34. package/dist/declarers/index.d.ts +10 -0
  35. package/dist/declarers/index.mjs +11 -0
  36. package/dist/declarers/pair.cjs +12 -0
  37. package/dist/declarers/pair.d.ts +4 -0
  38. package/dist/declarers/pair.mjs +10 -0
  39. package/dist/factories/with-syntax-layer.cjs +0 -2
  40. package/dist/factories/with-syntax-layer.mjs +0 -2
  41. package/dist/function-transformers/core.calc.cjs +225 -0
  42. package/dist/function-transformers/core.calc.d.ts +2 -0
  43. package/dist/function-transformers/core.calc.mjs +223 -0
  44. package/dist/function-transformers/core.variable.cjs +23 -0
  45. package/dist/function-transformers/core.variable.d.ts +7 -0
  46. package/dist/function-transformers/core.variable.mjs +21 -0
  47. package/dist/function-transformers/index.cjs +11 -0
  48. package/dist/function-transformers/index.d.ts +8 -0
  49. package/dist/function-transformers/index.mjs +9 -0
  50. package/dist/index.cjs +10 -1
  51. package/dist/index.d.ts +8 -2
  52. package/dist/index.mjs +6 -1
  53. package/dist/layer.cjs +2 -4
  54. package/dist/layer.mjs +2 -4
  55. package/dist/non-layer.cjs +2 -4
  56. package/dist/non-layer.mjs +2 -4
  57. package/dist/syntax-rule-type.cjs +8 -7
  58. package/dist/syntax-rule-type.d.ts +7 -7
  59. package/dist/syntax-rule-type.mjs +8 -7
  60. package/dist/syntax-rule.cjs +305 -253
  61. package/dist/syntax-rule.d.ts +2 -2
  62. package/dist/syntax-rule.mjs +306 -254
  63. package/dist/{utils/autofill-solid-style.cjs → transformers/auto-fill-solid.cjs} +3 -3
  64. package/dist/transformers/auto-fill-solid.d.ts +2 -0
  65. package/dist/{utils/autofill-solid-style.mjs → transformers/auto-fill-solid.mjs} +3 -3
  66. package/dist/transformers/index.cjs +9 -0
  67. package/dist/transformers/index.d.ts +6 -0
  68. package/dist/transformers/index.mjs +7 -0
  69. package/dist/types/common.d.ts +0 -2
  70. package/dist/types/config.d.ts +24 -20
  71. package/dist/types/syntax.d.ts +10 -8
  72. package/dist/utils/equal-selectors.cjs +1 -2
  73. package/dist/utils/equal-selectors.mjs +1 -2
  74. package/dist/utils/extend-config.cjs +83 -44
  75. package/dist/utils/extend-config.d.ts +10 -1
  76. package/dist/utils/extend-config.mjs +83 -44
  77. package/dist/utils/flatten-meta-object.cjs +38 -0
  78. package/dist/utils/flatten-meta-object.d.ts +13 -0
  79. package/dist/utils/flatten-meta-object.mjs +36 -0
  80. package/dist/utils/flatten-object.cjs +13 -9
  81. package/dist/utils/flatten-object.d.ts +1 -1
  82. package/dist/utils/flatten-object.mjs +13 -9
  83. package/dist/utils/generate-css.cjs +2 -2
  84. package/dist/utils/generate-css.d.ts +1 -2
  85. package/dist/utils/generate-css.mjs +2 -2
  86. package/dist/utils/generate-selector.cjs +2 -1
  87. package/dist/utils/generate-selector.mjs +2 -1
  88. package/dist/utils/minify-extended-config.cjs +64 -0
  89. package/dist/utils/minify-extended-config.d.ts +9 -0
  90. package/dist/utils/minify-extended-config.mjs +62 -0
  91. package/dist/utils/nest-meta-object.cjs +25 -0
  92. package/dist/utils/nest-meta-object.d.ts +7 -0
  93. package/dist/utils/nest-meta-object.mjs +23 -0
  94. package/dist/utils/parse-at.cjs +1 -2
  95. package/dist/utils/parse-at.d.ts +2 -2
  96. package/dist/utils/parse-at.mjs +1 -2
  97. package/dist/utils/parse-pair.cjs +3 -6
  98. package/dist/utils/parse-pair.mjs +3 -6
  99. package/dist/utils/parse-selector.cjs +6 -6
  100. package/dist/utils/parse-selector.d.ts +3 -3
  101. package/dist/utils/parse-selector.mjs +6 -6
  102. package/dist/utils/sort-readable-classes.cjs +2 -2
  103. package/dist/utils/sort-readable-classes.d.ts +1 -2
  104. package/dist/utils/sort-readable-classes.mjs +2 -2
  105. package/dist/variable-rule.cjs +35 -29
  106. package/dist/variable-rule.d.ts +16 -6
  107. package/dist/variable-rule.mjs +35 -29
  108. package/package.json +1 -1
  109. package/dist/utils/autofill-solid-style.d.ts +0 -3
package/dist/core.mjs CHANGED
@@ -1,7 +1,5 @@
1
1
  import { SyntaxRule } from './syntax-rule.mjs';
2
- import { config } from './config/index.mjs';
3
2
  import hexToRgb from './utils/hex-to-rgb.mjs';
4
- import { flattenObject } from './utils/flatten-object.mjs';
5
3
  import extendConfig from './utils/extend-config.mjs';
6
4
  import { Rule } from './rule.mjs';
7
5
  import SyntaxRuleType from './syntax-rule-type.mjs';
@@ -15,42 +13,29 @@ import parseSelector from './utils/parse-selector.mjs';
15
13
 
16
14
  class MasterCSS {
17
15
  customConfig;
18
- static config = config;
19
- definedRules;
16
+ baseConfig;
17
+ definedRules = [];
20
18
  config;
21
- layerStatementRule;
22
- rules;
23
- classRules;
24
- animationsNonLayer;
25
- baseLayer;
26
- themeLayer;
27
- presetLayer;
28
- componentsLayer;
29
- generalLayer;
30
- components;
31
- selectors;
32
- variables;
33
- atRules;
34
- animations;
35
- constructor(customConfig){
19
+ layerStatementRule = new Rule('layer-statement', '@layer base,theme,preset,components,general;');
20
+ rules = [
21
+ this.layerStatementRule
22
+ ];
23
+ classRules = new Map();
24
+ animationsNonLayer = new NonLayer(this);
25
+ baseLayer = new SyntaxLayer('base', this);
26
+ themeLayer = new Layer('theme', this);
27
+ presetLayer = new SyntaxLayer('preset', this);
28
+ componentsLayer = new SyntaxLayer('components', this);
29
+ generalLayer = new SyntaxLayer('general', this);
30
+ components = new Map();
31
+ selectors = new Map();
32
+ variables = new Map();
33
+ modes = [];
34
+ atRules = new Map();
35
+ animations = new Map();
36
+ constructor(customConfig, baseConfig){
36
37
  this.customConfig = customConfig;
37
- this.definedRules = [];
38
- this.layerStatementRule = new Rule('layer-statement', '@layer base,theme,preset,components,general;');
39
- this.rules = [
40
- this.layerStatementRule
41
- ];
42
- this.classRules = new Map();
43
- this.animationsNonLayer = new NonLayer(this);
44
- this.baseLayer = new SyntaxLayer('base', this);
45
- this.themeLayer = new Layer('theme', this);
46
- this.presetLayer = new SyntaxLayer('preset', this);
47
- this.componentsLayer = new SyntaxLayer('components', this);
48
- this.generalLayer = new SyntaxLayer('general', this);
49
- this.components = new Map();
50
- this.selectors = new Map();
51
- this.variables = new Map();
52
- this.atRules = new Map();
53
- this.animations = new Map();
38
+ this.baseConfig = baseConfig;
54
39
  this.resolve(customConfig);
55
40
  }
56
41
  get text() {
@@ -75,7 +60,7 @@ class MasterCSS {
75
60
  customConfig = this.customConfig;
76
61
  }
77
62
  // @ts-expect-error read-only
78
- this.config = customConfig?.override ? extendConfig(customConfig) : extendConfig(config, customConfig);
63
+ this.config = this.baseConfig ? extendConfig(this.baseConfig, customConfig) : extendConfig(customConfig);
79
64
  this.resolveVariables();
80
65
  this.resolveAnimations();
81
66
  this.resolveSelectors();
@@ -125,23 +110,25 @@ class MasterCSS {
125
110
  rulesEntries.push(...Object.entries(rules));
126
111
  }
127
112
  const rulesEntriesLength = rulesEntries.length;
128
- // Prepare color variables
129
- const colorNames = new Set([
130
- 'current',
131
- 'currentColor',
132
- 'transparent'
133
- ]);
134
- this.variables.forEach((v)=>{
135
- if (v.type === 'color') colorNames.add(v.name);
136
- });
137
- const colorPattern = [
138
- ...colorNames
139
- ].join('|');
140
113
  // Main loop
141
114
  rulesEntries.sort((a, b)=>{
142
115
  if (a[1].type !== b[1].type) {
143
116
  return (b[1].type || 0) - (a[1].type || 0);
144
117
  }
118
+ if (a[1].kind !== b[1].kind) {
119
+ if (!a[1].kind) return 1;
120
+ if (!b[1].kind) return -1;
121
+ const kindOrder = [
122
+ 'color',
123
+ 'number',
124
+ 'image'
125
+ ];
126
+ const aKindIndex = kindOrder.indexOf(a[1].kind);
127
+ const bKindIndex = kindOrder.indexOf(b[1].kind);
128
+ if (aKindIndex !== bKindIndex) {
129
+ return aKindIndex - bKindIndex;
130
+ }
131
+ }
145
132
  return b[0].localeCompare(a[0], undefined, {
146
133
  numeric: true
147
134
  });
@@ -150,7 +137,6 @@ class MasterCSS {
150
137
  const syntax = {
151
138
  id,
152
139
  keys: [],
153
- variables: {},
154
140
  matchers: {},
155
141
  order,
156
142
  definition: def
@@ -160,25 +146,42 @@ class MasterCSS {
160
146
  ','
161
147
  ];
162
148
  this.definedRules.push(syntax);
163
- const { matcher, type, subkey, ambiguousKeys, ambiguousValues, sign, key: originalKey, variables: ruleVariableGroups } = def;
149
+ let { matcher, type, subkey, aliasGroups, values, kind, sign, key: originalKey, namespaces } = def;
164
150
  const keys = [];
165
151
  let key = originalKey;
166
152
  // Helper: resolve variable groups
167
- const addResolvedVariables = (groupName)=>{
153
+ const addNamespace = (namespace)=>{
154
+ const dashedNamespace = namespace.replace(/\./g, '-');
168
155
  this.variables.forEach((v)=>{
169
- if (v.group === groupName) {
170
- syntax.variables[v.key] = v;
156
+ if (v.namespace === namespace || v.group === namespace) {
157
+ let variableKey = v.name;
158
+ if (variableKey.startsWith('-' + dashedNamespace) || variableKey.startsWith(dashedNamespace)) {
159
+ variableKey = variableKey.slice(dashedNamespace.length + 1);
160
+ }
161
+ if (syntax.variables) {
162
+ syntax.variables.set(variableKey, v);
163
+ } else {
164
+ syntax.variables = new Map([
165
+ [
166
+ variableKey,
167
+ v
168
+ ]
169
+ ]);
170
+ }
171
171
  }
172
172
  });
173
173
  };
174
- // Rule-defined variable groups
175
- if (ruleVariableGroups) {
176
- ruleVariableGroups.forEach(addResolvedVariables);
174
+ // 1. Auto variable binding
175
+ addNamespace(id);
176
+ // 2. Rule-defined variable groups
177
+ if (namespaces) {
178
+ namespaces.forEach(addNamespace);
177
179
  }
178
- // Auto variable binding
179
- addResolvedVariables(id);
180
- // Key derivation logic
181
- if (type === SyntaxRuleType.NativeShorthand || type === SyntaxRuleType.Native) {
180
+ if (id.endsWith('()')) {
181
+ if (!key) def.key = key = id;
182
+ const fnName = id.slice(0, -2);
183
+ matcher = new RegExp(`^${fnName}\\(`);
184
+ } else if (type === SyntaxRuleType.NativeShorthand || type === SyntaxRuleType.Native) {
182
185
  if (!key) def.key = key = id;
183
186
  keys.push(id);
184
187
  }
@@ -193,19 +196,30 @@ class MasterCSS {
193
196
  if (type === SyntaxRuleType.Shorthand) keys.push(id);
194
197
  }
195
198
  // Ambiguous keys and values
196
- if (ambiguousKeys?.length) {
197
- const keyPattern = ambiguousKeys.length > 1 ? `(?:${ambiguousKeys.join('|')})` : ambiguousKeys[0];
198
- const variableKeys = Object.keys(syntax.variables);
199
- if (ambiguousValues?.length) {
200
- const valuePatterns = ambiguousValues.map((val)=>val instanceof RegExp ? val.source.replace('\\$colors', colorPattern) : `${val}(?:\\b|_)`);
199
+ if (aliasGroups?.length) {
200
+ const keyPattern = aliasGroups.length > 1 ? `(?:${aliasGroups.join('|')})` : aliasGroups[0];
201
+ const variableKeys = Array.from(syntax.variables?.keys() || []);
202
+ const valuePatterns = values ? values.map((v)=>`${v}(?:\\b|_)`) : [];
203
+ switch(kind){
204
+ case 'color':
205
+ valuePatterns.push(`(?:#|(?:color|color-contrast|color-mix|hwb|lab|lch|oklab|oklch|rgb|rgba|hsl|hsla|light-dark)\\(.*\\)|(?:currentColor|transparent)(?![a-zA-Z0-9-]))`);
206
+ break;
207
+ case 'number':
208
+ valuePatterns.push('(?:[\\d.]|(?:max|min|calc|clamp)\\([^|]*\\))');
209
+ break;
210
+ case 'image':
211
+ valuePatterns.push('(?:url|linear-gradient|radial-gradient|repeating-linear-gradient|repeating-radial-gradient|conic-gradient)\\(.*\\)');
212
+ break;
213
+ }
214
+ if (valuePatterns?.length) {
201
215
  syntax.matchers.value = new RegExp(`^${keyPattern}:(?:${valuePatterns.join('|')})[^|]*?(?:@|$)`);
202
216
  }
203
217
  if (variableKeys.length) {
204
- syntax.matchers.variable = new RegExp(`^${keyPattern}:(?:${variableKeys.join('|')}(?![a-zA-Z0-9-]))[^|]*?(?:@|$)`);
218
+ syntax.matchers.variable = new RegExp(`^${keyPattern}:(?:${variableKeys.join('|')})(?![a-zA-Z0-9-])[^|]*?(?:@|$)`);
205
219
  }
206
220
  }
207
221
  } else {
208
- syntax.matchers.arbitrary = matcher;
222
+ syntax.matchers.arbitrary = new RegExp(matcher);
209
223
  }
210
224
  // Utility rule matcher
211
225
  if (type === SyntaxRuleType.Utility) {
@@ -220,16 +234,15 @@ class MasterCSS {
220
234
  });
221
235
  }
222
236
  resolveComponents() {
223
- const { components } = this.config;
224
- const flatComps = components ? flattenObject(components) : {};
225
- const flatNames = Object.keys(flatComps);
237
+ const { components = {} } = this.config;
238
+ const flatNames = Object.keys(components);
226
239
  const resolve = (name, currentClasses = [])=>{
227
240
  const compClasses = this.components.get(name);
228
241
  if (compClasses) {
229
242
  currentClasses.push(...compClasses);
230
243
  return;
231
244
  }
232
- const className = flatComps[name];
245
+ const className = components[name];
233
246
  if (!className) return;
234
247
  const classes = className.replace(/(?:\n\s*)+/g, ' ').trim().split(' ');
235
248
  for (const cls of classes){
@@ -247,32 +260,12 @@ class MasterCSS {
247
260
  for (const name of flatNames){
248
261
  resolve(name);
249
262
  }
250
- // Second pass: convert to grouped syntax rule strings
251
- this.components.forEach((compClasses, componentName)=>{
252
- const syntaxRulesByStateToken = {};
253
- for (const syntax of compClasses){
254
- const parsed = this.create(syntax);
255
- if (parsed?.valid) {
256
- const group = syntaxRulesByStateToken[parsed.stateToken] ||= [];
257
- group.push(parsed);
258
- } else {
259
- console.error(`Invalid class "${syntax}" found in ${componentName} component.`);
260
- }
261
- }
262
- const result = Object.entries(syntaxRulesByStateToken).map(([stateToken, syntaxRules])=>{
263
- if (syntaxRules.length === 1) return syntaxRules[0].name;
264
- const namesWithoutToken = syntaxRules.map((s)=>s.name.slice(0, s.name.length - s.stateToken.length));
265
- return `{${namesWithoutToken.join(';')}}${stateToken}`;
266
- });
267
- this.components.set(componentName, result);
268
- });
269
263
  }
270
264
  resolveSelectors() {
271
265
  const { selectors } = this.config;
272
266
  if (selectors) {
273
- const flatSelectors = flattenObject(selectors);
274
- for(const token in flatSelectors){
275
- const value = flatSelectors[token];
267
+ for(const token in selectors){
268
+ const value = selectors[token];
276
269
  const nodes = parseSelector(value, this, false);
277
270
  this.selectors.set(token, nodes);
278
271
  }
@@ -293,9 +286,8 @@ class MasterCSS {
293
286
  }
294
287
  }
295
288
  if (at) {
296
- const flatAt = flattenObject(at);
297
- for(const token in flatAt){
298
- const value = flatAt[token];
289
+ for(const token in at){
290
+ const value = at[token];
299
291
  if (typeof value === 'number') {
300
292
  const node = this.parseValue(value);
301
293
  this.atRules.set(token, {
@@ -312,194 +304,187 @@ class MasterCSS {
312
304
  }
313
305
  }
314
306
  resolveVariables() {
315
- const { variables, screens } = this.config;
307
+ const { variables, screens, modes } = this.config;
316
308
  const aliasVariableModeResolvers = new Map();
317
- const resolveVariable = (variableDefinition, name, mode)=>{
318
- if (variableDefinition === undefined || variableDefinition === null) return;
319
- const addVariable = (name, variable, replacedMode, alpha)=>{
320
- if (variable === undefined) return;
321
- const flatName = name.join('-');
322
- const groups = name.slice(0, -1).filter(Boolean);
323
- const key = (name[0] === '' ? '-' : '') + name[name.length - 1];
324
- variable.key = key;
325
- variable.name = flatName;
326
- if (groups.length) variable.group = groups.join('.');
327
- if (variable.type === 'color') {
328
- if (alpha) {
329
- const slashIndex = variable.value.indexOf('/');
330
- variable = {
331
- ...variable,
332
- value: slashIndex === -1 ? variable.value + ' / ' + (alpha.startsWith('0.') ? alpha.slice(1) : alpha) : variable.value.slice(0, slashIndex + 2) + String(+variable.value.slice(slashIndex + 2) * +alpha).slice(1)
333
- };
334
- }
335
- }
336
- const currentMode = replacedMode ?? mode;
337
- if (currentMode !== undefined) {
338
- const foundVariable = this.variables.get(flatName);
309
+ const resolveVariable = (variable, mode)=>{
310
+ const addVariable = (name, newVariable, currentMode)=>{
311
+ if (currentMode) {
312
+ const foundVariable = this.variables.get(name);
313
+ const newModeVariable = {
314
+ value: newVariable.value
315
+ };
316
+ if ('alpha' in newVariable) newModeVariable.alpha = newVariable.alpha;
317
+ if ('space' in newVariable) newModeVariable.space = newVariable.space;
339
318
  if (foundVariable) {
340
- if (currentMode) {
341
- if (!foundVariable.modes) {
342
- foundVariable.modes = {};
343
- }
344
- foundVariable.modes[currentMode] = variable;
345
- } else {
346
- foundVariable.value = variable.value;
347
- if (variable.type === 'color') {
348
- foundVariable.space = variable.space;
349
- }
319
+ if (newVariable.type && newVariable.type !== foundVariable.type) {
320
+ return;
350
321
  }
322
+ if (!foundVariable.modes) foundVariable.modes = {};
323
+ foundVariable.modes[currentMode] = newModeVariable;
351
324
  } else {
352
- if (currentMode) {
353
- const newVariable = {
354
- key: variable.key,
355
- name: variable.name,
356
- group: variable.group,
357
- type: variable.type,
358
- modes: {
359
- [currentMode]: variable
360
- }
361
- };
362
- if (variable.type === 'color') {
363
- newVariable.space = variable.space;
325
+ const newRootVaraible = {
326
+ name: newVariable.name,
327
+ key: newVariable.key,
328
+ type: newVariable.type,
329
+ modes: {
330
+ [currentMode]: newModeVariable
364
331
  }
365
- this.variables.set(flatName, newVariable);
366
- } else {
367
- this.variables.set(flatName, variable);
368
- }
332
+ };
333
+ if (newVariable.namespace) newRootVaraible.namespace = newVariable.namespace;
334
+ if (newVariable.group) newRootVaraible.group = newVariable.group;
335
+ this.variables.set(name, newRootVaraible);
369
336
  }
370
337
  } else {
371
- this.variables.set(flatName, variable);
338
+ this.variables.set(name, newVariable);
372
339
  }
373
340
  };
374
- if (typeof variableDefinition === 'object') {
375
- if (Array.isArray(variableDefinition)) {
376
- addVariable(name, {
377
- type: 'string',
378
- value: variableDefinition.join(',')
379
- });
380
- } else {
381
- const keys = Object.keys(variableDefinition);
382
- for (const eachKey of keys){
383
- if (eachKey === '' || eachKey.startsWith('@')) {
384
- resolveVariable(variableDefinition[eachKey], name, eachKey || keys.some((eachKey)=>eachKey.startsWith('@')) ? eachKey.slice(1) : undefined);
385
- } else {
386
- resolveVariable(variableDefinition[eachKey], [
387
- ...name,
388
- eachKey
389
- ]);
390
- }
391
- }
392
- }
393
- } else if (typeof variableDefinition === 'number') {
394
- addVariable(name, {
395
- type: 'number',
396
- value: variableDefinition
397
- });
398
- addVariable([
399
- '',
400
- ...name
401
- ], {
341
+ if (typeof variable.value === 'number') {
342
+ addVariable(variable.name, {
343
+ ...variable,
344
+ type: 'number'
345
+ }, mode);
346
+ addVariable('-' + variable.name, {
347
+ ...variable,
402
348
  type: 'number',
403
- value: variableDefinition * -1
404
- });
405
- } else if (typeof variableDefinition === 'string') {
406
- const aliasResult = /^\$\((.*?)\)(?: ?\/ ?(.+?))?$/.exec(variableDefinition);
407
- const flatName = name.join('-');
349
+ name: '-' + variable.name,
350
+ key: '-' + variable.key,
351
+ value: variable.value * -1
352
+ }, mode);
353
+ } else if (typeof variable.value === 'string') {
354
+ const aliasResult = /^\$\((.*?)\)(?: ?\/ ?(.+?))?$|^\$([a-zA-Z0-9-]+)(?: ?\/ ?(.+?))?$/.exec(variable.value);
408
355
  if (aliasResult) {
409
- let aliasVariableModeResolver = aliasVariableModeResolvers.get(flatName);
356
+ const alias = aliasResult[1] ?? aliasResult[3];
357
+ const alpha = aliasResult[2] ?? aliasResult[4];
358
+ let aliasVariableModeResolver = aliasVariableModeResolvers.get(variable.name);
410
359
  if (!aliasVariableModeResolver) {
411
- aliasVariableModeResolvers.set(flatName, aliasVariableModeResolver = {});
360
+ aliasVariableModeResolvers.set(variable.name, aliasVariableModeResolver = {});
412
361
  }
413
362
  aliasVariableModeResolver[mode] = ()=>{
414
363
  delete aliasVariableModeResolver[mode];
415
- const [alias, aliasMode] = aliasResult[1].split('@');
416
- if (alias) {
417
- const eachAliasModeVariableResolver = aliasVariableModeResolvers.get(alias);
418
- if (eachAliasModeVariableResolver) {
419
- for (const mode of Object.keys(eachAliasModeVariableResolver)){
420
- eachAliasModeVariableResolver[mode]?.();
364
+ if (!alias) return;
365
+ const eachAliasModeVariableResolver = aliasVariableModeResolvers.get(alias);
366
+ if (eachAliasModeVariableResolver) {
367
+ for (const mode of Object.keys(eachAliasModeVariableResolver)){
368
+ eachAliasModeVariableResolver[mode]?.();
369
+ }
370
+ }
371
+ const aliasVariable = this.variables.get(alias);
372
+ if (aliasVariable) {
373
+ let resolvedAlpha;
374
+ if (alpha) {
375
+ const numberAlpha = Number(alpha) * (aliasVariable.alpha || 1);
376
+ if (numberAlpha < 1) resolvedAlpha = numberAlpha;
377
+ }
378
+ const newVariable = {
379
+ ...variable,
380
+ type: aliasVariable.type,
381
+ value: aliasVariable.value
382
+ };
383
+ if (aliasVariable.type === 'color') {
384
+ if (resolvedAlpha !== undefined) newVariable.alpha = resolvedAlpha;
385
+ if (aliasVariable.space) {
386
+ newVariable.space = aliasVariable.space;
421
387
  }
422
388
  }
423
- const aliasVariable = this.variables.get(alias);
424
- if (aliasVariable) {
425
- if (aliasMode === undefined && aliasVariable.modes) {
426
- addVariable(name, {
427
- type: aliasVariable.type,
428
- value: aliasVariable.value,
429
- space: aliasVariable.space
430
- }, '', aliasResult[2]);
431
- for(const mode in aliasVariable.modes){
432
- addVariable(name, aliasVariable.modes[mode], mode, aliasResult[2]);
433
- }
434
- } else {
435
- const variable = aliasMode !== undefined ? aliasVariable.modes?.[aliasMode] : aliasVariable;
436
- if (variable) {
437
- const newVariable = {
438
- type: variable.type,
439
- value: variable.value
440
- };
441
- if (variable.type === 'color') {
442
- newVariable.space = variable.space;
443
- }
444
- addVariable(name, newVariable, undefined, aliasResult[2]);
445
- }
389
+ addVariable(newVariable.name, newVariable, mode);
390
+ if (aliasVariable.modes) {
391
+ for(const eachMode in aliasVariable.modes){
392
+ const aliasModeVariable = aliasVariable.modes[eachMode];
393
+ addVariable(newVariable.name, aliasModeVariable, eachMode);
446
394
  }
447
395
  }
448
396
  }
449
397
  };
450
398
  } else {
451
- const hexColorResult = /^#([A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/.exec(variableDefinition);
452
- if (hexColorResult) {
453
- const [r, g, b, a] = hexToRgb(hexColorResult[1]);
454
- addVariable(name, {
399
+ // 1. HEX
400
+ const hexMatch = /^#([a-f0-9]{3,4}|[a-f0-9]{6}|[a-f0-9]{8})$/i.exec(variable.value);
401
+ if (hexMatch) {
402
+ const [r, g, b, a] = hexToRgb(hexMatch[1]);
403
+ const newVariable = {
404
+ ...variable,
455
405
  type: 'color',
456
- value: `${r} ${g} ${b}${a === 1 ? '' : ' / ' + a}`,
406
+ value: `${r} ${g} ${b}`,
457
407
  space: 'rgb'
458
- });
459
- } else {
460
- const rgbFunctionResult = /^rgb\( *([0-9]{1,3})(?: *, *| +)([0-9]{1,3})(?: *, *| +)([0-9]{1,3}) *(?:(?:,|\/) *(.*?) *)?\)$/.exec(variableDefinition);
461
- if (rgbFunctionResult) {
462
- addVariable(name, {
463
- type: 'color',
464
- value: rgbFunctionResult[1] + ' ' + rgbFunctionResult[2] + ' ' + rgbFunctionResult[3] + (rgbFunctionResult[4] ? ' / ' + (rgbFunctionResult[4].startsWith('0.') ? rgbFunctionResult[4].slice(1) : rgbFunctionResult[4]) : ''),
465
- space: 'rgb'
466
- });
467
- } else {
468
- const hslFunctionResult = /^hsl\((.*?)\)$/.exec(variableDefinition);
469
- if (hslFunctionResult) {
470
- addVariable(name, {
471
- type: 'color',
472
- value: hslFunctionResult[1],
473
- space: 'hsl'
474
- });
475
- } else {
476
- addVariable(name, {
477
- type: 'string',
478
- value: variableDefinition
479
- });
480
- }
408
+ };
409
+ if (a !== undefined && a < 1) {
410
+ newVariable.alpha = a;
481
411
  }
412
+ addVariable(newVariable.name, newVariable, mode);
413
+ return;
482
414
  }
415
+ // 2. COLOR FUNCTION
416
+ const funcMatch = /^(color|color-contrast|color-mix|hwb|lab|lch|oklab|oklch|rgb|hsl|light-dark)\((.+)\)$/i.exec(variable.value);
417
+ if (funcMatch) {
418
+ let [, space, rawArgs] = funcMatch;
419
+ space = space.toLowerCase();
420
+ const normalizedArgs = rawArgs.replace(/\s*\/\s*/g, '/').replace(/\s*,\s*/g, ' ').replace(/\s+/g, ' ').trim();
421
+ let alpha;
422
+ const alphaMatch = /^(.+?)\/([^\s]+)$/.exec(normalizedArgs);
423
+ const finalArgs = alphaMatch ? alphaMatch[1].trim() : normalizedArgs;
424
+ alpha = alphaMatch ? Number(alphaMatch[2]) : undefined;
425
+ const newVariable = {
426
+ ...variable,
427
+ type: 'color',
428
+ value: finalArgs,
429
+ space
430
+ };
431
+ if (alpha !== undefined) {
432
+ newVariable.alpha = alpha;
433
+ }
434
+ addVariable(newVariable.name, newVariable, mode);
435
+ return;
436
+ }
437
+ // 3. Fallback
438
+ addVariable(variable.name, {
439
+ ...variable,
440
+ type: 'string'
441
+ }, mode);
483
442
  }
484
443
  }
485
444
  };
486
445
  if (variables) {
487
446
  for(const parnetKey in variables){
488
- resolveVariable(variables[parnetKey], [
489
- parnetKey
490
- ]);
447
+ const variable = variables[parnetKey];
448
+ resolveVariable(variable);
491
449
  }
492
- // todo: address to the target variable
493
- aliasVariableModeResolvers.forEach((aliasVariableModeResolver)=>{
494
- for (const mode of Object.keys(aliasVariableModeResolver)){
495
- aliasVariableModeResolver[mode]?.();
450
+ }
451
+ if (modes) {
452
+ for(const mode in modes){
453
+ const modeVariables = modes[mode];
454
+ if (!modeVariables) continue;
455
+ for(const key in modeVariables){
456
+ const variable = modeVariables[key];
457
+ resolveVariable(variable, mode);
496
458
  }
497
- });
459
+ }
498
460
  }
461
+ // todo: address to the target variable
462
+ aliasVariableModeResolvers.forEach((aliasVariableModeResolver)=>{
463
+ for (const mode of Object.keys(aliasVariableModeResolver)){
464
+ aliasVariableModeResolver[mode]?.();
465
+ }
466
+ });
499
467
  if (screens) {
500
- resolveVariable(screens, [
501
- 'screen'
502
- ]);
468
+ for(const key in screens){
469
+ const value = screens[key];
470
+ const name = 'screen-' + key;
471
+ this.variables.set(name, {
472
+ name,
473
+ key,
474
+ namespace: 'screen',
475
+ group: 'screen',
476
+ type: 'number',
477
+ value
478
+ });
479
+ this.variables.set('-' + name, {
480
+ name: '-' + name,
481
+ key: '-' + key,
482
+ namespace: 'screen',
483
+ group: 'screen',
484
+ type: 'number',
485
+ value: value * -1
486
+ });
487
+ }
503
488
  }
504
489
  }
505
490
  parseValue(token, unit = 'rem') {
@@ -518,7 +503,7 @@ class MasterCSS {
518
503
  }
519
504
  /**
520
505
  * 2. value (ambiguous.key * ambiguous.values)
521
- * @example bg:current box:content font:12
506
+ * @example bg:current box-content font:12
522
507
  */ for (const eachSyntax of this.definedRules){
523
508
  if (eachSyntax.matchers.value?.test(className)) return eachSyntax;
524
509
  }
@@ -548,7 +533,7 @@ class MasterCSS {
548
533
  if (syntaxRule && syntaxRule.valid) {
549
534
  syntaxRules.push(syntaxRule);
550
535
  } else {
551
- console.error(`Invalid syntax class "${cls}" is in ${className} component.`);
536
+ console.error(`Invalid class "${cls}" found in ${className} component.`);
552
537
  }
553
538
  });
554
539
  } else {
@@ -672,14 +657,11 @@ class MasterCSS {
672
657
  }
673
658
  }
674
659
  getModeSelector(modeName) {
675
- const mode = this.config.modes?.[modeName];
676
- if (mode) {
677
- switch(mode){
678
- case 'class':
679
- return '.' + modeName;
680
- case 'host':
681
- return ':host(.' + modeName + ')';
682
- }
660
+ switch(this.config.modeTrigger){
661
+ case 'class':
662
+ return '.' + modeName;
663
+ case 'host':
664
+ return ':host(.' + modeName + ')';
683
665
  }
684
666
  }
685
667
  }