@macive/ui 0.0.17 → 0.0.20
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.
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Image = exports.fallbackImageSVG = void 0;
|
|
4
4
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
-
// Fallback SVG as a data URL
|
|
7
6
|
exports.fallbackImageSVG = `
|
|
8
7
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="#ccc">
|
|
9
8
|
<rect width="100" height="100" />
|
|
@@ -13,27 +12,65 @@ exports.fallbackImageSVG = `
|
|
|
13
12
|
const fallbackSVG = `
|
|
14
13
|
<svg xmlns="http://www.w3.org/2000/svg" fill="#eae6e6" width="32" height="32" viewBox="0 0 32 32" stroke="#eae6e6">
|
|
15
14
|
<path d="M30 3.4141L28.5859 2 2 28.5859 3.4141 30l2-2H26a2.0027 2.0027 0 0 0 2-2V5.4141ZM26 26H7.4141l7.7929-7.793 2.3788 2.3787a2 2 0 0 0 2.8284 0L22 19l4 3.9973ZM26 20.1682l-2.5858-2.5859a2 2 0 0 0-2.8284 0L19 19.1682l-2.377-2.3771L26 7.4141Z"/>
|
|
16
|
-
<path d="M6 22V19l5-4.9966 1.3733 1.3733 1.4159-1.416-1.375-1.375a2 2 0 0 0-2.8284 0L6 16.1716V6H22V4H6A2.002 2.
|
|
15
|
+
<path d="M6 22V19l5-4.9966 1.3733 1.3733 1.4159-1.416-1.375-1.375a2 2 0 0 0-2.8284 0L6 16.1716V6H22V4H6A2.002 2.0027 0 0 0 4 6V22Z"/>
|
|
17
16
|
</svg>
|
|
18
17
|
`;
|
|
19
18
|
const errorkSVG = `
|
|
20
19
|
<svg xmlns="http://www.w3.org/2000/svg" fill="#dfc3c3" width="32" height="32" viewBox="0 0 32 32" stroke="#dfc3c3">
|
|
21
20
|
<path d="M30 3.4141L28.5859 2 2 28.5859 3.4141 30l2-2H26a2.0027 2.0027 0 0 0 2-2V5.4141ZM26 26H7.4141l7.7929-7.793 2.3788 2.3787a2 2 0 0 0 2.8284 0L22 19l4 3.9973ZM26 20.1682l-2.5858-2.5859a2 2 0 0 0-2.8284 0L19 19.1682l-2.377-2.3771L26 7.4141Z"/>
|
|
22
|
-
<path d="M6 22V19l5-4.9966 1.3733 1.3733 1.4159-1.416-1.375-1.375a2 2 0 0 0-2.8284 0L6 16.1716V6H22V4H6A2.002 2.
|
|
21
|
+
<path d="M6 22V19l5-4.9966 1.3733 1.3733 1.4159-1.416-1.375-1.375a2 2 0 0 0-2.8284 0L6 16.1716V6H22V4H6A2.002 2.0027 0 0 0 4 6V22Z"/>
|
|
23
22
|
</svg>
|
|
24
23
|
`;
|
|
25
24
|
const fallbackImageURL = `data:image/svg+xml;utf8,${encodeURIComponent(fallbackSVG)}`;
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
// SVG loader (spinning circle)
|
|
26
|
+
const loaderSVG = `
|
|
27
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="0 0 50 50">
|
|
28
|
+
<circle cx="25" cy="25" r="20" stroke="#ccc" stroke-width="5" fill="none" stroke-dasharray="31.415, 31.415">
|
|
29
|
+
<animateTransform attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="1s" repeatCount="indefinite" />
|
|
30
|
+
</circle>
|
|
31
|
+
</svg>
|
|
32
|
+
`;
|
|
33
|
+
const loaderImageURL = `data:image/svg+xml;utf8,${encodeURIComponent(loaderSVG)}`;
|
|
34
|
+
// Inline CSS for loader and image wrapper
|
|
35
|
+
const imageStyles = `
|
|
36
|
+
.image-wrapper {
|
|
37
|
+
position: relative;
|
|
38
|
+
width: 100%;
|
|
39
|
+
height: 100%;
|
|
40
|
+
display: flex;
|
|
41
|
+
align-items: center;
|
|
42
|
+
justify-content: center;
|
|
43
|
+
background: #f0f0f0; /* Light background for loader */
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.loader {
|
|
47
|
+
width: 40px;
|
|
48
|
+
height: 40px;
|
|
49
|
+
z-index: 1; /* Ensure loader is above background */
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.image {
|
|
53
|
+
width: 100%;
|
|
54
|
+
height: 100%;
|
|
55
|
+
object-fit: cover;
|
|
56
|
+
transition: opacity 0.3s ease-in-out;
|
|
57
|
+
z-index: 2; /* Ensure image is above loader */
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.image-loading {
|
|
61
|
+
opacity: 0;
|
|
62
|
+
}
|
|
63
|
+
`;
|
|
64
|
+
function Image({ alt, sourceSet, source, crossOrigin, onLoad, onError, className, rounded, lazy = true, style, ...rest }) {
|
|
28
65
|
const [isLoading, setIsLoading] = (0, react_1.useState)(true);
|
|
29
|
-
const [imgSrc, setImgSrc] = (0, react_1.useState)(lazy ?
|
|
30
|
-
const [isInView, setIsInView] = (0, react_1.useState)(!lazy);
|
|
66
|
+
const [imgSrc, setImgSrc] = (0, react_1.useState)(lazy ? null : source);
|
|
67
|
+
const [isInView, setIsInView] = (0, react_1.useState)(!lazy);
|
|
31
68
|
const imgRef = (0, react_1.useRef)(null);
|
|
32
|
-
// Intersection Observer for lazy loading
|
|
33
69
|
(0, react_1.useEffect)(() => {
|
|
34
|
-
if (!lazy)
|
|
70
|
+
if (!lazy) {
|
|
71
|
+
setIsInView(true); // Ensure non-lazy images load immediately
|
|
35
72
|
return;
|
|
36
|
-
|
|
73
|
+
}
|
|
37
74
|
const currentImgRef = imgRef.current;
|
|
38
75
|
if (!currentImgRef)
|
|
39
76
|
return;
|
|
@@ -41,12 +78,12 @@ style, ...rest }) {
|
|
|
41
78
|
entries.forEach((entry) => {
|
|
42
79
|
if (entry.isIntersecting) {
|
|
43
80
|
setIsInView(true);
|
|
44
|
-
observer.unobserve(entry.target);
|
|
81
|
+
observer.unobserve(entry.target);
|
|
45
82
|
}
|
|
46
83
|
});
|
|
47
84
|
}, {
|
|
48
85
|
rootMargin: '0px',
|
|
49
|
-
threshold: 0.1,
|
|
86
|
+
threshold: 0.1,
|
|
50
87
|
});
|
|
51
88
|
observer.observe(currentImgRef);
|
|
52
89
|
return () => {
|
|
@@ -55,7 +92,6 @@ style, ...rest }) {
|
|
|
55
92
|
}
|
|
56
93
|
};
|
|
57
94
|
}, [lazy]);
|
|
58
|
-
// Load image when in view
|
|
59
95
|
(0, react_1.useEffect)(() => {
|
|
60
96
|
if (!isInView)
|
|
61
97
|
return;
|
|
@@ -76,30 +112,30 @@ style, ...rest }) {
|
|
|
76
112
|
onLoad === null || onLoad === void 0 ? void 0 : onLoad();
|
|
77
113
|
};
|
|
78
114
|
img.onerror = () => {
|
|
79
|
-
setImgSrc(fallbackImageURL);
|
|
115
|
+
setImgSrc(fallbackImageURL);
|
|
80
116
|
setIsLoading(false);
|
|
81
117
|
onError === null || onError === void 0 ? void 0 : onError();
|
|
82
118
|
};
|
|
83
119
|
}
|
|
84
120
|
catch (error) {
|
|
85
|
-
setImgSrc(fallbackImageURL);
|
|
121
|
+
setImgSrc(fallbackImageURL);
|
|
86
122
|
setIsLoading(false);
|
|
87
123
|
onError === null || onError === void 0 ? void 0 : onError();
|
|
88
124
|
}
|
|
89
125
|
};
|
|
90
126
|
loadImage();
|
|
91
127
|
}, [source, sourceSet, crossOrigin, onLoad, onError, isInView]);
|
|
92
|
-
// Combine styles for rounded corners
|
|
93
128
|
const combinedStyle = {
|
|
94
129
|
...(rounded ? { borderRadius: 'var(--p-border-radius-3)' } : {}),
|
|
95
130
|
...style,
|
|
96
131
|
};
|
|
97
|
-
return (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
132
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("style", { children: imageStyles }), (0, jsx_runtime_1.jsxs)("div", { style: combinedStyle, className: `image-wrapper ${className || ''}`, children: [isLoading && ((0, jsx_runtime_1.jsx)("img", { src: loaderImageURL, alt: 'Loading', className: 'loader', "aria-hidden": 'true' })), (0, jsx_runtime_1.jsx)("img", { ...rest, ref: imgRef, alt: alt, src: imgSrc !== null && imgSrc !== void 0 ? imgSrc : undefined, crossOrigin: crossOrigin, srcSet: sourceSet === null || sourceSet === void 0 ? void 0 : sourceSet.map(({ source, descriptor }) => `${source} ${descriptor || ''}`).join(', '), className: `image ${isLoading ? 'image-loading' : ''}`, loading: lazy ? 'lazy' : 'eager', onLoad: () => {
|
|
133
|
+
setIsLoading(false); // Ensure isLoading is false on direct load
|
|
134
|
+
onLoad === null || onLoad === void 0 ? void 0 : onLoad();
|
|
135
|
+
}, onError: (e) => {
|
|
136
|
+
e.currentTarget.src = fallbackImageURL;
|
|
137
|
+
setIsLoading(false);
|
|
138
|
+
onError === null || onError === void 0 ? void 0 : onError();
|
|
139
|
+
} })] })] }));
|
|
104
140
|
}
|
|
105
141
|
exports.Image = Image;
|