Skip to content

Commit 4052eb2

Browse files
Allow @variant to be used at the top-level (#16129)
This makes it so `@variant` is replaced at the top level and not just within rules. This also fixes a bug where `@variant` wasn't handled when inside an `@media` at-rule.
1 parent 7f1d097 commit 4052eb2

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Do not emit `@keyframes` in `@theme reference` ([#16120](https://github.com/tailwindlabs/tailwindcss/pull/16120))
2222
- Discard invalid declarations when parsing CSS ([#16093](https://github.com/tailwindlabs/tailwindcss/pull/16093))
2323
- Do not emit empty CSS rules and at-rules ([#16121](https://github.com/tailwindlabs/tailwindcss/pull/16121))
24+
- Handle `@variant` when at the top-level of a stylesheet ([#16129](https://github.com/tailwindlabs/tailwindcss/pull/16129))
2425

2526
## [4.0.1] - 2025-01-29
2627

packages/tailwindcss/src/index.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -3510,6 +3510,38 @@ describe('@variant', () => {
35103510
background: white;
35113511
}
35123512
}
3513+
3514+
@variant hover {
3515+
@variant landscape {
3516+
.btn2 {
3517+
color: red;
3518+
}
3519+
}
3520+
}
3521+
3522+
@variant hover {
3523+
.foo {
3524+
color: red;
3525+
}
3526+
@variant landscape {
3527+
.bar {
3528+
color: blue;
3529+
}
3530+
}
3531+
.baz {
3532+
@variant portrait {
3533+
color: green;
3534+
}
3535+
}
3536+
}
3537+
3538+
@media something {
3539+
@variant landscape {
3540+
@page {
3541+
color: red;
3542+
}
3543+
}
3544+
}
35133545
`,
35143546
[],
35153547
),
@@ -3522,6 +3554,38 @@ describe('@variant', () => {
35223554
.btn {
35233555
background: #fff;
35243556
}
3557+
}
3558+
3559+
@media (hover: hover) {
3560+
@media (orientation: landscape) {
3561+
:scope:hover .btn2 {
3562+
color: red;
3563+
}
3564+
}
3565+
3566+
:scope:hover .foo {
3567+
color: red;
3568+
}
3569+
3570+
@media (orientation: landscape) {
3571+
:scope:hover .bar {
3572+
color: #00f;
3573+
}
3574+
}
3575+
3576+
@media (orientation: portrait) {
3577+
:scope:hover .baz {
3578+
color: green;
3579+
}
3580+
}
3581+
}
3582+
3583+
@media something {
3584+
@media (orientation: landscape) {
3585+
@page {
3586+
color: red;
3587+
}
3588+
}
35253589
}"
35263590
`)
35273591
})

packages/tailwindcss/src/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ async function parseCss(
244244
return WalkAction.Stop
245245
}
246246
})
247+
248+
// No `@slot` found, so this is still a regular `@variant` at-rule
249+
if (node.name === '@variant') {
250+
variantNodes.push(node)
251+
}
247252
}
248253
}
249254

@@ -429,6 +434,13 @@ async function parseCss(
429434
replaceWith(node.nodes)
430435
}
431436

437+
walk(node.nodes, (node) => {
438+
if (node.kind !== 'at-rule') return
439+
if (node.name !== '@variant') return
440+
441+
variantNodes.push(node)
442+
})
443+
432444
return WalkAction.Skip
433445
}
434446

0 commit comments

Comments
 (0)