panelui-native 0.110.0 → 0.112.0
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/lib/module/components/avatar-cropper/avatar-cropper-math.js +68 -28
- package/lib/module/components/avatar-cropper/avatar-cropper-math.js.map +1 -1
- package/lib/module/components/avatar-cropper/index.js +190 -24
- package/lib/module/components/avatar-cropper/index.js.map +1 -1
- package/lib/module/components/file-upload/file-upload-core.js +317 -0
- package/lib/module/components/file-upload/file-upload-core.js.map +1 -0
- package/lib/module/components/file-upload/index.js +1214 -0
- package/lib/module/components/file-upload/index.js.map +1 -0
- package/lib/module/icons/index.js +10 -0
- package/lib/module/icons/index.js.map +1 -1
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/components/avatar-cropper/avatar-cropper-math.d.ts +32 -10
- package/lib/typescript/src/components/avatar-cropper/avatar-cropper-math.d.ts.map +1 -1
- package/lib/typescript/src/components/avatar-cropper/index.d.ts +30 -3
- package/lib/typescript/src/components/avatar-cropper/index.d.ts.map +1 -1
- package/lib/typescript/src/components/file-upload/file-upload-core.d.ts +136 -0
- package/lib/typescript/src/components/file-upload/file-upload-core.d.ts.map +1 -0
- package/lib/typescript/src/components/file-upload/index.d.ts +265 -0
- package/lib/typescript/src/components/file-upload/index.d.ts.map +1 -0
- package/lib/typescript/src/icons/index.d.ts +2 -0
- package/lib/typescript/src/icons/index.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +5 -1
- package/src/components/avatar-cropper/avatar-cropper-math.ts +78 -42
- package/src/components/avatar-cropper/index.tsx +251 -23
- package/src/components/file-upload/file-upload-core.ts +359 -0
- package/src/components/file-upload/index.tsx +1435 -0
- package/src/icons/index.tsx +4 -0
- package/src/index.ts +24 -0
|
@@ -0,0 +1,1214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* FileUpload — pick files and photos, check them, and run their uploads.
|
|
5
|
+
*
|
|
6
|
+
* ```tsx
|
|
7
|
+
* <FileUpload
|
|
8
|
+
* value={files}
|
|
9
|
+
* onValueChange={setFiles}
|
|
10
|
+
* sources={['camera', 'library', 'files']}
|
|
11
|
+
* accept={['image/*', 'application/pdf']}
|
|
12
|
+
* multiple
|
|
13
|
+
* maxFiles={5}
|
|
14
|
+
* maxSize={10 * 1024 * 1024}
|
|
15
|
+
* upload={(file, { signal, onProgress }) => send(file, { signal, onProgress })}
|
|
16
|
+
* >
|
|
17
|
+
* <FileUpload.Dropzone />
|
|
18
|
+
* <FileUpload.List />
|
|
19
|
+
* </FileUpload>
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* ## One component for the whole trip
|
|
23
|
+
*
|
|
24
|
+
* Choosing a document, choosing or taking a photo, and sending either of them
|
|
25
|
+
* somewhere are one flow from the user's side: they add things, watch them go,
|
|
26
|
+
* and fix the ones that failed. So the root owns all of it — the list of files,
|
|
27
|
+
* the rules they have to pass, and the upload queue — and the parts are only
|
|
28
|
+
* different ways of drawing that list. `Dropzone` and `Trigger` add to it,
|
|
29
|
+
* `List` draws it as rows, `Grid` draws it as photo tiles, and `useFileUpload()`
|
|
30
|
+
* hands the same state to anything drawn by hand.
|
|
31
|
+
*
|
|
32
|
+
* ## The upload belongs to the app
|
|
33
|
+
*
|
|
34
|
+
* The component never makes a request. `upload` is the app's function: it
|
|
35
|
+
* receives one file, an `AbortSignal` and an `onProgress` callback, and
|
|
36
|
+
* resolves when the server has the file. That keeps the storage provider, the
|
|
37
|
+
* auth headers and the retry policy where they already live, and leaves the
|
|
38
|
+
* component with the part every app repeats — running at most `concurrency`
|
|
39
|
+
* at once, showing progress, and wiring cancel, retry and remove to the right
|
|
40
|
+
* file. The status of every file lives in `value`, so a form can read whether
|
|
41
|
+
* everything has finished without asking the component.
|
|
42
|
+
*
|
|
43
|
+
* ## Native modules are optional
|
|
44
|
+
*
|
|
45
|
+
* Each source reaches its Expo module only when it is used: `files` through
|
|
46
|
+
* `expo-document-picker`, `camera` and `library` through `expo-image-picker`,
|
|
47
|
+
* and resizing through `expo-image-manipulator`. An app that only takes
|
|
48
|
+
* documents never installs the camera module. A source whose module is
|
|
49
|
+
* missing reports an error with the install command rather than crashing.
|
|
50
|
+
*/
|
|
51
|
+
import { Children, cloneElement, createContext, forwardRef, isValidElement, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
52
|
+
import { AccessibilityInfo, Image, ScrollView, View } from 'react-native';
|
|
53
|
+
import Animated, { useAnimatedProps, useSharedValue, withTiming } from 'react-native-reanimated';
|
|
54
|
+
import Svg, { Circle } from 'react-native-svg';
|
|
55
|
+
import { tv } from 'tailwind-variants';
|
|
56
|
+
import { useCSSVariable } from 'uniwind';
|
|
57
|
+
import { CameraIcon, CheckCircleIcon, FileIcon, FolderIcon, ImageIcon, PlusIcon, RotateCcwIcon, TrashIcon, UploadIcon, VideoIcon, XIcon } from "../../icons/index.js";
|
|
58
|
+
import { AnimatedPressable } from "../../primitives/animated-pressable.js";
|
|
59
|
+
import { useControllableState } from "../../primitives/controllable-state.js";
|
|
60
|
+
import { Text } from "../../primitives/text.js";
|
|
61
|
+
import { cn } from "../../utils/cn.js";
|
|
62
|
+
import { Attachment } from "../attachment/index.js";
|
|
63
|
+
import { useFieldLabelledBy } from "../field/index.js";
|
|
64
|
+
import { ImageViewer } from "../image-viewer/index.js";
|
|
65
|
+
import { Menu } from "../menu/index.js";
|
|
66
|
+
import { createFileId, describeRules, extensionOf, fitWithin, formatFileSize, isActive, kindOf, libraryMediaTypes, nextToStart, partitionFiles, pickerTypes, truncateMiddle } from "./file-upload-core.js";
|
|
67
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
68
|
+
export { describeRules as describeFileUploadRules, formatFileSize };
|
|
69
|
+
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
// Optional dependencies
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Each `require` sits directly in its `try` block, which is what lets Metro
|
|
76
|
+
* bundle an app that does not have the package.
|
|
77
|
+
*/
|
|
78
|
+
function loadImagePicker() {
|
|
79
|
+
try {
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
81
|
+
return require('expo-image-picker');
|
|
82
|
+
} catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function loadDocumentPicker() {
|
|
87
|
+
try {
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
89
|
+
return require('expo-document-picker');
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function loadManipulator() {
|
|
95
|
+
try {
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
97
|
+
return require('expo-image-manipulator');
|
|
98
|
+
} catch {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const MODULE_FOR = {
|
|
103
|
+
camera: 'expo-image-picker',
|
|
104
|
+
library: 'expo-image-picker',
|
|
105
|
+
files: 'expo-document-picker'
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/** True when the module a source needs is installed. */
|
|
109
|
+
export function hasFileUploadSource(source) {
|
|
110
|
+
return source === 'files' ? loadDocumentPicker() !== null : loadImagePicker() !== null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
// Types
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
|
|
117
|
+
/** What the upload handler is given besides the file. */
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Sends one file. Resolve when the server has it — the value is kept as the
|
|
121
|
+
* file's `response` — and throw to mark it failed; the error's message is shown
|
|
122
|
+
* on the row.
|
|
123
|
+
*/
|
|
124
|
+
|
|
125
|
+
/** How photos are prepared before they are added. */
|
|
126
|
+
|
|
127
|
+
const FileUploadContext = /*#__PURE__*/createContext(null);
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The state and actions of the nearest `FileUpload`, for drawing a part of it
|
|
131
|
+
* by hand — a single avatar slot, a counter, a submit button that waits for
|
|
132
|
+
* every upload.
|
|
133
|
+
*/
|
|
134
|
+
export function useFileUpload() {
|
|
135
|
+
const context = useContext(FileUploadContext);
|
|
136
|
+
if (!context) throw new Error('useFileUpload must be used inside <FileUpload>.');
|
|
137
|
+
return context;
|
|
138
|
+
}
|
|
139
|
+
const DEFAULT_SOURCES = ['library', 'files'];
|
|
140
|
+
const DEFAULT_LABELS = {
|
|
141
|
+
camera: 'Take photo',
|
|
142
|
+
library: 'Choose from library',
|
|
143
|
+
files: 'Browse files'
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** How long the chooser sheet takes to leave before a system picker opens over it. */
|
|
147
|
+
const CHOOSER_EXIT_MS = 320;
|
|
148
|
+
|
|
149
|
+
// ---------------------------------------------------------------------------
|
|
150
|
+
// Root
|
|
151
|
+
// ---------------------------------------------------------------------------
|
|
152
|
+
|
|
153
|
+
function FileUploadRoot({
|
|
154
|
+
value: valueProp,
|
|
155
|
+
defaultValue = [],
|
|
156
|
+
onValueChange,
|
|
157
|
+
sources: sourcesProp = DEFAULT_SOURCES,
|
|
158
|
+
accept,
|
|
159
|
+
multiple = false,
|
|
160
|
+
maxFiles: maxFilesProp,
|
|
161
|
+
maxSize,
|
|
162
|
+
minSize,
|
|
163
|
+
validate,
|
|
164
|
+
onReject,
|
|
165
|
+
onError,
|
|
166
|
+
image,
|
|
167
|
+
upload: uploadHandler,
|
|
168
|
+
concurrency = 2,
|
|
169
|
+
autoUpload = true,
|
|
170
|
+
onUploadComplete,
|
|
171
|
+
sourceLabels,
|
|
172
|
+
disabled = false,
|
|
173
|
+
invalid = false,
|
|
174
|
+
children
|
|
175
|
+
}) {
|
|
176
|
+
const maxFiles = multiple ? maxFilesProp : 1;
|
|
177
|
+
const {
|
|
178
|
+
value,
|
|
179
|
+
setValue
|
|
180
|
+
} = useControllableState({
|
|
181
|
+
value: valueProp,
|
|
182
|
+
defaultValue,
|
|
183
|
+
onChange: onValueChange
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
/*
|
|
187
|
+
* The list as this component last wrote it. Uploads report several changes
|
|
188
|
+
* inside one tick — a file starts, the next one queues, progress arrives —
|
|
189
|
+
* and a controlled owner has not re-rendered between them, so building each
|
|
190
|
+
* change on `value` would drop all but the last. Every change is built on
|
|
191
|
+
* this ref instead, and the ref takes the owner's value whenever it changes.
|
|
192
|
+
*/
|
|
193
|
+
const latest = useRef(value);
|
|
194
|
+
const seen = useRef(value);
|
|
195
|
+
if (value !== seen.current) {
|
|
196
|
+
seen.current = value;
|
|
197
|
+
latest.current = value;
|
|
198
|
+
}
|
|
199
|
+
const commit = useCallback(change => {
|
|
200
|
+
const next = change(latest.current);
|
|
201
|
+
if (next === latest.current) return;
|
|
202
|
+
latest.current = next;
|
|
203
|
+
setValue(next);
|
|
204
|
+
}, [setValue]);
|
|
205
|
+
const patch = useCallback((id, fields) => commit(files => files.some(file => file.id === id) ? files.map(file => file.id === id ? {
|
|
206
|
+
...file,
|
|
207
|
+
...fields
|
|
208
|
+
} : file) : files), [commit]);
|
|
209
|
+
const [rejections, setRejections] = useState([]);
|
|
210
|
+
|
|
211
|
+
// Callbacks read through a ref so the queue never runs a stale handler.
|
|
212
|
+
const handlers = useRef({
|
|
213
|
+
uploadHandler,
|
|
214
|
+
onUploadComplete,
|
|
215
|
+
onReject,
|
|
216
|
+
onError,
|
|
217
|
+
validate
|
|
218
|
+
});
|
|
219
|
+
handlers.current = {
|
|
220
|
+
uploadHandler,
|
|
221
|
+
onUploadComplete,
|
|
222
|
+
onReject,
|
|
223
|
+
onError,
|
|
224
|
+
validate
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// ---- upload queue -------------------------------------------------------
|
|
228
|
+
|
|
229
|
+
const jobs = useRef(new Map());
|
|
230
|
+
const pendingProgress = useRef(new Map());
|
|
231
|
+
const frame = useRef(null);
|
|
232
|
+
const flushProgress = useCallback(() => {
|
|
233
|
+
frame.current = null;
|
|
234
|
+
const pending = pendingProgress.current;
|
|
235
|
+
if (pending.size === 0) return;
|
|
236
|
+
pendingProgress.current = new Map();
|
|
237
|
+
commit(files => files.map(file => pending.has(file.id) && file.status === 'uploading' ? {
|
|
238
|
+
...file,
|
|
239
|
+
progress: pending.get(file.id)
|
|
240
|
+
} : file));
|
|
241
|
+
}, [commit]);
|
|
242
|
+
const pump = useCallback(() => {
|
|
243
|
+
const handler = handlers.current.uploadHandler;
|
|
244
|
+
if (!handler) return;
|
|
245
|
+
for (const id of nextToStart(latest.current, concurrency)) {
|
|
246
|
+
const file = latest.current.find(entry => entry.id === id);
|
|
247
|
+
if (!file) continue;
|
|
248
|
+
const controller = new AbortController();
|
|
249
|
+
jobs.current.set(id, controller);
|
|
250
|
+
patch(id, {
|
|
251
|
+
status: 'uploading',
|
|
252
|
+
progress: 0,
|
|
253
|
+
error: undefined
|
|
254
|
+
});
|
|
255
|
+
const context = {
|
|
256
|
+
signal: controller.signal,
|
|
257
|
+
onProgress: progress => {
|
|
258
|
+
if (controller.signal.aborted) return;
|
|
259
|
+
pendingProgress.current.set(id, Math.min(1, Math.max(0, progress)));
|
|
260
|
+
frame.current ??= requestAnimationFrame(flushProgress);
|
|
261
|
+
},
|
|
262
|
+
setProcessing: () => {
|
|
263
|
+
if (controller.signal.aborted) return;
|
|
264
|
+
pendingProgress.current.delete(id);
|
|
265
|
+
patch(id, {
|
|
266
|
+
status: 'processing',
|
|
267
|
+
progress: 1
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
Promise.resolve().then(() => handler({
|
|
272
|
+
...file,
|
|
273
|
+
status: 'uploading',
|
|
274
|
+
progress: 0
|
|
275
|
+
}, context)).then(response => {
|
|
276
|
+
if (controller.signal.aborted) return;
|
|
277
|
+
jobs.current.delete(id);
|
|
278
|
+
pendingProgress.current.delete(id);
|
|
279
|
+
patch(id, {
|
|
280
|
+
status: 'done',
|
|
281
|
+
progress: 1,
|
|
282
|
+
response,
|
|
283
|
+
error: undefined
|
|
284
|
+
});
|
|
285
|
+
const done = latest.current.find(entry => entry.id === id);
|
|
286
|
+
if (done) handlers.current.onUploadComplete?.(done);
|
|
287
|
+
}, error => {
|
|
288
|
+
if (controller.signal.aborted) return;
|
|
289
|
+
jobs.current.delete(id);
|
|
290
|
+
pendingProgress.current.delete(id);
|
|
291
|
+
patch(id, {
|
|
292
|
+
status: 'error',
|
|
293
|
+
error: error instanceof Error && error.message ? error.message : 'Upload failed'
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
}, [concurrency, flushProgress, patch]);
|
|
298
|
+
|
|
299
|
+
// Whatever changed the list — a pick, a finish, a retry — may have freed a
|
|
300
|
+
// slot or queued a file. Starting is idempotent, so running on every change
|
|
301
|
+
// is the whole scheduler.
|
|
302
|
+
useEffect(() => {
|
|
303
|
+
pump();
|
|
304
|
+
}, [value, pump, uploadHandler]);
|
|
305
|
+
useEffect(() => {
|
|
306
|
+
const running = jobs.current;
|
|
307
|
+
return () => {
|
|
308
|
+
for (const controller of running.values()) controller.abort();
|
|
309
|
+
running.clear();
|
|
310
|
+
if (frame.current !== null) cancelAnimationFrame(frame.current);
|
|
311
|
+
};
|
|
312
|
+
}, []);
|
|
313
|
+
const stop = useCallback(id => {
|
|
314
|
+
jobs.current.get(id)?.abort();
|
|
315
|
+
jobs.current.delete(id);
|
|
316
|
+
pendingProgress.current.delete(id);
|
|
317
|
+
}, []);
|
|
318
|
+
const remove = useCallback(id => {
|
|
319
|
+
stop(id);
|
|
320
|
+
setRejections([]);
|
|
321
|
+
commit(files => files.filter(file => file.id !== id));
|
|
322
|
+
}, [commit, stop]);
|
|
323
|
+
const cancel = useCallback(id => {
|
|
324
|
+
const file = latest.current.find(entry => entry.id === id);
|
|
325
|
+
if (!file || !(isActive(file.status) || file.status === 'queued')) return;
|
|
326
|
+
stop(id);
|
|
327
|
+
patch(id, {
|
|
328
|
+
status: 'canceled',
|
|
329
|
+
progress: undefined
|
|
330
|
+
});
|
|
331
|
+
}, [patch, stop]);
|
|
332
|
+
const retry = useCallback(id => {
|
|
333
|
+
if (!handlers.current.uploadHandler) return;
|
|
334
|
+
patch(id, {
|
|
335
|
+
status: 'queued',
|
|
336
|
+
progress: 0,
|
|
337
|
+
error: undefined
|
|
338
|
+
});
|
|
339
|
+
}, [patch]);
|
|
340
|
+
const uploadNow = useCallback(id => {
|
|
341
|
+
if (!handlers.current.uploadHandler) return;
|
|
342
|
+
commit(files => files.map(file => (id === undefined || file.id === id) && (file.status === 'idle' || file.status === 'canceled' || file.status === 'error') ? {
|
|
343
|
+
...file,
|
|
344
|
+
status: 'queued',
|
|
345
|
+
progress: 0,
|
|
346
|
+
error: undefined
|
|
347
|
+
} : file));
|
|
348
|
+
}, [commit]);
|
|
349
|
+
const clear = useCallback(() => {
|
|
350
|
+
for (const id of [...jobs.current.keys()]) stop(id);
|
|
351
|
+
setRejections([]);
|
|
352
|
+
commit(() => []);
|
|
353
|
+
}, [commit, stop]);
|
|
354
|
+
|
|
355
|
+
// ---- picking ------------------------------------------------------------
|
|
356
|
+
|
|
357
|
+
const report = useCallback(error => {
|
|
358
|
+
if (handlers.current.onError) handlers.current.onError(error);else if (__DEV__) console.warn(`FileUpload: ${error.message}`);
|
|
359
|
+
}, []);
|
|
360
|
+
const pick = useCallback(async source => {
|
|
361
|
+
if (disabled) return;
|
|
362
|
+
// A single-file upload replaces its file rather than filling up.
|
|
363
|
+
const replacing = !multiple;
|
|
364
|
+
const held = replacing ? 0 : latest.current.length;
|
|
365
|
+
const remaining = maxFiles === undefined ? undefined : Math.max(0, maxFiles - held);
|
|
366
|
+
if (remaining === 0) return;
|
|
367
|
+
const many = multiple && remaining !== 1;
|
|
368
|
+
const quality = image?.quality ?? 0.9;
|
|
369
|
+
const crop = !many ? image?.crop : undefined;
|
|
370
|
+
let picked;
|
|
371
|
+
try {
|
|
372
|
+
if (source === 'files') {
|
|
373
|
+
const picker = loadDocumentPicker();
|
|
374
|
+
if (!picker) throw missing(source);
|
|
375
|
+
const result = await picker.getDocumentAsync({
|
|
376
|
+
type: pickerTypes(accept),
|
|
377
|
+
multiple: many,
|
|
378
|
+
copyToCacheDirectory: true
|
|
379
|
+
});
|
|
380
|
+
if (result.canceled) return;
|
|
381
|
+
picked = (result.assets ?? []).map(asset => fromDocument(asset));
|
|
382
|
+
} else {
|
|
383
|
+
const picker = loadImagePicker();
|
|
384
|
+
if (!picker) throw missing(source);
|
|
385
|
+
const options = {
|
|
386
|
+
mediaTypes: source === 'camera' ? ['images'] : libraryMediaTypes(accept),
|
|
387
|
+
allowsMultipleSelection: source === 'library' && many,
|
|
388
|
+
selectionLimit: many ? remaining ?? 0 : 1,
|
|
389
|
+
orderedSelection: true,
|
|
390
|
+
allowsEditing: crop !== undefined,
|
|
391
|
+
aspect: crop?.aspect,
|
|
392
|
+
quality,
|
|
393
|
+
exif: false
|
|
394
|
+
};
|
|
395
|
+
let result;
|
|
396
|
+
if (source === 'camera') {
|
|
397
|
+
const permission = await picker.requestCameraPermissionsAsync();
|
|
398
|
+
if (!permission.granted) {
|
|
399
|
+
throw new Error('Camera permission was refused. It can be turned on in Settings.');
|
|
400
|
+
}
|
|
401
|
+
result = await picker.launchCameraAsync(options);
|
|
402
|
+
} else {
|
|
403
|
+
result = await picker.launchImageLibraryAsync(options);
|
|
404
|
+
}
|
|
405
|
+
if (result.canceled) return;
|
|
406
|
+
picked = await Promise.all((result.assets ?? []).map((asset, index) => prepareImage(fromPickerAsset(asset, index), image, quality)));
|
|
407
|
+
}
|
|
408
|
+
} catch (error) {
|
|
409
|
+
report(error instanceof Error ? error : new Error(String(error)));
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
const {
|
|
413
|
+
accepted,
|
|
414
|
+
rejected
|
|
415
|
+
} = partitionFiles(picked, held, {
|
|
416
|
+
accept,
|
|
417
|
+
maxSize,
|
|
418
|
+
minSize,
|
|
419
|
+
maxFiles,
|
|
420
|
+
validate: handlers.current.validate
|
|
421
|
+
});
|
|
422
|
+
setRejections(rejected);
|
|
423
|
+
if (rejected.length > 0) {
|
|
424
|
+
handlers.current.onReject?.(rejected);
|
|
425
|
+
AccessibilityInfo.announceForAccessibility(rejected.map(entry => entry.message).join(' '));
|
|
426
|
+
}
|
|
427
|
+
if (accepted.length === 0) return;
|
|
428
|
+
const status = handlers.current.uploadHandler && autoUpload ? 'queued' : 'idle';
|
|
429
|
+
const added = accepted.map(file => ({
|
|
430
|
+
...file,
|
|
431
|
+
status
|
|
432
|
+
}));
|
|
433
|
+
if (replacing) {
|
|
434
|
+
for (const file of latest.current) stop(file.id);
|
|
435
|
+
commit(() => added.slice(0, 1));
|
|
436
|
+
} else {
|
|
437
|
+
commit(files => [...files, ...added]);
|
|
438
|
+
}
|
|
439
|
+
}, [accept, autoUpload, commit, disabled, image, maxFiles, maxSize, minSize, multiple, report, stop]);
|
|
440
|
+
|
|
441
|
+
// A source whose picker could never return an allowed file is left out.
|
|
442
|
+
const sources = useMemo(() => sourcesProp.filter(source => {
|
|
443
|
+
if (source === 'files') return true;
|
|
444
|
+
const media = libraryMediaTypes(accept);
|
|
445
|
+
return source === 'camera' ? media.includes('images') : media.length > 0;
|
|
446
|
+
}), [accept, sourcesProp]);
|
|
447
|
+
const context = useMemo(() => ({
|
|
448
|
+
files: value,
|
|
449
|
+
sources,
|
|
450
|
+
sourceLabels: {
|
|
451
|
+
...DEFAULT_LABELS,
|
|
452
|
+
...sourceLabels
|
|
453
|
+
},
|
|
454
|
+
accept,
|
|
455
|
+
maxFiles,
|
|
456
|
+
maxSize,
|
|
457
|
+
multiple,
|
|
458
|
+
disabled,
|
|
459
|
+
invalid,
|
|
460
|
+
canAddMore: !disabled && (!multiple || maxFiles === undefined || value.length < maxFiles),
|
|
461
|
+
hasUpload: uploadHandler !== undefined,
|
|
462
|
+
rejections,
|
|
463
|
+
pick,
|
|
464
|
+
remove,
|
|
465
|
+
cancel,
|
|
466
|
+
retry,
|
|
467
|
+
upload: uploadNow,
|
|
468
|
+
clear
|
|
469
|
+
}), [accept, cancel, clear, disabled, invalid, maxFiles, maxSize, multiple, pick, rejections, remove, retry, sourceLabels, sources, uploadHandler, uploadNow, value]);
|
|
470
|
+
return /*#__PURE__*/_jsx(FileUploadContext.Provider, {
|
|
471
|
+
value: context,
|
|
472
|
+
children: children
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
function missing(source) {
|
|
476
|
+
const name = MODULE_FOR[source];
|
|
477
|
+
return new Error(`Adding from "${source}" needs ${name}. Install it with: npx expo install ${name}`);
|
|
478
|
+
}
|
|
479
|
+
function fromDocument(asset) {
|
|
480
|
+
const file = {
|
|
481
|
+
uri: asset.uri,
|
|
482
|
+
name: asset.name,
|
|
483
|
+
size: asset.size ?? undefined,
|
|
484
|
+
mimeType: asset.mimeType ?? undefined
|
|
485
|
+
};
|
|
486
|
+
return {
|
|
487
|
+
id: createFileId(),
|
|
488
|
+
...file,
|
|
489
|
+
kind: kindOf(file),
|
|
490
|
+
status: 'idle'
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
function fromPickerAsset(asset, index) {
|
|
494
|
+
const video = asset.type === 'video' || asset.mimeType?.startsWith('video/');
|
|
495
|
+
const fallbackExt = video ? 'mp4' : 'jpg';
|
|
496
|
+
const name = asset.fileName || `${video ? 'Video' : 'Photo'} ${timestamp()}${index ? `-${index + 1}` : ''}.${fallbackExt}`;
|
|
497
|
+
const mimeType = asset.mimeType ?? (video ? 'video/mp4' : extensionOf(name) === 'png' ? 'image/png' : 'image/jpeg');
|
|
498
|
+
return {
|
|
499
|
+
id: createFileId(),
|
|
500
|
+
uri: asset.uri,
|
|
501
|
+
name,
|
|
502
|
+
size: asset.fileSize ?? undefined,
|
|
503
|
+
mimeType,
|
|
504
|
+
width: asset.width,
|
|
505
|
+
height: asset.height,
|
|
506
|
+
kind: video ? 'video' : 'image',
|
|
507
|
+
status: 'idle'
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
function timestamp() {
|
|
511
|
+
const now = new Date();
|
|
512
|
+
const pad = n => String(n).padStart(2, '0');
|
|
513
|
+
return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}.${pad(now.getMinutes())}.${pad(now.getSeconds())}`;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** Scale a photo into `maxWidth`/`maxHeight`, when it is larger and the manipulator is installed. */
|
|
517
|
+
async function prepareImage(file, options, quality) {
|
|
518
|
+
if (file.kind !== 'image' || !options || !file.width || !file.height) return file;
|
|
519
|
+
const target = fitWithin({
|
|
520
|
+
width: file.width,
|
|
521
|
+
height: file.height
|
|
522
|
+
}, options);
|
|
523
|
+
if (!target) return file;
|
|
524
|
+
const manipulator = loadManipulator()?.ImageManipulator;
|
|
525
|
+
if (!manipulator) {
|
|
526
|
+
if (__DEV__) {
|
|
527
|
+
console.warn('FileUpload: resizing photos needs expo-image-manipulator. Install it with: npx expo install expo-image-manipulator');
|
|
528
|
+
}
|
|
529
|
+
return file;
|
|
530
|
+
}
|
|
531
|
+
const rendered = await manipulator.manipulate(file.uri).resize({
|
|
532
|
+
width: target.width
|
|
533
|
+
}).renderAsync();
|
|
534
|
+
const saved = await rendered.saveAsync({
|
|
535
|
+
format: 'jpeg',
|
|
536
|
+
compress: quality
|
|
537
|
+
});
|
|
538
|
+
const name = file.name.replace(/\.[^.]+$/, '') + '.jpg';
|
|
539
|
+
// The resized file's size is not reported; it is smaller than the original.
|
|
540
|
+
return {
|
|
541
|
+
...file,
|
|
542
|
+
uri: saved.uri,
|
|
543
|
+
width: saved.width,
|
|
544
|
+
height: saved.height,
|
|
545
|
+
name,
|
|
546
|
+
size: undefined,
|
|
547
|
+
mimeType: 'image/jpeg'
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// ---------------------------------------------------------------------------
|
|
552
|
+
// Source chooser
|
|
553
|
+
// ---------------------------------------------------------------------------
|
|
554
|
+
|
|
555
|
+
const SOURCE_ICONS = {
|
|
556
|
+
camera: color => /*#__PURE__*/_jsx(CameraIcon, {
|
|
557
|
+
size: 18,
|
|
558
|
+
color: color
|
|
559
|
+
}),
|
|
560
|
+
library: color => /*#__PURE__*/_jsx(ImageIcon, {
|
|
561
|
+
size: 18,
|
|
562
|
+
color: color
|
|
563
|
+
}),
|
|
564
|
+
files: color => /*#__PURE__*/_jsx(FolderIcon, {
|
|
565
|
+
size: 18,
|
|
566
|
+
color: color
|
|
567
|
+
})
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Wraps one pressable. With one source the press opens its picker; with more,
|
|
572
|
+
* a sheet of the sources opens first, and the picker waits for it to leave so
|
|
573
|
+
* the two never animate over each other.
|
|
574
|
+
*/
|
|
575
|
+
function SourceChooser({
|
|
576
|
+
children
|
|
577
|
+
}) {
|
|
578
|
+
const {
|
|
579
|
+
sources,
|
|
580
|
+
sourceLabels,
|
|
581
|
+
pick,
|
|
582
|
+
canAddMore
|
|
583
|
+
} = useFileUpload();
|
|
584
|
+
const iconColor = useCSSVariable('--color-foreground');
|
|
585
|
+
const color = typeof iconColor === 'string' ? iconColor : undefined;
|
|
586
|
+
if (sources.length <= 1) {
|
|
587
|
+
const only = sources[0];
|
|
588
|
+
return /*#__PURE__*/cloneElement(children, {
|
|
589
|
+
onPress: (...args) => {
|
|
590
|
+
children.props.onPress?.(...args);
|
|
591
|
+
if (only && canAddMore) void pick(only);
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
if (!canAddMore) return children;
|
|
596
|
+
const choose = source => {
|
|
597
|
+
setTimeout(() => void pick(source), CHOOSER_EXIT_MS);
|
|
598
|
+
};
|
|
599
|
+
return /*#__PURE__*/_jsxs(Menu, {
|
|
600
|
+
presentation: "bottom-sheet",
|
|
601
|
+
children: [/*#__PURE__*/_jsx(Menu.Trigger, {
|
|
602
|
+
children: children
|
|
603
|
+
}), /*#__PURE__*/_jsx(Menu.Content, {
|
|
604
|
+
children: sources.map(source => /*#__PURE__*/_jsx(Menu.Item, {
|
|
605
|
+
icon: SOURCE_ICONS[source](color),
|
|
606
|
+
onSelect: () => choose(source),
|
|
607
|
+
children: sourceLabels[source]
|
|
608
|
+
}, source))
|
|
609
|
+
})]
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// ---------------------------------------------------------------------------
|
|
614
|
+
// Trigger
|
|
615
|
+
// ---------------------------------------------------------------------------
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Turns any pressable into the way files are added. With several sources it
|
|
619
|
+
* opens a sheet listing them; with one it opens that picker directly.
|
|
620
|
+
*/
|
|
621
|
+
function FileUploadTrigger({
|
|
622
|
+
children
|
|
623
|
+
}) {
|
|
624
|
+
const {
|
|
625
|
+
disabled,
|
|
626
|
+
canAddMore
|
|
627
|
+
} = useFileUpload();
|
|
628
|
+
const child = Children.only(children);
|
|
629
|
+
const locked = disabled || !canAddMore;
|
|
630
|
+
return /*#__PURE__*/_jsx(SourceChooser, {
|
|
631
|
+
children: /*#__PURE__*/isValidElement(child) && locked ? /*#__PURE__*/cloneElement(child, {
|
|
632
|
+
disabled: true
|
|
633
|
+
}) : child
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// ---------------------------------------------------------------------------
|
|
638
|
+
// Dropzone
|
|
639
|
+
// ---------------------------------------------------------------------------
|
|
640
|
+
|
|
641
|
+
const dropzoneVariants = tv({
|
|
642
|
+
slots: {
|
|
643
|
+
root: 'items-center justify-center rounded-2xl border-2 border-dashed border-border bg-muted/40 px-6 py-7 active:bg-muted',
|
|
644
|
+
// The pressable animates its own opacity for press feedback, which would
|
|
645
|
+
// overwrite a dimming class on it — so the contents are what dim.
|
|
646
|
+
body: 'items-center gap-3',
|
|
647
|
+
badge: 'h-12 w-12 items-center justify-center rounded-full border border-border bg-background',
|
|
648
|
+
title: 'text-center text-base font-semibold text-foreground',
|
|
649
|
+
hint: 'text-center text-sm text-muted-foreground',
|
|
650
|
+
error: 'text-center text-sm text-destructive'
|
|
651
|
+
},
|
|
652
|
+
variants: {
|
|
653
|
+
invalid: {
|
|
654
|
+
true: {
|
|
655
|
+
root: 'border-destructive bg-destructive-subtle'
|
|
656
|
+
}
|
|
657
|
+
},
|
|
658
|
+
disabled: {
|
|
659
|
+
true: {
|
|
660
|
+
body: 'opacity-50'
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
/**
|
|
666
|
+
* The large dashed area at the top of a form. Pressing anywhere on it adds
|
|
667
|
+
* files, and the line under the title says what is allowed before anybody
|
|
668
|
+
* picks something that is not. When a pick is turned away, the reason replaces
|
|
669
|
+
* that line until the next pick.
|
|
670
|
+
*/
|
|
671
|
+
const FileUploadDropzone = /*#__PURE__*/forwardRef(({
|
|
672
|
+
className,
|
|
673
|
+
title,
|
|
674
|
+
description,
|
|
675
|
+
icon,
|
|
676
|
+
children,
|
|
677
|
+
...props
|
|
678
|
+
}, ref) => {
|
|
679
|
+
const {
|
|
680
|
+
accept,
|
|
681
|
+
maxSize,
|
|
682
|
+
maxFiles,
|
|
683
|
+
multiple,
|
|
684
|
+
disabled,
|
|
685
|
+
invalid,
|
|
686
|
+
canAddMore,
|
|
687
|
+
rejections,
|
|
688
|
+
files
|
|
689
|
+
} = useFileUpload();
|
|
690
|
+
const labelledBy = useFieldLabelledBy();
|
|
691
|
+
const primary = useCSSVariable('--color-foreground');
|
|
692
|
+
const full = multiple && maxFiles !== undefined && files.length >= maxFiles;
|
|
693
|
+
const first = rejections[0];
|
|
694
|
+
const rejected = first ? rejections.length > 1 ? `${first.message} ${rejections.length - 1} more could not be added.` : first.message : null;
|
|
695
|
+
const slots = dropzoneVariants({
|
|
696
|
+
invalid: invalid || rejected !== null,
|
|
697
|
+
disabled: disabled || full
|
|
698
|
+
});
|
|
699
|
+
const heading = title ?? (multiple ? 'Choose files' : 'Choose a file');
|
|
700
|
+
const hint = full ? `${files.length} of ${maxFiles} added. Remove one to add another.` : description ?? describeRules({
|
|
701
|
+
accept,
|
|
702
|
+
maxSize,
|
|
703
|
+
maxFiles: multiple ? maxFiles : undefined
|
|
704
|
+
});
|
|
705
|
+
return /*#__PURE__*/_jsx(SourceChooser, {
|
|
706
|
+
children: /*#__PURE__*/_jsx(AnimatedPressable, {
|
|
707
|
+
ref: ref,
|
|
708
|
+
accessibilityRole: "button",
|
|
709
|
+
accessibilityLabel: [typeof heading === 'string' ? heading : null, typeof hint === 'string' ? hint : null].filter(Boolean).join('. '),
|
|
710
|
+
accessibilityState: {
|
|
711
|
+
disabled: disabled || !canAddMore
|
|
712
|
+
},
|
|
713
|
+
accessibilityLabelledBy: labelledBy,
|
|
714
|
+
disabled: disabled || !canAddMore,
|
|
715
|
+
className: slots.root({
|
|
716
|
+
className
|
|
717
|
+
}),
|
|
718
|
+
...props,
|
|
719
|
+
children: /*#__PURE__*/_jsx(View, {
|
|
720
|
+
className: slots.body(),
|
|
721
|
+
children: children ?? /*#__PURE__*/_jsxs(_Fragment, {
|
|
722
|
+
children: [/*#__PURE__*/_jsx(View, {
|
|
723
|
+
className: slots.badge(),
|
|
724
|
+
children: icon ?? /*#__PURE__*/_jsx(UploadIcon, {
|
|
725
|
+
size: 22,
|
|
726
|
+
color: typeof primary === 'string' ? primary : undefined
|
|
727
|
+
})
|
|
728
|
+
}), /*#__PURE__*/_jsxs(View, {
|
|
729
|
+
className: "gap-1",
|
|
730
|
+
children: [typeof heading === 'string' ? /*#__PURE__*/_jsx(Text, {
|
|
731
|
+
className: slots.title(),
|
|
732
|
+
children: heading
|
|
733
|
+
}) : heading, hint ? typeof hint === 'string' ? /*#__PURE__*/_jsx(Text, {
|
|
734
|
+
className: slots.hint(),
|
|
735
|
+
children: hint
|
|
736
|
+
}) : hint : null, rejected ? /*#__PURE__*/_jsx(Text, {
|
|
737
|
+
className: slots.error(),
|
|
738
|
+
accessibilityLiveRegion: "polite",
|
|
739
|
+
children: rejected
|
|
740
|
+
}) : null]
|
|
741
|
+
})]
|
|
742
|
+
})
|
|
743
|
+
})
|
|
744
|
+
})
|
|
745
|
+
});
|
|
746
|
+
});
|
|
747
|
+
FileUploadDropzone.displayName = 'FileUpload.Dropzone';
|
|
748
|
+
|
|
749
|
+
// ---------------------------------------------------------------------------
|
|
750
|
+
// Item and List
|
|
751
|
+
// ---------------------------------------------------------------------------
|
|
752
|
+
|
|
753
|
+
const ATTACHMENT_STATE = {
|
|
754
|
+
idle: 'idle',
|
|
755
|
+
queued: 'idle',
|
|
756
|
+
uploading: 'uploading',
|
|
757
|
+
processing: 'processing',
|
|
758
|
+
done: 'done',
|
|
759
|
+
error: 'error',
|
|
760
|
+
canceled: 'idle'
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
/** "PDF · 2.4 MB", "1.1 of 2.4 MB · 46%", "Waiting", "Upload failed". */
|
|
764
|
+
export function describeFileStatus(file) {
|
|
765
|
+
const ext = extensionOf(file.name).toUpperCase();
|
|
766
|
+
const size = file.size !== undefined ? formatFileSize(file.size) : '';
|
|
767
|
+
const percent = `${Math.round((file.progress ?? 0) * 100)}%`;
|
|
768
|
+
switch (file.status) {
|
|
769
|
+
case 'queued':
|
|
770
|
+
return ['Waiting', size].filter(Boolean).join(' · ');
|
|
771
|
+
case 'uploading':
|
|
772
|
+
return file.size !== undefined ? `${formatFileSize(file.size * (file.progress ?? 0))} of ${size} · ${percent}` : `Uploading · ${percent}`;
|
|
773
|
+
case 'processing':
|
|
774
|
+
return 'Processing…';
|
|
775
|
+
case 'done':
|
|
776
|
+
return [size, 'Uploaded'].filter(Boolean).join(' · ');
|
|
777
|
+
case 'error':
|
|
778
|
+
return file.error ?? 'Upload failed';
|
|
779
|
+
case 'canceled':
|
|
780
|
+
return ['Canceled', size].filter(Boolean).join(' · ');
|
|
781
|
+
default:
|
|
782
|
+
return [ext, size].filter(Boolean).join(' · ');
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
function KindIcon({
|
|
786
|
+
kind,
|
|
787
|
+
size = 18,
|
|
788
|
+
color
|
|
789
|
+
}) {
|
|
790
|
+
if (kind === 'image') return /*#__PURE__*/_jsx(ImageIcon, {
|
|
791
|
+
size: size,
|
|
792
|
+
color: color
|
|
793
|
+
});
|
|
794
|
+
if (kind === 'video') return /*#__PURE__*/_jsx(VideoIcon, {
|
|
795
|
+
size: size,
|
|
796
|
+
color: color
|
|
797
|
+
});
|
|
798
|
+
return /*#__PURE__*/_jsx(FileIcon, {
|
|
799
|
+
size: size,
|
|
800
|
+
color: color
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* One file as an Attachment row: a thumbnail or type icon, the name, a status
|
|
805
|
+
* line, and the action that fits the state — cancel while it is moving, retry
|
|
806
|
+
* when it failed or was stopped, remove otherwise.
|
|
807
|
+
*/
|
|
808
|
+
function FileUploadItem({
|
|
809
|
+
file,
|
|
810
|
+
className,
|
|
811
|
+
size = 'default',
|
|
812
|
+
nameLength = 36
|
|
813
|
+
}) {
|
|
814
|
+
const {
|
|
815
|
+
remove,
|
|
816
|
+
cancel,
|
|
817
|
+
retry,
|
|
818
|
+
disabled,
|
|
819
|
+
hasUpload
|
|
820
|
+
} = useFileUpload();
|
|
821
|
+
const muted = useCSSVariable('--color-muted-foreground');
|
|
822
|
+
const success = useCSSVariable('--color-success');
|
|
823
|
+
const mutedColor = typeof muted === 'string' ? muted : undefined;
|
|
824
|
+
const moving = isActive(file.status) || file.status === 'queued';
|
|
825
|
+
const failed = file.status === 'error' || file.status === 'canceled';
|
|
826
|
+
const status = describeFileStatus(file);
|
|
827
|
+
return /*#__PURE__*/_jsxs(Attachment, {
|
|
828
|
+
state: ATTACHMENT_STATE[file.status],
|
|
829
|
+
progress: file.status === 'uploading' ? file.progress ?? 0 : undefined,
|
|
830
|
+
size: size,
|
|
831
|
+
disabled: disabled,
|
|
832
|
+
accessibilityLabel: `${file.name}, ${status}`,
|
|
833
|
+
className: className,
|
|
834
|
+
children: [file.kind === 'image' ? /*#__PURE__*/_jsx(Attachment.Media, {
|
|
835
|
+
variant: "image",
|
|
836
|
+
children: /*#__PURE__*/_jsx(Image, {
|
|
837
|
+
source: {
|
|
838
|
+
uri: file.uri
|
|
839
|
+
},
|
|
840
|
+
className: "h-full w-full",
|
|
841
|
+
resizeMode: "cover"
|
|
842
|
+
})
|
|
843
|
+
}) : /*#__PURE__*/_jsx(Attachment.Media, {
|
|
844
|
+
variant: "icon",
|
|
845
|
+
children: /*#__PURE__*/_jsx(KindIcon, {
|
|
846
|
+
kind: file.kind,
|
|
847
|
+
color: mutedColor,
|
|
848
|
+
size: size === 'sm' ? 16 : 18
|
|
849
|
+
})
|
|
850
|
+
}), /*#__PURE__*/_jsxs(Attachment.Content, {
|
|
851
|
+
children: [/*#__PURE__*/_jsx(Attachment.Title, {
|
|
852
|
+
numberOfLines: 1,
|
|
853
|
+
children: truncateMiddle(file.name, nameLength)
|
|
854
|
+
}), /*#__PURE__*/_jsx(Attachment.Description, {
|
|
855
|
+
numberOfLines: file.status === 'error' ? 2 : 1,
|
|
856
|
+
children: status
|
|
857
|
+
})]
|
|
858
|
+
}), /*#__PURE__*/_jsxs(Attachment.Actions, {
|
|
859
|
+
children: [file.status === 'done' ? /*#__PURE__*/_jsx(View, {
|
|
860
|
+
accessibilityElementsHidden: true,
|
|
861
|
+
importantForAccessibility: "no-hide-descendants",
|
|
862
|
+
children: /*#__PURE__*/_jsx(CheckCircleIcon, {
|
|
863
|
+
size: 18,
|
|
864
|
+
color: typeof success === 'string' ? success : undefined
|
|
865
|
+
})
|
|
866
|
+
}) : null, failed && hasUpload ? /*#__PURE__*/_jsx(Attachment.Action, {
|
|
867
|
+
accessibilityLabel: `Retry ${file.name}`,
|
|
868
|
+
disabled: disabled,
|
|
869
|
+
onPress: () => retry(file.id),
|
|
870
|
+
children: /*#__PURE__*/_jsx(RotateCcwIcon, {
|
|
871
|
+
size: 16,
|
|
872
|
+
color: mutedColor
|
|
873
|
+
})
|
|
874
|
+
}) : null, /*#__PURE__*/_jsx(Attachment.Action, {
|
|
875
|
+
accessibilityLabel: moving ? `Cancel upload of ${file.name}` : `Remove ${file.name}`,
|
|
876
|
+
disabled: disabled,
|
|
877
|
+
onPress: () => moving ? cancel(file.id) : remove(file.id),
|
|
878
|
+
children: moving ? /*#__PURE__*/_jsx(XIcon, {
|
|
879
|
+
size: 16,
|
|
880
|
+
color: mutedColor
|
|
881
|
+
}) : /*#__PURE__*/_jsx(TrashIcon, {
|
|
882
|
+
size: 16,
|
|
883
|
+
color: mutedColor
|
|
884
|
+
})
|
|
885
|
+
})]
|
|
886
|
+
})]
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
/** Every file as a row, in the order it was added. */
|
|
890
|
+
const FileUploadList = /*#__PURE__*/forwardRef(({
|
|
891
|
+
className,
|
|
892
|
+
horizontal = false,
|
|
893
|
+
size,
|
|
894
|
+
renderItem,
|
|
895
|
+
empty,
|
|
896
|
+
...props
|
|
897
|
+
}, ref) => {
|
|
898
|
+
const {
|
|
899
|
+
files
|
|
900
|
+
} = useFileUpload();
|
|
901
|
+
if (files.length === 0) return empty ? /*#__PURE__*/_jsx(_Fragment, {
|
|
902
|
+
children: empty
|
|
903
|
+
}) : null;
|
|
904
|
+
const rows = files.map(file => renderItem ? /*#__PURE__*/_jsx(View, {
|
|
905
|
+
children: renderItem(file)
|
|
906
|
+
}, file.id) : /*#__PURE__*/_jsx(FileUploadItem, {
|
|
907
|
+
file: file,
|
|
908
|
+
size: size,
|
|
909
|
+
className: horizontal ? 'w-72' : undefined,
|
|
910
|
+
nameLength: horizontal ? 16 : undefined
|
|
911
|
+
}, file.id));
|
|
912
|
+
if (horizontal) {
|
|
913
|
+
return /*#__PURE__*/_jsx(View, {
|
|
914
|
+
ref: ref,
|
|
915
|
+
className: className,
|
|
916
|
+
...props,
|
|
917
|
+
children: /*#__PURE__*/_jsx(ScrollView, {
|
|
918
|
+
horizontal: true,
|
|
919
|
+
showsHorizontalScrollIndicator: false,
|
|
920
|
+
contentContainerClassName: "gap-2",
|
|
921
|
+
children: rows
|
|
922
|
+
})
|
|
923
|
+
});
|
|
924
|
+
}
|
|
925
|
+
return /*#__PURE__*/_jsx(View, {
|
|
926
|
+
ref: ref,
|
|
927
|
+
accessibilityRole: "list",
|
|
928
|
+
className: cn('gap-2', className),
|
|
929
|
+
...props,
|
|
930
|
+
children: rows
|
|
931
|
+
});
|
|
932
|
+
});
|
|
933
|
+
FileUploadList.displayName = 'FileUpload.List';
|
|
934
|
+
|
|
935
|
+
// ---------------------------------------------------------------------------
|
|
936
|
+
// Grid
|
|
937
|
+
// ---------------------------------------------------------------------------
|
|
938
|
+
|
|
939
|
+
const AnimatedCircle = Animated.createAnimatedComponent(Circle);
|
|
940
|
+
|
|
941
|
+
/** A ring that fills with upload progress, drawn over a tile's scrim. */
|
|
942
|
+
function ProgressRing({
|
|
943
|
+
progress,
|
|
944
|
+
size = 36
|
|
945
|
+
}) {
|
|
946
|
+
const stroke = 3;
|
|
947
|
+
const radius = (size - stroke) / 2;
|
|
948
|
+
const circumference = 2 * Math.PI * radius;
|
|
949
|
+
const shown = useSharedValue(progress);
|
|
950
|
+
useEffect(() => {
|
|
951
|
+
shown.value = withTiming(progress, {
|
|
952
|
+
duration: 180
|
|
953
|
+
});
|
|
954
|
+
}, [progress, shown]);
|
|
955
|
+
const animatedProps = useAnimatedProps(() => ({
|
|
956
|
+
strokeDashoffset: circumference * (1 - shown.value)
|
|
957
|
+
}));
|
|
958
|
+
return /*#__PURE__*/_jsxs(Svg, {
|
|
959
|
+
width: size,
|
|
960
|
+
height: size,
|
|
961
|
+
style: {
|
|
962
|
+
transform: [{
|
|
963
|
+
rotate: '-90deg'
|
|
964
|
+
}]
|
|
965
|
+
},
|
|
966
|
+
children: [/*#__PURE__*/_jsx(Circle, {
|
|
967
|
+
cx: size / 2,
|
|
968
|
+
cy: size / 2,
|
|
969
|
+
r: radius,
|
|
970
|
+
stroke: "rgba(255,255,255,0.3)",
|
|
971
|
+
strokeWidth: stroke,
|
|
972
|
+
fill: "none"
|
|
973
|
+
}), /*#__PURE__*/_jsx(AnimatedCircle, {
|
|
974
|
+
cx: size / 2,
|
|
975
|
+
cy: size / 2,
|
|
976
|
+
r: radius,
|
|
977
|
+
stroke: "#fff",
|
|
978
|
+
strokeWidth: stroke,
|
|
979
|
+
strokeLinecap: "round",
|
|
980
|
+
strokeDasharray: `${circumference} ${circumference}`,
|
|
981
|
+
animatedProps: animatedProps,
|
|
982
|
+
fill: "none"
|
|
983
|
+
})]
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
function GridTile({
|
|
987
|
+
file,
|
|
988
|
+
size,
|
|
989
|
+
cover,
|
|
990
|
+
radius
|
|
991
|
+
}) {
|
|
992
|
+
const {
|
|
993
|
+
remove,
|
|
994
|
+
cancel,
|
|
995
|
+
retry,
|
|
996
|
+
disabled,
|
|
997
|
+
hasUpload
|
|
998
|
+
} = useFileUpload();
|
|
999
|
+
const muted = useCSSVariable('--color-muted-foreground');
|
|
1000
|
+
const foreground = useCSSVariable('--color-foreground');
|
|
1001
|
+
const moving = isActive(file.status) || file.status === 'queued';
|
|
1002
|
+
const failed = file.status === 'error' || file.status === 'canceled';
|
|
1003
|
+
const status = describeFileStatus(file);
|
|
1004
|
+
return /*#__PURE__*/_jsxs(View, {
|
|
1005
|
+
style: {
|
|
1006
|
+
width: size,
|
|
1007
|
+
height: size
|
|
1008
|
+
},
|
|
1009
|
+
className: cn(disabled && 'opacity-50'),
|
|
1010
|
+
accessibilityLabel: `${file.name}, ${status}`,
|
|
1011
|
+
children: [file.kind === 'image' ? /*#__PURE__*/_jsx(View, {
|
|
1012
|
+
style: {
|
|
1013
|
+
borderRadius: radius
|
|
1014
|
+
},
|
|
1015
|
+
className: "h-full w-full overflow-hidden bg-muted",
|
|
1016
|
+
children: /*#__PURE__*/_jsx(ImageViewer.Trigger, {
|
|
1017
|
+
source: {
|
|
1018
|
+
uri: file.uri
|
|
1019
|
+
},
|
|
1020
|
+
width: file.width,
|
|
1021
|
+
height: file.height,
|
|
1022
|
+
alt: file.name,
|
|
1023
|
+
radius: radius,
|
|
1024
|
+
disabled: moving,
|
|
1025
|
+
className: "h-full w-full"
|
|
1026
|
+
})
|
|
1027
|
+
}) : /*#__PURE__*/_jsxs(View, {
|
|
1028
|
+
style: {
|
|
1029
|
+
borderRadius: radius
|
|
1030
|
+
},
|
|
1031
|
+
className: "h-full w-full items-center justify-center gap-1.5 border border-border bg-muted px-2",
|
|
1032
|
+
children: [/*#__PURE__*/_jsx(KindIcon, {
|
|
1033
|
+
kind: file.kind,
|
|
1034
|
+
size: 22,
|
|
1035
|
+
color: typeof muted === 'string' ? muted : undefined
|
|
1036
|
+
}), /*#__PURE__*/_jsx(Text, {
|
|
1037
|
+
numberOfLines: 1,
|
|
1038
|
+
className: "text-xs text-muted-foreground",
|
|
1039
|
+
children: file.name
|
|
1040
|
+
})]
|
|
1041
|
+
}), moving || failed ? /*#__PURE__*/_jsx(View, {
|
|
1042
|
+
pointerEvents: "box-none",
|
|
1043
|
+
style: {
|
|
1044
|
+
borderRadius: radius
|
|
1045
|
+
},
|
|
1046
|
+
className: "absolute inset-0 items-center justify-center bg-black/45",
|
|
1047
|
+
children: file.status === 'uploading' ? /*#__PURE__*/_jsx(ProgressRing, {
|
|
1048
|
+
progress: file.progress ?? 0
|
|
1049
|
+
}) : file.status === 'queued' || file.status === 'processing' ? /*#__PURE__*/_jsx(Text, {
|
|
1050
|
+
className: "text-xs font-medium text-white",
|
|
1051
|
+
children: file.status === 'queued' ? 'Waiting' : 'Processing'
|
|
1052
|
+
}) : hasUpload ? /*#__PURE__*/_jsx(AnimatedPressable, {
|
|
1053
|
+
accessibilityRole: "button",
|
|
1054
|
+
accessibilityLabel: `Retry ${file.name}`,
|
|
1055
|
+
disabled: disabled,
|
|
1056
|
+
onPress: () => retry(file.id),
|
|
1057
|
+
className: "h-9 w-9 items-center justify-center rounded-full bg-white/20 active:bg-white/30",
|
|
1058
|
+
children: /*#__PURE__*/_jsx(RotateCcwIcon, {
|
|
1059
|
+
size: 18,
|
|
1060
|
+
color: "#fff"
|
|
1061
|
+
})
|
|
1062
|
+
}) : null
|
|
1063
|
+
}) : null, cover ? /*#__PURE__*/_jsx(View, {
|
|
1064
|
+
className: "absolute bottom-1.5 left-1.5 rounded-full bg-background/90 px-2 py-0.5",
|
|
1065
|
+
children: /*#__PURE__*/_jsx(Text, {
|
|
1066
|
+
className: "text-[11px] font-semibold text-foreground",
|
|
1067
|
+
children: "Cover"
|
|
1068
|
+
})
|
|
1069
|
+
}) : null, /*#__PURE__*/_jsx(AnimatedPressable, {
|
|
1070
|
+
accessibilityRole: "button",
|
|
1071
|
+
accessibilityLabel: moving ? `Cancel upload of ${file.name}` : `Remove ${file.name}`,
|
|
1072
|
+
disabled: disabled,
|
|
1073
|
+
hitSlop: 8,
|
|
1074
|
+
onPress: () => moving ? cancel(file.id) : remove(file.id),
|
|
1075
|
+
className: "absolute right-1.5 top-1.5 h-6 w-6 items-center justify-center rounded-full border border-border bg-background active:bg-muted",
|
|
1076
|
+
children: /*#__PURE__*/_jsx(XIcon, {
|
|
1077
|
+
size: 12,
|
|
1078
|
+
color: typeof foreground === 'string' ? foreground : undefined,
|
|
1079
|
+
strokeWidth: 2.5
|
|
1080
|
+
})
|
|
1081
|
+
})]
|
|
1082
|
+
});
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
* Photos as square tiles, led by a tile that adds more. Each photo opens in
|
|
1086
|
+
* the full-screen viewer, carries its own remove button, and shows a ring
|
|
1087
|
+
* while it uploads. The add tile counts against `maxFiles`, so the limit is
|
|
1088
|
+
* visible before anybody hits it.
|
|
1089
|
+
*/
|
|
1090
|
+
const FileUploadGrid = /*#__PURE__*/forwardRef(({
|
|
1091
|
+
className,
|
|
1092
|
+
columns = 3,
|
|
1093
|
+
horizontal = false,
|
|
1094
|
+
tileSize = 88,
|
|
1095
|
+
gap = 8,
|
|
1096
|
+
radius = 12,
|
|
1097
|
+
cover = false,
|
|
1098
|
+
placeholders = false,
|
|
1099
|
+
addLabel,
|
|
1100
|
+
hideAdd = false,
|
|
1101
|
+
onLayout,
|
|
1102
|
+
...props
|
|
1103
|
+
}, ref) => {
|
|
1104
|
+
const {
|
|
1105
|
+
files,
|
|
1106
|
+
maxFiles,
|
|
1107
|
+
multiple,
|
|
1108
|
+
canAddMore
|
|
1109
|
+
} = useFileUpload();
|
|
1110
|
+
const [width, setWidth] = useState(0);
|
|
1111
|
+
const background = useCSSVariable('--color-background');
|
|
1112
|
+
const muted = useCSSVariable('--color-muted-foreground');
|
|
1113
|
+
const size = horizontal ? tileSize : width > 0 ? Math.floor((width - gap * (columns - 1)) / columns) : 0;
|
|
1114
|
+
const showAdd = !hideAdd && canAddMore;
|
|
1115
|
+
const slotCount = placeholders && maxFiles !== undefined ? Math.max(0, maxFiles - files.length - (showAdd ? 1 : 0)) : 0;
|
|
1116
|
+
// Empty slots only finish the current row; a page of them is noise.
|
|
1117
|
+
const filled = files.length + (showAdd ? 1 : 0);
|
|
1118
|
+
const rowRemainder = filled % columns === 0 ? 0 : columns - filled % columns;
|
|
1119
|
+
const visibleSlots = horizontal ? slotCount : Math.min(slotCount, rowRemainder);
|
|
1120
|
+
const label = addLabel ?? (multiple ? 'Add photos' : 'Add photo');
|
|
1121
|
+
const tiles = [];
|
|
1122
|
+
if (showAdd && size > 0) {
|
|
1123
|
+
tiles.push(/*#__PURE__*/_jsx(SourceChooser, {
|
|
1124
|
+
children: /*#__PURE__*/_jsxs(AnimatedPressable, {
|
|
1125
|
+
accessibilityRole: "button",
|
|
1126
|
+
accessibilityLabel: maxFiles !== undefined ? `${label}, ${files.length} of ${maxFiles}` : label,
|
|
1127
|
+
style: {
|
|
1128
|
+
width: size,
|
|
1129
|
+
height: size,
|
|
1130
|
+
borderRadius: radius
|
|
1131
|
+
},
|
|
1132
|
+
className: "items-center justify-center gap-1 border-2 border-dashed border-border bg-muted/40 active:bg-muted",
|
|
1133
|
+
children: [/*#__PURE__*/_jsx(View, {
|
|
1134
|
+
className: "h-8 w-8 items-center justify-center rounded-full bg-foreground",
|
|
1135
|
+
children: /*#__PURE__*/_jsx(PlusIcon, {
|
|
1136
|
+
size: 16,
|
|
1137
|
+
strokeWidth: 2.5,
|
|
1138
|
+
color: typeof background === 'string' ? background : undefined
|
|
1139
|
+
})
|
|
1140
|
+
}), size >= 84 ? /*#__PURE__*/_jsx(Text, {
|
|
1141
|
+
className: "text-xs font-medium text-foreground",
|
|
1142
|
+
numberOfLines: 1,
|
|
1143
|
+
children: label
|
|
1144
|
+
}) : null, maxFiles !== undefined && multiple ? /*#__PURE__*/_jsxs(Text, {
|
|
1145
|
+
className: "text-[11px] text-muted-foreground",
|
|
1146
|
+
children: [files.length, "/", maxFiles]
|
|
1147
|
+
}) : null]
|
|
1148
|
+
})
|
|
1149
|
+
}, "add"));
|
|
1150
|
+
}
|
|
1151
|
+
if (size > 0) {
|
|
1152
|
+
files.forEach((file, index) => tiles.push(/*#__PURE__*/_jsx(GridTile, {
|
|
1153
|
+
file: file,
|
|
1154
|
+
size: size,
|
|
1155
|
+
cover: cover && index === 0,
|
|
1156
|
+
radius: radius
|
|
1157
|
+
}, file.id)));
|
|
1158
|
+
for (let i = 0; i < visibleSlots; i += 1) {
|
|
1159
|
+
tiles.push(/*#__PURE__*/_jsx(View, {
|
|
1160
|
+
accessibilityElementsHidden: true,
|
|
1161
|
+
importantForAccessibility: "no-hide-descendants",
|
|
1162
|
+
style: {
|
|
1163
|
+
width: size,
|
|
1164
|
+
height: size,
|
|
1165
|
+
borderRadius: radius
|
|
1166
|
+
},
|
|
1167
|
+
className: "items-center justify-center border border-dashed border-border",
|
|
1168
|
+
children: /*#__PURE__*/_jsx(ImageIcon, {
|
|
1169
|
+
size: 18,
|
|
1170
|
+
color: typeof muted === 'string' ? muted : undefined,
|
|
1171
|
+
opacity: 0.5
|
|
1172
|
+
})
|
|
1173
|
+
}, `slot-${i}`));
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
const body = horizontal ? /*#__PURE__*/_jsx(ScrollView, {
|
|
1177
|
+
horizontal: true,
|
|
1178
|
+
showsHorizontalScrollIndicator: false,
|
|
1179
|
+
contentContainerStyle: {
|
|
1180
|
+
gap
|
|
1181
|
+
},
|
|
1182
|
+
children: tiles
|
|
1183
|
+
}) : /*#__PURE__*/_jsx(View, {
|
|
1184
|
+
className: "flex-row flex-wrap",
|
|
1185
|
+
style: {
|
|
1186
|
+
gap
|
|
1187
|
+
},
|
|
1188
|
+
children: tiles
|
|
1189
|
+
});
|
|
1190
|
+
return /*#__PURE__*/_jsx(View, {
|
|
1191
|
+
ref: ref,
|
|
1192
|
+
className: className,
|
|
1193
|
+
onLayout: event => {
|
|
1194
|
+
setWidth(event.nativeEvent.layout.width);
|
|
1195
|
+
onLayout?.(event);
|
|
1196
|
+
},
|
|
1197
|
+
...props,
|
|
1198
|
+
children: /*#__PURE__*/_jsx(ImageViewer, {
|
|
1199
|
+
children: body
|
|
1200
|
+
})
|
|
1201
|
+
});
|
|
1202
|
+
});
|
|
1203
|
+
FileUploadGrid.displayName = 'FileUpload.Grid';
|
|
1204
|
+
FileUploadRoot.displayName = 'FileUpload';
|
|
1205
|
+
FileUploadTrigger.displayName = 'FileUpload.Trigger';
|
|
1206
|
+
FileUploadItem.displayName = 'FileUpload.Item';
|
|
1207
|
+
export const FileUpload = Object.assign(FileUploadRoot, {
|
|
1208
|
+
Trigger: FileUploadTrigger,
|
|
1209
|
+
Dropzone: FileUploadDropzone,
|
|
1210
|
+
List: FileUploadList,
|
|
1211
|
+
Item: FileUploadItem,
|
|
1212
|
+
Grid: FileUploadGrid
|
|
1213
|
+
});
|
|
1214
|
+
//# sourceMappingURL=index.js.map
|