镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Merged
philipp-spiess merged 8 commits into
mainfrom
fix/reference-mode-var-fallback
Feb 25, 2025
Merged

philipp-spiess merged 8 commits into
mainfrom
fix/reference-mode-var-fallback

Conversation

@philipp-spiess

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

Copy link
Copy Markdown
Contributor

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
Comment thread packages/tailwindcss/src/index.test.ts Resolved
@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
Copy Markdown
Contributor 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
Comment thread packages/tailwindcss/src/index.ts Outdated
@@ -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
Copy Markdown
Contributor

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
Copy Markdown
Contributor 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
Copy Markdown
Contributor

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
Copy Markdown
Contributor 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
input,
opts,
)
let { designSystem, ast, globs, root, utilitiesNode, features } = await parseCss(input, opts)

Copy link
Copy Markdown
Contributor

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
@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