UNPKG

@prismicio/next

Version:

Helpers to integrate Prismic into Next.js apps

62 lines (61 loc) 3.42 kB
"use client"; "use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const jsxRuntime = require("react/jsx-runtime"); const react = require("react"); const Image = require("next/image"); const imgixUrlBuilder = require("imgix-url-builder"); const client = require("@prismicio/client"); const devMsg = require("./lib/devMsg.cjs"); const resolveDefaultExport = require("./lib/resolveDefaultExport.cjs"); const imgixLoader = require("./imgixLoader.cjs"); const castInt = (input) => { if (typeof input === "number" || typeof input === "undefined") { return input; } else { const parsed = Number.parseInt(input); if (Number.isNaN(parsed)) { return void 0; } else { return parsed; } } }; const PrismicNextImage = react.forwardRef(function PrismicNextImage2({ field, imgixParams = {}, alt, fallbackAlt, fill, width, height, fallback = null, loader = 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 ${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 ${devMsg.devMsg("alt-must-be-an-empty-string")}`); } } if (!client.isFilled.imageThumbnail(field)) { return jsxRuntime.jsx(jsxRuntime.Fragment, { children: fallback }); } const resolvedImgixParams = imgixParams; for (const x in imgixParams) { if (resolvedImgixParams[x] === null) { resolvedImgixParams[x] = void 0; } } const src = imgixUrlBuilder.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); } const ResolvedImage = resolveDefaultExport.resolveDefaultExport(Image); return jsxRuntime.jsx(ResolvedImage, { ref, src, width: fill ? void 0 : resolvedWidth, height: fill ? void 0 : resolvedHeight, alt: resolvedAlt, fill, loader: loader === null ? void 0 : loader, ...restProps }); }); exports.PrismicNextImage = PrismicNextImage; //# sourceMappingURL=PrismicNextImage.cjs.map