Skip to content

Commit b11db0e

Browse files
authored
Merge pull request tailwindlabs#1024 from tailwindcss/first-last-child-variants
Add first-child and last-child variants
2 parents d58945c + 78554a3 commit b11db0e

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 first-child variants', () => {
139+
const input = `
140+
@variants first {
141+
.banana { color: yellow; }
142+
.chocolate { color: brown; }
143+
}
144+
`
145+
146+
const output = `
147+
.banana { color: yellow; }
148+
.chocolate { color: brown; }
149+
.first\\:banana:first-child { color: yellow; }
150+
.first\\:chocolate:first-child { 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 last-child variants', () => {
160+
const input = `
161+
@variants last {
162+
.banana { color: yellow; }
163+
.chocolate { color: brown; }
164+
}
165+
`
166+
167+
const output = `
168+
.banana { color: yellow; }
169+
.chocolate { color: brown; }
170+
.last\\:banana:last-child { color: yellow; }
171+
.last\\:chocolate:last-child { 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,12 +3,12 @@ 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, selectorPrefix = pseudoClass) {
77
return generateVariantFunction(({ modifySelectors, separator }) => {
88
return modifySelectors(({ selector }) => {
99
return selectorParser(selectors => {
1010
selectors.walkClasses(sel => {
11-
sel.value = `${pseudoClass}${separator}${sel.value}`
11+
sel.value = `${selectorPrefix}${separator}${sel.value}`
1212
sel.parent.insertAfter(sel, selectorParser.pseudo({ value: `:${pseudoClass}` }))
1313
})
1414
}).processSync(selector)
@@ -38,6 +38,8 @@ const defaultVariantGenerators = {
3838
active: generatePseudoClassVariant('active'),
3939
visited: generatePseudoClassVariant('visited'),
4040
disabled: generatePseudoClassVariant('disabled'),
41+
first: generatePseudoClassVariant('first-child', 'first'),
42+
last: generatePseudoClassVariant('last-child', 'last'),
4143
}
4244

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

0 commit comments

Comments
 (0)