Skip to content

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

Merged
merged 7 commits into from
Jan 21, 2025
Merged

Implement @variant #15663

merged 7 commits into from
Jan 21, 2025

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Jan 17, 2025

This PR replaces @variant with @custom-variant for registering custom variants via your CSS.

In addition, this PR introduces @variant that can be used in your CSS to use a variant while writing custom CSS.

E.g.:

.btn {
  background: white;

  @variant dark {
    background: black;
  }
}

Compiles to:

.btn {
  background: white;
}

@media (prefers-color-scheme: dark) {
  .btn {
    background: black;
  }
}

For backwards compatibility, the @variant rules that don't have a body and are
defined inline:

@variant hocus (&:hover, &:focus);

And @variant rules that are defined with a body and a @slot:

@variant hocus {
  &:hover, &:focus {
    @slot;
  }
}

Will automatically be upgraded to @custom-variant internally, 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, nesting @variant <variant> {} will be the way to go. Only a single @variant <variant> can be used, if you want to use multiple, nesting should be used:
.foo {
  @variant hover {
    @variant focus {
      color: red;
    }
  }
}

Sorry, something went wrong.

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;
  }
}
```
@RobinMalfait RobinMalfait force-pushed the feat/use-variant-in-css branch from 6a102d0 to bab1ee3 Compare January 20, 2025 15:26
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)
}
}
Copy link
Member Author

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.

@RobinMalfait RobinMalfait marked this pull request as ready for review January 20, 2025 15:30
@RobinMalfait RobinMalfait requested a review from a team as a code owner January 20, 2025 15:30
let variantAst = designSystem.parseVariant(variant)
if (variantAst === null) {
throw new Error(`Cannot use \`@variant\` with unknown variant: ${variant}`)
}
Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Member

@philipp-spiess philipp-spiess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@RobinMalfait RobinMalfait merged commit 4035ab0 into next Jan 21, 2025
5 checks passed
@RobinMalfait RobinMalfait deleted the feat/use-variant-in-css branch January 21, 2025 15:20
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants