Skip to content

Commit 7dc0261

Browse files
committed
Only wrap with variants if rules don't already contain any variants
1 parent 3f6d31e commit 7dc0261

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

__tests__/processPlugins.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,33 @@ test('plugins can use the array shorthand to add variants to components', () =>
12611261
`)
12621262
})
12631263

1264+
test('components that add variants manually do not add an extra variants wrapper', () => {
1265+
const { components } = processPlugins(
1266+
[
1267+
function({ addComponents }) {
1268+
addComponents({
1269+
'@variants responsive': {
1270+
'.btn-blue': {
1271+
backgroundColor: 'blue',
1272+
},
1273+
},
1274+
})
1275+
},
1276+
],
1277+
makeConfig()
1278+
)
1279+
1280+
expect(css(components)).toMatchCss(`
1281+
@layer components {
1282+
@variants responsive {
1283+
.btn-blue {
1284+
background-color: blue
1285+
}
1286+
}
1287+
}
1288+
`)
1289+
})
1290+
12641291
test("component declarations are not affected by the 'important' option", () => {
12651292
const { components } = processPlugins(
12661293
[

src/util/wrapWithVariants.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import postcss from 'postcss'
22
import cloneNodes from './cloneNodes'
33

44
export default function wrapWithVariants(rules, variants) {
5+
let foundVariantAtRule = false
6+
7+
postcss.root({ nodes: rules }).walkAtRules('variants', () => {
8+
foundVariantAtRule = true
9+
})
10+
11+
if (foundVariantAtRule) {
12+
return cloneNodes(rules)
13+
}
14+
515
return postcss
616
.atRule({
717
name: 'variants',

0 commit comments

Comments
 (0)