11
11
*/
12
12
import type { Condition , CSSProperties , CSSValue , CustomValue , PropertyFunction , PropertyValueDefinition , PropertyValueMap , StyleFunction , StyleValue , Theme , ThemeProperties , Value } from './types' ;
13
13
14
+ // Prefix Spectrum 1 style macro classes to avoid name collisions with S2 style macro.
15
+ const PREFIX = 's1-' ;
16
+
14
17
export function createArbitraryProperty < T extends Value > ( fn : ( value : T , property : string ) => CSSProperties ) : PropertyFunction < T > {
15
18
return ( value , property ) => {
16
19
let selector = Array . isArray ( value ) ? value . map ( v => generateArbitraryValueSelector ( String ( v ) ) ) . join ( '' ) : generateArbitraryValueSelector ( String ( value ) ) ;
@@ -67,11 +70,11 @@ function mapConditionalValue<T, U>(value: PropertyValueDefinition<T>, fn: (value
67
70
}
68
71
}
69
72
70
- function createValueLookup ( values : Array < CSSValue > , atStart = false ) {
73
+ function createValueLookup ( values : Array < CSSValue > ) {
71
74
let map = new Map < CSSValue , string > ( ) ;
72
75
for ( let value of values ) {
73
76
if ( ! map . has ( value ) ) {
74
- map . set ( value , generateName ( map . size , atStart ) ) ;
77
+ map . set ( value , ` ${ PREFIX } ${ generateName ( map . size ) } ` ) ;
75
78
}
76
79
}
77
80
return map ;
@@ -91,8 +94,8 @@ interface MacroContext {
91
94
}
92
95
93
96
export function createTheme < T extends Theme > ( theme : T ) : StyleFunction < ThemeProperties < T > , 'default' | Extract < keyof T [ 'conditions' ] , string > > {
94
- let themePropertyMap = createValueLookup ( Object . keys ( theme . properties ) , true ) ;
95
- let themeConditionMap = createValueLookup ( Object . keys ( theme . conditions ) , true ) ;
97
+ let themePropertyMap = createValueLookup ( Object . keys ( theme . properties ) ) ;
98
+ let themeConditionMap = createValueLookup ( Object . keys ( theme . conditions ) ) ;
96
99
let propertyFunctions = new Map ( Object . entries ( theme . properties ) . map ( ( [ k , v ] ) => {
97
100
if ( typeof v === 'function' ) {
98
101
return [ k , v ] ;
@@ -159,7 +162,7 @@ export function createTheme<T extends Theme>(theme: T): StyleFunction<ThemePrope
159
162
} else {
160
163
css += ', ' ;
161
164
}
162
- css += generateName ( i , true ) ;
165
+ css += ` ${ PREFIX } ${ generateName ( i ) } ` ;
163
166
}
164
167
css += ';\n\n' ;
165
168
@@ -267,7 +270,7 @@ export function createTheme<T extends Theme>(theme: T): StyleFunction<ThemePrope
267
270
let prelude = theme . conditions [ condition ] || condition ;
268
271
if ( prelude . startsWith ( ':' ) ) {
269
272
return [ {
270
- prelude : `@layer ${ generateName ( priority , true ) } ` ,
273
+ prelude : `@layer ${ PREFIX } ${ generateName ( priority ) } ` ,
271
274
body : rules . map ( rule => ( { prelude : rule . prelude , body : [ { ...rule , prelude : '&' + prelude } ] , condition : '' } ) ) ,
272
275
condition : ''
273
276
} ] ;
@@ -277,7 +280,7 @@ export function createTheme<T extends Theme>(theme: T): StyleFunction<ThemePrope
277
280
return [ {
278
281
// Top level layer is based on the priority of the rule, not the condition.
279
282
// Also group in a sub-layer based on the condition so that lightningcss can more effectively deduplicate rules.
280
- prelude : `@layer ${ generateName ( priority , true ) } .${ themeConditionMap . get ( condition ) || generateArbitraryValueSelector ( condition , true ) } ` ,
283
+ prelude : `@layer ${ PREFIX } ${ generateName ( priority ) } .${ themeConditionMap . get ( condition ) || generateArbitraryValueSelector ( condition , true ) } ` ,
281
284
body : [ { prelude, body : rules , condition : '' } ] ,
282
285
condition : ''
283
286
} ] ;
@@ -367,7 +370,7 @@ interface Rule {
367
370
// This maps to an alphabet containing lower case letters, upper case letters, and numbers.
368
371
// For numbers larger than 62, an underscore is prepended.
369
372
// This encoding allows easy parsing to enable runtime merging by property.
370
- function generateName ( index : number , atStart = false ) : string {
373
+ function generateName ( index : number ) : string {
371
374
if ( index < 26 ) {
372
375
// lower case letters
373
376
return String . fromCharCode ( index + 97 ) ;
@@ -380,11 +383,7 @@ function generateName(index: number, atStart = false): string {
380
383
381
384
if ( index < 62 ) {
382
385
// numbers
383
- let res = String . fromCharCode ( ( index - 52 ) + 48 ) ;
384
- if ( atStart ) {
385
- res = '_' + res ;
386
- }
387
- return res ;
386
+ return String . fromCharCode ( ( index - 52 ) + 48 ) ;
388
387
}
389
388
390
389
return '_' + generateName ( index - 62 ) ;
0 commit comments