UNPKG

react-img-component

Version:

A responsive lazy-loading image component for React

63 lines (61 loc) 2.04 kB
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 index_exports = {}; __export(index_exports, { default: () => Image_default }); module.exports = __toCommonJS(index_exports); // src/Image.tsx var import_react = require("react"); var import_jsx_runtime = require("react/jsx-runtime"); function ReactImage(props) { const { src, alt, placeholderSrc, ...restProps } = props; const [isVisible, setIsVisible] = (0, import_react.useState)(false); const imgRef = (0, import_react.useRef)(null); (0, import_react.useEffect)(() => { const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { setIsVisible(true); observer.disconnect(); } }); }, { threshold: 0.1 } ); if (imgRef.current) observer.observe(imgRef.current); return () => observer.disconnect(); }, []); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "img", { ref: imgRef, src: isVisible ? src : placeholderSrc, alt, style: { filter: isVisible ? "none" : "blur(10px)", transition: "filter 0.3s ease-in-out" }, ...restProps } ); } var Image_default = ReactImage; //# sourceMappingURL=index.js.map