fix(tailwind): use a value another class defines instead of the fallback

MA
6h ago
+153-2

Related issue or discussion

Refs #3931. This fixes the text shadow and gradient cases; drop-shadow follows in a separate PR.

What this changes

resolveAllCssVariables runs over the whole stylesheet before styles are inlined per element. When a rule used a variable it doesn't define, its fallback was written in at that stage, before the element's other classes were seen. So text-shadow-red-500 was ignored, and bg-radial-[at_25%_25%], bg-linear-[25deg] and bg-conic-[from_45deg] lost their color stops.

#3803 already leaves a custom property's fallback for inline time, because another class may still define it. This does the same for regular properties when another class rule defines the variable, and makeInlineStylesFor resolves it per element (#3768). Only rules whose selector is a single class make a fallback wait, and a rule's own definitions still resolve early. Whether a fallback waits depends on the cached stylesheet, but every built-in utility on its own renders the same either way.

Other classes that read a variable another class sets follow too: leading-[24px] text-sm keeps 24px, and duration-300 transition gives 300ms.

A value left for inline time could pick up initial from a class like duration-initial, so when a variable is set to a CSS-wide keyword and there is a fallback or registered initial value, makeInlineStylesFor now uses that instead, as a browser does when no ancestor sets the variable. That also fixes the -initial and -inherit variants of shadow, inset-shadow, ring and ring-offset, plus via-none and from-inherit, which inlined the keyword.

drop-shadow-lg bakes its own --tw-drop-shadow into filter through a different path, which needs more changes, so it comes separately.

Not addressed
  • With an opacity modifier (text-shadow-red-500/50) the color now applies but keeps an unfolded color-mix, as shadow-md shadow-blue-500/50 does (#3803's note). It used to render black.
  • Alpha colors such as text-shadow-[#ff000080] now get the five-argument rgb() that shadow-[#ff000080] already gets, which browsers drop, so the shadow disappears.
  • Attribute and descendant variants (aria-checked:, data-[state=open]:, [&_p]:) reach shadows and gradient stops when a plain class for the same variable is in the stylesheet, from this render or an earlier one, as they already do for box shadows. In user CSS, .dark .card applies the same way once a plain class defines its variable.
  • In user CSS and arbitrary values, a fallback left for inline time skips the stylesheet's value handling: margin-block: var(--m, 1rem 2rem) and padding-inline split it wrongly, and calc(infinity * 1px) stays instead of becoming 9999px. With arbitrary values it depends on the other classes in the stylesheet, from this render or an earlier one. A variable read as var(--m,) and set to initial now stays as var(--m,).
  • A registered @property initial value now replaces a fallback once any class in the stylesheet defines the variable, as in browsers.

Tests

  • Resolver: a fallback left when another class defines the variable; descendant and attribute rules don't count.
  • makeInlineStylesFor: a variable set to initial falls back, and keeps initial with nothing to fall back to.
  • <Tailwind>: colored and default siblings, leading-[24px] text-sm and the gradient; a render-order guard.
  • The resolver, makeInlineStylesFor and render tests fail on canary; the guards pass there. No existing snapshot changed.
  • Every named built-in utility this can affect, rendered alone, matches canary on its own sheet and beside the classes that share its variables.
  • The issue's text shadow and gradient cases match Tailwind's CSS in Chromium, WebKit and Firefox.
  • pnpm lint and pnpm typecheck pass; pnpm test passes except two CLI specs that fail on canary on Windows too.

AI disclosure: written with Claude's help. I reviewed the change and the tests before opening this.

Checklist

  • I understand the code in this PR and the problem it solves.
  • I read this description, and it accurately describes the change.
  • I added a changeset, or this change doesn't affect a published package.

Summary by cubic

Fixes <Tailwind> so utilities that read a CSS variable another class on the same element defines now use that value instead of the fallback. This corrects text-shadow-* colors, gradients with arbitrary positions (e.g., bg-radial-[at_25%_25%]) losing their color stops, and leading-* being overridden by text-*. When a variable is set to a CSS-wide keyword like initial or inherit, the fallback is now used instead of inlining the keyword, matching browser behavior.

Key changes

  • Fallback deferral only happens for rules whose selector is a single class; descendant and attribute selectors don't count.
  • makeInlineStylesFor treats a variable declared with a CSS-wide keyword as unset when a fallback exists.
  • Tests cover resolver, inlining, and render behavior; no existing snapshots change.