import React from "react"; import NextImage from "next/image"; import type { ComponentProps, CssProperties, Asset } from "@webiny/website-builder-react"; import { getAssetUrl, getImageDimensions, getImageSrcSet, normalizeToAsset } from "@webiny/website-builder-react"; type ImageProps = ComponentProps<{ title: string; altText: string; highPriority: boolean; // Accepts the unified asset shape or a legacy value (existing pages). image: Asset; }>; export const ImageComponent = (props: ImageProps) => { const { title = "", altText, image, highPriority } = props.inputs; const asset = normalizeToAsset(image); if (!asset?.src) { return ; } const alt = altText || asset.image?.alt || ""; // SVGs are vector — nothing to crop, resize, or re-encode. if (asset.src.endsWith(".svg")) { return ( ); } // The delivery bakes the per-usage crop (`?crop`), resizes (`?width`), and serves // a modern format negotiated from Accept (`?format=auto`). We hand `next/image` a // loader that builds that URL, and it drives the responsive `srcSet`. const loader = ({ width }: { width: number }) => getAssetUrl(asset, { width, format: "auto" }); // Intrinsic size of the *delivered* (cropped) image, so next/image lays out at // the correct aspect ratio. Falls back to a plain when dimensions are // unknown (next/image requires width + height unless `fill`). const { width, height } = getImageDimensions(asset); const style: CssProperties = { maxWidth: "100%", height: "auto", ...props.styles }; if (!width || !height) { // No intrinsic size to lay out with — fall back to a plain , still // responsive via the SDK's framework-agnostic srcSet (crop/format baked in). const { src, srcSet } = getImageSrcSet(asset, { format: "auto", cssWidth: props.styles?.width }); // eslint-disable-next-line @next/next/no-img-element return ( {alt} ); } return ( ); }; const ImagePlaceholder = ({ style }: { style: CssProperties }) => { return (
); };