UNPKG

@prismicio/next

Version:

Helpers to integrate Prismic into Next.js apps

67 lines (66 loc) 4.14 kB
"use client"; const require_runtime = require("./_virtual/_rolldown/runtime.cjs"); const require_resolveDefaultExport = require("./lib/resolveDefaultExport.cjs"); const require_imgixLoader = require("./imgixLoader.cjs"); const require_devMsg = require("./lib/devMsg.cjs"); let _prismicio_client = require("@prismicio/client"); let react = require("react"); let react_jsx_runtime = require("react/jsx-runtime"); let imgix_url_builder = require("imgix-url-builder"); let next_image_js = require("next/image.js"); next_image_js = require_runtime.__toESM(next_image_js); //#region src/PrismicNextImage.tsx const castInt = (input) => { if (typeof input === "number" || typeof input === "undefined") return input; else { const parsed = Number.parseInt(input); if (Number.isNaN(parsed)) return; else return parsed; } }; /** * React component that renders an image from a Prismic Image field or one of its thumbnails using * `next/image`. It will automatically set the `alt` attribute using the Image field's `alt` * property. * * It uses an Imgix URL-based loader by default. A custom loader can be provided with the `loader` * prop. If you would like to use the Next.js Image Optimization API instead, set * `loader={undefined}`. * * @param props - Props for the component. * @returns A responsive image component using `next/image` for the given Image * field. * @see To learn more about `next/image`, see: https://nextjs.org/docs/api-reference/next/image */ const PrismicNextImage = (0, react.forwardRef)(function PrismicNextImage({ field, imgixParams = {}, alt, fallbackAlt, fill, width, height, fallback = null, loader = require_imgixLoader.imgixLoader, ...restProps }, ref) { if (process.env.NODE_ENV === "development") { if (typeof alt === "string" && alt !== "") console.warn(`[PrismicNextImage] 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 ${require_devMsg.devMsg("alt-must-be-an-empty-string")}`); if (typeof fallbackAlt === "string" && fallbackAlt !== "") console.warn(`[PrismicNextImage] 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 ${require_devMsg.devMsg("alt-must-be-an-empty-string")}`); } if (!_prismicio_client.isFilled.imageThumbnail(field)) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: fallback }); const resolvedImgixParams = imgixParams; for (const x in imgixParams) if (resolvedImgixParams[x] === null) resolvedImgixParams[x] = void 0; const src = (0, imgix_url_builder.buildURL)(field.url, imgixParams); const ar = field.dimensions.width / field.dimensions.height; const castedWidth = castInt(width); const castedHeight = castInt(height); let resolvedWidth = castedWidth ?? field.dimensions.width; let resolvedHeight = castedHeight ?? field.dimensions.height; if (castedWidth != null && castedHeight == null) resolvedHeight = castedWidth / ar; else if (castedWidth == null && castedHeight != null) resolvedWidth = castedHeight * ar; const resolvedAlt = alt ?? (field.alt || fallbackAlt); if (process.env.NODE_ENV === "development" && typeof resolvedAlt !== "string") console.error(`[PrismicNextImage] The following image is missing an "alt" property. Please add Alternative Text to the image in Prismic. To mark the image as decorative instead, add one of \`alt=""\` or \`fallbackAlt=""\`.`, src); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_resolveDefaultExport.resolveDefaultExport(next_image_js.default), { ref, src, width: fill ? void 0 : resolvedWidth, height: fill ? void 0 : resolvedHeight, alt: resolvedAlt, fill, loader: loader === null ? void 0 : loader, ...restProps }); }); //#endregion exports.PrismicNextImage = PrismicNextImage; //# sourceMappingURL=PrismicNextImage.cjs.map