-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Implement @variant
#15663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement @variant
#15663
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
| // 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) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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}`) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
philipp-spiess
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
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
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: