Skip to content

Commit

Permalink
Allow @variant to be used at the top-level (#16129)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thecrypticace authored Jan 31, 2025
1 parent 7f1d097 commit 4052eb2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Do not emit `@keyframes` in `@theme reference` ([#16120](https://github.com/tailwindlabs/tailwindcss/pull/16120))
- Discard invalid declarations when parsing CSS ([#16093](https://github.com/tailwindlabs/tailwindcss/pull/16093))
- Do not emit empty CSS rules and at-rules ([#16121](https://github.com/tailwindlabs/tailwindcss/pull/16121))
- Handle `@variant` when at the top-level of a stylesheet ([#16129](https://github.com/tailwindlabs/tailwindcss/pull/16129))

## [4.0.1] - 2025-01-29

Expand Down
64 changes: 64 additions & 0 deletions packages/tailwindcss/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3510,6 +3510,38 @@ describe('@variant', () => {
background: white;
}
}
@variant hover {
@variant landscape {
.btn2 {
color: red;
}
}
}
@variant hover {
.foo {
color: red;
}
@variant landscape {
.bar {
color: blue;
}
}
.baz {
@variant portrait {
color: green;
}
}
}
@media something {
@variant landscape {
@page {
color: red;
}
}
}
`,
[],
),
Expand All @@ -3522,6 +3554,38 @@ describe('@variant', () => {
.btn {
background: #fff;
}
}
@media (hover: hover) {
@media (orientation: landscape) {
:scope:hover .btn2 {
color: red;
}
}
:scope:hover .foo {
color: red;
}
@media (orientation: landscape) {
:scope:hover .bar {
color: #00f;
}
}
@media (orientation: portrait) {
:scope:hover .baz {
color: green;
}
}
}
@media something {
@media (orientation: landscape) {
@page {
color: red;
}
}
}"
`)
})
Expand Down
12 changes: 12 additions & 0 deletions packages/tailwindcss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ async function parseCss(
return WalkAction.Stop
}
})

// No `@slot` found, so this is still a regular `@variant` at-rule
if (node.name === '@variant') {
variantNodes.push(node)
}
}
}

Expand Down Expand Up @@ -429,6 +434,13 @@ async function parseCss(
replaceWith(node.nodes)
}

walk(node.nodes, (node) => {
if (node.kind !== 'at-rule') return
if (node.name !== '@variant') return

variantNodes.push(node)
})

return WalkAction.Skip
}

Expand Down

0 comments on commit 4052eb2

Please sign in to comment.