use-images-tracker 1.0.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/LICENSE +21 -0
- package/README.md +75 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +119 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HBI Developer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# use-images-tracker
|
|
2
|
+
|
|
3
|
+
React Hook to check whether images inside a tracked element/component are loaded.
|
|
4
|
+
|
|
5
|
+
Features
|
|
6
|
+
|
|
7
|
+
- Lightweight React hook that tracks images inside a container and reports load/failure state.
|
|
8
|
+
|
|
9
|
+
Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install use-images-tracker
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Peer dependencies
|
|
16
|
+
|
|
17
|
+
- This package requires React (>=16.8.0).
|
|
18
|
+
|
|
19
|
+
Usage
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import React from "react";
|
|
23
|
+
import useImagesTracker from "use-images-tracker";
|
|
24
|
+
|
|
25
|
+
function Example() {
|
|
26
|
+
const { tracker, isLoaded, failures, repeating } = useImagesTracker();
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<div>
|
|
30
|
+
<div ref={tracker}>
|
|
31
|
+
<img src="/path/to/image1.jpg" alt="img1" />
|
|
32
|
+
<img src="/path/to/image2.jpg" alt="img2" />
|
|
33
|
+
</div>
|
|
34
|
+
|
|
35
|
+
{isLoaded ? <p>All images loaded</p> : <p>Loading images…</p>}
|
|
36
|
+
|
|
37
|
+
{failures.length > 0 && (
|
|
38
|
+
<ul>
|
|
39
|
+
{failures.map((f, i) => (
|
|
40
|
+
<li key={i}>
|
|
41
|
+
{f.url} — code: {f.code}
|
|
42
|
+
</li>
|
|
43
|
+
))}
|
|
44
|
+
</ul>
|
|
45
|
+
)}
|
|
46
|
+
</div>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export default Example;
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
API
|
|
54
|
+
|
|
55
|
+
- `tracker`: React ref to attach to the container element that holds images.
|
|
56
|
+
- `isLoaded`: boolean, becomes `true` when all tracked images are finished (loaded or errored).
|
|
57
|
+
- `failures`: array of failure records `{ code, image, url }` for images that errored.
|
|
58
|
+
- `repeating`: function to restart tracking (useful when images change dynamically).
|
|
59
|
+
|
|
60
|
+
Build from source
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npm install
|
|
64
|
+
npm run build
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Publish
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npm publish --access public
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
License
|
|
74
|
+
|
|
75
|
+
This project is licensed under the MIT License
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
38
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
39
|
+
if (ar || !(i in from)) {
|
|
40
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
41
|
+
ar[i] = from[i];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
45
|
+
};
|
|
46
|
+
import { useEffect, useRef, useState } from "react";
|
|
47
|
+
export default function useImagesTracker() {
|
|
48
|
+
var _this = this;
|
|
49
|
+
var tracker = useRef(null), _a = useState(false), isStarted = _a[0], setIsStarted = _a[1], isFirstTime = useRef(true), images = useRef([]), _b = useState([]), failures = _b[0], setFailures = _b[1], _c = useState(0), counter = _c[0], setCounter = _c[1], _d = useState(false), isLoaded = _d[0], setIsLoaded = _d[1], tracking = function () {
|
|
50
|
+
var _a;
|
|
51
|
+
if (isStarted)
|
|
52
|
+
return;
|
|
53
|
+
setIsStarted(true);
|
|
54
|
+
images.current = ((_a = tracker.current) === null || _a === void 0 ? void 0 : _a.querySelectorAll("img")) || [];
|
|
55
|
+
if (images.current.length === 0) {
|
|
56
|
+
setIsLoaded(true);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
var increaseCounter = function () {
|
|
60
|
+
setCounter(function (prev) { return prev + 1; });
|
|
61
|
+
};
|
|
62
|
+
[].forEach.call(images.current, function (image) {
|
|
63
|
+
if (image.complete)
|
|
64
|
+
increaseCounter();
|
|
65
|
+
else {
|
|
66
|
+
image.addEventListener("load", increaseCounter);
|
|
67
|
+
image.addEventListener("error", function () { return __awaiter(_this, void 0, void 0, function () {
|
|
68
|
+
var error, url, _1;
|
|
69
|
+
return __generator(this, function (_a) {
|
|
70
|
+
switch (_a.label) {
|
|
71
|
+
case 0:
|
|
72
|
+
error = { image: image, code: 0, url: image.src };
|
|
73
|
+
increaseCounter();
|
|
74
|
+
_a.label = 1;
|
|
75
|
+
case 1:
|
|
76
|
+
_a.trys.push([1, 3, , 4]);
|
|
77
|
+
return [4 /*yield*/, fetch(image.src)];
|
|
78
|
+
case 2:
|
|
79
|
+
url = _a.sent();
|
|
80
|
+
if (!url.ok) {
|
|
81
|
+
error.code = url.status;
|
|
82
|
+
}
|
|
83
|
+
return [3 /*break*/, 4];
|
|
84
|
+
case 3:
|
|
85
|
+
_1 = _a.sent();
|
|
86
|
+
error.code = 500;
|
|
87
|
+
return [3 /*break*/, 4];
|
|
88
|
+
case 4:
|
|
89
|
+
setFailures(function (prev) { return __spreadArray(__spreadArray([], prev, true), [error], false); });
|
|
90
|
+
return [2 /*return*/];
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}); });
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}, repeating = function () {
|
|
97
|
+
if (!isStarted || isFirstTime.current)
|
|
98
|
+
return;
|
|
99
|
+
setCounter(0);
|
|
100
|
+
setIsLoaded(false);
|
|
101
|
+
setTimeout(function () {
|
|
102
|
+
setIsStarted(false);
|
|
103
|
+
}, 0);
|
|
104
|
+
};
|
|
105
|
+
useEffect(function () {
|
|
106
|
+
tracking();
|
|
107
|
+
}, []);
|
|
108
|
+
useEffect(function () {
|
|
109
|
+
if (!isStarted)
|
|
110
|
+
tracking();
|
|
111
|
+
}, [isStarted]);
|
|
112
|
+
useEffect(function () {
|
|
113
|
+
if (counter >= images.current.length) {
|
|
114
|
+
setIsLoaded(true);
|
|
115
|
+
isFirstTime.current = false;
|
|
116
|
+
}
|
|
117
|
+
}, [counter]);
|
|
118
|
+
return { tracker: tracker, isLoaded: isLoaded, failures: failures, repeating: repeating };
|
|
119
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-images-tracker",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React Hook to Check if images inside tracked element/component is loaded.",
|
|
5
|
+
"homepage": "https://github.com/HBI-Developer/use-images-tracker",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"prepare": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"react": ">=16.8.0"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react",
|
|
20
|
+
"hook",
|
|
21
|
+
"track",
|
|
22
|
+
"images",
|
|
23
|
+
"loaded",
|
|
24
|
+
"track images",
|
|
25
|
+
"images loaded",
|
|
26
|
+
"is loaded",
|
|
27
|
+
"is images loaded"
|
|
28
|
+
],
|
|
29
|
+
"author": "HBI Developer",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"type": "module",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/react": "^19.2.14",
|
|
34
|
+
"typescript": "^5.9.3"
|
|
35
|
+
},
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/HBI-Developer/use-images-tracker.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/HBI-Developer/use-images-tracker/issues"
|
|
42
|
+
}
|
|
43
|
+
}
|