Skip to content

Commit 43e75ac

Browse files
committed
Prevent atomizing :root rules
1 parent a4ea7c4 commit 43e75ac

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

packages/css-engine/src/core/atomic.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { NestingRule } from "./rules";
44
import { toValue, type TransformValue } from "./to-value";
55

66
type Options = {
7-
getKey: (rule: NestingRule) => string;
7+
/** in case of undefined the rule will not be split into atomics */
8+
getKey: (rule: NestingRule) => string | undefined;
89
transformValue?: TransformValue;
910
classes?: Map<string, string[]>;
1011
};
@@ -16,6 +17,10 @@ export const generateAtomic = (sheet: StyleSheet, options: Options) => {
1617
for (const rule of sheet.nestingRules.values()) {
1718
const descendantSuffix = rule.getDescendantSuffix();
1819
const groupKey = getKey(rule);
20+
if (groupKey === undefined) {
21+
atomicRules.set(rule.getSelector(), rule);
22+
continue;
23+
}
1924
// a few rules can be in the same group
2025
// when rule have descendant suffix
2126
let classList = classes.get(groupKey);

packages/react-sdk/src/css/css.test.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ Map {
517517
});
518518

519519
test("generate :root preset and user styles", () => {
520-
const { cssText, classes } = generateAllCss({
520+
const { cssText, atomicCssText, classes, atomicClasses } = generateAllCss({
521521
assets: new Map(),
522522
...renderJsx(
523523
<$.Body ws:id="body">
@@ -538,6 +538,15 @@ test("generate :root preset and user styles", () => {
538538
value: { type: "keyword", value: "blue" },
539539
},
540540
],
541+
[
542+
"local:base:fontSize",
543+
{
544+
styleSourceId: "local",
545+
breakpointId: "base",
546+
property: "fontSize",
547+
value: { type: "keyword", value: "medium" },
548+
},
549+
],
541550
]),
542551
componentMetas: new Map([
543552
[
@@ -567,9 +576,24 @@ test("generate :root preset and user styles", () => {
567576
}
568577
@media all {
569578
:root {
570-
color: blue
579+
color: blue;
580+
font-size: medium
571581
}
572582
}"
573583
`);
574584
expect(classes).toEqual(new Map());
585+
expect(atomicCssText).toMatchInlineSnapshot(`
586+
"@media all {
587+
:root {
588+
display: grid
589+
}
590+
}
591+
@media all {
592+
:root {
593+
color: blue;
594+
font-size: medium
595+
}
596+
}"
597+
`);
598+
expect(atomicClasses).toEqual(new Map());
575599
});

packages/react-sdk/src/css/css.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export const generateCss = ({
154154
if (instanceId === ROOT_INSTANCE_ID) {
155155
const rule = sheet.addNestingRule(`:root`);
156156
rule.applyMixins(values);
157-
instanceByRule.set(rule, instanceId);
157+
// avoid storing in instanceByRule to prevent conversion into atomic styles
158158
continue;
159159
}
160160
let descendantSuffix = "";
@@ -187,7 +187,7 @@ export const generateCss = ({
187187

188188
if (atomic) {
189189
const { cssText } = generateAtomic(sheet, {
190-
getKey: (rule) => instanceByRule.get(rule) ?? "",
190+
getKey: (rule) => instanceByRule.get(rule),
191191
transformValue: imageValueTransformer,
192192
classes,
193193
});

0 commit comments

Comments
 (0)