Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

feat: Add `FunctionValue` to the css-engine to describe css functions effeciently. (#2986)

Jaya Krishna Namburu0687ae10354f
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 2df9844824..dc5e494405 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -695,7 +695,8 @@ const traverseStyleValue = (
value.type === "unparsed" ||
value.type === "invalid" ||
value.type === "unset" ||
- value.type === "rgb"
+ value.type === "rgb" ||
+ value.type === "function"
) {
callback(value);
return;
diff --git a/packages/css-engine/src/core/to-value.test.ts b/packages/css-engine/src/core/to-value.test.ts
index 7ace3985d7..96bf6b1025 100644
--- a/packages/css-engine/src/core/to-value.test.ts
+++ b/packages/css-engine/src/core/to-value.test.ts
@@ -132,6 +132,34 @@ describe("Convert WS CSS Values to native CSS strings", () => {
expect(value).toBe("10px 20px 30px 40px");
});
+ test("function", () => {
+ const translate3D = toValue({
+ type: "function",
+ name: "translate3d",
+ args: {
+ type: "keyword",
+ value: "42px, -62px, -135px",
+ },
+ });
+
+ const dropShadowValue = toValue({
+ type: "function",
+ name: "drop-shadow",
+ args: {
+ type: "tuple",
+ value: [
+ { type: "unit", value: 10, unit: "px" },
+ { type: "unit", value: 10, unit: "px" },
+ { type: "unit", value: 10, unit: "px" },
+ { type: "keyword", value: "red" },
+ ],
+ },
+ });
+
+ expect(translate3D).toBe("translate3d(42px, -62px, -135px)");
+ expect(dropShadowValue).toBe("drop-shadow(10px 10px 10px red)");
+ });
+
test("sanitize url", () => {
const assets = new Map<string, { path: string }>([
["1234567890", { path: `fo"o\\o.png` }],
diff --git a/packages/css-engine/src/core/to-value.ts b/packages/css-engine/src/core/to-value.ts
index c0d9856c90..44c53833bb 100644
--- a/packages/css-engine/src/core/to-value.ts
+++ b/packages/css-engine/src/core/to-value.ts
@@ -112,5 +112,9 @@ export const toValue = (
return value.value.map((value) => toValue(value, transformValue)).join(" ");
}
+ if (value.type === "function") {
+ return `${value.name}(${toValue(value.args, transformValue)})`;
+ }
+
return captureError(new Error("Unknown value type"), value);
};
diff --git a/packages/css-engine/src/schema.ts b/packages/css-engine/src/schema.ts
index 3abc019540..df44adffa3 100644
--- a/packages/css-engine/src/schema.ts
+++ b/packages/css-engine/src/schema.ts
@@ -54,6 +54,18 @@ const RgbValue = z.object({
});
export type RgbValue = z.infer<typeof RgbValue>;
+export type FunctionValue = z.infer<typeof FunctionValue>;
+
+export const FunctionValue: z.ZodType<{
+ type: "function";
+ name: string;
+ args: StyleValue;
+}> = z.object({
+ type: z.literal("function"),
+ name: z.string(),
+ args: z.lazy(() => StyleValue),
+});
+
export const ImageValue = z.object({
type: z.literal("image"),
value: z.union([
@@ -87,6 +99,7 @@ export const TupleValueItem = z.union([
KeywordValue,
UnparsedValue,
RgbValue,
+ FunctionValue,
]);
export type TupleValueItem = z.infer<typeof TupleValueItem>;
@@ -127,6 +140,7 @@ const ValidStaticStyleValue = z.union([
RgbValue,
UnparsedValue,
TupleValue,
+ FunctionValue,
]);
export type ValidStaticStyleValue = z.infer<typeof ValidStaticStyleValue>;
@@ -149,7 +163,8 @@ export const isValidStaticStyleValue = (
staticStyleValue.type === "fontFamily" ||
staticStyleValue.type === "rgb" ||
staticStyleValue.type === "unparsed" ||
- staticStyleValue.type === "tuple"
+ staticStyleValue.type === "tuple" ||
+ staticStyleValue.type === "function"
);
};