@uncinc/uncinc-react-kitchen-sink 2.11.5 → 2.11.6
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.
|
@@ -5,6 +5,7 @@ interface Props extends Omit<React.ImgHTMLAttributes<HTMLImageElement>, 'src'> {
|
|
|
5
5
|
onLoad: (e: any) => void;
|
|
6
6
|
objectFit?: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down';
|
|
7
7
|
aspectRatio?: number | string;
|
|
8
|
+
resizeDelay?: number;
|
|
8
9
|
}
|
|
9
10
|
declare const AdaptivePicture: React.FC<Props>;
|
|
10
11
|
export default AdaptivePicture;
|
|
@@ -18,7 +18,9 @@ var AdaptivePicture = function AdaptivePicture(_ref) {
|
|
|
18
18
|
alt = _ref.alt,
|
|
19
19
|
objectFit = _ref.objectFit,
|
|
20
20
|
_ref$aspectRatio = _ref.aspectRatio,
|
|
21
|
-
aspectRatio = _ref$aspectRatio === void 0 ? 1 : _ref$aspectRatio
|
|
21
|
+
aspectRatio = _ref$aspectRatio === void 0 ? 1 : _ref$aspectRatio,
|
|
22
|
+
_ref$resizeDelay = _ref.resizeDelay,
|
|
23
|
+
resizeDelay = _ref$resizeDelay === void 0 ? 100 : _ref$resizeDelay;
|
|
22
24
|
var sourcesAsArray = Array.isArray(src) ? src : [src];
|
|
23
25
|
var validatedSources = sourcesAsArray.filter(isValidImage);
|
|
24
26
|
var fallbackImage = validatedSources[0];
|
|
@@ -35,6 +37,8 @@ var AdaptivePicture = function AdaptivePicture(_ref) {
|
|
|
35
37
|
_React$useState4 = _slicedToArray(_React$useState3, 2),
|
|
36
38
|
targetSrc = _React$useState4[0],
|
|
37
39
|
setTargetSrc = _React$useState4[1];
|
|
40
|
+
var resizingDelay = React.useRef(0);
|
|
41
|
+
var resizingDelayTimer = React.useRef();
|
|
38
42
|
React.useEffect(function () {
|
|
39
43
|
// @ts-ignore
|
|
40
44
|
if (global.ssr) return;
|
|
@@ -44,32 +48,43 @@ var AdaptivePicture = function AdaptivePicture(_ref) {
|
|
|
44
48
|
var container = image.parentElement;
|
|
45
49
|
if (!container) return;
|
|
46
50
|
var resizeObserver = new ResizeObserver(function (entries) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
// In order to prevent the 'ResizeObserver loop completed with undelivered notifications.'
|
|
52
|
+
// message, debounce handling the ResizeObserver action by adding a timeout.
|
|
53
|
+
clearTimeout(resizingDelayTimer.current);
|
|
54
|
+
resizingDelayTimer.current = setTimeout(function () {
|
|
55
|
+
var widthAfterResize = entries[0].contentRect.width;
|
|
56
|
+
var newSrcRequired = false;
|
|
57
|
+
if (image.width < widthAfterResize) {
|
|
58
|
+
var newTargetSrc = calculateTargetSrc(image.width, validatedSources, fallbackImage);
|
|
59
|
+
if (targetSrc !== newTargetSrc) {
|
|
60
|
+
setTargetSrc(newTargetSrc);
|
|
61
|
+
newSrcRequired = true;
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
var _newTargetSrc = calculateTargetSrc(widthAfterResize, validatedSources, fallbackImage);
|
|
65
|
+
if (targetSrc !== _newTargetSrc) {
|
|
66
|
+
setTargetSrc(_newTargetSrc);
|
|
67
|
+
newSrcRequired = true;
|
|
68
|
+
}
|
|
54
69
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
70
|
+
if (status === 'PENDING') {
|
|
71
|
+
setStatus('INITIAL_BREAKPOINT_CALCULATED');
|
|
72
|
+
|
|
73
|
+
// We always load the smallest source. If that's also the source we need
|
|
74
|
+
// set INITIAL_LOADED directly.
|
|
75
|
+
// With caching turned on, the onLoad function on the img would only trigger once
|
|
76
|
+
// and thus never unblur the smallest
|
|
77
|
+
if (!newSrcRequired) {
|
|
78
|
+
setStatus('INITIAL_LOADED');
|
|
79
|
+
}
|
|
60
80
|
}
|
|
61
|
-
}
|
|
62
|
-
if (status === 'PENDING') {
|
|
63
|
-
setStatus('INITIAL_BREAKPOINT_CALCULATED');
|
|
64
81
|
|
|
65
|
-
//
|
|
66
|
-
// set
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (!newSrcRequired) {
|
|
70
|
-
setStatus('INITIAL_LOADED');
|
|
82
|
+
// On page load, we want 0 delay.
|
|
83
|
+
// After the observer has been triggered once, we set a small debounce.
|
|
84
|
+
if (resizingDelay.current === 0) {
|
|
85
|
+
resizingDelay.current = resizeDelay;
|
|
71
86
|
}
|
|
72
|
-
}
|
|
87
|
+
}, resizingDelay.current);
|
|
73
88
|
});
|
|
74
89
|
resizeObserver.observe(container);
|
|
75
90
|
|
|
@@ -77,7 +92,7 @@ var AdaptivePicture = function AdaptivePicture(_ref) {
|
|
|
77
92
|
return function () {
|
|
78
93
|
resizeObserver.unobserve(container);
|
|
79
94
|
};
|
|
80
|
-
}, [fallbackImage, status, targetSrc, validatedSources]);
|
|
95
|
+
}, [fallbackImage, status, targetSrc, validatedSources, resizeDelay]);
|
|
81
96
|
var loadedUrl = useImageLoader(fallbackImage, targetSrc, imageRef);
|
|
82
97
|
function onErrorFallback(e) {
|
|
83
98
|
// Make sure that both URLs are absolute URLs, so we can compare them.
|
|
@@ -8,11 +8,13 @@ declare namespace Image {
|
|
|
8
8
|
lazyload: PropTypes.Requireable<boolean>;
|
|
9
9
|
aspectRatio: PropTypes.Requireable<number>;
|
|
10
10
|
objectFit: PropTypes.Requireable<string>;
|
|
11
|
+
resizeDelay: PropTypes.Requireable<number>;
|
|
11
12
|
};
|
|
12
13
|
namespace defaultProps {
|
|
13
14
|
let lazyload: boolean;
|
|
14
|
-
let aspectRatio:
|
|
15
|
+
let aspectRatio: number;
|
|
15
16
|
let objectFit: null;
|
|
17
|
+
let resizeDelay: number;
|
|
16
18
|
}
|
|
17
19
|
}
|
|
18
20
|
import React from 'react';
|
|
@@ -24,11 +24,13 @@ Image.propTypes = _objectSpread({
|
|
|
24
24
|
src: PropTypes.oneOfType([PropTypes.string, ImmutablePropTypes.map, PropTypes.arrayOf(PropTypes.string)]).isRequired,
|
|
25
25
|
lazyload: PropTypes.bool,
|
|
26
26
|
aspectRatio: PropTypes.number,
|
|
27
|
-
objectFit: PropTypes.oneOf(['fill', 'contain', 'cover', 'none', 'scale-down'])
|
|
27
|
+
objectFit: PropTypes.oneOf(['fill', 'contain', 'cover', 'none', 'scale-down']),
|
|
28
|
+
resizeDelay: PropTypes.number
|
|
28
29
|
}, Picture.propTypes);
|
|
29
30
|
Image.defaultProps = {
|
|
30
31
|
lazyload: true,
|
|
31
|
-
aspectRatio:
|
|
32
|
-
objectFit: null
|
|
32
|
+
aspectRatio: 1,
|
|
33
|
+
objectFit: null,
|
|
34
|
+
resizeDelay: 100
|
|
33
35
|
};
|
|
34
36
|
export default Image;
|