Skip to content
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

Make @reference emit variable fallbacks instead of CSS variable declarations #16774

Merged
merged 8 commits into from
Feb 25, 2025

Conversation

philipp-spiess
Copy link
Member

@philipp-spiess philipp-spiess commented Feb 24, 2025

Fixes #16725

When using @reference "tailwindcss"; inside a separate CSS root (e.g. Svelte <style> components, CSS modules, etc.), we have no guarantee that the CSS variables will be defined in the main stylesheet (or if there even is one). To work around potential issues with this we decided in #16676 that we would emit all used CSS variables from the @theme inside the @reference block.

However, this is not only a bit surprising but also unexpected in CSS modules and Next.js that requires CSS module files to only create scope-able declarations. To fix this issue, we decided to not emit CSS variables but instead ensure all var(…) calls we create for theme values in reference mode will simply have their fallback value added.

This ensures styles work as-expected even if the root Tailwind file does not pick up the variable as being used or if you don't add a root at all. Furthermore we do not duplicate any variable declarations across your stylesheets and you still have the ability to change variables at runtime.

Test plan

  • Updated snapshots everywhere (see diff)
  • New Next.js CSS modules integration test

@philipp-spiess philipp-spiess changed the title Remove usages of @import "…" theme(reference) in tests Make @reference emit variable fallbacks instead of CSS variable declarations Feb 24, 2025
@philipp-spiess philipp-spiess force-pushed the fix/reference-mode-var-fallback branch from 85b933b to 5c568c1 Compare February 24, 2025 12:09
50% {
transform: rotate(3deg);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this was a bug before? Whoopsie

@philipp-spiess philipp-spiess force-pushed the fix/reference-mode-var-fallback branch 2 times, most recently from e3be9fc to ebcd576 Compare February 24, 2025 12:36
@philipp-spiess philipp-spiess marked this pull request as ready for review February 24, 2025 14:12
@philipp-spiess philipp-spiess requested a review from a team as a code owner February 24, 2025 14:12
@@ -464,10 +468,6 @@ async function parseCss(
// since the `@theme` rule itself will be removed.
if (child.kind === 'at-rule' && child.name === '@keyframes') {
// Do not track/emit `@keyframes`, if they are part of a `@theme reference`.
Copy link
Member

Choose a reason for hiding this comment

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

I think this comment can go as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

ah yep good catch :D

}
.tab-4 {
tab-size: var(--tab-size-4);
tab-size: var(--tab-size-4, 4);
Copy link
Member

Choose a reason for hiding this comment

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

Not sure what's up with the indents here. Maybe a GitHub fluke 🫣

image

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks good on my machine hahaha
Screenshot 2025-02-25 at 11 31 47

Comment on lines -619 to +618
let { designSystem, ast, globs, root, utilitiesNode, features, firstThemeRule } = await parseCss(
input,
opts,
)
let { designSystem, ast, globs, root, utilitiesNode, features } = await parseCss(input, opts)
Copy link
Member

Choose a reason for hiding this comment

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

Love that we don't need to pass around this firstThemeRule anymore 😍

@philipp-spiess philipp-spiess force-pushed the fix/reference-mode-var-fallback branch from fc7fbd2 to 12f4701 Compare February 25, 2025 10:32
@philipp-spiess philipp-spiess merged commit b389483 into main Feb 25, 2025
5 checks passed
@philipp-spiess philipp-spiess deleted the fix/reference-mode-var-fallback branch February 25, 2025 10:36
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.

Build Error: Selector ":root,:host" is not pure (nextjs/postcss)
2 participants