@uniweb/kit 0.1.2 → 0.1.3
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/package.json +2 -2
- package/src/components/Image/Image.jsx +66 -60
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniweb/kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Standard component library for Uniweb foundations",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"tailwind-merge": "^2.6.0",
|
|
38
|
-
"@uniweb/core": "0.1.
|
|
38
|
+
"@uniweb/core": "0.1.5"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"react": "^18.0.0 || ^19.0.0",
|
|
@@ -11,22 +11,22 @@
|
|
|
11
11
|
* @module @uniweb/kit/Image
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
import React, { useState, useCallback } from
|
|
15
|
-
import { Link } from
|
|
16
|
-
import { cn } from
|
|
14
|
+
import React, { useState, useCallback } from "react";
|
|
15
|
+
import { Link } from "../Link/index.js";
|
|
16
|
+
import { cn } from "../../utils/index.js";
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Size presets for images
|
|
20
20
|
*/
|
|
21
21
|
const SIZE_CLASSES = {
|
|
22
|
-
xs:
|
|
23
|
-
sm:
|
|
24
|
-
md:
|
|
25
|
-
lg:
|
|
26
|
-
xl:
|
|
27
|
-
|
|
28
|
-
full:
|
|
29
|
-
}
|
|
22
|
+
xs: "w-8 h-8",
|
|
23
|
+
sm: "w-12 h-12",
|
|
24
|
+
md: "w-16 h-16",
|
|
25
|
+
lg: "w-24 h-24",
|
|
26
|
+
xl: "w-32 h-32",
|
|
27
|
+
"2xl": "w-48 h-48",
|
|
28
|
+
full: "w-full h-full",
|
|
29
|
+
};
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* Build CSS filter string from filter object
|
|
@@ -34,18 +34,18 @@ const SIZE_CLASSES = {
|
|
|
34
34
|
* @returns {string} CSS filter value
|
|
35
35
|
*/
|
|
36
36
|
function buildFilterStyle(filter) {
|
|
37
|
-
if (!filter || typeof filter !==
|
|
37
|
+
if (!filter || typeof filter !== "object") return undefined;
|
|
38
38
|
|
|
39
|
-
const filters = []
|
|
39
|
+
const filters = [];
|
|
40
40
|
|
|
41
|
-
if (filter.blur) filters.push(`blur(${filter.blur}px)`)
|
|
42
|
-
if (filter.brightness) filters.push(`brightness(${filter.brightness}%)`)
|
|
43
|
-
if (filter.contrast) filters.push(`contrast(${filter.contrast}%)`)
|
|
44
|
-
if (filter.grayscale) filters.push(`grayscale(${filter.grayscale}%)`)
|
|
45
|
-
if (filter.saturate) filters.push(`saturate(${filter.saturate}%)`)
|
|
46
|
-
if (filter.sepia) filters.push(`sepia(${filter.sepia}%)`)
|
|
41
|
+
if (filter.blur) filters.push(`blur(${filter.blur}px)`);
|
|
42
|
+
if (filter.brightness) filters.push(`brightness(${filter.brightness}%)`);
|
|
43
|
+
if (filter.contrast) filters.push(`contrast(${filter.contrast}%)`);
|
|
44
|
+
if (filter.grayscale) filters.push(`grayscale(${filter.grayscale}%)`);
|
|
45
|
+
if (filter.saturate) filters.push(`saturate(${filter.saturate}%)`);
|
|
46
|
+
if (filter.sepia) filters.push(`sepia(${filter.sepia}%)`);
|
|
47
47
|
|
|
48
|
-
return filters.length > 0 ? filters.join(
|
|
48
|
+
return filters.length > 0 ? filters.join(" ") : undefined;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
@@ -91,78 +91,84 @@ export function Image({
|
|
|
91
91
|
value,
|
|
92
92
|
src,
|
|
93
93
|
url,
|
|
94
|
-
alt =
|
|
94
|
+
alt = "",
|
|
95
95
|
href,
|
|
96
96
|
rounded,
|
|
97
97
|
filter,
|
|
98
|
-
loading =
|
|
98
|
+
loading = "lazy",
|
|
99
99
|
className,
|
|
100
100
|
ariaHidden,
|
|
101
101
|
onError,
|
|
102
102
|
onLoad,
|
|
103
103
|
...props
|
|
104
104
|
}) {
|
|
105
|
-
const [hasError, setHasError] = useState(false)
|
|
106
|
-
const [imageSrc, setImageSrc] = useState(null)
|
|
105
|
+
const [hasError, setHasError] = useState(false);
|
|
106
|
+
const [imageSrc, setImageSrc] = useState(null);
|
|
107
107
|
|
|
108
108
|
// Determine the image source
|
|
109
|
-
let resolvedSrc = src || url ||
|
|
110
|
-
let resolvedAlt = alt
|
|
109
|
+
let resolvedSrc = src || url || "";
|
|
110
|
+
let resolvedAlt = alt;
|
|
111
111
|
|
|
112
112
|
// Handle profile-based images
|
|
113
113
|
if (profile && type) {
|
|
114
|
-
if (type ===
|
|
114
|
+
if (type === "avatar" || type === "banner") {
|
|
115
115
|
// Use profile methods if available
|
|
116
|
-
if (typeof profile.getImageInfo ===
|
|
117
|
-
const imageInfo = profile.getImageInfo(type, size)
|
|
118
|
-
resolvedSrc = imageInfo?.url || resolvedSrc
|
|
119
|
-
resolvedAlt = imageInfo?.alt || resolvedAlt
|
|
116
|
+
if (typeof profile.getImageInfo === "function") {
|
|
117
|
+
const imageInfo = profile.getImageInfo(type, size);
|
|
118
|
+
resolvedSrc = imageInfo?.url || resolvedSrc;
|
|
119
|
+
resolvedAlt = imageInfo?.alt || resolvedAlt;
|
|
120
120
|
}
|
|
121
|
-
} else if (value && typeof profile.getAssetInfo ===
|
|
122
|
-
const assetInfo = profile.getAssetInfo(value, true, alt)
|
|
123
|
-
resolvedSrc = assetInfo?.src || resolvedSrc
|
|
124
|
-
resolvedAlt = assetInfo?.alt || resolvedAlt
|
|
121
|
+
} else if (value && typeof profile.getAssetInfo === "function") {
|
|
122
|
+
const assetInfo = profile.getAssetInfo(value, true, alt);
|
|
123
|
+
resolvedSrc = assetInfo?.src || resolvedSrc;
|
|
124
|
+
resolvedAlt = assetInfo?.alt || resolvedAlt;
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
// Handle value as direct source
|
|
129
129
|
if (!resolvedSrc && value) {
|
|
130
|
-
if (typeof value ===
|
|
131
|
-
resolvedSrc = value
|
|
130
|
+
if (typeof value === "string") {
|
|
131
|
+
resolvedSrc = value;
|
|
132
132
|
} else if (value.url || value.src) {
|
|
133
|
-
resolvedSrc = value.url || value.src
|
|
134
|
-
resolvedAlt = value.alt || resolvedAlt
|
|
133
|
+
resolvedSrc = value.url || value.src;
|
|
134
|
+
resolvedAlt = value.alt || resolvedAlt;
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
// Build classes
|
|
139
|
-
const sizeClass = size && SIZE_CLASSES[size]
|
|
140
|
-
const roundedClass =
|
|
139
|
+
const sizeClass = size && SIZE_CLASSES[size];
|
|
140
|
+
const roundedClass =
|
|
141
|
+
rounded === true
|
|
142
|
+
? "rounded-full"
|
|
143
|
+
: typeof rounded === "string"
|
|
144
|
+
? rounded
|
|
145
|
+
: "";
|
|
141
146
|
|
|
142
|
-
const imageClasses = cn(
|
|
143
|
-
'object-cover',
|
|
144
|
-
sizeClass,
|
|
145
|
-
roundedClass,
|
|
146
|
-
className
|
|
147
|
-
)
|
|
147
|
+
const imageClasses = cn("object-cover", sizeClass, roundedClass, className);
|
|
148
148
|
|
|
149
149
|
// Build filter style
|
|
150
|
-
const filterStyle = buildFilterStyle(filter)
|
|
150
|
+
const filterStyle = buildFilterStyle(filter);
|
|
151
151
|
|
|
152
152
|
// Handle error
|
|
153
|
-
const handleError = useCallback(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
153
|
+
const handleError = useCallback(
|
|
154
|
+
(e) => {
|
|
155
|
+
setHasError(true);
|
|
156
|
+
onError?.(e);
|
|
157
|
+
},
|
|
158
|
+
[onError]
|
|
159
|
+
);
|
|
157
160
|
|
|
158
161
|
// Handle load
|
|
159
|
-
const handleLoad = useCallback(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
+
const handleLoad = useCallback(
|
|
163
|
+
(e) => {
|
|
164
|
+
onLoad?.(e);
|
|
165
|
+
},
|
|
166
|
+
[onLoad]
|
|
167
|
+
);
|
|
162
168
|
|
|
163
169
|
// Don't render if no source or error
|
|
164
170
|
if (!resolvedSrc || hasError) {
|
|
165
|
-
return null
|
|
171
|
+
return null;
|
|
166
172
|
}
|
|
167
173
|
|
|
168
174
|
const imageElement = (
|
|
@@ -177,7 +183,7 @@ export function Image({
|
|
|
177
183
|
aria-hidden={ariaHidden}
|
|
178
184
|
{...props}
|
|
179
185
|
/>
|
|
180
|
-
)
|
|
186
|
+
);
|
|
181
187
|
|
|
182
188
|
// Wrap in link if href provided
|
|
183
189
|
if (href) {
|
|
@@ -185,10 +191,10 @@ export function Image({
|
|
|
185
191
|
<Link to={href} className="inline-block">
|
|
186
192
|
{imageElement}
|
|
187
193
|
</Link>
|
|
188
|
-
)
|
|
194
|
+
);
|
|
189
195
|
}
|
|
190
196
|
|
|
191
|
-
return imageElement
|
|
197
|
+
return imageElement;
|
|
192
198
|
}
|
|
193
199
|
|
|
194
|
-
export default Image
|
|
200
|
+
export default Image;
|