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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Image component
istarkov5d1452928d1c
diff --git a/packages/react-sdk/LICENSE-3RD-PARTY b/packages/react-sdk/LICENSE-3RD-PARTYnew file mode 100644index 0000000000..e3dba448bf--- /dev/null+++ b/packages/react-sdk/LICENSE-3RD-PARTY@@ -0,0 +1,15 @@+The MIT License (MIT)++applies to:++- https://github.com/vercel/next.js, Copyright (c) 2022 Vercel, Inc.++The MIT License (MIT)++Copyright (c) 2022 Vercel, Inc.++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.diff --git a/packages/react-sdk/package.json b/packages/react-sdk/package.jsonindex 3656b68019..21243f8607 100644--- a/packages/react-sdk/package.json+++ b/packages/react-sdk/package.json@@ -59,12 +59,13 @@"dependencies": {"@stitches/core": "^1.2.7","@webstudio-is/asset-uploader": "^*",+ "@webstudio-is/fonts": "*","@webstudio-is/icons": "*","@webstudio-is/jest-config": "*",- "@webstudio-is/fonts": "*","immer": "^9.0.12","mitt": "^3.0.0","react-nano-state": "^0.4.0",+ "warn-once": "^0.1.1","zod": "^3.17.3"},"main": "lib/index.js",diff --git a/packages/react-sdk/src/component-utils/image.test.ts b/packages/react-sdk/src/component-utils/image.test.tsnew file mode 100644index 0000000000..4f6d79b6d4--- /dev/null+++ b/packages/react-sdk/src/component-utils/image.test.ts@@ -0,0 +1,147 @@+import { getImageAttributes } from "./image";++describe("Image optimizations applied", () => {+ test("width is number, create pixel density descriptor 'x'", () => {+ const imgAttr = getImageAttributes({+ optimize: true,+ width: 100,+ src: "https://webstudio.is/logo.webp",+ srcSet: undefined,+ sizes: undefined,+ quality: 100,+ });++ expect(imgAttr).toMatchInlineSnapshot(`+ Object {+ "sizes": undefined,+ "src": "/cdn-cgi/image/width=256,quality=100,format=auto/https://webstudio.is/logo.webp",+ "srcSet": "/cdn-cgi/image/width=128,quality=100,format=auto/https://webstudio.is/logo.webp 1x, /cdn-cgi/image/width=256,quality=100,format=auto/https://webstudio.is/logo.webp 2x",+ }+ `);+ });++ test("width is undefined, create 'w' descriptor and sizes prop", () => {+ const imgAttr = getImageAttributes({+ optimize: true,+ width: undefined,+ src: "https://webstudio.is/logo.webp",+ srcSet: undefined,+ sizes: undefined,+ quality: 90,+ });++ expect(imgAttr).toMatchInlineSnapshot(`+ Object {+ "sizes": "(min-width: 1280px) 50vw, 100vw",+ "src": "/cdn-cgi/image/width=3840,quality=90,format=auto/https://webstudio.is/logo.webp",+ "srcSet": "/cdn-cgi/image/width=384,quality=90,format=auto/https://webstudio.is/logo.webp 384w, /cdn-cgi/image/width=640,quality=90,format=auto/https://webstudio.is/logo.webp 640w, /cdn-cgi/image/width=750,quality=90,format=auto/https://webstudio.is/logo.webp 750w, /cdn-cgi/image/width=828,quality=90,format=auto/https://webstudio.is/logo.webp 828w, /cdn-cgi/image/width=1080,quality=90,format=auto/https://webstudio.is/logo.webp 1080w, /cdn-cgi/image/width=1200,quality=90,format=auto/https://webstudio.is/logo.webp 1200w, /cdn-cgi/image/width=1920,quality=90,format=auto/https://webstudio.is/logo.webp 1920w, /cdn-cgi/image/width=2048,quality=90,format=auto/https://webstudio.is/logo.webp 2048w, /cdn-cgi/image/width=3840,quality=90,format=auto/https://webstudio.is/logo.webp 3840w",+ }+ `);+ });++ test("width is undefined and size defined, creates 'w' descriptor and use input sizes props", () => {+ const imgAttr = getImageAttributes({+ optimize: true,+ width: undefined,+ src: "https://webstudio.is/logo.webp",+ srcSet: undefined,+ sizes: "100vw",+ quality: 70,+ });++ expect(imgAttr).toMatchInlineSnapshot(`+ Object {+ "sizes": "100vw",+ "src": "/cdn-cgi/image/width=3840,quality=70,format=auto/https://webstudio.is/logo.webp",+ "srcSet": "/cdn-cgi/image/width=640,quality=70,format=auto/https://webstudio.is/logo.webp 640w, /cdn-cgi/image/width=750,quality=70,format=auto/https://webstudio.is/logo.webp 750w, /cdn-cgi/image/width=828,quality=70,format=auto/https://webstudio.is/logo.webp 828w, /cdn-cgi/image/width=1080,quality=70,format=auto/https://webstudio.is/logo.webp 1080w, /cdn-cgi/image/width=1200,quality=70,format=auto/https://webstudio.is/logo.webp 1200w, /cdn-cgi/image/width=1920,quality=70,format=auto/https://webstudio.is/logo.webp 1920w, /cdn-cgi/image/width=2048,quality=70,format=auto/https://webstudio.is/logo.webp 2048w, /cdn-cgi/image/width=3840,quality=70,format=auto/https://webstudio.is/logo.webp 3840w",+ }+ `);+ });++ test("custom loader", () => {+ const imgAttr = getImageAttributes({+ optimize: true,+ width: undefined,+ src: "https://webstudio.is/logo.webp",+ srcSet: undefined,+ sizes: "100vw",+ quality: 70,+ loader: ({ width, src, quality }) =>+ `${new URL(src).pathname}?w=${width}&q=${quality}`,+ });++ expect(imgAttr).toMatchInlineSnapshot(`+ Object {+ "sizes": "100vw",+ "src": "/logo.webp?w=3840&q=70",+ "srcSet": "/logo.webp?w=640&q=70 640w, /logo.webp?w=750&q=70 750w, /logo.webp?w=828&q=70 828w, /logo.webp?w=1080&q=70 1080w, /logo.webp?w=1200&q=70 1200w, /logo.webp?w=1920&q=70 1920w, /logo.webp?w=2048&q=70 2048w, /logo.webp?w=3840&q=70 3840w",+ }+ `);+ });+});++describe("Image optimizations not applied", () => {+ test("optimize is false", () => {+ const imgAttr = getImageAttributes({+ optimize: false,+ width: 100,+ src: "https://webstudio.is/logo.webp",+ srcSet: undefined,+ sizes: undefined,+ quality: 100,+ });++ expect(imgAttr).toMatchInlineSnapshot(`+ Object {+ "sizes": undefined,+ "src": "https://webstudio.is/logo.webp",+ "srcSet": undefined,+ }+ `);+ });++ test("srcSet is defined", () => {+ const imgAttr = getImageAttributes({+ optimize: true,+ width: 100,+ src: "https://webstudio.is/logo.webp",+ srcSet: "user-defined-srcset",+ sizes: undefined,+ quality: 100,+ });++ expect(imgAttr).toMatchInlineSnapshot(`+ Object {+ "sizes": undefined,+ "src": "https://webstudio.is/logo.webp",+ "srcSet": "user-defined-srcset",+ }+ `);+ });++ test("src is empty", () => {+ const imgAttr = getImageAttributes({+ optimize: true,+ width: 100,+ src: "",+ srcSet: undefined,+ sizes: undefined,+ quality: 100,+ });++ expect(imgAttr).toMatchInlineSnapshot(`null`);+ });++ test("src is undefined", () => {+ const imgAttr = getImageAttributes({+ optimize: true,+ width: 100,+ src: undefined,+ srcSet: undefined,+ sizes: undefined,+ quality: 100,+ });++ expect(imgAttr).toMatchInlineSnapshot(`null`);+ });+});diff --git a/packages/react-sdk/src/component-utils/image.ts b/packages/react-sdk/src/component-utils/image.tsnew file mode 100644index 0000000000..b3dd0f6314--- /dev/null+++ b/packages/react-sdk/src/component-utils/image.ts@@ -0,0 +1,268 @@+/**+ * # Responsive Image component helpers.+ *+ * ## Quick summary about img srcset and sizes attributes:+ *+ * There are 2 ways to define what image will be loaded in the img property srcset.+ *+ * 1. via pixel density descriptor 'x', like `srcset="photo-small.jpg 1x, photo-medium.jpg 1.5x, photo-huge.jpg 2x"`+ * src will be selected depending on `device-pixel-ratio`.+ *+ * 2. via viewport width descriptor 'w' and sizes property containing source size descriptors, like+ * `srcset="photo-small.jpg 320w, photo-medium.jpg 640w, photo-huge.jpg 1280w"`+ * `sizes="(max-width: 600px) 400px, (max-width: 1200px) 70vw, 50vw"`+ *+ * The browser finds the first matching media query from source size descriptors,+ * then use source size value to generate internally srcset+ * with pixel density descriptors dividing width descriptor value by source size value.+ *+ * Using the example above for viewport width 800px.+ * The first matching media query is (max-width: 1200px)+ * source size value is 70vw equal to 800px * 0,7 = 560px+ *+ * browser internal srcset will be (we divide `w` descriptor by source size value):+ * photo-small.jpg 320/560x, photo-medium.jpg 640/560x, photo-huge.jpg 1280/560x =>+ * photo-small.jpg 0.57x, photo-medium.jpg 1.14x, photo-huge.jpg 2.28x+ *+ * Finally same rules as for pixel density descriptor 'x' are applied.+ *+ * ## Algorithm (without optimizations):+ *+ * We have a predefined array of all supported image sizes allSizes, this is the real width of an image in pixels.+ * This is good for caching, as we can cache image with specific width and then use it for different devices.+ *+ * > allSizes array is a tradeoff between cache and the best possible image size you deliver to the user.+ * > If allSizes.length is too small, you will deliver too big images to the user,+ * > if allSizes.length is too big, you will have many caches misses.+ *+ * If img has a defined width property.+ * 1. find the first value from allSizes which is greater or equal to the width property+ * 2. use found value to generate srcset with pixel density descriptor 'x'+ *+ *+ * If img has no defined width property.+ * 1. Generate srcset = allSizes.map((w) => `${getImageSrcAtWidth(w)} ${w}w`)+ * 2. Use sizes property, or if it is not defined use opinionated DEFAULT_SIZES = "(min-width: 1280px) 50vw, 100vw";+ *+ * Optimizations applied now:+ *+ * - If the sizes property is defined, we can exclude from `srcsets` all images+ * which are smaller than the `smallestRatio * smallesDeviceSize`+ *+ * Future (not implemented) optimizations and improvements:+ *+ * - Knowing image size on different viewport widths we can provide nondefault sizes property+ * - Knowledge of Image aspect-ratio would allow cropping images serverside.+ * - Early hints for high priority images https://blog.cloudflare.com/early-hints/+ * - Slow networks optimizations+ * - 404 etc processing with CSS - https://bitsofco.de/styling-broken-images/ (has some opinionated issues) or js solution with custom user fallback.+ *+ * # Attributions+ *+ * The MIT License (MIT)+ *+ * applies to:+ *+ * - https://github.com/vercel/next.js, Copyright (c) 2022 Vercel, Inc.+ *+ * The MIT License (MIT)+ *+ * Copyright (c) 2022 Vercel, Inc.+ *+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software+ * and associated documentation files (the "Software"), to deal in the Software without restriction,+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software+ * is furnished to do so, subject to the following conditions:+ *+ * The above copyright notice and this permission notice shall be included in all copies+ * or substantial portions of the Software.+ *+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.+ **/++import warnOnce from "warn-once";++export type ImageLoader = (props: {+ width: number;+ quality: number;+ src: string;+}) => string;++/**+ * max(...imageSizes) must be less then min(...deviceSizes)+ **/+const imageSizes = [16, 32, 48, 64, 96, 128, 256, 384];+const deviceSizes = [640, 750, 828, 1080, 1200, 1920, 2048, 3840];++export const allSizes = [...imageSizes, ...deviceSizes];++/**+ * https://github.com/vercel/next.js/blob/canary/packages/next/client/image.tsx+ **/+const getWidths = (+ width: number | undefined,+ sizes: string | undefined+): { widths: number[]; kind: "w" | "x" } => {+ if (sizes) {+ // Find all the "vw" percent sizes used in the sizes prop+ const viewportWidthRe = /(^|\s)(1?\d?\d)vw/g;+ const percentSizes = [];+ for (let match; (match = viewportWidthRe.exec(sizes)); match) {+ percentSizes.push(parseInt(match[2], 10));+ }++ if (percentSizes.length) {+ // we can exclude from srcSets all images which are smaller than the smallestRatio * smallesDeviceSize+ const smallestRatio = Math.min(...percentSizes) * 0.01;+ return {+ widths: allSizes.filter((s) => s >= deviceSizes[0] * smallestRatio),+ kind: "w",+ };+ }+ return { widths: allSizes, kind: "w" };+ }++ if (width == null) {+ return { widths: deviceSizes, kind: "w" };+ }++ const widths = [+ ...new Set(+ [width, width * 2].map(+ (w) => allSizes.find((p) => p >= w) || allSizes[allSizes.length - 1]+ )+ ),+ ];+ return { widths, kind: "x" };+};++const generateImgAttrs = ({+ src,+ width,+ quality,+ sizes,+ loader,+}: {+ src: string;+ quality: number;+ width: number | undefined;+ sizes: string | undefined;+ loader: ImageLoader;+}): {+ src: string;+ srcSet: string | undefined;+ sizes: string | undefined;+} => {+ const { widths, kind } = getWidths(width, sizes);++ return {+ sizes: !sizes && kind === "w" ? "100vw" : sizes,+ srcSet: widths+ .map(+ (w, i) =>+ `${loader({ src, quality, width: w })} ${+ kind === "w" ? w : i + 1+ }${kind}`+ )+ .join(", "),++ // Must be last, to prevent Safari to load images twice+ src: loader({+ src,+ quality,+ width: widths[widths.length - 1],+ }),+ };+};++/**+ * Default image loader in case of no loader provided+ * https://developers.cloudflare.com/images/image-resizing/url-format/+ **/+const cloudflareImageLoader: ImageLoader = ({ width, src, quality }) => {+ if (process.env.NODE_ENV !== "production") {+ warnOnce(+ !allSizes.includes(width),+ "Width must be only from allowed values"+ );+ }++ const options = `width=${width},quality=${quality},format=auto`;+ // Cloudflare docs say that we don't need to urlencode the path params+ const pathname = `/cdn-cgi/image/${options}/${src}`;+ return pathname;+};++const getInt = (value: unknown): number | undefined => {+ if (typeof value === "number") {+ return Math.round(value);+ }++ if (typeof value === "string") {+ const vNum = Number.parseFloat(value);++ if (!Number.isNaN(vNum)) {+ return Math.round(vNum);+ }+ }++ return undefined;+};++/**+ * DEFAULT_SIZES Just an assumption that most images (except hero and icons) are 100% wide on mobile and 50% on desktop.+ * For icons width are usually set explicitly so DEFAULT_SIZES is not applied.+ * For hero images, we can allow in UI to select sizes=100vw explicitly.+ * Anyway, the best would be to calculate this based on canvas data from different breakpoints.+ * See ../component-utils/image for detailed description+ **/+const DEFAULT_SIZES = "(min-width: 1280px) 50vw, 100vw";++const DEFAULT_QUALITY = 80;++export const getImageAttributes = (props: {+ src: string | undefined;+ srcSet: string | undefined;+ sizes: string | undefined;+ width: string | number | undefined;+ quality: number | undefined;+ loader?: ImageLoader;+ optimize: boolean;+}): {+ src: string;+ srcSet: string | undefined;+ sizes: string | undefined;+} | null => {+ const width = getInt(props.width);+ const quality = Math.max(+ Math.min(getInt(props.quality) ?? DEFAULT_QUALITY, 100),+ 0+ );++ if (props.src != null && props.src != "") {+ if (props.srcSet == null && props.optimize) {+ const sizes =+ props.sizes ?? (props.width == null ? DEFAULT_SIZES : undefined);++ const loader = props.loader ?? cloudflareImageLoader;++ return generateImgAttrs({+ src: props.src,+ width,+ quality,+ sizes,+ loader,+ });+ }++ return { src: props.src, srcSet: props.srcSet, sizes: props.sizes };+ }++ return null;+};diff --git a/packages/react-sdk/src/components/image-dev.stories.tsx b/packages/react-sdk/src/components/image-dev.stories.tsxnew file mode 100644index 0000000000..aaf54898af--- /dev/null+++ b/packages/react-sdk/src/components/image-dev.stories.tsx@@ -0,0 +1,198 @@+// Story for image development, see https://github.com/webstudio-is/webstudio-designer/issues/387++import React, { type ComponentProps, type HTMLAttributes } from "react";+import type { ComponentMeta, ComponentStory } from "@storybook/react";+import { Image as ImagePrimitive } from "./image";+import { type ImageLoader } from "../component-utils/image";+import argTypes from "./image.props.json";++// to not allow include local assets everywhere, just enable it for this file+// eslint-disable-next-line @typescript-eslint/ban-ts-comment+// @ts-ignore+import localLogoImage from "../../storybook-assets/logo.webp";++export default {+ title: "Components/ImageDev",+ component: ImagePrimitive,+ argTypes,+} as ComponentMeta<typeof ImagePrimitive>;++type ImageProps = ComponentProps<typeof ImagePrimitive>;++/**+ * In case you need to test img with real cloudflare trasforms+ * set USE_CLOUDFLARE_IMAGE_TRANSFORM = true+ **/+const USE_CLOUDFLARE_IMAGE_TRANSFORM = false;++// For cloudflare image transform testing, logo should be the most consistent image on the site+const REMOTE_SELF_DOMAIN_IMAGE = "https://webstudio.is/logo.webp";++const imageSrc = USE_CLOUDFLARE_IMAGE_TRANSFORM+ ? REMOTE_SELF_DOMAIN_IMAGE+ : localLogoImage;++// In case of REMOTE_DEBUG+const cloudflareImageLoader: ImageLoader = ({ width, src, quality }) => {+ // No image transformation in development+ const resizeOrigin = "https://webstudio.is";++ const pathParams = [+ `width=${width}`,+ quality != null && `quality=${quality}`,+ "format=auto",+ ].filter(Boolean);++ const pathname = `/cdn-cgi/image/${pathParams.join(",")}/${src}`;++ const url = new URL(pathname, resizeOrigin);+ return url.href;+};++const localImageLoader: ImageLoader = ({ width, src, quality }) => {+ // Just emulate like we really resize the image+ const params = new URLSearchParams();+ params.set("width", `${width}`);+ params.set("quality", `${quality}`);+ return `${src}?${params.toString()}`;+};++const imageLoader = USE_CLOUDFLARE_IMAGE_TRANSFORM+ ? cloudflareImageLoader+ : localImageLoader;++const ImageTemplate: ComponentStory<+ React.ForwardRefExoticComponent<+ ImageProps & { style: HTMLAttributes<"img">["style"] }+ >+> = (args) => {+ const style = { maxWidth: "100%", ...args.style };++ return (+ <ImagePrimitive+ {...args}+ optimize={true}+ loader={imageLoader}+ style={style}+ />+ );+};++export const FixedWidthImage = ImageTemplate.bind({});++FixedWidthImage.parameters = {+ docs: {+ description: {+ story: "Load images depending on image width and device per pixel ratio.",+ },+ },+};++FixedWidthImage.args = {+ src: imageSrc,+ width: 300,+ height: 400,+};++export const FixedWidthImageCover = ImageTemplate.bind({});++FixedWidthImageCover.parameters = {+ docs: {+ description: {+ story:+ "Preserve ratio using object-fit: cover. Load images depending on image width and device per pixel ratio.",+ },+ },+};++FixedWidthImageCover.args = {+ src: imageSrc,+ width: 300,+ height: 400,+ style: { objectFit: "cover" },+};++export const UnknownWidthImage = ImageTemplate.bind({});++UnknownWidthImage.parameters = {+ docs: {+ description: {+ story: `Load images depending on the viewport width.`,+ },+ },+};++UnknownWidthImage.args = {+ src: imageSrc,+};++export const AspectRatioImage = ImageTemplate.bind({});++AspectRatioImage.parameters = {+ docs: {+ description: {+ story: `Fit width of the parent container, has own aspect-ratio and object-fit=cover. Load images depending on the viewport width.`,+ },+ },+};++AspectRatioImage.decorators = [+ (Story) => <div style={{ maxWidth: "400px" }}>{<Story />}</div>,+];++AspectRatioImage.args = {+ src: imageSrc,+ style: { aspectRatio: "2/1", objectFit: "cover", width: "100%" },+};++export const FillParentImage = ImageTemplate.bind({});++FillParentImage.parameters = {+ docs: {+ description: {+ story: `Fill width and height of the relative parent container, object-fit=cover. Load images depending on the viewport width.`,+ },+ },+};++FillParentImage.decorators = [+ (Story) => (+ <div+ style={{+ maxWidth: "400px",+ aspectRatio: "2/1",+ position: "relative",+ border: "1px solid red",+ }}+ >+ {<Story />}+ </div>+ ),+];++FillParentImage.args = {+ src: imageSrc,+ style: {+ position: "absolute",+ inset: 0,+ width: "100%",+ height: "100%",+ objectFit: "cover",+ },+};++export const HeroImage = ImageTemplate.bind({});++HeroImage.parameters = {+ docs: {+ description: {+ story: `"sizes" attribute explicitly equal to 100vw allowing to skip the default behavior. See DEFAULT_SIZES in the Image component. Load images depending on the viewport width.`,+ },+ },+};++HeroImage.args = {+ src: imageSrc,+ sizes: "100vw",+ style: { aspectRatio: "3/4", objectFit: "cover", width: "100%" },+};diff --git a/packages/react-sdk/src/components/image.props.json b/packages/react-sdk/src/components/image.props.jsonindex dc8cffeec8..d9de266fdb 100644--- a/packages/react-sdk/src/components/image.props.json+++ b/packages/react-sdk/src/components/image.props.json@@ -158,7 +158,7 @@"options": ["async", "auto", "sync"]},"height": {- "defaultValue": null,+ "defaultValue": "","description": "","name": "height","parent": {@@ -363,7 +363,7 @@}},"width": {- "defaultValue": null,+ "defaultValue": "","description": "","name": "width","parent": {@@ -2522,7 +2522,7 @@}},"quality": {- "defaultValue": 80,+ "defaultValue": null,"description": "","name": "quality","declarations": [@@ -2538,5 +2538,41 @@"control": {"type": "number"}+ },+ "loader": {+ "defaultValue": null,+ "description": "",+ "name": "loader",+ "declarations": [+ {+ "fileName": "src/components/image.tsx",+ "name": "TypeLiteral"+ }+ ],+ "required": false,+ "type": {+ "name": "ImageLoader"+ },+ "control": {+ "type": "text"+ }+ },+ "optimize": {+ "defaultValue": null,+ "description": "",+ "name": "optimize",+ "declarations": [+ {+ "fileName": "src/components/image.tsx",+ "name": "TypeLiteral"+ }+ ],+ "required": false,+ "type": {+ "name": "boolean"+ },+ "control": {+ "type": "boolean"+ }}}diff --git a/packages/react-sdk/src/components/image.tsx b/packages/react-sdk/src/components/image.tsxindex b4011ecb86..be01ae2e3d 100644--- a/packages/react-sdk/src/components/image.tsx+++ b/packages/react-sdk/src/components/image.tsx@@ -1,24 +1,46 @@import React, { forwardRef, type ElementRef, type ComponentProps } from "react";+import { getImageAttributes, type ImageLoader } from "../component-utils/image";const defaultTag = "img";-type ImageProps = ComponentProps<typeof defaultTag>;+type ImageProps = ComponentProps<typeof defaultTag> & {+ quality?: number;+ loader?: ImageLoader;+ optimize?: boolean;+};export const Image = forwardRef<ElementRef<typeof defaultTag>, ImageProps>(- (props, ref) => <img {...props} ref={ref} />+ ({ quality, loader, optimize, ...imageProps }, ref) => {+ // Temporary set to false, to support previous image behaviour+ const DEFAULT_OPTIMIZE = false;++ const imageAttributes = getImageAttributes({+ src: imageProps.src,+ srcSet: imageProps.srcSet,+ sizes: imageProps.sizes,+ width: imageProps.width,+ quality,+ loader,+ optimize: optimize ?? DEFAULT_OPTIMIZE,+ }) ?? { src: imagePlaceholderSvg };++ return (+ <img {...imageProps} {...imageAttributes} decoding="async" ref={ref} />+ );+ });Image.defaultProps = {src: "",+ width: "",+ height: "",loading: "lazy",- width: "auto",- height: "auto",alt: "",};Image.displayName = "Image";-const imagePlaceholderSvg = `<svg+const imagePlaceholderSvg = `data:image/svg+xml;base64,${btoa(`<svgwidth="140"height="140"viewBox="0 0 600 600"@@ -40,11 +62,4 @@ const imagePlaceholderSvg = `<svgd="M160 405V367.205L221.609 306.364L256.552 338.628L358.161 234L440 316.043V405H160Z"fill="#A2A2A2"/>-</svg>`;--// Defining src here prevents analyzer from putting the image into the UI.-// @todo it would be better to have the image hosted like a normal image,-// the problem is inside sdk bundler doesn't know how to do that.-Image.defaultProps.src = `data:image/svg+xml;base64,${btoa(- imagePlaceholderSvg-)}`;+</svg>`)}`;diff --git a/packages/react-sdk/src/components/image.ws.tsx b/packages/react-sdk/src/components/image.ws.tsxindex 384d00f272..afa6b02367 100644--- a/packages/react-sdk/src/components/image.ws.tsx+++ b/packages/react-sdk/src/components/image.ws.tsx@@ -2,10 +2,19 @@ import { ImageIcon } from "@webstudio-is/icons";import type { WsComponentMeta } from "./component-type";import { Image } from "./image";+const defaultStyle = {+ maxWidth: {+ type: "unit",+ unit: "%",+ value: 100,+ },+} as const;+const meta: WsComponentMeta<typeof Image> = {Icon: ImageIcon,Component: Image,canAcceptChildren: false,+ defaultStyle,isContentEditable: false,isInlineOnly: false,isListed: true,diff --git a/packages/react-sdk/storybook-assets/logo.webp b/packages/react-sdk/storybook-assets/logo.webpnew file mode 100644index 0000000000..fa18ef216dBinary files /dev/null and b/packages/react-sdk/storybook-assets/logo.webp differdiff --git a/yarn.lock b/yarn.lockindex 8878d08dc6..d1c5500c7e 100644--- a/yarn.lock+++ b/yarn.lock@@ -18136,6 +18136,11 @@ walker@^1.0.7, walker@~1.0.5:dependencies:makeerror "1.0.12"+warn-once@^0.1.1:+ version "0.1.1"+ resolved "https://registry.yarnpkg.com/warn-once/-/warn-once-0.1.1.tgz#952088f4fb56896e73fd4e6a3767272a3fccce43"+ integrity sha512-VkQZJbO8zVImzYFteBXvBOZEl1qL175WH8VmZcxF2fZAoudNhNDvHi+doCaAEdU2l2vtcIwa2zn0QK5+I1HQ3Q==+watchpack-chokidar2@^2.0.1:version "2.0.1"resolved "https://registry.yarnpkg.com/watchpack-chokidar2/-/watchpack-chokidar2-2.0.1.tgz#38500072ee6ece66f3769936950ea1771be1c957"