Files
webstudio/packages/sdk-components-react/src/image-utils.ts
T
Oleg Isonen db3b11970f fix: Optimize images in Markdown Embed (#6089)
## Summary

Markdown Embed now renders image tags with the same optimized attributes
and canvas behavior as the SDK Image component.

The implementation shares the complete attribute-resolution path without
rendering React inside the Markdown pipeline:

- `@webstudio-is/image` exposes one framework-independent
`getImageAttributes` function for optimization, placeholder fallback,
loading, decoding, dimensions, alternative text, and responsive sources.
- React-only element props such as `className`, `style`, handlers,
context, and refs stay in React callers.
- The SDK Image component and Markdown renderer both use
`getSdkImageProps` for canvas-specific loading, decoding, asset
identity, and incomplete-upload handling.
- Markdown retains only the necessary HTML attribute-name conversion,
sanitization, and Safari-safe `sizes` / `srcset` / `src` ordering.
- The redundant low-level React Image primitive was removed. Its callers
now render native `img` elements, and the image package no longer has
React runtime or peer dependencies.

## Compatibility and tradeoffs

- The SDK Image component remains the public React component because it
owns React context and ref forwarding.
- The removed low-level `@webstudio-is/image` Image export is replaced
by the pure attribute function for internal callers.
- No data migration or schema change is required.
- Loader output is sanitized after transformation, while unsafe input
URLs are sanitized before reaching the loader.

## Todo

- [x] Share image attribute resolution between native, SDK, and Markdown
images.
- [x] Preserve SDK canvas behavior for Markdown images.
- [x] Preserve safe URL sanitization and Safari-safe responsive
attribute ordering.
- [x] Remove the redundant low-level React image primitive and migrate
its callers.
- [x] Remove the overlapping `getImageProps` abstraction.
- [x] Add regression coverage comparing Markdown, SDK Image, and native
attribute output.
- [x] Review documentation impact and update the Markdown Embed
documentation.
- [x] Run targeted tests, typechecks, builds, lint, formatting,
package-boundary checks, and fixture generation.

## Verification

- `pnpm --filter @webstudio-is/image test`
- `pnpm --filter @webstudio-is/image typecheck`
- `pnpm --filter @webstudio-is/image build`
- `pnpm --filter @webstudio-is/image dts`
- `pnpm --filter @webstudio-is/sdk-components-react test`
- `pnpm --filter @webstudio-is/sdk-components-react typecheck`
- `pnpm --filter @webstudio-is/sdk-components-react build`
- `pnpm --filter @webstudio-is/sdk-components-react dts`
- `pnpm --filter @webstudio-is/builder typecheck`
- `pnpm --filter @webstudio-is/design-system typecheck`
- Targeted Oxlint and Oxfmt checks
- `pnpm check:package-boundaries`
- `pnpm fixtures` (no fixture diff)

Ref #6014
2026-08-10 18:21:45 +02:00

78 lines
1.7 KiB
TypeScript

import { getImageAttributes, type ImageLoader } from "@webstudio-is/image";
import type { ComponentPropsWithoutRef } from "react";
export type SdkImageProps = ComponentPropsWithoutRef<"img"> & {
quality?: number;
/** Optimize the image for enhanced performance. */
optimize?: boolean;
// @todo: This builder-only prop should not be part of the runtime component.
$webstudio$canvasOnly$assetId?: string;
};
export const getSdkImageProps = ({
props,
imageLoader,
renderer,
}: {
props: SdkImageProps;
imageLoader: ImageLoader;
renderer: "canvas" | "preview" | undefined;
}) => {
let {
loading = "lazy",
width,
height,
optimize = true,
decoding,
quality,
$webstudio$canvasOnly$assetId,
alt,
sizes,
srcSet,
src: source,
...imageProps
} = props;
const src = String(source ?? "");
let key = src;
if (renderer === "canvas") {
// With disabled cache and loading lazy, Chrome may not render the image.
loading = "eager";
// Avoid image flickering when switching from preview to an uploaded asset.
decoding = "sync";
key = $webstudio$canvasOnly$assetId ?? src;
// NaN dimensions mean the image is still uploading and cannot be optimized.
if (
width !== undefined &&
height !== undefined &&
Number.isNaN(width) &&
Number.isNaN(height)
) {
optimize = false;
width = undefined;
height = undefined;
}
}
return {
key,
imageProps: {
...imageProps,
...getImageAttributes({
alt,
width,
height,
sizes,
src,
srcSet,
quality,
loader: imageLoader,
optimize,
loading,
decoding,
}),
},
};
};