Skip to content

Commit

Permalink
Merge pull request #970 from tailwindcss/support-pseudo-elements-with…
Browse files Browse the repository at this point in the history
…-variants

Support built-in variants for utilities that include pseudo-elements
  • Loading branch information
adamwathan committed Jul 6, 2019
2 parents b800a69 + a80720a commit da24fba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
22 changes: 22 additions & 0 deletions __tests__/variantsAtRule.test.js
Expand Up @@ -218,6 +218,28 @@ test('variants are generated in the order specified', () => {
})
})

test('the built-in variant pseudo-selectors are appended before any pseudo-elements', () => {
const input = `
@variants hover, focus-within, focus, active, group-hover {
.placeholder-yellow::placeholder { color: yellow; }
}
`

const output = `
.placeholder-yellow::placeholder { color: yellow; }
.hover\\:placeholder-yellow:hover::placeholder { color: yellow; }
.focus-within\\:placeholder-yellow:focus-within::placeholder { color: yellow; }
.focus\\:placeholder-yellow:focus::placeholder { color: yellow; }
.active\\:placeholder-yellow:active::placeholder { color: yellow; }
.group:hover .group-hover\\:placeholder-yellow::placeholder { color: yellow; }
`

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

test('the default variant can be generated in a specified position', () => {
const input = `
@variants focus, active, default, hover {
Expand Down
20 changes: 15 additions & 5 deletions src/lib/substituteVariantsAtRules.js
@@ -1,12 +1,17 @@
import _ from 'lodash'
import postcss from 'postcss'
import selectorParser from 'postcss-selector-parser'
import generateVariantFunction from '../util/generateVariantFunction'
import e from '../util/escapeClassName'

function generatePseudoClassVariant(pseudoClass) {
return generateVariantFunction(({ modifySelectors, separator }) => {
return modifySelectors(({ className }) => {
return `.${e(`${pseudoClass}${separator}${className}`)}:${pseudoClass}`
return modifySelectors(({ selector }) => {
return selectorParser(selectors => {
selectors.walkClasses(sel => {
sel.value = `${pseudoClass}${separator}${sel.value}`
sel.parent.insertAfter(sel, selectorParser.pseudo({ value: `:${pseudoClass}` }))
})
}).processSync(selector)
})
})
}
Expand All @@ -18,8 +23,13 @@ function ensureIncludesDefault(variants) {
const defaultVariantGenerators = {
default: generateVariantFunction(() => {}),
'group-hover': generateVariantFunction(({ modifySelectors, separator }) => {
return modifySelectors(({ className }) => {
return `.group:hover .${e(`group-hover${separator}${className}`)}`
return modifySelectors(({ selector }) => {
return selectorParser(selectors => {
selectors.walkClasses(sel => {
sel.value = `group-hover${separator}${sel.value}`
sel.parent.insertBefore(sel, selectorParser().astSync('.group:hover '))
})
}).processSync(selector)
})
}),
hover: generatePseudoClassVariant('hover'),
Expand Down

0 comments on commit da24fba

Please sign in to comment.