react-lazy-img-observer 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +112 -125
- package/dist/cjs/ImageLazy.js +15 -9
- package/dist/cjs/ImageLazy.min.js +1 -1
- package/dist/esm/ImageLazy.js +14 -9
- package/dist/esm/ImageLazy.min.js +1 -1
- package/dist/types/ImageLazy.d.ts +1 -0
- package/package.json +54 -37
- package/src/ImageLazy.tsx +0 -74
- package/tsconfig.cjs.json +0 -7
- package/tsconfig.esm.json +0 -7
- package/tsconfig.json +0 -19
package/README.md
CHANGED
@@ -1,125 +1,112 @@
|
|
1
|
-
# ImageLazy
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
}
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
|
44
|
-
|
45
|
-
| `
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
}
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
##
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
.
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
MIT
|
114
|
-
|
115
|
-
## Contributing
|
116
|
-
|
117
|
-
Contributions are welcome! Please open an issue or submit a pull request for any bugs or feature requests.
|
118
|
-
|
119
|
-
## Author
|
120
|
-
|
121
|
-
Percy Chuzon
|
122
|
-
|
123
|
-
## Acknowledgments
|
124
|
-
|
125
|
-
Thanks to the React and Intersection Observer API documentation for guidance.
|
1
|
+
# 📷 ImageLazy
|
2
|
+
|
3
|
+
**ImageLazy** is a lightweight React component for lazy loading images using the Intersection Observer API. It delays the loading of off-screen images until they appear in the viewport, helping optimize performance and reduce initial load times.
|
4
|
+
|
5
|
+

|
6
|
+

|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
## 📦 Installation
|
11
|
+
|
12
|
+
```bash
|
13
|
+
npm install react-lazy-img-observer
|
14
|
+
```
|
15
|
+
|
16
|
+
---
|
17
|
+
|
18
|
+
## 🚀 Usage
|
19
|
+
|
20
|
+
### Basic example (JSX or TSX)
|
21
|
+
|
22
|
+
```tsx
|
23
|
+
import ImageLazy from "react-lazy-img-observer";
|
24
|
+
|
25
|
+
const Example = () => (
|
26
|
+
<ImageLazy
|
27
|
+
src="https://example.com/image.jpg"
|
28
|
+
alt="Description of the image"
|
29
|
+
width={600}
|
30
|
+
height={400}
|
31
|
+
className="custom-class"
|
32
|
+
id="image-1"
|
33
|
+
title="Image title"
|
34
|
+
extraData={{ "data-custom": "value" }}
|
35
|
+
/>
|
36
|
+
);
|
37
|
+
```
|
38
|
+
|
39
|
+
---
|
40
|
+
|
41
|
+
## 🧩 Props
|
42
|
+
|
43
|
+
| Prop | Type | Required | Description |
|
44
|
+
|-------------|---------------------------------------------|----------|-------------------------------------------------------|
|
45
|
+
| `src` | `string` | ✅ | The source URL of the image. |
|
46
|
+
| `alt` | `string` | ✅ | The alt text for accessibility. |
|
47
|
+
| `width` | `number` | ❌ | The width of the image. |
|
48
|
+
| `height` | `number` | ❌ | The height of the image. |
|
49
|
+
| `id` | `string \| number` | ❌ | Optional ID for the image element. |
|
50
|
+
| `className` | `string` | ❌ | CSS class for custom styling. |
|
51
|
+
| `title` | `string` | ❌ | Tooltip text shown on hover. |
|
52
|
+
| `extraData` | `React.ImgHTMLAttributes<HTMLImageElement>` | ❌ | Any extra HTML attributes (e.g., `data-*`). |
|
53
|
+
|
54
|
+
---
|
55
|
+
|
56
|
+
## 📚 How It Works
|
57
|
+
|
58
|
+
The component uses the [`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) API to detect when the image enters the viewport. Once at least **50%** of the image is visible, the real `src` is assigned to the image, triggering the browser to load it.
|
59
|
+
|
60
|
+
---
|
61
|
+
|
62
|
+
## ⚙️ Intersection Observer Configuration
|
63
|
+
|
64
|
+
- `root`: `null` (default viewport)
|
65
|
+
- `rootMargin`: `"0px"`
|
66
|
+
- `threshold`: `0.5` (50% visibility required)
|
67
|
+
|
68
|
+
---
|
69
|
+
|
70
|
+
## 🎨 Styling
|
71
|
+
|
72
|
+
The image starts blurred and transitions smoothly once it’s loaded. You can override or enhance this with your own styles:
|
73
|
+
|
74
|
+
```css
|
75
|
+
.custom-class {
|
76
|
+
filter: blur(20px);
|
77
|
+
transition: filter 0.9s ease;
|
78
|
+
}
|
79
|
+
|
80
|
+
.custom-class.loaded {
|
81
|
+
filter: none;
|
82
|
+
}
|
83
|
+
```
|
84
|
+
|
85
|
+
You may also override the `style` prop directly if preferred.
|
86
|
+
|
87
|
+
---
|
88
|
+
|
89
|
+
## 📄 License
|
90
|
+
|
91
|
+
[ISC License](./LICENSE)
|
92
|
+
|
93
|
+
---
|
94
|
+
|
95
|
+
## 🤝 Contributing
|
96
|
+
|
97
|
+
Contributions are welcome!
|
98
|
+
Feel free to [open an issue](https://github.com/perch33/react-lazy-img-observer/issues) or submit a pull request.
|
99
|
+
|
100
|
+
---
|
101
|
+
|
102
|
+
## 👤 Author
|
103
|
+
|
104
|
+
**Percy Chuzon**
|
105
|
+
📧 contacto@percychuzon.com
|
106
|
+
🌐 [https://percychuzon.com](https://percychuzon.com)
|
107
|
+
|
108
|
+
---
|
109
|
+
|
110
|
+
## 🙏 Acknowledgments
|
111
|
+
|
112
|
+
Thanks to the React community and the MDN docs for inspiration and guidance.
|
package/dist/cjs/ImageLazy.js
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ImageLazy = void 0;
|
3
4
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
4
5
|
const react_1 = require("react");
|
5
6
|
const ImageLazy = ({ src, alt, width, height, className, id, extraData, title, }) => {
|
6
7
|
const imageRef = (0, react_1.useRef)(null);
|
7
8
|
const [isIntersecting, setIsIntersecting] = (0, react_1.useState)(false);
|
8
9
|
(0, react_1.useEffect)(() => {
|
10
|
+
if (typeof IntersectionObserver === "undefined" ||
|
11
|
+
!imageRef.current) {
|
12
|
+
return;
|
13
|
+
}
|
9
14
|
const observer = new IntersectionObserver((entries) => {
|
10
15
|
entries.forEach((entry) => {
|
11
16
|
if (entry.isIntersecting) {
|
@@ -16,19 +21,20 @@ const ImageLazy = ({ src, alt, width, height, className, id, extraData, title, }
|
|
16
21
|
observer.unobserve(entry.target);
|
17
22
|
}
|
18
23
|
});
|
19
|
-
}, {
|
20
|
-
|
21
|
-
|
22
|
-
|
24
|
+
}, {
|
25
|
+
root: null,
|
26
|
+
rootMargin: "0px",
|
27
|
+
threshold: 0.5,
|
28
|
+
});
|
29
|
+
observer.observe(imageRef.current);
|
23
30
|
return () => {
|
24
|
-
|
25
|
-
observer.unobserve(imageRef.current);
|
26
|
-
}
|
31
|
+
observer.disconnect();
|
27
32
|
};
|
28
33
|
}, [src]);
|
29
|
-
return ((0, jsx_runtime_1.jsx)("img", {
|
34
|
+
return ((0, jsx_runtime_1.jsx)("img", { ref: imageRef, "data-src": src, alt: alt, title: title, className: className, width: width, height: height, loading: "lazy", id: id?.toString(), style: {
|
30
35
|
filter: isIntersecting ? "none" : "blur(20px)",
|
31
36
|
transition: "filter 0.9s",
|
32
|
-
},
|
37
|
+
}, ...extraData }));
|
33
38
|
};
|
39
|
+
exports.ImageLazy = ImageLazy;
|
34
40
|
exports.default = ImageLazy;
|
@@ -1 +1 @@
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),ImageLazy=({src:e,alt:t,width:r,height:s,className:a,id:n,extraData:
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ImageLazy=void 0;const jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),ImageLazy=({src:e,alt:t,width:r,height:s,className:a,id:n,extraData:i,title:c})=>{const o=(0,react_1.useRef)(null),[u,l]=(0,react_1.useState)(!1);return(0,react_1.useEffect)((()=>{if("undefined"==typeof IntersectionObserver||!o.current)return;const e=new IntersectionObserver((t=>{t.forEach((t=>{t.isIntersecting&&(l(!0),o.current&&(o.current.src=o.current.dataset.src||""),e.unobserve(t.target))}))}),{root:null,rootMargin:"0px",threshold:.5});return e.observe(o.current),()=>{e.disconnect()}}),[e]),(0,jsx_runtime_1.jsx)("img",{ref:o,"data-src":e,alt:t,title:c,className:a,width:r,height:s,loading:"lazy",id:n?.toString(),style:{filter:u?"none":"blur(20px)",transition:"filter 0.9s"},...i})};exports.ImageLazy=ImageLazy,exports.default=ImageLazy;
|
package/dist/esm/ImageLazy.js
CHANGED
@@ -4,6 +4,10 @@ const ImageLazy = ({ src, alt, width, height, className, id, extraData, title, }
|
|
4
4
|
const imageRef = useRef(null);
|
5
5
|
const [isIntersecting, setIsIntersecting] = useState(false);
|
6
6
|
useEffect(() => {
|
7
|
+
if (typeof IntersectionObserver === "undefined" ||
|
8
|
+
!imageRef.current) {
|
9
|
+
return;
|
10
|
+
}
|
7
11
|
const observer = new IntersectionObserver((entries) => {
|
8
12
|
entries.forEach((entry) => {
|
9
13
|
if (entry.isIntersecting) {
|
@@ -14,19 +18,20 @@ const ImageLazy = ({ src, alt, width, height, className, id, extraData, title, }
|
|
14
18
|
observer.unobserve(entry.target);
|
15
19
|
}
|
16
20
|
});
|
17
|
-
}, {
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
+
}, {
|
22
|
+
root: null,
|
23
|
+
rootMargin: "0px",
|
24
|
+
threshold: 0.5,
|
25
|
+
});
|
26
|
+
observer.observe(imageRef.current);
|
21
27
|
return () => {
|
22
|
-
|
23
|
-
observer.unobserve(imageRef.current);
|
24
|
-
}
|
28
|
+
observer.disconnect();
|
25
29
|
};
|
26
30
|
}, [src]);
|
27
|
-
return (_jsx("img", {
|
31
|
+
return (_jsx("img", { ref: imageRef, "data-src": src, alt: alt, title: title, className: className, width: width, height: height, loading: "lazy", id: id?.toString(), style: {
|
28
32
|
filter: isIntersecting ? "none" : "blur(20px)",
|
29
33
|
transition: "filter 0.9s",
|
30
|
-
},
|
34
|
+
}, ...extraData }));
|
31
35
|
};
|
32
36
|
export default ImageLazy;
|
37
|
+
export { ImageLazy };
|
@@ -1 +1 @@
|
|
1
|
-
import{jsx as _jsx}from"react/jsx-runtime";import{useEffect,useRef,useState}from"react";const ImageLazy=({src:e,alt:t,width:r,height:s,className:
|
1
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{useEffect,useRef,useState}from"react";const ImageLazy=({src:e,alt:t,width:r,height:s,className:n,id:a,extraData:i,title:o})=>{const c=useRef(null),[u,l]=useState(!1);return useEffect((()=>{if("undefined"==typeof IntersectionObserver||!c.current)return;const e=new IntersectionObserver((t=>{t.forEach((t=>{t.isIntersecting&&(l(!0),c.current&&(c.current.src=c.current.dataset.src||""),e.unobserve(t.target))}))}),{root:null,rootMargin:"0px",threshold:.5});return e.observe(c.current),()=>{e.disconnect()}}),[e]),_jsx("img",{ref:c,"data-src":e,alt:t,title:o,className:n,width:r,height:s,loading:"lazy",id:a?.toString(),style:{filter:u?"none":"blur(20px)",transition:"filter 0.9s"},...i})};export default ImageLazy;export{ImageLazy};
|
package/package.json
CHANGED
@@ -1,37 +1,54 @@
|
|
1
|
-
{
|
2
|
-
"name": "react-lazy-img-observer",
|
3
|
-
"version": "1.
|
4
|
-
"description": "A React component for lazy loading images with Intersection Observer",
|
5
|
-
"main": "dist/cjs/ImageLazy.min.js",
|
6
|
-
"module": "dist/esm/ImageLazy.min.js",
|
7
|
-
"types": "dist/types/ImageLazy.d.ts",
|
8
|
-
"
|
9
|
-
"
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
"
|
21
|
-
|
22
|
-
"
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
"
|
29
|
-
"
|
30
|
-
|
31
|
-
|
32
|
-
"
|
33
|
-
|
34
|
-
|
35
|
-
"
|
36
|
-
|
37
|
-
}
|
1
|
+
{
|
2
|
+
"name": "react-lazy-img-observer",
|
3
|
+
"version": "1.1.0",
|
4
|
+
"description": "A React component for lazy loading images with Intersection Observer",
|
5
|
+
"main": "dist/cjs/ImageLazy.min.js",
|
6
|
+
"module": "dist/esm/ImageLazy.min.js",
|
7
|
+
"types": "dist/types/ImageLazy.d.ts",
|
8
|
+
"exports": {
|
9
|
+
".": {
|
10
|
+
"import": "./dist/esm/ImageLazy.min.js",
|
11
|
+
"require": "./dist/cjs/ImageLazy.min.js",
|
12
|
+
"types": "./dist/types/ImageLazy.d.ts"
|
13
|
+
}
|
14
|
+
},
|
15
|
+
"files": [
|
16
|
+
"dist/cjs",
|
17
|
+
"dist/esm",
|
18
|
+
"dist/types"
|
19
|
+
],
|
20
|
+
"sideEffects": false,
|
21
|
+
"scripts": {
|
22
|
+
"clean": "rimraf dist",
|
23
|
+
"build": "npm run clean && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && terser dist/cjs/ImageLazy.js -o dist/cjs/ImageLazy.min.js --compress --mangle && terser dist/esm/ImageLazy.js -o dist/esm/ImageLazy.min.js --compress --mangle"
|
24
|
+
},
|
25
|
+
"keywords": [
|
26
|
+
"react",
|
27
|
+
"lazy-load",
|
28
|
+
"image",
|
29
|
+
"intersection-observer"
|
30
|
+
],
|
31
|
+
"author": "Percy Chuzon <contacto@percychuzon.com>",
|
32
|
+
"license": "ISC",
|
33
|
+
"homepage": "https://imagelazy.percychuzon.com/",
|
34
|
+
"repository": {
|
35
|
+
"type": "git",
|
36
|
+
"url": "https://github.com/perch33/react-lazy-img-observer.git"
|
37
|
+
},
|
38
|
+
"bugs": {
|
39
|
+
"url": "https://github.com/perch33/react-lazy-img-observer/issues"
|
40
|
+
},
|
41
|
+
"dependencies": {
|
42
|
+
"terser": "^5.39.0"
|
43
|
+
},
|
44
|
+
"peerDependencies": {
|
45
|
+
"react": ">=17.0.0",
|
46
|
+
"react-dom": ">=17.0.0"
|
47
|
+
},
|
48
|
+
"devDependencies": {
|
49
|
+
"@types/react": "^18.3.21",
|
50
|
+
"@types/react-dom": "^18.3.7",
|
51
|
+
"rimraf": "^6.0.1",
|
52
|
+
"typescript": "^5.8.3"
|
53
|
+
}
|
54
|
+
}
|
package/src/ImageLazy.tsx
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
import { useEffect, useRef, useState } from "react";
|
2
|
-
|
3
|
-
type ImagesLazy = {
|
4
|
-
src: string;
|
5
|
-
alt: string;
|
6
|
-
width?: number;
|
7
|
-
height?: number;
|
8
|
-
id?: number | string;
|
9
|
-
className?: string;
|
10
|
-
title?: string;
|
11
|
-
extraData?: React.ImgHTMLAttributes<HTMLImageElement>;
|
12
|
-
};
|
13
|
-
|
14
|
-
const ImageLazy = ({
|
15
|
-
src,
|
16
|
-
alt,
|
17
|
-
width,
|
18
|
-
height,
|
19
|
-
className,
|
20
|
-
id,
|
21
|
-
extraData,
|
22
|
-
title,
|
23
|
-
}: ImagesLazy) => {
|
24
|
-
const imageRef = useRef<HTMLImageElement>(null);
|
25
|
-
const [isIntersecting, setIsIntersecting] = useState<boolean>(false);
|
26
|
-
|
27
|
-
useEffect(() => {
|
28
|
-
const observer = new IntersectionObserver(
|
29
|
-
(entries) => {
|
30
|
-
entries.forEach((entry) => {
|
31
|
-
if (entry.isIntersecting) {
|
32
|
-
setIsIntersecting(true);
|
33
|
-
if (imageRef.current) {
|
34
|
-
imageRef.current.src = imageRef.current.dataset.src || "";
|
35
|
-
}
|
36
|
-
observer.unobserve(entry.target);
|
37
|
-
}
|
38
|
-
});
|
39
|
-
},
|
40
|
-
{ root: null, rootMargin: "0px", threshold: 0.5 }
|
41
|
-
);
|
42
|
-
|
43
|
-
if (imageRef.current) {
|
44
|
-
observer.observe(imageRef.current);
|
45
|
-
}
|
46
|
-
|
47
|
-
return () => {
|
48
|
-
if (imageRef.current) {
|
49
|
-
observer.unobserve(imageRef.current);
|
50
|
-
}
|
51
|
-
};
|
52
|
-
}, [src]);
|
53
|
-
|
54
|
-
return (
|
55
|
-
<img
|
56
|
-
title={title}
|
57
|
-
className={className}
|
58
|
-
loading="lazy"
|
59
|
-
ref={imageRef}
|
60
|
-
data-src={src}
|
61
|
-
alt={alt}
|
62
|
-
width={width}
|
63
|
-
height={height}
|
64
|
-
style={{
|
65
|
-
filter: isIntersecting ? "none" : "blur(20px)",
|
66
|
-
transition: "filter 0.9s",
|
67
|
-
}}
|
68
|
-
id={id?.toString()}
|
69
|
-
{...extraData}
|
70
|
-
/>
|
71
|
-
);
|
72
|
-
};
|
73
|
-
|
74
|
-
export default ImageLazy;
|
package/tsconfig.cjs.json
DELETED
package/tsconfig.esm.json
DELETED
package/tsconfig.json
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"compilerOptions": {
|
3
|
-
"target": "ES2020",
|
4
|
-
"module": "ESNext",
|
5
|
-
"declaration": true,
|
6
|
-
"jsx": "react-jsx",
|
7
|
-
"declarationDir": "dist/types",
|
8
|
-
"removeComments": true,
|
9
|
-
"strict": true,
|
10
|
-
"esModuleInterop": true,
|
11
|
-
"skipLibCheck": true,
|
12
|
-
"forceConsistentCasingInFileNames": true,
|
13
|
-
"outDir": "dist",
|
14
|
-
"moduleResolution": "node"
|
15
|
-
|
16
|
-
},
|
17
|
-
"include": ["src"],
|
18
|
-
"exclude": ["node_modules"]
|
19
|
-
}
|