Skip to content

Commit

Permalink
Only wrap with variants if rules don't already contain any variants
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Jul 15, 2020
1 parent 3f6d31e commit 7dc0261
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
27 changes: 27 additions & 0 deletions __tests__/processPlugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,33 @@ test('plugins can use the array shorthand to add variants to components', () =>
`)
})

test('components that add variants manually do not add an extra variants wrapper', () => {
const { components } = processPlugins(
[
function({ addComponents }) {
addComponents({
'@variants responsive': {
'.btn-blue': {
backgroundColor: 'blue',
},
},
})
},
],
makeConfig()
)

expect(css(components)).toMatchCss(`
@layer components {
@variants responsive {
.btn-blue {
background-color: blue
}
}
}
`)
})

test("component declarations are not affected by the 'important' option", () => {
const { components } = processPlugins(
[
Expand Down
10 changes: 10 additions & 0 deletions src/util/wrapWithVariants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import postcss from 'postcss'
import cloneNodes from './cloneNodes'

export default function wrapWithVariants(rules, variants) {
let foundVariantAtRule = false

postcss.root({ nodes: rules }).walkAtRules('variants', () => {
foundVariantAtRule = true
})

if (foundVariantAtRule) {
return cloneNodes(rules)
}

return postcss
.atRule({
name: 'variants',
Expand Down

0 comments on commit 7dc0261

Please sign in to comment.