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
26 changes: 20 additions & 6 deletions src/__tests__/compiler/logical-borders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ describe("logical border styles", () => {
});
});

// Every Tailwind `border-x-*` / `border-y-*` width utility writes these through `--tw-border-style`
test.each([
"border-inline-style",
"border-inline-start-style",
"border-inline-end-style",
"border-block-style",
"border-block-start-style",
"border-block-end-style",
])("%s: var() is dropped without warning", (property) => {
const { rule, warnings } = getRule(`${property}: var(--tw-border-style);`);
expect(rule).toBeUndefined();
expect(warnings).toStrictEqual({});
});

test("border-inline shorthand with a dashed style warns", () => {
const { warnings } = getRule("border-inline: 2px dashed red;");
expect(warnings).toStrictEqual({
Expand All @@ -129,14 +143,14 @@ describe("a two-edge logical property via var() compiles to a runtime call", ()
[
"border-inline-color: var(--c);",
["borderStartColor", "borderEndColor"],
[{}, "var", "c", 1],
[{}, "var", "c", 1],
[{}, "axisColorStart", [{}, "var", "c", 1], 1],
[{}, "axisColorEnd", [{}, "var", "c", 1], 1],
],
[
"border-inline-color: var(--c, red);",
["borderStartColor", "borderEndColor"],
[{}, "var", ["c", "red"], 1],
[{}, "var", ["c", "red"], 1],
[{}, "axisColorStart", [{}, "var", ["c", "red"], 1], 1],
[{}, "axisColorEnd", [{}, "var", ["c", "red"], 1], 1],
],
[
"border-inline-color: var(--a) var(--b);",
Expand All @@ -153,8 +167,8 @@ describe("a two-edge logical property via var() compiles to a runtime call", ()
[
"border-block-color: var(--c);",
["borderTopColor", "borderBottomColor"],
[{}, "var", "c", 1],
[{}, "var", "c", 1],
[{}, "axisColorStart", [{}, "var", "c", 1], 1],
[{}, "axisColorEnd", [{}, "var", "c", 1], 1],
],
[
"border-block-color: var(--a) var(--b);",
Expand Down
96 changes: 75 additions & 21 deletions src/__tests__/native/logical-borders.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,79 @@ describe("the block axis reaching the component", () => {
* deliberate edit, which is why the negative control below pins the eleven
* names this file exists to keep out.
*/
describe("a var() axis colour or shorthand resolved at runtime", () => {
test.each([
[
"border-inline-color",
{ borderStartColor: "#26e", borderEndColor: "#6af" },
],
[
"border-block-color",
{ borderTopColor: "#26e", borderBottomColor: "#6af" },
],
])(
"%s takes each edge's colour from a two-colour variable",
(property, expected) => {
registerCSS(`
.my-class { ${property}: var(--pair); }
:root { --pair: #2266ee #66aaff; }
.redefine { --pair: #000; }
`);

render(<View testID={testID} className="my-class" />);
expect(screen.getByTestId(testID).props.style).toStrictEqual(expected);
},
);

test.each([
[
"border-inline",
{
borderStartWidth: 1,
borderEndWidth: 1,
borderStartColor: "red",
borderEndColor: "red",
},
],
["border-inline-start", { borderStartWidth: 1, borderStartColor: "red" }],
["border-inline-end", { borderEndWidth: 1, borderEndColor: "red" }],
[
"border-block",
{
borderTopWidth: 1,
borderBottomWidth: 1,
borderTopColor: "red",
borderBottomColor: "red",
},
],
["border-block-start", { borderTopWidth: 1, borderBlockStartColor: "red" }],
["border-block-end", { borderBottomWidth: 1, borderBlockEndColor: "red" }],
])("%s via var() reaches the edges it names", (property, expected) => {
registerCSS(`
.my-class { ${property}: var(--shorthand); }
${shorthandDefinitions}
`);

render(<View testID={testID} className="my-class" />);
expect(screen.getByTestId(testID).props.style).toStrictEqual(expected);
});

test("a later edge colour replaces a var() shorthand's colour on that edge", () => {
registerCSS(`
.my-class { border-inline: var(--shorthand); border-inline-end-color: #2266ee; }
${shorthandDefinitions}
`);

render(<View testID={testID} className="my-class" />);
expect(screen.getByTestId(testID).props.style).toStrictEqual({
borderStartWidth: 1,
borderEndWidth: 1,
borderStartColor: "red",
borderEndColor: "#26e",
});
});
});

describe("no logical border property reaches a key React Native lacks", () => {
/** Every `border-{inline,block}[-start|-end][-width|-style|-color]`. */
const FAMILY: string[] = ["inline", "block"].flatMap((axis) =>
Expand Down Expand Up @@ -799,15 +872,7 @@ describe("no logical border property reaches a key React Native lacks", () => {
).toStrictEqual([]);
});

// The six three-part shorthands keep a var() for the runtime unparsed, as main does
const VAR_COVERED = FAMILY.filter(
(property) =>
property.endsWith("-color") ||
property.endsWith("-width") ||
property.endsWith("-style"),
);

test.each(VAR_COVERED)("%s (var) emits only real style keys", (property) => {
test.each(FAMILY)("%s (var) emits only real style keys", (property) => {
registerCSS(`
.my-class { ${property}: ${varFor(property)}; }
:root { --style: solid; }
Expand Down Expand Up @@ -838,18 +903,7 @@ describe("no logical border property reaches a key React Native lacks", () => {
* colour, so the style object keeps both and the value that paints is
* whichever one the platform ranks higher, not the one the scheme selected.
*/
// The others send their dark half to the property's own name, as on main
const LIGHT_DARK_COVERED = COLOUR_BEARING.filter((property) =>
[
"border-inline-color",
"border-block",
"border-block-color",
"border-block-start-color",
"border-block-end-color",
].includes(property),
);

test.each(LIGHT_DARK_COVERED)(
test.each(COLOUR_BEARING)(
"%s (light-dark) emits only real style keys in both schemes",
(property) => {
registerCSS(`.my-class { ${property}: ${lightDarkFor(property)}; }`);
Expand Down
47 changes: 35 additions & 12 deletions src/compiler/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ const unparsedRuntimeParsing = new Set([
"animation",
"animation-name",
"border",
"border-block",
"border-block-end",
"border-block-start",
"border-block-width",
"border-inline",
"border-inline-end",
"border-inline-start",
"border-inline-width",
"box-shadow",
"filter",
Expand Down Expand Up @@ -519,7 +525,7 @@ function parseBorderBlockStart(
) {
builder.addDescriptor(
"border-block-start-color",
parseColor(value.color, builder),
parseColorFor(["border-block-start-color"], value.color, builder),
);
builder.addDescriptor(
"border-top-width",
Expand All @@ -538,7 +544,7 @@ function parseBorderBlockEnd(
) {
builder.addDescriptor(
"border-block-end-color",
parseColor(value.color, builder),
parseColorFor(["border-block-end-color"], value.color, builder),
);
builder.addDescriptor(
"border-bottom-width",
Expand All @@ -555,11 +561,11 @@ function parseBorderInline(
{ value }: DeclarationType<"border-inline">,
builder: StylesheetBuilder,
) {
const color = parseColor(value.color, builder);
const color = parseColorFor(inlineColorEdges, value.color, builder);
const width = parseBorderSideWidth(value.width, builder);

builder.addDescriptor("border-start-color", color);
builder.addDescriptor("border-end-color", color);
builder.addDescriptor(inlineColorEdges[0], color);
builder.addDescriptor(inlineColorEdges[1], color);
builder.addDescriptor("border-start-width", width);
builder.addDescriptor("border-end-width", width);
dropUnsupportedInlineStyle(
Expand All @@ -573,7 +579,10 @@ function parseBorderInlineStart(
{ value }: DeclarationType<"border-inline-start">,
builder: StylesheetBuilder,
) {
builder.addDescriptor("border-start-color", parseColor(value.color, builder));
builder.addDescriptor(
"border-start-color",
parseColorFor(["border-start-color"], value.color, builder),
);
builder.addDescriptor(
"border-start-width",
parseBorderSideWidth(value.width, builder),
Expand All @@ -589,7 +598,10 @@ function parseBorderInlineEnd(
{ value }: DeclarationType<"border-inline-end">,
builder: StylesheetBuilder,
) {
builder.addDescriptor("border-end-color", parseColor(value.color, builder));
builder.addDescriptor(
"border-end-color",
parseColorFor(["border-end-color"], value.color, builder),
);
builder.addDescriptor(
"border-end-width",
parseBorderSideWidth(value.width, builder),
Expand Down Expand Up @@ -987,8 +999,8 @@ export function parseUnparsedDeclaration(
return;
}

// A var() keeps the value unknown, and an unknown value is not a known non-solid one, so it drops as quietly as solid
if (unsupportedLogicalStyles.has(property)) {
builder.addWarning("property", property);
return;
}

Expand All @@ -1015,8 +1027,18 @@ export function parseUnparsedDeclaration(
const value = parseUnparsed(declaration.value.value, builder, property);
const pair =
Array.isArray(value) && !isStyleFunction(value) && value.length === 2;
builder.addDescriptor(edges[0], pair ? value[0] : value);
builder.addDescriptor(edges[1], pair ? value[1] : value);

if (pair) {
builder.addDescriptor(edges[0], value[0]);
builder.addDescriptor(edges[1], value[1]);
} else if (isStyleFunction(value) && value[1] === "var") {
// A variable may hold one colour or two, so each edge picks its own from the resolved value
builder.addDescriptor(edges[0], [{}, "axisColorStart", value, 1]);
builder.addDescriptor(edges[1], [{}, "axisColorEnd", value, 1]);
} else {
builder.addDescriptor(edges[0], value);
builder.addDescriptor(edges[1], value);
}
return;
}

Expand Down Expand Up @@ -1748,9 +1770,10 @@ export function parseColorDeclaration(
declaration: Extract<Declaration, { value: CssColor }>,
builder: StylesheetBuilder,
) {
const property = propertyRename[declaration.property] ?? declaration.property;
builder.addDescriptor(
propertyRename[declaration.property] ?? declaration.property,
parseColor(declaration.value, builder),
property,
parseColorFor([property], declaration.value, builder),
);
}

Expand Down
80 changes: 77 additions & 3 deletions src/native/styles/shorthands/border.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,84 @@
import { ShortHandSymbol } from "../constants";
import type { StyleResolver } from "../resolve";
import { shorthandHandler } from "./_handler";

const width = ["borderWidth", "number"] as const;
const style = ["borderStyle", "string"] as const;
const color = ["borderColor", "color", "color"] as const;

export const border = shorthandHandler(
[[width, style, color], [style, color], [width, style], [style]],
[],
// `<line-width> || <line-style> || <color>`, the grammar `border` and the six logical-axis shorthands share
const mappings = [
[width, style, color],
[style, color],
[width, style],
[style],
];

export const border = shorthandHandler(mappings, []);

const matchBorder = shorthandHandler(mappings, [], "object");

// React Native has no inline-axis prop and no per-edge style, so the inline axis lands on borderStart/borderEnd, the block axis on its physical edges, and a style component is dropped
const axisTargets = {
borderInline: {
borderWidth: ["borderStartWidth", "borderEndWidth"],
borderColor: ["borderStartColor", "borderEndColor"],
},
borderInlineStart: {
borderWidth: ["borderStartWidth"],
borderColor: ["borderStartColor"],
},
borderInlineEnd: {
borderWidth: ["borderEndWidth"],
borderColor: ["borderEndColor"],
},
borderBlock: {
borderWidth: ["borderTopWidth", "borderBottomWidth"],
borderColor: ["borderTopColor", "borderBottomColor"],
},
borderBlockStart: {
borderWidth: ["borderTopWidth"],
borderColor: ["borderBlockStartColor"],
},
borderBlockEnd: {
borderWidth: ["borderBottomWidth"],
borderColor: ["borderBlockEndColor"],
},
} as const;

type AxisTargets = (typeof axisTargets)[keyof typeof axisTargets];

function axisBorderHandler(targets: AxisTargets): StyleResolver {
return (resolveValue, value, get, options) => {
const parsed = matchBorder(resolveValue, value, get, options);

if (typeof parsed !== "object" || !parsed) {
return;
}

const target: Record<string, unknown> = { [ShortHandSymbol]: true };

if ("borderWidth" in parsed) {
for (const property of targets.borderWidth) {
target[property] = parsed.borderWidth;
}
}

if ("borderColor" in parsed) {
for (const property of targets.borderColor) {
target[property] = parsed.borderColor;
}
}

return target;
};
}

export const borderInline = axisBorderHandler(axisTargets.borderInline);
export const borderInlineStart = axisBorderHandler(
axisTargets.borderInlineStart,
);
export const borderInlineEnd = axisBorderHandler(axisTargets.borderInlineEnd);
export const borderBlock = axisBorderHandler(axisTargets.borderBlock);
export const borderBlockStart = axisBorderHandler(axisTargets.borderBlockStart);
export const borderBlockEnd = axisBorderHandler(axisTargets.borderBlockEnd);
1 change: 1 addition & 0 deletions src/native/styles/shorthands/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./animation";
export * from "./border";
export * from "./logical-border-color";
export * from "./logical-border-width";
export * from "./box-shadow";
export * from "./text-shadow";
Expand Down
Loading
Loading