Skip to content

Commit

Permalink
css: Fix conditional rule merging bug (#1425)
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis authored Jun 14, 2024
1 parent e50a078 commit b86f3f5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/four-humans-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/css': patch
---

Fixes a bug where declarations with identical selectors would not be merged correctly inside conditional rules
6 changes: 5 additions & 1 deletion packages/css/src/conditionalRulesets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ export class ConditionalRuleset {
const selectors: any = {};

for (const rule of rules) {
selectors[rule.selector] = rule.rule;
selectors[rule.selector] = {
// Preserve existing declarations if a rule with the same selector has already been added
...selectors[rule.selector],
...rule.rule,
};
}

Object.assign(selectors, ...children.renderToArray());
Expand Down
41 changes: 41 additions & 0 deletions packages/css/src/transformCss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,47 @@ describe('transformCss', () => {
`);
});

it('should merge declarations with the same selector when merging conditional rules', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: [],
cssObjs: [
{
type: 'local',
selector: '.testClass',
rule: {
'@layer': {
myLayer: {
color: 'red',
},
},
},
},
{
type: 'local',
selector: '.testClass',
rule: {
'@layer': {
myLayer: {
fontSize: '32px',
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
@layer myLayer;
@layer myLayer {
.testClass {
color: red;
font-size: 32px;
}
}
`);
});

it('should handle simple pseudos', () => {
expect(
transformCss({
Expand Down

0 comments on commit b86f3f5

Please sign in to comment.