-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.dist.js
74 lines (63 loc) · 2.54 KB
/
index.dist.js
1
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = useLazyLoadImage;
var _react = _interopRequireDefault(require("react"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function useLazyLoadImage() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$imageAttribute = _ref.imageAttribute,
imageAttribute = _ref$imageAttribute === void 0 ? '[data-img-src]' : _ref$imageAttribute,
_ref$imageAttributeKe = _ref.imageAttributeKey,
imageAttributeKey = _ref$imageAttributeKe === void 0 ? 'imgSrc' : _ref$imageAttributeKe,
_ref$rootMargin = _ref.rootMargin,
rootMargin = _ref$rootMargin === void 0 ? '200px 0px' : _ref$rootMargin,
_ref$threshold = _ref.threshold,
threshold = _ref$threshold === void 0 ? 0.01 : _ref$threshold,
_ref$debug = _ref.debug,
debug = _ref$debug === void 0 ? false : _ref$debug,
_ref$dependencies = _ref.dependencies,
dependencies = _ref$dependencies === void 0 ? [] : _ref$dependencies;
function log() {
var _console;
// eslint-disable-next-line no-console
if (debug) (_console = console).log.apply(_console, arguments);
}
function loadImage(image) {
var imageUrlToLoad = image.dataset[imageAttributeKey];
log('Loading image url', imageUrlToLoad); // eslint-disable-next-line no-param-reassign
image.src = imageUrlToLoad;
} // eslint-disable-next-line consistent-return
_react["default"].useEffect(function () {
var images = document.querySelectorAll(imageAttribute);
if (!window.IntersectionObserver) {
log('IntersectionObserver not available on this browser, loading all images now');
Array.from(images).forEach(function (image) {
return loadImage(image);
});
} else {
log('Adding intersection observer to all elements that match', imageAttribute);
var observer = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.intersectionRatio > 0) {
log('Unobserving image');
observer.unobserve(entry.target);
loadImage(entry.target);
}
});
}, {
rootMargin: rootMargin,
threshold: threshold
});
images.forEach(function (image) {
return observer.observe(image);
});
return function () {
images.forEach(function (image) {
return observer.unobserve(image);
});
};
}
}, dependencies);
}