Skip to content

Commit 1124d1d

Browse files
Add nth-* variants (tailwindlabs#13661)
* Add `nth-*` variants * Update changelog * Update snapshots * Update * update older `toMatchInlineSnapshot` to `toEqual` --------- Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
1 parent 2428f8f commit 1124d1d

File tree

5 files changed

+142
-1
lines changed

5 files changed

+142
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Use `length` data type for `background-size` instead of `background-position` ([#13771](https://github.com/tailwindlabs/tailwindcss/pull/13771))
1313
- Support negative values for `{col,row}-{start,end}` utilities ([#13780](https://github.com/tailwindlabs/tailwindcss/pull/13780))
1414

15+
### Added
16+
17+
- Add `nth-*` variants ([#13661](https://github.com/tailwindlabs/tailwindcss/pull/13661))
18+
1519
## [4.0.0-alpha.15] - 2024-05-08
1620

1721
### Fixed

packages/tailwindcss/src/__snapshots__/intellisense.test.ts.snap

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,10 @@ exports[`getVariants 1`] = `
19331933
"has",
19341934
"aria",
19351935
"data",
1936+
"nth",
1937+
"nth-last",
1938+
"nth-of-type",
1939+
"nth-last-of-type",
19361940
"ltr",
19371941
"rtl",
19381942
],
@@ -1980,6 +1984,10 @@ exports[`getVariants 1`] = `
19801984
"has",
19811985
"aria",
19821986
"data",
1987+
"nth",
1988+
"nth-last",
1989+
"nth-of-type",
1990+
"nth-last-of-type",
19831991
"ltr",
19841992
"rtl",
19851993
],
@@ -2307,6 +2315,10 @@ exports[`getVariants 1`] = `
23072315
"has",
23082316
"aria",
23092317
"data",
2318+
"nth",
2319+
"nth-last",
2320+
"nth-of-type",
2321+
"nth-last-of-type",
23102322
"ltr",
23112323
"rtl",
23122324
],
@@ -2335,6 +2347,34 @@ exports[`getVariants 1`] = `
23352347
"selectors": [Function],
23362348
"values": [],
23372349
},
2350+
{
2351+
"hasDash": true,
2352+
"isArbitrary": true,
2353+
"name": "nth",
2354+
"selectors": [Function],
2355+
"values": [],
2356+
},
2357+
{
2358+
"hasDash": true,
2359+
"isArbitrary": true,
2360+
"name": "nth-last",
2361+
"selectors": [Function],
2362+
"values": [],
2363+
},
2364+
{
2365+
"hasDash": true,
2366+
"isArbitrary": true,
2367+
"name": "nth-of-type",
2368+
"selectors": [Function],
2369+
"values": [],
2370+
},
2371+
{
2372+
"hasDash": true,
2373+
"isArbitrary": true,
2374+
"name": "nth-last-of-type",
2375+
"selectors": [Function],
2376+
"values": [],
2377+
},
23382378
{
23392379
"hasDash": true,
23402380
"isArbitrary": true,

packages/tailwindcss/src/utilities.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6253,7 +6253,7 @@ test('divide-color', () => {
62536253
'-divide-[#0088cc]/[0.5]',
62546254
'-divide-[#0088cc]/[50%]',
62556255
]),
6256-
).toMatchInlineSnapshot(`""`)
6256+
).toEqual('')
62576257
})
62586258

62596259
test('place-self', () => {

packages/tailwindcss/src/variants.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,67 @@ test('forced-colors', () => {
15941594
`)
15951595
})
15961596

1597+
test('nth', () => {
1598+
expect(
1599+
run([
1600+
'nth-3:flex',
1601+
'nth-[2n+1]:flex',
1602+
'nth-[2n+1_of_.foo]:flex',
1603+
'nth-last-3:flex',
1604+
'nth-last-[2n+1]:flex',
1605+
'nth-last-[2n+1_of_.foo]:flex',
1606+
'nth-of-type-3:flex',
1607+
'nth-of-type-[2n+1]:flex',
1608+
'nth-last-of-type-3:flex',
1609+
'nth-last-of-type-[2n+1]:flex',
1610+
]),
1611+
).toMatchInlineSnapshot(`
1612+
".nth-3\\:flex:nth-child(3) {
1613+
display: flex;
1614+
}
1615+
1616+
.nth-\\[2n\\+1\\]\\:flex:nth-child(odd) {
1617+
display: flex;
1618+
}
1619+
1620+
.nth-\\[2n\\+1_of_\\.foo\\]\\:flex:nth-child(odd of .foo) {
1621+
display: flex;
1622+
}
1623+
1624+
.nth-last-3\\:flex:nth-last-child(3) {
1625+
display: flex;
1626+
}
1627+
1628+
.nth-last-\\[2n\\+1\\]\\:flex:nth-last-child(odd) {
1629+
display: flex;
1630+
}
1631+
1632+
.nth-last-\\[2n\\+1_of_\\.foo\\]\\:flex:nth-last-child(odd of .foo) {
1633+
display: flex;
1634+
}
1635+
1636+
.nth-of-type-3\\:flex:nth-of-type(3) {
1637+
display: flex;
1638+
}
1639+
1640+
.nth-of-type-\\[2n\\+1\\]\\:flex:nth-of-type(odd) {
1641+
display: flex;
1642+
}
1643+
1644+
.nth-last-of-type-3\\:flex:nth-last-of-type(3) {
1645+
display: flex;
1646+
}
1647+
1648+
.nth-last-of-type-\\[2n\\+1\\]\\:flex:nth-last-of-type(odd) {
1649+
display: flex;
1650+
}"
1651+
`)
1652+
1653+
expect(
1654+
run(['nth-foo:flex', 'nth-of-type-foo:flex', 'nth-last-foo:flex', 'nth-last-of-type-foo:flex']),
1655+
).toEqual('')
1656+
})
1657+
15971658
test('container queries', () => {
15981659
expect(
15991660
compileCss(

packages/tailwindcss/src/variants.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,42 @@ export function createVariants(theme: Theme): Variants {
373373
ruleNode.nodes = [rule(`&[data-${variant.value.value}]`, ruleNode.nodes)]
374374
})
375375

376+
variants.functional('nth', (ruleNode, variant) => {
377+
if (variant.value === null) return null
378+
379+
// Only numeric bare values are allowed
380+
if (variant.value.kind === 'named' && Number.isNaN(Number(variant.value.value))) return null
381+
382+
ruleNode.nodes = [rule(`&:nth-child(${variant.value.value})`, ruleNode.nodes)]
383+
})
384+
385+
variants.functional('nth-last', (ruleNode, variant) => {
386+
if (variant.value === null) return null
387+
388+
// Only numeric bare values are allowed
389+
if (variant.value.kind === 'named' && Number.isNaN(Number(variant.value.value))) return null
390+
391+
ruleNode.nodes = [rule(`&:nth-last-child(${variant.value.value})`, ruleNode.nodes)]
392+
})
393+
394+
variants.functional('nth-of-type', (ruleNode, variant) => {
395+
if (variant.value === null) return null
396+
397+
// Only numeric bare values are allowed
398+
if (variant.value.kind === 'named' && Number.isNaN(Number(variant.value.value))) return null
399+
400+
ruleNode.nodes = [rule(`&:nth-of-type(${variant.value.value})`, ruleNode.nodes)]
401+
})
402+
403+
variants.functional('nth-last-of-type', (ruleNode, variant) => {
404+
if (variant.value === null) return null
405+
406+
// Only numeric bare values are allowed
407+
if (variant.value.kind === 'named' && Number.isNaN(Number(variant.value.value))) return null
408+
409+
ruleNode.nodes = [rule(`&:nth-last-of-type(${variant.value.value})`, ruleNode.nodes)]
410+
})
411+
376412
variants.functional(
377413
'supports',
378414
(ruleNode, variant) => {

0 commit comments

Comments
 (0)