UNPKG

@prismicio/react

Version:

React components and hooks to fetch and present Prismic content

51 lines (50 loc) 2.44 kB
import { jsx, Fragment } from "react/jsx-runtime"; import { forwardRef } from "react"; import { isFilled, asImageWidthSrcSet, asImagePixelDensitySrcSet } from "@prismicio/client"; import { DEV } from "esm-env"; import { devMsg } from "./lib/devMsg.js"; const PrismicImage = forwardRef(function PrismicImage2(props, ref) { const { field, alt, fallbackAlt, imgixParams = {}, widths, pixelDensities, fallback, ...restProps } = props; if (DEV) { if (typeof alt === "string" && alt !== "") { console.warn(`[PrismicImage] The "alt" prop can only be used to declare an image as decorative by passing an empty string (alt="") but was provided a non-empty string. You can resolve this warning by removing the "alt" prop or changing it to alt="". For more details, see ${devMsg("alt-must-be-an-empty-string")}`); } if (typeof fallbackAlt === "string" && fallbackAlt !== "") { console.warn(`[PrismicImage] The "fallbackAlt" prop can only be used to declare an image as decorative by passing an empty string (fallbackAlt="") but was provided a non-empty string. You can resolve this warning by removing the "fallbackAlt" prop or changing it to fallbackAlt="". For more details, see ${devMsg("alt-must-be-an-empty-string")}`); } if (widths && pixelDensities) { console.warn(`[PrismicImage] Only one of "widths" or "pixelDensities" props can be provided. You can resolve this warning by removing either the "widths" or "pixelDensities" prop. "widths" will be used in this case.`); } } if (!isFilled.imageThumbnail(field)) { return jsx(Fragment, { children: fallback }); } const resolvedImgixParams = imgixParams; for (const x in imgixParams) { if (resolvedImgixParams[x] === null) { resolvedImgixParams[x] = void 0; } } let src; let srcSet; if (widths || !pixelDensities) { const res = asImageWidthSrcSet(field, { ...resolvedImgixParams, widths: widths === "defaults" ? void 0 : widths }); src = res.src; srcSet = res.srcset; } else if (pixelDensities) { const res = asImagePixelDensitySrcSet(field, { ...resolvedImgixParams, pixelDensities: pixelDensities === "defaults" ? void 0 : pixelDensities }); src = res.src; srcSet = res.srcset; } return jsx("img", { ref, src, srcSet, alt: alt ?? (field.alt || fallbackAlt), ...restProps }); }); export { PrismicImage }; //# sourceMappingURL=PrismicImage.js.map