@chakra-ui/image
Version:
React component for progressive image loading
191 lines (184 loc) • 5.64 kB
JavaScript
'use client'
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
Image: () => Image2,
Img: () => Img,
useImage: () => useImage
});
module.exports = __toCommonJS(src_exports);
// src/image.tsx
var import_system2 = require("@chakra-ui/system");
// ../../utilities/object-utils/src/omit.ts
function omit(object, keysToOmit = []) {
const clone = Object.assign({}, object);
for (const key of keysToOmit) {
if (key in clone) {
delete clone[key];
}
}
return clone;
}
// src/use-image.ts
var import_react_use_safe_layout_effect = require("@chakra-ui/react-use-safe-layout-effect");
var import_react = require("react");
function useImage(props) {
const {
loading,
src,
srcSet,
onLoad,
onError,
crossOrigin,
sizes,
ignoreFallback
} = props;
const [status, setStatus] = (0, import_react.useState)("pending");
(0, import_react.useEffect)(() => {
setStatus(src ? "loading" : "pending");
}, [src]);
const imageRef = (0, import_react.useRef)();
const load = (0, import_react.useCallback)(() => {
if (!src)
return;
flush();
const img = new Image();
img.src = src;
if (crossOrigin)
img.crossOrigin = crossOrigin;
if (srcSet)
img.srcset = srcSet;
if (sizes)
img.sizes = sizes;
if (loading)
img.loading = loading;
img.onload = (event) => {
flush();
setStatus("loaded");
onLoad == null ? void 0 : onLoad(event);
};
img.onerror = (error) => {
flush();
setStatus("failed");
onError == null ? void 0 : onError(error);
};
imageRef.current = img;
}, [src, crossOrigin, srcSet, sizes, onLoad, onError, loading]);
const flush = () => {
if (imageRef.current) {
imageRef.current.onload = null;
imageRef.current.onerror = null;
imageRef.current = null;
}
};
(0, import_react_use_safe_layout_effect.useSafeLayoutEffect)(() => {
if (ignoreFallback)
return void 0;
if (status === "loading") {
load();
}
return () => {
flush();
};
}, [status, load, ignoreFallback]);
return ignoreFallback ? "loaded" : status;
}
var shouldShowFallbackImage = (status, fallbackStrategy) => status !== "loaded" && fallbackStrategy === "beforeLoadOrError" || status === "failed" && fallbackStrategy === "onError";
// src/native-image.tsx
var import_system = require("@chakra-ui/system");
var import_jsx_runtime = require("react/jsx-runtime");
var NativeImage = (0, import_system.forwardRef)(function NativeImage2(props, ref) {
const { htmlWidth, htmlHeight, alt, ...rest } = props;
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("img", { width: htmlWidth, height: htmlHeight, ref, alt, ...rest });
});
NativeImage.displayName = "NativeImage";
// src/image.tsx
var import_jsx_runtime2 = require("react/jsx-runtime");
var Image2 = (0, import_system2.forwardRef)(function Image3(props, ref) {
const {
fallbackSrc,
fallback,
src,
srcSet,
align,
fit,
loading,
ignoreFallback,
crossOrigin,
fallbackStrategy = "beforeLoadOrError",
referrerPolicy,
...rest
} = props;
const providedFallback = fallbackSrc !== void 0 || fallback !== void 0;
const shouldIgnoreFallbackImage = loading != null || // use can opt out of fallback image
ignoreFallback || // if the user doesn't provide any kind of fallback we should ignore it
!providedFallback;
const status = useImage({
...props,
crossOrigin,
ignoreFallback: shouldIgnoreFallbackImage
});
const showFallbackImage = shouldShowFallbackImage(status, fallbackStrategy);
const shared = {
ref,
objectFit: fit,
objectPosition: align,
...shouldIgnoreFallbackImage ? rest : omit(rest, ["onError", "onLoad"])
};
if (showFallbackImage) {
if (fallback)
return fallback;
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
import_system2.chakra.img,
{
as: NativeImage,
className: "chakra-image__placeholder",
src: fallbackSrc,
...shared
}
);
}
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
import_system2.chakra.img,
{
as: NativeImage,
src,
srcSet,
crossOrigin,
loading,
referrerPolicy,
className: "chakra-image",
...shared
}
);
});
Image2.displayName = "Image";
// src/img.tsx
var import_system3 = require("@chakra-ui/system");
var import_jsx_runtime3 = require("react/jsx-runtime");
var Img = (0, import_system3.forwardRef)((props, ref) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_system3.chakra.img, { ref, as: NativeImage, className: "chakra-image", ...props }));
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Image,
Img,
useImage
});
//# sourceMappingURL=index.js.map