From 193c0001fe338f222b9bc8b79cfc3d82b53677d1 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Mon, 23 Nov 2026 15:09:08 -0500 Subject: [PATCH] fix: let a custom property set to initial override other declarations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The compiler dropped `--x: initial` with a warning, so any other rule declaring the same variable won: Tailwind's `md:via-none`, `md:shadow-initial` and `md:inset-shadow-initial` kept the colour that `via-*`, `shadow-*` and `inset-shadow-*` set. The declaration now compiles to an `initial` marker. When it is an element's own winning value, var() skips the element's other rules, `*` and the parent, and takes the registered initial value or else its fallback (css-variables-1 §2.2, css-properties-values-api-1 §2). An ancestor's marker already resolves to nothing, which main treats as the guaranteed-invalid value. Co-authored-by: Yevhenii Co-Authored-By: Claude Opus 5.5 --- src/__tests__/native/variables.test.tsx | 70 ++++++++++++++++ .../tailwind/initial-overrides.test.tsx | 82 +++++++++++++++++++ src/compiler/declarations.ts | 18 ++++ src/native/styles/variables.ts | 9 ++ 4 files changed, 179 insertions(+) create mode 100644 src/__tests__/vendor/tailwind/initial-overrides.test.tsx diff --git a/src/__tests__/native/variables.test.tsx b/src/__tests__/native/variables.test.tsx index 25ac30f5..2f37f0d8 100644 --- a/src/__tests__/native/variables.test.tsx +++ b/src/__tests__/native/variables.test.tsx @@ -351,3 +351,73 @@ test("variable overriding with classes", () => { const component = screen.getByTestId(testID); expect(component.props.style).toStrictEqual({ color: "#f00" }); }); + +// css-variables-1 §2.2: a custom property set to `initial` is a declaration of the guaranteed-invalid value, or of a registered initial value, so it hides an earlier rule's value and its parent's +describe.each([ + ["inlined", {}], + ["kept", { inlineVariables: false }], +] as const)( + "a custom property set to initial, with variables %s", + (_mode, options) => { + test.each([ + [ + "an earlier rule", + ".red { --x: red; } .clear { --x: initial; } .reader { color: var(--x, green); }", + "red clear reader", + undefined, + { color: "green" }, + ], + [ + "a later rule, with !important", + ".clear { --x: initial !important; } .red { --x: red; } .reader { color: var(--x, green); }", + "red clear reader", + undefined, + { color: "green" }, + ], + [ + "an earlier rule, inside @media", + ".red { --x: red; } .clear { @media (min-width: 1px) { --x: initial; } } .reader { color: var(--x, green); }", + "red clear reader", + undefined, + { color: "green" }, + ], + [ + "its parent's value", + ".red { --x: red; } .clear { --x: initial; } .reader { color: var(--x, green); }", + "clear reader", + "red", + { color: "green" }, + ], + [ + "an earlier rule, leaving a registered initial value", + "@property --x { syntax: ''; inherits: false; initial-value: blue; } .red { --x: red; } .clear { --x: initial; } .reader { color: var(--x, green); }", + "red clear reader", + undefined, + { color: "#00f" }, + ], + ])("overrides %s", (_overrides, css, className, parentClassName, style) => { + registerCSS(css, options); + + render( + + + , + ); + + expect(screen.getByTestId(testID).props.style).toStrictEqual(style); + }); + + test("is overridden by a later rule", () => { + registerCSS( + ".clear { --x: initial; } .red { --x: red; } .reader { color: var(--x, green); }", + options, + ); + + render(); + + expect(screen.getByTestId(testID).props.style).toStrictEqual({ + color: "red", + }); + }); + }, +); diff --git a/src/__tests__/vendor/tailwind/initial-overrides.test.tsx b/src/__tests__/vendor/tailwind/initial-overrides.test.tsx new file mode 100644 index 00000000..5ea390c5 --- /dev/null +++ b/src/__tests__/vendor/tailwind/initial-overrides.test.tsx @@ -0,0 +1,82 @@ +import { dimensions } from "../../../native/reactivity"; +import { renderSimple } from "./_tailwind"; + +// `via-none` and `*-initial` set a variable to `initial`, so under a variant they override the colour another utility sets, and the reader takes its fallback +describe.each([ + ["narrow", 375], + ["md", 1024], +] as const)( + "Tailwind utilities that set a variable to initial, %s", + (_size, width) => { + beforeEach(() => { + dimensions.set({ ...dimensions.get(), width }); + }); + + test("md:via-none", async () => { + const via = width >= 768 ? "" : " #00c950 50%,"; + + expect( + await renderSimple({ + className: + "bg-linear-to-r from-red-500 via-green-500 md:via-none to-blue-500", + }), + ).toStrictEqual({ + props: { + style: { + experimental_backgroundImage: `linear-gradient(to right, #fb2c36 0%,${via} #2b7fff 100%)`, + }, + }, + }); + }); + + test("md:shadow-initial", async () => { + const color = width >= 768 ? "#0000001a" : "#fb2c36"; + + expect( + await renderSimple({ + className: "shadow-md shadow-red-500 md:shadow-initial", + }), + ).toStrictEqual({ + props: { + style: { + boxShadow: [ + { + blurRadius: 6, + color, + offsetX: 0, + offsetY: 4, + spreadDistance: -1, + }, + { + blurRadius: 4, + color, + offsetX: 0, + offsetY: 2, + spreadDistance: -2, + }, + ], + }, + }, + }); + }); + + test("md:inset-shadow-initial", async () => { + const color = width >= 768 ? "#0000000d" : "#fb2c36"; + + expect( + await renderSimple({ + className: + "inset-shadow-sm inset-shadow-red-500 md:inset-shadow-initial", + }), + ).toStrictEqual({ + props: { + style: { + boxShadow: [ + { blurRadius: 4, color, inset: true, offsetX: 0, offsetY: 2 }, + ], + }, + }, + }); + }); + }, +); diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index 366b878c..e7e4528a 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -1110,6 +1110,12 @@ export function parseCustomDeclaration( if (parsedFontVariant !== undefined) { builder.addDescriptor(property, parsedFontVariant); } + } else if ( + property.startsWith("--") && + isSoleInitial(declaration.value.value) + ) { + // css-variables-1 §2.2: the guaranteed-invalid value, which still overrides an earlier declaration + builder.addDescriptor(property, [{}, "initial"]); } else if ( validProperties.has(property) || property.startsWith("--") || @@ -1124,6 +1130,18 @@ export function parseCustomDeclaration( } } +function isSoleInitial(tokens: TokenOrValue[]) { + const value = tokens.filter( + (token) => token.type !== "token" || token.value.type !== "white-space", + ); + return ( + value.length === 1 && + value[0]?.type === "token" && + value[0].value.type === "ident" && + value[0].value.value.toLowerCase() === "initial" + ); +} + export function reduceParseUnparsed( tokenOrValues: TokenOrValue[], builder: StylesheetBuilder, diff --git a/src/native/styles/variables.ts b/src/native/styles/variables.ts index e65c6d68..8443958f 100644 --- a/src/native/styles/variables.ts +++ b/src/native/styles/variables.ts @@ -64,6 +64,11 @@ export function varResolver( return value; } + // `initial` resolves to nothing, and hides every other declaration and the ancestor's value, leaving the registered initial value or the fallback + if (isInitial(inlineVariables?.[name])) { + return resolve(get(registeredInitialValues(name))) ?? resolve(fallback); + } + // A universal selector declares the variable on this element. It has lower // specificity than the element's class declarations, but beats inheritance. value = resolve(get(universalVariables(name))); @@ -123,6 +128,10 @@ export function varResolver( } } +function isInitial(value: unknown) { + return Array.isArray(value) && value.length === 2 && value[1] === "initial"; +} + function inherits(name: string, get: Getter) { return get(registeredInheritance(name)); }