Skip to content

Commit b86f3f5

Browse files
authored
css: Fix conditional rule merging bug (#1425)
1 parent e50a078 commit b86f3f5

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

.changeset/four-humans-hammer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/css': patch
3+
---
4+
5+
Fixes a bug where declarations with identical selectors would not be merged correctly inside conditional rules

packages/css/src/conditionalRulesets.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ export class ConditionalRuleset {
196196
const selectors: any = {};
197197

198198
for (const rule of rules) {
199-
selectors[rule.selector] = rule.rule;
199+
selectors[rule.selector] = {
200+
// Preserve existing declarations if a rule with the same selector has already been added
201+
...selectors[rule.selector],
202+
...rule.rule,
203+
};
200204
}
201205

202206
Object.assign(selectors, ...children.renderToArray());

packages/css/src/transformCss.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,47 @@ describe('transformCss', () => {
549549
`);
550550
});
551551

552+
it('should merge declarations with the same selector when merging conditional rules', () => {
553+
expect(
554+
transformCss({
555+
composedClassLists: [],
556+
localClassNames: [],
557+
cssObjs: [
558+
{
559+
type: 'local',
560+
selector: '.testClass',
561+
rule: {
562+
'@layer': {
563+
myLayer: {
564+
color: 'red',
565+
},
566+
},
567+
},
568+
},
569+
{
570+
type: 'local',
571+
selector: '.testClass',
572+
rule: {
573+
'@layer': {
574+
myLayer: {
575+
fontSize: '32px',
576+
},
577+
},
578+
},
579+
},
580+
],
581+
}).join('\n'),
582+
).toMatchInlineSnapshot(`
583+
@layer myLayer;
584+
@layer myLayer {
585+
.testClass {
586+
color: red;
587+
font-size: 32px;
588+
}
589+
}
590+
`);
591+
});
592+
552593
it('should handle simple pseudos', () => {
553594
expect(
554595
transformCss({

0 commit comments

Comments
 (0)