Implement @variant#15663
Merged
Merged
Conversation
When the CSS contains a style rule that only has a selector of `&`, then
that node can be flattened by moving all the children into the parent.
```css
.foo {
& {
& {
.bar {
color: red;
}
}
}
}
```
Becomes:
```css
.foo {
.bar {
color: red;
}
}
```
6a102d0 to
bab1ee3
Compare
RobinMalfait
commented
Jan 20, 2025
Comment on lines
+259
to
+266
| // Rules with `&` as the selector should be flattened | ||
| if (node.selector === '&') { | ||
| for (let child of node.nodes) { | ||
| let nodes: AstNode[] = [] | ||
| transform(child, nodes, depth + 1) | ||
| parent.push(...nodes) | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
Just an optimization to flatten:
.foo {
& {
color: red;
}
}Into:
.foo {
color: red;
}Which is more relevant when introducing this new @variant you can use.
| let variantAst = designSystem.parseVariant(variant) | ||
| if (variantAst === null) { | ||
| throw new Error(`Cannot use \`@variant\` with unknown variant: ${variant}`) | ||
| } |
Contributor
There was a problem hiding this comment.
Could someone theoretically register a hover:focus variant through configs and then try using that with @variant?
We might want explicitly to error on uses of : if that's possible
Member
Author
There was a problem hiding this comment.
So you can technically add a custom variant with a :, but you will never be able to use it because we split it on : anyway.
Added some validation (relatively strict) which we can loosen up down the line if needed.
thecrypticace
approved these changes
Jan 20, 2025
thecrypticace
approved these changes
Jan 21, 2025
thecrypticace
added a commit
to tailwindlabs/tailwindcss-intellisense
that referenced
this pull request
Jan 21, 2025
This adds support for `@custom-variant`, adds variant suggestions for `@variant`, and tweaks when at-rules are suggested such that only ones that are valid in the given context should be suggested (e.g. when at the root or when nested in some rule). This is the IntelliSense portion of tailwindlabs/tailwindcss#15663
rozsazoltan
added a commit
to rozsazoltan/rozsazoltan.vercel.app
that referenced
this pull request
Sep 22, 2025
This was referenced Apr 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR replaces
@variantwith@custom-variantfor registering custom variants via your CSS.In addition, this PR introduces
@variantthat can be used in your CSS to use a variant while writing custom CSS.E.g.:
Compiles to:
For backwards compatibility, the
@variantrules that don't have a body and aredefined inline:
And
@variantrules that are defined with a body and a@slot:Will automatically be upgraded to
@custom-variantinternally, so no breaking changes are introduced with this PR.TODO:
Decide whether we want to allow multiple variants and if so, what syntax should be used. If not, nestingOnly a single@variant <variant> {}will be the way to go.@variant <variant>can be used, if you want to use multiple, nesting should be used: