diff --git a/src/__tests__/compiler/logical-borders.test.ts b/src/__tests__/compiler/logical-borders.test.ts index 83c66bce..8a1002f3 100644 --- a/src/__tests__/compiler/logical-borders.test.ts +++ b/src/__tests__/compiler/logical-borders.test.ts @@ -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({ @@ -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);", @@ -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);", diff --git a/src/__tests__/native/logical-borders.test.tsx b/src/__tests__/native/logical-borders.test.tsx index c7276bcc..f75fbcc2 100644 --- a/src/__tests__/native/logical-borders.test.tsx +++ b/src/__tests__/native/logical-borders.test.tsx @@ -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(); + 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(); + 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(); + 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) => @@ -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; } @@ -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)}; }`); diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index 771fde0f..4e3f2cba 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -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", @@ -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", @@ -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", @@ -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( @@ -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), @@ -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), @@ -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; } @@ -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; } @@ -1748,9 +1770,10 @@ export function parseColorDeclaration( declaration: Extract, 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), ); } diff --git a/src/native/styles/shorthands/border.ts b/src/native/styles/shorthands/border.ts index cefdb005..1a60e90c 100644 --- a/src/native/styles/shorthands/border.ts +++ b/src/native/styles/shorthands/border.ts @@ -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]], - [], +// ` || || `, 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 = { [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); diff --git a/src/native/styles/shorthands/index.ts b/src/native/styles/shorthands/index.ts index f9ed4196..8fc81501 100644 --- a/src/native/styles/shorthands/index.ts +++ b/src/native/styles/shorthands/index.ts @@ -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"; diff --git a/src/native/styles/shorthands/logical-border-color.ts b/src/native/styles/shorthands/logical-border-color.ts new file mode 100644 index 00000000..586fdb83 --- /dev/null +++ b/src/native/styles/shorthands/logical-border-color.ts @@ -0,0 +1,25 @@ +import type { StyleFunctionResolver } from "../resolve"; + +function isColor(value: unknown): boolean { + return ( + typeof value === "string" || + (typeof value === "object" && value !== null && !Array.isArray(value)) + ); +} + +// css-logical-1 ยง4.5.3: an axis colour takes one or two colours, the second defaulting to the first +function axisColorEdge(edge: 0 | 1): StyleFunctionResolver { + return (resolve, descriptor) => { + const resolved = resolve(descriptor[2]); + const values: readonly unknown[] = Array.isArray(resolved) + ? resolved + : [resolved]; + if (values.length < 1 || values.length > 2 || !values.every(isColor)) { + return; + } + return values[edge] ?? values[0]; + }; +} + +export const axisColorStart = axisColorEdge(0); +export const axisColorEnd = axisColorEdge(1);