Merge pull request #1024 from tailwindcss/first-last-child-variants
Add first-child and last-child variants
This commit is contained in:
commit
b11db0e06e
2 changed files with 46 additions and 2 deletions
|
|
@ -135,6 +135,48 @@ test('it can generate focus-within variants', () => {
|
|||
})
|
||||
})
|
||||
|
||||
test('it can generate first-child variants', () => {
|
||||
const input = `
|
||||
@variants first {
|
||||
.banana { color: yellow; }
|
||||
.chocolate { color: brown; }
|
||||
}
|
||||
`
|
||||
|
||||
const output = `
|
||||
.banana { color: yellow; }
|
||||
.chocolate { color: brown; }
|
||||
.first\\:banana:first-child { color: yellow; }
|
||||
.first\\:chocolate:first-child { color: brown; }
|
||||
`
|
||||
|
||||
return run(input).then(result => {
|
||||
expect(result.css).toMatchCss(output)
|
||||
expect(result.warnings().length).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
test('it can generate last-child variants', () => {
|
||||
const input = `
|
||||
@variants last {
|
||||
.banana { color: yellow; }
|
||||
.chocolate { color: brown; }
|
||||
}
|
||||
`
|
||||
|
||||
const output = `
|
||||
.banana { color: yellow; }
|
||||
.chocolate { color: brown; }
|
||||
.last\\:banana:last-child { color: yellow; }
|
||||
.last\\:chocolate:last-child { color: brown; }
|
||||
`
|
||||
|
||||
return run(input).then(result => {
|
||||
expect(result.css).toMatchCss(output)
|
||||
expect(result.warnings().length).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
test('it can generate group-hover variants', () => {
|
||||
const input = `
|
||||
@variants group-hover {
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ import postcss from 'postcss'
|
|||
import selectorParser from 'postcss-selector-parser'
|
||||
import generateVariantFunction from '../util/generateVariantFunction'
|
||||
|
||||
function generatePseudoClassVariant(pseudoClass) {
|
||||
function generatePseudoClassVariant(pseudoClass, selectorPrefix = pseudoClass) {
|
||||
return generateVariantFunction(({ modifySelectors, separator }) => {
|
||||
return modifySelectors(({ selector }) => {
|
||||
return selectorParser(selectors => {
|
||||
selectors.walkClasses(sel => {
|
||||
sel.value = `${pseudoClass}${separator}${sel.value}`
|
||||
sel.value = `${selectorPrefix}${separator}${sel.value}`
|
||||
sel.parent.insertAfter(sel, selectorParser.pseudo({ value: `:${pseudoClass}` }))
|
||||
})
|
||||
}).processSync(selector)
|
||||
|
|
@ -38,6 +38,8 @@ const defaultVariantGenerators = {
|
|||
active: generatePseudoClassVariant('active'),
|
||||
visited: generatePseudoClassVariant('visited'),
|
||||
disabled: generatePseudoClassVariant('disabled'),
|
||||
first: generatePseudoClassVariant('first-child', 'first'),
|
||||
last: generatePseudoClassVariant('last-child', 'last'),
|
||||
}
|
||||
|
||||
export default function(config, { variantGenerators: pluginVariantGenerators }) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue