Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/__tests__/native/variables.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<color>'; 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(
<View className={parentClassName}>
<View testID={testID} className={className} />
</View>,
);

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(<View testID={testID} className="red clear reader" />);

expect(screen.getByTestId(testID).props.style).toStrictEqual({
color: "red",
});
});
},
);
82 changes: 82 additions & 0 deletions src/__tests__/vendor/tailwind/initial-overrides.test.tsx
Original file line number Diff line number Diff line change
@@ -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 },
],
},
},
});
});
},
);
18 changes: 18 additions & 0 deletions src/compiler/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("--") ||
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/native/styles/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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));
}
Expand Down
Loading