@xsolla/xui-image-uploader 0.147.1

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/web/index.js ADDED
@@ -0,0 +1,711 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.tsx
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ ImageUploader: () => ImageUploader
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/ImageUploader.tsx
38
+ var import_react3 = __toESM(require("react"));
39
+
40
+ // ../../foundation/primitives-web/src/Box.tsx
41
+ var import_react2 = __toESM(require("react"));
42
+ var import_styled_components = __toESM(require("styled-components"));
43
+
44
+ // ../../foundation/primitives-web/src/filterDOMProps.ts
45
+ var import_react = __toESM(require("react"));
46
+
47
+ // ../../../node_modules/@emotion/memoize/dist/memoize.esm.js
48
+ function memoize(fn) {
49
+ var cache = {};
50
+ return function(arg) {
51
+ if (cache[arg] === void 0) cache[arg] = fn(arg);
52
+ return cache[arg];
53
+ };
54
+ }
55
+ var memoize_esm_default = memoize;
56
+
57
+ // ../../../node_modules/@emotion/is-prop-valid/dist/is-prop-valid.esm.js
58
+ var reactPropsRegex = /^((children|dangerouslySetInnerHTML|key|ref|autoFocus|defaultValue|defaultChecked|innerHTML|suppressContentEditableWarning|suppressHydrationWarning|valueLink|accept|acceptCharset|accessKey|action|allow|allowUserMedia|allowPaymentRequest|allowFullScreen|allowTransparency|alt|async|autoComplete|autoPlay|capture|cellPadding|cellSpacing|challenge|charSet|checked|cite|classID|className|cols|colSpan|content|contentEditable|contextMenu|controls|controlsList|coords|crossOrigin|data|dateTime|decoding|default|defer|dir|disabled|disablePictureInPicture|download|draggable|encType|form|formAction|formEncType|formMethod|formNoValidate|formTarget|frameBorder|headers|height|hidden|high|href|hrefLang|htmlFor|httpEquiv|id|inputMode|integrity|is|keyParams|keyType|kind|label|lang|list|loading|loop|low|marginHeight|marginWidth|max|maxLength|media|mediaGroup|method|min|minLength|multiple|muted|name|nonce|noValidate|open|optimum|pattern|placeholder|playsInline|poster|preload|profile|radioGroup|readOnly|referrerPolicy|rel|required|reversed|role|rows|rowSpan|sandbox|scope|scoped|scrolling|seamless|selected|shape|size|sizes|slot|span|spellCheck|src|srcDoc|srcLang|srcSet|start|step|style|summary|tabIndex|target|title|type|useMap|value|width|wmode|wrap|about|datatype|inlist|prefix|property|resource|typeof|vocab|autoCapitalize|autoCorrect|autoSave|color|inert|itemProp|itemScope|itemType|itemID|itemRef|on|results|security|unselectable|accentHeight|accumulate|additive|alignmentBaseline|allowReorder|alphabetic|amplitude|arabicForm|ascent|attributeName|attributeType|autoReverse|azimuth|baseFrequency|baselineShift|baseProfile|bbox|begin|bias|by|calcMode|capHeight|clip|clipPathUnits|clipPath|clipRule|colorInterpolation|colorInterpolationFilters|colorProfile|colorRendering|contentScriptType|contentStyleType|cursor|cx|cy|d|decelerate|descent|diffuseConstant|direction|display|divisor|dominantBaseline|dur|dx|dy|edgeMode|elevation|enableBackground|end|exponent|externalResourcesRequired|fill|fillOpacity|fillRule|filter|filterRes|filterUnits|floodColor|floodOpacity|focusable|fontFamily|fontSize|fontSizeAdjust|fontStretch|fontStyle|fontVariant|fontWeight|format|from|fr|fx|fy|g1|g2|glyphName|glyphOrientationHorizontal|glyphOrientationVertical|glyphRef|gradientTransform|gradientUnits|hanging|horizAdvX|horizOriginX|ideographic|imageRendering|in|in2|intercept|k|k1|k2|k3|k4|kernelMatrix|kernelUnitLength|kerning|keyPoints|keySplines|keyTimes|lengthAdjust|letterSpacing|lightingColor|limitingConeAngle|local|markerEnd|markerMid|markerStart|markerHeight|markerUnits|markerWidth|mask|maskContentUnits|maskUnits|mathematical|mode|numOctaves|offset|opacity|operator|order|orient|orientation|origin|overflow|overlinePosition|overlineThickness|panose1|paintOrder|pathLength|patternContentUnits|patternTransform|patternUnits|pointerEvents|points|pointsAtX|pointsAtY|pointsAtZ|preserveAlpha|preserveAspectRatio|primitiveUnits|r|radius|refX|refY|renderingIntent|repeatCount|repeatDur|requiredExtensions|requiredFeatures|restart|result|rotate|rx|ry|scale|seed|shapeRendering|slope|spacing|specularConstant|specularExponent|speed|spreadMethod|startOffset|stdDeviation|stemh|stemv|stitchTiles|stopColor|stopOpacity|strikethroughPosition|strikethroughThickness|string|stroke|strokeDasharray|strokeDashoffset|strokeLinecap|strokeLinejoin|strokeMiterlimit|strokeOpacity|strokeWidth|surfaceScale|systemLanguage|tableValues|targetX|targetY|textAnchor|textDecoration|textRendering|textLength|to|transform|u1|u2|underlinePosition|underlineThickness|unicode|unicodeBidi|unicodeRange|unitsPerEm|vAlphabetic|vHanging|vIdeographic|vMathematical|values|vectorEffect|version|vertAdvY|vertOriginX|vertOriginY|viewBox|viewTarget|visibility|widths|wordSpacing|writingMode|x|xHeight|x1|x2|xChannelSelector|xlinkActuate|xlinkArcrole|xlinkHref|xlinkRole|xlinkShow|xlinkTitle|xlinkType|xmlBase|xmlns|xmlnsXlink|xmlLang|xmlSpace|y|y1|y2|yChannelSelector|z|zoomAndPan|for|class|autofocus)|(([Dd][Aa][Tt][Aa]|[Aa][Rr][Ii][Aa]|x)-.*))$/;
59
+ var index = memoize_esm_default(
60
+ function(prop) {
61
+ return reactPropsRegex.test(prop) || prop.charCodeAt(0) === 111 && prop.charCodeAt(1) === 110 && prop.charCodeAt(2) < 91;
62
+ }
63
+ /* Z+1 */
64
+ );
65
+ var is_prop_valid_esm_default = index;
66
+
67
+ // ../../foundation/primitives-web/src/filterDOMProps.ts
68
+ var ADDITIONAL_BLOCKED_PROPS = /* @__PURE__ */ new Set([
69
+ // RN-only event handlers (pass isPropValid's on* pattern)
70
+ "onPress",
71
+ "onChangeText",
72
+ "onLayout",
73
+ "onMoveShouldSetResponder",
74
+ "onResponderGrant",
75
+ "onResponderMove",
76
+ "onResponderRelease",
77
+ "onResponderTerminate",
78
+ // SVG attributes that pass isPropValid
79
+ "strokeWidth",
80
+ // CSS properties that pass isPropValid but are used as component props
81
+ "overflow",
82
+ "cursor",
83
+ "fontSize",
84
+ "fontWeight",
85
+ "fontFamily",
86
+ "textDecoration"
87
+ ]);
88
+ function shouldForwardProp(key) {
89
+ if (ADDITIONAL_BLOCKED_PROPS.has(key)) return false;
90
+ return is_prop_valid_esm_default(key);
91
+ }
92
+ function createFilteredElement(defaultTag) {
93
+ const Component = import_react.default.forwardRef(
94
+ ({ children, elementType, ...props }, ref) => {
95
+ const Tag = elementType || defaultTag;
96
+ const htmlProps = {};
97
+ for (const key of Object.keys(props)) {
98
+ if (shouldForwardProp(key)) {
99
+ htmlProps[key] = props[key];
100
+ }
101
+ }
102
+ return import_react.default.createElement(
103
+ Tag,
104
+ { ref, ...htmlProps },
105
+ children
106
+ );
107
+ }
108
+ );
109
+ Component.displayName = `Filtered(${defaultTag})`;
110
+ return Component;
111
+ }
112
+
113
+ // ../../foundation/primitives-web/src/Box.tsx
114
+ var import_jsx_runtime = require("react/jsx-runtime");
115
+ var FilteredDiv = createFilteredElement("div");
116
+ var StyledBox = (0, import_styled_components.default)(FilteredDiv)`
117
+ display: flex;
118
+ box-sizing: border-box;
119
+ background-color: ${(props) => props.backgroundColor || "transparent"};
120
+ border-color: ${(props) => props.borderColor || "transparent"};
121
+ border-width: ${(props) => typeof props.borderWidth === "number" ? `${props.borderWidth}px` : props.borderWidth || 0};
122
+
123
+ ${(props) => props.borderBottomWidth !== void 0 && `
124
+ border-bottom-width: ${typeof props.borderBottomWidth === "number" ? `${props.borderBottomWidth}px` : props.borderBottomWidth};
125
+ border-bottom-color: ${props.borderBottomColor || props.borderColor || "transparent"};
126
+ border-bottom-style: solid;
127
+ `}
128
+ ${(props) => props.borderTopWidth !== void 0 && `
129
+ border-top-width: ${typeof props.borderTopWidth === "number" ? `${props.borderTopWidth}px` : props.borderTopWidth};
130
+ border-top-color: ${props.borderTopColor || props.borderColor || "transparent"};
131
+ border-top-style: solid;
132
+ `}
133
+ ${(props) => props.borderLeftWidth !== void 0 && `
134
+ border-left-width: ${typeof props.borderLeftWidth === "number" ? `${props.borderLeftWidth}px` : props.borderLeftWidth};
135
+ border-left-color: ${props.borderLeftColor || props.borderColor || "transparent"};
136
+ border-left-style: solid;
137
+ `}
138
+ ${(props) => props.borderRightWidth !== void 0 && `
139
+ border-right-width: ${typeof props.borderRightWidth === "number" ? `${props.borderRightWidth}px` : props.borderRightWidth};
140
+ border-right-color: ${props.borderRightColor || props.borderColor || "transparent"};
141
+ border-right-style: solid;
142
+ `}
143
+
144
+ border-style: ${(props) => props.borderStyle || (props.borderWidth || props.borderBottomWidth || props.borderTopWidth || props.borderLeftWidth || props.borderRightWidth ? "solid" : "none")};
145
+ border-radius: ${(props) => typeof props.borderRadius === "number" ? `${props.borderRadius}px` : props.borderRadius || 0};
146
+ height: ${(props) => typeof props.height === "number" ? `${props.height}px` : props.height || "auto"};
147
+ width: ${(props) => typeof props.width === "number" ? `${props.width}px` : props.width || "auto"};
148
+ min-width: ${(props) => typeof props.minWidth === "number" ? `${props.minWidth}px` : props.minWidth || "auto"};
149
+ min-height: ${(props) => typeof props.minHeight === "number" ? `${props.minHeight}px` : props.minHeight || "auto"};
150
+ max-width: ${(props) => typeof props.maxWidth === "number" ? `${props.maxWidth}px` : props.maxWidth || "none"};
151
+ max-height: ${(props) => typeof props.maxHeight === "number" ? `${props.maxHeight}px` : props.maxHeight || "none"};
152
+
153
+ padding: ${(props) => typeof props.padding === "number" ? `${props.padding}px` : props.padding || 0};
154
+ ${(props) => props.paddingHorizontal && `
155
+ padding-left: ${typeof props.paddingHorizontal === "number" ? `${props.paddingHorizontal}px` : props.paddingHorizontal};
156
+ padding-right: ${typeof props.paddingHorizontal === "number" ? `${props.paddingHorizontal}px` : props.paddingHorizontal};
157
+ `}
158
+ ${(props) => props.paddingVertical && `
159
+ padding-top: ${typeof props.paddingVertical === "number" ? `${props.paddingVertical}px` : props.paddingVertical};
160
+ padding-bottom: ${typeof props.paddingVertical === "number" ? `${props.paddingVertical}px` : props.paddingVertical};
161
+ `}
162
+ ${(props) => props.paddingTop !== void 0 && `padding-top: ${typeof props.paddingTop === "number" ? `${props.paddingTop}px` : props.paddingTop};`}
163
+ ${(props) => props.paddingBottom !== void 0 && `padding-bottom: ${typeof props.paddingBottom === "number" ? `${props.paddingBottom}px` : props.paddingBottom};`}
164
+ ${(props) => props.paddingLeft !== void 0 && `padding-left: ${typeof props.paddingLeft === "number" ? `${props.paddingLeft}px` : props.paddingLeft};`}
165
+ ${(props) => props.paddingRight !== void 0 && `padding-right: ${typeof props.paddingRight === "number" ? `${props.paddingRight}px` : props.paddingRight};`}
166
+
167
+ margin: ${(props) => typeof props.margin === "number" ? `${props.margin}px` : props.margin || 0};
168
+ ${(props) => props.marginTop !== void 0 && `margin-top: ${typeof props.marginTop === "number" ? `${props.marginTop}px` : props.marginTop};`}
169
+ ${(props) => props.marginBottom !== void 0 && `margin-bottom: ${typeof props.marginBottom === "number" ? `${props.marginBottom}px` : props.marginBottom};`}
170
+ ${(props) => props.marginLeft !== void 0 && `margin-left: ${typeof props.marginLeft === "number" ? `${props.marginLeft}px` : props.marginLeft};`}
171
+ ${(props) => props.marginRight !== void 0 && `margin-right: ${typeof props.marginRight === "number" ? `${props.marginRight}px` : props.marginRight};`}
172
+
173
+ flex-direction: ${(props) => props.flexDirection || "column"};
174
+ flex-wrap: ${(props) => props.flexWrap || "nowrap"};
175
+ align-items: ${(props) => props.alignItems || "stretch"};
176
+ justify-content: ${(props) => props.justifyContent || "flex-start"};
177
+ cursor: ${(props) => props.cursor ? props.cursor : props.onClick || props.onPress ? "pointer" : "inherit"};
178
+ position: ${(props) => props.position || "static"};
179
+ top: ${(props) => typeof props.top === "number" ? `${props.top}px` : props.top};
180
+ bottom: ${(props) => typeof props.bottom === "number" ? `${props.bottom}px` : props.bottom};
181
+ left: ${(props) => typeof props.left === "number" ? `${props.left}px` : props.left};
182
+ right: ${(props) => typeof props.right === "number" ? `${props.right}px` : props.right};
183
+ flex: ${(props) => props.flex};
184
+ flex-shrink: ${(props) => props.flexShrink ?? 1};
185
+ gap: ${(props) => typeof props.gap === "number" ? `${props.gap}px` : props.gap || 0};
186
+ align-self: ${(props) => props.alignSelf || "auto"};
187
+ overflow: ${(props) => props.overflow || "visible"};
188
+ overflow-x: ${(props) => props.overflowX || "visible"};
189
+ overflow-y: ${(props) => props.overflowY || "visible"};
190
+ z-index: ${(props) => props.zIndex};
191
+ opacity: ${(props) => props.disabled ? 0.5 : 1};
192
+ pointer-events: ${(props) => props.disabled ? "none" : "auto"};
193
+
194
+ &:hover {
195
+ ${(props) => props.hoverStyle?.backgroundColor && `background-color: ${props.hoverStyle.backgroundColor};`}
196
+ ${(props) => props.hoverStyle?.borderColor && `border-color: ${props.hoverStyle.borderColor};`}
197
+ }
198
+
199
+ &:active {
200
+ ${(props) => props.pressStyle?.backgroundColor && `background-color: ${props.pressStyle.backgroundColor};`}
201
+ }
202
+ `;
203
+ var Box = import_react2.default.forwardRef(
204
+ ({
205
+ children,
206
+ onPress,
207
+ onKeyDown,
208
+ onKeyUp,
209
+ role,
210
+ "aria-label": ariaLabel,
211
+ "aria-labelledby": ariaLabelledBy,
212
+ "aria-current": ariaCurrent,
213
+ "aria-disabled": ariaDisabled,
214
+ "aria-live": ariaLive,
215
+ "aria-busy": ariaBusy,
216
+ "aria-describedby": ariaDescribedBy,
217
+ "aria-expanded": ariaExpanded,
218
+ "aria-haspopup": ariaHasPopup,
219
+ "aria-pressed": ariaPressed,
220
+ "aria-controls": ariaControls,
221
+ tabIndex,
222
+ as,
223
+ src,
224
+ alt,
225
+ type,
226
+ disabled,
227
+ id,
228
+ testID,
229
+ "data-testid": dataTestId,
230
+ ...props
231
+ }, ref) => {
232
+ if (as === "img" && src) {
233
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
234
+ "img",
235
+ {
236
+ src,
237
+ alt: alt || "",
238
+ style: {
239
+ display: "block",
240
+ objectFit: "cover",
241
+ width: typeof props.width === "number" ? `${props.width}px` : props.width,
242
+ height: typeof props.height === "number" ? `${props.height}px` : props.height,
243
+ borderRadius: typeof props.borderRadius === "number" ? `${props.borderRadius}px` : props.borderRadius,
244
+ position: props.position,
245
+ top: typeof props.top === "number" ? `${props.top}px` : props.top,
246
+ left: typeof props.left === "number" ? `${props.left}px` : props.left,
247
+ right: typeof props.right === "number" ? `${props.right}px` : props.right,
248
+ bottom: typeof props.bottom === "number" ? `${props.bottom}px` : props.bottom
249
+ }
250
+ }
251
+ );
252
+ }
253
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
254
+ StyledBox,
255
+ {
256
+ ref,
257
+ elementType: as,
258
+ id,
259
+ type: as === "button" ? type || "button" : void 0,
260
+ disabled: as === "button" ? disabled : void 0,
261
+ onClick: onPress,
262
+ onKeyDown,
263
+ onKeyUp,
264
+ role,
265
+ "aria-label": ariaLabel,
266
+ "aria-labelledby": ariaLabelledBy,
267
+ "aria-current": ariaCurrent,
268
+ "aria-disabled": ariaDisabled,
269
+ "aria-busy": ariaBusy,
270
+ "aria-describedby": ariaDescribedBy,
271
+ "aria-expanded": ariaExpanded,
272
+ "aria-haspopup": ariaHasPopup,
273
+ "aria-pressed": ariaPressed,
274
+ "aria-controls": ariaControls,
275
+ "aria-live": ariaLive,
276
+ tabIndex: tabIndex !== void 0 ? tabIndex : void 0,
277
+ "data-testid": dataTestId || testID,
278
+ ...props,
279
+ children
280
+ }
281
+ );
282
+ }
283
+ );
284
+ Box.displayName = "Box";
285
+
286
+ // ../../foundation/primitives-web/src/Text.tsx
287
+ var import_styled_components2 = __toESM(require("styled-components"));
288
+ var import_jsx_runtime2 = require("react/jsx-runtime");
289
+ var FilteredSpan = createFilteredElement("span");
290
+ var StyledText = (0, import_styled_components2.default)(FilteredSpan)`
291
+ color: ${(props) => props.color || "inherit"};
292
+ font-size: ${(props) => typeof props.fontSize === "number" ? `${props.fontSize}px` : props.fontSize || "inherit"};
293
+ font-weight: ${(props) => props.fontWeight || "normal"};
294
+ font-family: ${(props) => props.fontFamily || '"Aktiv Grotesk", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif'};
295
+ line-height: ${(props) => typeof props.lineHeight === "number" ? `${props.lineHeight}px` : props.lineHeight || "inherit"};
296
+ white-space: ${(props) => props.whiteSpace || "normal"};
297
+ text-align: ${(props) => props.textAlign || "inherit"};
298
+ text-decoration: ${(props) => props.textDecoration || "none"};
299
+ `;
300
+ var Text = ({
301
+ style,
302
+ className,
303
+ id,
304
+ role,
305
+ numberOfLines: _numberOfLines,
306
+ ...props
307
+ }) => {
308
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
309
+ StyledText,
310
+ {
311
+ ...props,
312
+ style,
313
+ className,
314
+ id,
315
+ role
316
+ }
317
+ );
318
+ };
319
+
320
+ // ../../foundation/primitives-web/src/index.tsx
321
+ var isWeb = true;
322
+
323
+ // src/ImageUploader.tsx
324
+ var import_xui_core = require("@xsolla/xui-core");
325
+ var import_xui_icons_base = require("@xsolla/xui-icons-base");
326
+ var import_xui_spinner = require("@xsolla/xui-spinner");
327
+ var import_jsx_runtime3 = require("react/jsx-runtime");
328
+ var ImageUploader = (0, import_react3.forwardRef)(
329
+ ({
330
+ size = "xl",
331
+ placeholder = "Upload",
332
+ uploadingPlaceholder = "Uploading",
333
+ description,
334
+ errorMessage,
335
+ wideView = false,
336
+ accept = "image/*",
337
+ openPicker,
338
+ value,
339
+ onUpload,
340
+ onChange,
341
+ onDelete,
342
+ disabled = false,
343
+ loading = false,
344
+ themeMode,
345
+ themeProductContext
346
+ }, ref) => {
347
+ const { theme } = (0, import_xui_core.useResolvedTheme)({ themeMode, themeProductContext });
348
+ const fileInputRef = (0, import_react3.useRef)(null);
349
+ const isControlled = value !== void 0;
350
+ const [internalPreview, setInternalPreview] = (0, import_react3.useState)(null);
351
+ const [isHover, setIsHover] = (0, import_react3.useState)(false);
352
+ const [isFocus, setIsFocus] = (0, import_react3.useState)(false);
353
+ const [isPreviewHover, setIsPreviewHover] = (0, import_react3.useState)(false);
354
+ const [isHoverless] = (0, import_react3.useState)(
355
+ () => !isWeb || typeof window !== "undefined" && !!window.matchMedia?.("(hover: none)").matches
356
+ );
357
+ const reactId = (0, import_xui_core.useId)();
358
+ const baseId = `image-uploader-${reactId.replace(/[^a-zA-Z0-9-]/g, "")}`;
359
+ const descriptionId = `${baseId}-description`;
360
+ const errorId = `${baseId}-error`;
361
+ const preview = isControlled ? value?.url ?? null : internalPreview;
362
+ (0, import_react3.useEffect)(() => {
363
+ if (!isControlled && value === null) setInternalPreview(null);
364
+ }, [isControlled, value]);
365
+ import_react3.default.useImperativeHandle(
366
+ ref,
367
+ () => fileInputRef.current,
368
+ []
369
+ );
370
+ const sizeStyles = theme.sizing.imageUploader(size);
371
+ const inputColors = theme.colors.control.input;
372
+ const focusColors = theme.colors.control.focus;
373
+ const alertBorder = theme.colors.control.alert.border;
374
+ const alertText = theme.colors.content.alert.primary;
375
+ const scrim = theme.colors.layer.scrim;
376
+ const hasError = !!errorMessage;
377
+ let state;
378
+ if (disabled) state = "disable";
379
+ else if (loading) state = "uploading";
380
+ else if (hasError) state = "error";
381
+ else if (preview)
382
+ state = isPreviewHover || isHoverless ? "uploadedHover" : "uploaded";
383
+ else if (isFocus) state = "focus";
384
+ else if (isHover) state = "hover";
385
+ else state = "default";
386
+ const emitFile = (img) => {
387
+ if (!isControlled) setInternalPreview(img.uri);
388
+ onUpload?.(img);
389
+ onChange?.({
390
+ filename: img.name,
391
+ url: img.uri
392
+ });
393
+ };
394
+ const handleWebFile = (file) => {
395
+ if (!file.type.startsWith("image/")) return;
396
+ const reader = new FileReader();
397
+ reader.onload = (e) => {
398
+ const uri = e.target?.result;
399
+ emitFile({
400
+ name: file.name,
401
+ size: file.size,
402
+ uri,
403
+ mimeType: file.type,
404
+ file
405
+ });
406
+ };
407
+ reader.readAsDataURL(file);
408
+ };
409
+ const handleClick = async () => {
410
+ if (disabled || loading) return;
411
+ if (preview) {
412
+ removeImage();
413
+ return;
414
+ }
415
+ if (openPicker) {
416
+ const picked = await openPicker();
417
+ if (picked) emitFile(picked);
418
+ return;
419
+ }
420
+ if (isWeb) {
421
+ fileInputRef.current?.click();
422
+ }
423
+ };
424
+ const handleFileChange = (e) => {
425
+ const file = e.target.files?.[0];
426
+ e.target.value = "";
427
+ if (file) handleWebFile(file);
428
+ };
429
+ const handleDragOver = (e) => {
430
+ e.preventDefault();
431
+ e.stopPropagation();
432
+ if (!disabled && !loading && !preview) setIsHover(true);
433
+ };
434
+ const handleDragLeave = (e) => {
435
+ e.preventDefault();
436
+ e.stopPropagation();
437
+ setIsHover(false);
438
+ };
439
+ const handleDrop = (e) => {
440
+ e.preventDefault();
441
+ e.stopPropagation();
442
+ setIsHover(false);
443
+ if (disabled || loading || preview) return;
444
+ const file = e.dataTransfer.files?.[0];
445
+ if (file) handleWebFile(file);
446
+ };
447
+ const removeImage = (e) => {
448
+ e?.stopPropagation?.();
449
+ if (!isControlled) setInternalPreview(null);
450
+ setIsPreviewHover(false);
451
+ onDelete?.();
452
+ onChange?.(null);
453
+ if (isWeb && fileInputRef.current) fileInputRef.current.value = "";
454
+ };
455
+ let backgroundColor = inputColors.bg;
456
+ let borderColor = inputColors.border;
457
+ let borderStyle = "dashed";
458
+ let labelColor = inputColors.text;
459
+ let descriptionColor = inputColors.placeholder;
460
+ let iconColor = inputColors.text;
461
+ switch (state) {
462
+ case "hover":
463
+ backgroundColor = inputColors.bgHover;
464
+ borderColor = inputColors.borderHover;
465
+ break;
466
+ case "focus":
467
+ case "uploading":
468
+ backgroundColor = focusColors.bg;
469
+ borderColor = focusColors.border;
470
+ break;
471
+ case "uploaded":
472
+ case "uploadedHover":
473
+ backgroundColor = "transparent";
474
+ borderColor = "transparent";
475
+ break;
476
+ case "error":
477
+ backgroundColor = inputColors.bg;
478
+ borderColor = alertBorder;
479
+ borderStyle = "solid";
480
+ labelColor = alertText;
481
+ iconColor = alertText;
482
+ break;
483
+ case "disable":
484
+ backgroundColor = inputColors.bgDisable;
485
+ borderColor = inputColors.borderDisable;
486
+ labelColor = inputColors.textDisable;
487
+ descriptionColor = inputColors.textDisable;
488
+ iconColor = inputColors.textDisable;
489
+ break;
490
+ }
491
+ const rootGap = state === "error" ? sizeStyles.errorGap : 0;
492
+ const renderInner = () => {
493
+ if (state === "uploaded" || state === "uploadedHover") {
494
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
495
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
496
+ Box,
497
+ {
498
+ as: "img",
499
+ src: preview || void 0,
500
+ alt: value?.filename ?? "Uploaded image",
501
+ position: "absolute",
502
+ top: 0,
503
+ left: 0,
504
+ right: 0,
505
+ bottom: 0,
506
+ borderRadius: sizeStyles.radius,
507
+ resizeMode: "cover",
508
+ ...isWeb && { width: "100%", height: "100%" },
509
+ style: isWeb ? { objectFit: "cover" } : void 0
510
+ }
511
+ ),
512
+ state === "uploadedHover" && !disabled && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
513
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
514
+ Box,
515
+ {
516
+ position: "absolute",
517
+ top: 0,
518
+ left: 0,
519
+ right: 0,
520
+ bottom: 0,
521
+ backgroundColor: scrim,
522
+ borderRadius: sizeStyles.radius,
523
+ ...isWeb && { "aria-hidden": "true" }
524
+ }
525
+ ),
526
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
527
+ Box,
528
+ {
529
+ position: "relative",
530
+ width: sizeStyles.iconSize,
531
+ height: sizeStyles.iconSize,
532
+ alignItems: "center",
533
+ justifyContent: "center",
534
+ zIndex: 1,
535
+ pointerEvents: "none",
536
+ ...isWeb && { "aria-hidden": "true" },
537
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_xui_icons_base.TrashCan, { size: sizeStyles.iconSize, color: "#ffffff" })
538
+ }
539
+ )
540
+ ] })
541
+ ] });
542
+ }
543
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
544
+ state === "uploading" ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
545
+ import_xui_spinner.Spinner,
546
+ {
547
+ size,
548
+ color: theme.colors.content.brand.primary,
549
+ ...isWeb && { "aria-hidden": "true" }
550
+ }
551
+ ) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Box, { ...isWeb && { "aria-hidden": "true" }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_xui_icons_base.Image, { size: sizeStyles.iconSize, color: iconColor }) }),
552
+ placeholder && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
553
+ Text,
554
+ {
555
+ "data-testid": "image-uploader__placeholder",
556
+ color: labelColor,
557
+ fontSize: sizeStyles.labelFontSize,
558
+ lineHeight: sizeStyles.labelLineHeight,
559
+ fontWeight: "500",
560
+ textAlign: "center",
561
+ numberOfLines: 1,
562
+ style: isWeb ? {
563
+ maxWidth: "100%",
564
+ overflow: "hidden",
565
+ textOverflow: "ellipsis",
566
+ whiteSpace: "nowrap"
567
+ } : void 0,
568
+ children: state === "uploading" ? uploadingPlaceholder : placeholder
569
+ }
570
+ ),
571
+ description && wideView && state !== "uploading" && state !== "error" && (typeof description === "string" ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
572
+ Text,
573
+ {
574
+ "data-testid": "image-uploader__description",
575
+ color: descriptionColor,
576
+ fontSize: sizeStyles.descriptionFontSize,
577
+ lineHeight: sizeStyles.descriptionLineHeight,
578
+ textAlign: "center",
579
+ numberOfLines: 1,
580
+ style: isWeb ? {
581
+ maxWidth: "100%",
582
+ overflow: "hidden",
583
+ textOverflow: "ellipsis",
584
+ whiteSpace: "nowrap"
585
+ } : void 0,
586
+ ...isWeb && { id: descriptionId },
587
+ children: description
588
+ }
589
+ ) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
590
+ Box,
591
+ {
592
+ "data-testid": "image-uploader__description",
593
+ ...isWeb && { id: descriptionId },
594
+ children: description
595
+ }
596
+ ))
597
+ ] });
598
+ };
599
+ const webOnlyHandlers = isWeb ? {
600
+ onMouseEnter: () => {
601
+ if (disabled || loading) return;
602
+ if (preview) setIsPreviewHover(true);
603
+ else setIsHover(true);
604
+ },
605
+ onMouseLeave: () => {
606
+ if (preview) setIsPreviewHover(false);
607
+ else setIsHover(false);
608
+ },
609
+ onFocus: (e) => {
610
+ if (disabled || loading || preview) return;
611
+ if (typeof e.target?.matches === "function" && !e.target.matches(":focus-visible")) {
612
+ return;
613
+ }
614
+ setIsFocus(true);
615
+ },
616
+ onBlur: () => setIsFocus(false),
617
+ onDragOver: handleDragOver,
618
+ onDragLeave: handleDragLeave,
619
+ onDrop: handleDrop
620
+ } : {};
621
+ const webOnlyStyle = isWeb ? {
622
+ boxSizing: "border-box",
623
+ display: "flex",
624
+ cursor: disabled || loading ? "not-allowed" : "pointer",
625
+ outline: "none",
626
+ transition: "background-color 0.15s ease, border-color 0.15s ease"
627
+ } : {};
628
+ const buttonAriaLabel = preview ? `Remove image${value?.filename ? `: ${value.filename}` : ""}` : state === "uploading" ? uploadingPlaceholder : placeholder;
629
+ const describedBy = [
630
+ state === "error" && errorMessage ? errorId : null,
631
+ description && wideView && state !== "uploading" && state !== "error" ? descriptionId : null
632
+ ].filter(Boolean).join(" ") || void 0;
633
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
634
+ Box,
635
+ {
636
+ "data-testid": "image-uploader",
637
+ flexDirection: "column",
638
+ gap: rootGap,
639
+ alignItems: "flex-start",
640
+ width: wideView ? "100%" : void 0,
641
+ maxWidth: wideView ? sizeStyles.wideWidth : void 0,
642
+ children: [
643
+ isWeb && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
644
+ "input",
645
+ {
646
+ type: "file",
647
+ ref: fileInputRef,
648
+ accept,
649
+ onChange: handleFileChange,
650
+ style: { display: "none" },
651
+ disabled,
652
+ tabIndex: -1,
653
+ "aria-hidden": "true",
654
+ "data-testid": "image-uploader__input"
655
+ }
656
+ ),
657
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
658
+ Box,
659
+ {
660
+ as: isWeb ? "button" : "div",
661
+ disabled: disabled || loading,
662
+ "data-testid": "image-uploader__button",
663
+ onPress: handleClick,
664
+ ...isWeb && {
665
+ "aria-label": buttonAriaLabel,
666
+ "aria-busy": loading || void 0,
667
+ "aria-invalid": state === "error" || void 0,
668
+ "aria-describedby": describedBy
669
+ },
670
+ ...webOnlyHandlers,
671
+ position: "relative",
672
+ width: wideView ? "100%" : sizeStyles.box,
673
+ maxWidth: wideView ? sizeStyles.wideWidth : void 0,
674
+ height: sizeStyles.box,
675
+ flexDirection: "column",
676
+ alignItems: "center",
677
+ justifyContent: "center",
678
+ gap: sizeStyles.gap,
679
+ padding: state === "uploaded" ? 0 : sizeStyles.padding,
680
+ borderWidth: sizeStyles.borderWidth,
681
+ borderStyle,
682
+ borderColor,
683
+ borderRadius: sizeStyles.radius,
684
+ backgroundColor,
685
+ overflow: "hidden",
686
+ style: webOnlyStyle,
687
+ children: renderInner()
688
+ }
689
+ ),
690
+ state === "error" && errorMessage && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
691
+ Text,
692
+ {
693
+ "data-testid": "image-uploader__error",
694
+ color: alertText,
695
+ fontSize: sizeStyles.errorFontSize,
696
+ lineHeight: sizeStyles.errorLineHeight,
697
+ ...isWeb && { id: errorId, role: "alert" },
698
+ children: errorMessage
699
+ }
700
+ )
701
+ ]
702
+ }
703
+ );
704
+ }
705
+ );
706
+ ImageUploader.displayName = "ImageUploader";
707
+ // Annotate the CommonJS export names for ESM import in node:
708
+ 0 && (module.exports = {
709
+ ImageUploader
710
+ });
711
+ //# sourceMappingURL=index.js.map