Skip to content

Commit

Permalink
Allow users to customize default variant placement
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Feb 15, 2019
1 parent 42fcc06 commit c0ec2c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
27 changes: 27 additions & 0 deletions __tests__/variantsAtRule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,33 @@ test('variants are generated in the order specified', () => {
})
})

test('the default variant can be generated in a specified position', () => {
const input = `
@variants focus, active, default, hover {
.banana { color: yellow; }
.chocolate { color: brown; }
}
`

const output = `
.focus\\:banana:focus { color: yellow; }
.focus\\:chocolate:focus { color: brown; }
.active\\:banana:active { color: yellow; }
.active\\:chocolate:active { color: brown; }
.banana { color: yellow; }
.chocolate { color: brown; }
.hover\\:banana:hover { color: yellow; }
.hover\\:chocolate:hover { color: brown; }
`

return run(input, {
...config,
}).then(result => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

test('plugin variants can modify rules using the raw PostCSS API', () => {
const input = `
@variants important {
Expand Down
9 changes: 6 additions & 3 deletions src/lib/substituteVariantsAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ function generatePseudoClassVariant(pseudoClass) {
})
}

function ensureIncludesDefault(variants) {
return variants.includes('default') ? variants : ['default', ...variants]
}

const defaultVariantGenerators = {
default: generateVariantFunction(() => {}),
'group-hover': generateVariantFunction(({ modifySelectors, separator }) => {
return modifySelectors(({ className }) => {
return `.group:hover .group-hover${separator}${className}`
Expand Down Expand Up @@ -38,9 +43,7 @@ export default function(config, { variantGenerators: pluginVariantGenerators })
responsiveParent.append(atRule)
}

atRule.before(atRule.clone().nodes)

_.forEach(_.without(variants, 'responsive'), variant => {
_.forEach(_.without(ensureIncludesDefault(variants), 'responsive'), variant => {
variantGenerators[variant](atRule, config)
})

Expand Down

0 comments on commit c0ec2c5

Please sign in to comment.