Skip to content

Commit c21a0d7

Browse files
committed
Append variant pseudo-selectors after classes instead of replacing entire selector
1 parent ca670f5 commit c21a0d7

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

__tests__/variantsAtRule.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,27 @@ test('variants are generated in the order specified', () => {
218218
})
219219
})
220220

221+
test('the built-in variant pseudo-selectors are appended before any pseudo-elements', () => {
222+
const input = `
223+
@variants hover, focus-within, focus, active {
224+
.placeholder-yellow::placeholder { color: yellow; }
225+
}
226+
`
227+
228+
const output = `
229+
.placeholder-yellow::placeholder { color: yellow; }
230+
.hover\\:placeholder-yellow:hover::placeholder { color: yellow; }
231+
.focus-within\\:placeholder-yellow:focus-within::placeholder { color: yellow; }
232+
.focus\\:placeholder-yellow:focus::placeholder { color: yellow; }
233+
.active\\:placeholder-yellow:active::placeholder { color: yellow; }
234+
`
235+
236+
return run(input).then(result => {
237+
expect(result.css).toMatchCss(output)
238+
expect(result.warnings().length).toBe(0)
239+
})
240+
})
241+
221242
test('the default variant can be generated in a specified position', () => {
222243
const input = `
223244
@variants focus, active, default, hover {

src/lib/substituteVariantsAtRules.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import _ from 'lodash'
22
import postcss from 'postcss'
3+
import selectorParser from 'postcss-selector-parser'
34
import generateVariantFunction from '../util/generateVariantFunction'
45
import e from '../util/escapeClassName'
56

67
function generatePseudoClassVariant(pseudoClass) {
78
return generateVariantFunction(({ modifySelectors, separator }) => {
8-
return modifySelectors(({ className }) => {
9-
return `.${e(`${pseudoClass}${separator}${className}`)}:${pseudoClass}`
9+
return modifySelectors(({ selector }) => {
10+
return selectorParser(selectors => {
11+
selectors.walkClasses(sel => {
12+
sel.value = `${pseudoClass}${separator}${sel.value}`
13+
sel.parent.insertAfter(sel, selectorParser.pseudo({ value: `:${pseudoClass}` }))
14+
})
15+
}).processSync(selector)
1016
})
1117
})
1218
}

0 commit comments

Comments
 (0)