Skip to content

Commit a98da55

Browse files
authored
fix: prefix S1 style macro class names (#7365)
* prefix s1 style macro class names * lint
1 parent 3a658e7 commit a98da55

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

packages/@react-spectrum/style-macro-s1/src/style-macro.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*/
1212
import type {Condition, CSSProperties, CSSValue, CustomValue, PropertyFunction, PropertyValueDefinition, PropertyValueMap, StyleFunction, StyleValue, Theme, ThemeProperties, Value} from './types';
1313

14+
// Prefix Spectrum 1 style macro classes to avoid name collisions with S2 style macro.
15+
const PREFIX = 's1-';
16+
1417
export function createArbitraryProperty<T extends Value>(fn: (value: T, property: string) => CSSProperties): PropertyFunction<T> {
1518
return (value, property) => {
1619
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
6770
}
6871
}
6972

70-
function createValueLookup(values: Array<CSSValue>, atStart = false) {
73+
function createValueLookup(values: Array<CSSValue>) {
7174
let map = new Map<CSSValue, string>();
7275
for (let value of values) {
7376
if (!map.has(value)) {
74-
map.set(value, generateName(map.size, atStart));
77+
map.set(value, `${PREFIX}${generateName(map.size)}`);
7578
}
7679
}
7780
return map;
@@ -91,8 +94,8 @@ interface MacroContext {
9194
}
9295

9396
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));
9699
let propertyFunctions = new Map(Object.entries(theme.properties).map(([k, v]) => {
97100
if (typeof v === 'function') {
98101
return [k, v];
@@ -159,7 +162,7 @@ export function createTheme<T extends Theme>(theme: T): StyleFunction<ThemePrope
159162
} else {
160163
css += ', ';
161164
}
162-
css += generateName(i, true);
165+
css += `${PREFIX}${generateName(i)}`;
163166
}
164167
css += ';\n\n';
165168

@@ -267,7 +270,7 @@ export function createTheme<T extends Theme>(theme: T): StyleFunction<ThemePrope
267270
let prelude = theme.conditions[condition] || condition;
268271
if (prelude.startsWith(':')) {
269272
return [{
270-
prelude: `@layer ${generateName(priority, true)}`,
273+
prelude: `@layer ${PREFIX}${generateName(priority)}`,
271274
body: rules.map(rule => ({prelude: rule.prelude, body: [{...rule, prelude: '&' + prelude}], condition: ''})),
272275
condition: ''
273276
}];
@@ -277,7 +280,7 @@ export function createTheme<T extends Theme>(theme: T): StyleFunction<ThemePrope
277280
return [{
278281
// Top level layer is based on the priority of the rule, not the condition.
279282
// 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)}`,
281284
body: [{prelude, body: rules, condition: ''}],
282285
condition: ''
283286
}];
@@ -367,7 +370,7 @@ interface Rule {
367370
// This maps to an alphabet containing lower case letters, upper case letters, and numbers.
368371
// For numbers larger than 62, an underscore is prepended.
369372
// 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 {
371374
if (index < 26) {
372375
// lower case letters
373376
return String.fromCharCode(index + 97);
@@ -380,11 +383,7 @@ function generateName(index: number, atStart = false): string {
380383

381384
if (index < 62) {
382385
// 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);
388387
}
389388

390389
return '_' + generateName(index - 62);

0 commit comments

Comments
 (0)