Skip to content

Commit d4aac29

Browse files
author
Cody
committed
add odd and even nth child pseudo selectors
1 parent c9bba9d commit d4aac29

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

__tests__/variantsAtRule.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,48 @@ test('it can generate focus-within variants', () => {
135135
})
136136
})
137137

138+
test('it can generate odd-child variants', () => {
139+
const input = `
140+
@variants odd-child {
141+
.banana { color: yellow; }
142+
.chocolate { color: brown; }
143+
}
144+
`
145+
146+
const output = `
147+
.banana { color: yellow; }
148+
.chocolate { color: brown; }
149+
.odd-child\\:banana:nth-child(odd) { color: yellow; }
150+
.odd-child\\:chocolate:nth-child(odd) { color: brown; }
151+
`
152+
153+
return run(input).then(result => {
154+
expect(result.css).toMatchCss(output)
155+
expect(result.warnings().length).toBe(0)
156+
})
157+
})
158+
159+
test('it can generate even-child variants', () => {
160+
const input = `
161+
@variants even-child {
162+
.banana { color: yellow; }
163+
.chocolate { color: brown; }
164+
}
165+
`
166+
167+
const output = `
168+
.banana { color: yellow; }
169+
.chocolate { color: brown; }
170+
.even-child\\:banana:nth-child(even) { color: yellow; }
171+
.even-child\\:chocolate:nth-child(even) { color: brown; }
172+
`
173+
174+
return run(input).then(result => {
175+
expect(result.css).toMatchCss(output)
176+
expect(result.warnings().length).toBe(0)
177+
})
178+
})
179+
138180
test('it can generate group-hover variants', () => {
139181
const input = `
140182
@variants group-hover {

src/lib/substituteVariantsAtRules.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import postcss from 'postcss'
33
import selectorParser from 'postcss-selector-parser'
44
import generateVariantFunction from '../util/generateVariantFunction'
55

6-
function generatePseudoClassVariant(pseudoClass) {
6+
function generatePseudoClassVariant(pseudoClass, pseudoSelector = pseudoClass) {
77
return generateVariantFunction(({ modifySelectors, separator }) => {
88
return modifySelectors(({ selector }) => {
99
return selectorParser(selectors => {
1010
selectors.walkClasses(sel => {
1111
sel.value = `${pseudoClass}${separator}${sel.value}`
12-
sel.parent.insertAfter(sel, selectorParser.pseudo({ value: `:${pseudoClass}` }))
12+
sel.parent.insertAfter(sel, selectorParser.pseudo({ value: `:${pseudoSelector}` }))
1313
})
1414
}).processSync(selector)
1515
})
@@ -38,6 +38,8 @@ const defaultVariantGenerators = {
3838
active: generatePseudoClassVariant('active'),
3939
visited: generatePseudoClassVariant('visited'),
4040
disabled: generatePseudoClassVariant('disabled'),
41+
'odd-child': generatePseudoClassVariant('odd-child', 'nth-child(odd)'),
42+
'even-child': generatePseudoClassVariant('even-child', 'nth-child(even)'),
4143
}
4244

4345
export default function(config, { variantGenerators: pluginVariantGenerators }) {

0 commit comments

Comments
 (0)