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 ‘optimize’ property to Image component (#3599)

Ivan Starkov6a2a512e8d7b
diff --git a/packages/generate-arg-types/src/arg-types.ts b/packages/generate-arg-types/src/arg-types.ts
index 3fdd72ee58..f4e41762ad 100644
--- a/packages/generate-arg-types/src/arg-types.ts
+++ b/packages/generate-arg-types/src/arg-types.ts
@@ -15,6 +15,7 @@ export const propsToArgTypes = (
})
// Exclude webstudio builder props see react-sdk/src/tree/webstudio-component.tsx
.filter(([propName]) => propName.startsWith("data-ws-") === false)
+ .filter(([propName]) => propName.startsWith("$webstudio") === false)
// Exclude props that are in the exclude list
.filter(([propName]) => exclude.includes(propName) === false)
.map(([propName, propItem]) => {
diff --git a/packages/sdk-components-react/src/__generated__/image.props.ts b/packages/sdk-components-react/src/__generated__/image.props.ts
index 1fb7abb0c1..af6ba50dd4 100644
--- a/packages/sdk-components-react/src/__generated__/image.props.ts
+++ b/packages/sdk-components-react/src/__generated__/image.props.ts
@@ -527,7 +527,12 @@ export const props: Record<string, PropMeta> = {
"Determines whether the image will load as soon as possible (Eager), or when it scrolls into view (Lazy). Lazy loading is a great option for pages with many images because it can significantly reduce the time it takes for the page to load initially.",
},
nonce: { required: false, control: "text", type: "string" },
- optimize: { required: false, control: "boolean", type: "boolean" },
+ optimize: {
+ required: false,
+ control: "boolean",
+ type: "boolean",
+ defaultValue: true,
+ },
prefix: { required: false, control: "text", type: "string" },
property: { required: false, control: "text", type: "string" },
quality: { required: false, control: "number", type: "number" },
diff --git a/packages/sdk-components-react/src/image.tsx b/packages/sdk-components-react/src/image.tsx
index 50ce5bbcf1..eeb27f20ab 100644
--- a/packages/sdk-components-react/src/image.tsx
+++ b/packages/sdk-components-react/src/image.tsx
@@ -18,9 +18,9 @@ export const Image = forwardRef<
(
{
loading = "lazy",
- width: widthProp,
- height: heightProp,
- optimize: optimizeProp,
+ width,
+ height,
+ optimize = true,
decoding: decodingProp,
$webstudio$canvasOnly$assetId,
...props
@@ -36,11 +36,6 @@ export const Image = forwardRef<
let key = src;
- let optimize = optimizeProp;
-
- let width = widthProp;
- let height = heightProp;
-
if (renderer === "canvas") {
// With disabled cache and loading lazy, chrome may not render the image at all
loading = "eager";
@@ -53,12 +48,12 @@ export const Image = forwardRef<
// NaN width and height means that the image is not yet uploaded, and should not be optimized on canvas
if (
- widthProp !== undefined &&
- heightProp !== undefined &&
- Number.isNaN(widthProp) &&
- Number.isNaN(heightProp)
+ width !== undefined &&
+ height !== undefined &&
+ Number.isNaN(width) &&
+ Number.isNaN(height)
) {
- optimize = optimizeProp ?? false;
+ optimize = false;
width = undefined;
height = undefined;
}
diff --git a/packages/sdk-components-react/src/image.ws.ts b/packages/sdk-components-react/src/image.ws.ts
index 25aa41c7a7..343397bcde 100644
--- a/packages/sdk-components-react/src/image.ws.ts
+++ b/packages/sdk-components-react/src/image.ws.ts
@@ -56,10 +56,23 @@ export const propsOverrides = {
},
} as const;
+const optimize = props.optimize;
+optimize.description = "Optimize the image for enhanced performance.";
+
export const propsMeta: WsComponentPropsMeta = {
props: {
...props,
...propsOverrides,
+ optimize,
},
- initialProps: ["id", "className", "src", "width", "height", "alt", "loading"],
+ initialProps: [
+ "id",
+ "className",
+ "src",
+ "width",
+ "height",
+ "alt",
+ "loading",
+ "optimize",
+ ],
};