From a329a90a4ee0fdbe13614001459e88fb8a217e5c Mon Sep 17 00:00:00 2001 From: Jelte Homminga Date: Sat, 5 Dec 2026 13:36:26 +0100 Subject: [PATCH] feat(compiler): compile transform-origin to React Native's transformOrigin The origin-* classes compiled to nothing on native, with a warning. transform-origin now compiles to transformOrigin as [x, y, 0]: left and top become 0, center becomes "50%", right and bottom become "100%". Lengths and percentages go through the existing length parser, and calc() drops with the usual value warning. A var() value still drops with the warning from before, because React Native throws in development on a transformOrigin that is not exactly [x, y, z], and a variable can hold any value. The metro warnings test used transform-origin as its unsupported property; its fixture uses text-indent now. Closes #509 --- src/__tests__/metro/warnings.test.ts | 4 +- .../vendor/tailwind/transform.test.ts | 80 +++++++++++++++---- src/compiler/declarations.ts | 36 +++++++++ 3 files changed, 102 insertions(+), 18 deletions(-) diff --git a/src/__tests__/metro/warnings.test.ts b/src/__tests__/metro/warnings.test.ts index d942b707..eb35f9de 100644 --- a/src/__tests__/metro/warnings.test.ts +++ b/src/__tests__/metro/warnings.test.ts @@ -17,7 +17,7 @@ const FIFTEEN_UNSUPPORTED_PROPERTIES = `.a { mix-blend-mode: multiply; background-blend-mode: screen; touch-action: none; - transform-origin: top left; + text-indent: 2px; font-variant-numeric: ordinal; border-spacing: 2px; background-position: center; @@ -253,7 +253,7 @@ describe("formatCompilerWarnings", () => { ).toBe( [ "react-native-css: src/global.css - 15 declarations dropped, no React Native equivalent", - " properties: backdrop-filter, background-blend-mode, background-position, border-spacing, break-after, break-before, clear, font-variant-numeric, mix-blend-mode, order, resize, touch-action, transform-origin, white-space, will-change", + " properties: backdrop-filter, background-blend-mode, background-position, border-spacing, break-after, break-before, clear, font-variant-numeric, mix-blend-mode, order, resize, text-indent, touch-action, white-space, will-change", ].join("\n"), ); }); diff --git a/src/__tests__/vendor/tailwind/transform.test.ts b/src/__tests__/vendor/tailwind/transform.test.ts index 7d859046..992afd9e 100644 --- a/src/__tests__/vendor/tailwind/transform.test.ts +++ b/src/__tests__/vendor/tailwind/transform.test.ts @@ -248,53 +248,101 @@ describe("Transforms - Mixed", () => { describe("Transforms - Transform Origin", () => { test("origin-center", async () => { expect(await renderCurrentTest()).toStrictEqual({ - props: {}, - warnings: { properties: ["transform-origin"] }, + props: { + style: { + transformOrigin: ["50%", "50%", 0], + }, + }, }); }); test("origin-top", async () => { expect(await renderCurrentTest()).toStrictEqual({ - props: {}, - warnings: { properties: ["transform-origin"] }, + props: { + style: { + transformOrigin: ["50%", 0, 0], + }, + }, }); }); test("origin-top-right", async () => { expect(await renderCurrentTest()).toStrictEqual({ - props: {}, - warnings: { properties: ["transform-origin"] }, + props: { + style: { + transformOrigin: ["100%", 0, 0], + }, + }, }); }); test("origin-right", async () => { expect(await renderCurrentTest()).toStrictEqual({ - props: {}, - warnings: { properties: ["transform-origin"] }, + props: { + style: { + transformOrigin: ["100%", "50%", 0], + }, + }, }); }); test("origin-bottom-right", async () => { expect(await renderCurrentTest()).toStrictEqual({ - props: {}, - warnings: { properties: ["transform-origin"] }, + props: { + style: { + transformOrigin: ["100%", "100%", 0], + }, + }, }); }); test("origin-bottom", async () => { expect(await renderCurrentTest()).toStrictEqual({ - props: {}, - warnings: { properties: ["transform-origin"] }, + props: { + style: { + transformOrigin: ["50%", "100%", 0], + }, + }, }); }); test("origin-bottom-left", async () => { expect(await renderCurrentTest()).toStrictEqual({ - props: {}, - warnings: { properties: ["transform-origin"] }, + props: { + style: { + transformOrigin: [0, "100%", 0], + }, + }, }); }); test("origin-left", async () => { expect(await renderCurrentTest()).toStrictEqual({ - props: {}, - warnings: { properties: ["transform-origin"] }, + props: { + style: { + transformOrigin: [0, "50%", 0], + }, + }, }); }); test("origin-top-left", async () => { + expect(await renderCurrentTest()).toStrictEqual({ + props: { + style: { + transformOrigin: [0, 0, 0], + }, + }, + }); + }); + test("origin-[25%_10px]", async () => { + expect(await renderCurrentTest()).toStrictEqual({ + props: { + style: { + transformOrigin: ["25%", 10, 0], + }, + }, + }); + }); + test("origin-[calc(100%-1px)_0]", async () => { + expect(await renderCurrentTest()).toStrictEqual({ + props: {}, + warnings: { values: { "transform-origin": "calc()" } }, + }); + }); + test("origin-(--my-origin)", async () => { expect(await renderCurrentTest()).toStrictEqual({ props: {}, warnings: { properties: ["transform-origin"] }, diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index 5fff580b..49d3593b 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -27,6 +27,7 @@ import type { LineStyle, MaxSize, NumberOrPercentage, + Position, Scale, Size, Size2DFor_DimensionPercentageFor_LengthValue, @@ -295,6 +296,7 @@ const parsers: { "text-transform": ({ value }) => value.case, "top": parseSizeWithAutoDeclaration, "transform": parseTransform, + "transform-origin": parseTransformOrigin, "transition": addTransitionValue, "transition-delay": addTransitionValue, "transition-duration": addTransitionValue, @@ -859,6 +861,34 @@ function parseTransform( builder.addDescriptor("transform", [{}, "transform", functions]); } +function parseTransformOrigin( + { value }: DeclarationType<"transform-origin">, + builder: StylesheetBuilder, +) { + const x = parseTransformOriginPosition(value.x, builder); + const y = parseTransformOriginPosition(value.y, builder); + + if (x === undefined || y === undefined) { + return; + } + + return [x, y, 0]; +} + +function parseTransformOriginPosition( + position: Position["x"] | Position["y"], + builder: StylesheetBuilder, +): StyleDescriptor { + switch (position.type) { + case "center": + return "50%"; + case "length": + return parseLength(position.value, builder); + case "side": + return position.side === "left" || position.side === "top" ? 0 : "100%"; + } +} + function parseTranslate( { value }: DeclarationType<"translate">, builder: StylesheetBuilder, @@ -1003,6 +1033,12 @@ export function parseUnparsedDeclaration( return; } + // React Native throws on a transformOrigin that is not [x, y, z] + if (property === "transform-origin") { + builder.addWarning("property", property); + return; + } + builder.setWarningProperty(property); /**