@rafal.lemieszewski/tide-ui 0.89.0 → 0.90.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.
@@ -1,68 +1,71 @@
1
- import { jsx as s, jsxs as b, Fragment as Q } from "react/jsx-runtime";
2
- import * as i from "react";
3
- import { cn as f } from "../../lib/utils.js";
4
- import { cva as Y } from "class-variance-authority";
5
- import { Button as P } from "./button.js";
6
- import { Progress as Z } from "./progress.js";
7
- import { Icon as z } from "./icon.js";
8
- import { X as _ } from "lucide-react";
9
- import { Badge as ee } from "./badge.js";
10
- const M = i.createContext(
1
+ import { jsx as i, jsxs as F, Fragment as Z } from "react/jsx-runtime";
2
+ import * as d from "react";
3
+ import { cn as v } from "../../lib/utils.js";
4
+ import { cva as _ } from "class-variance-authority";
5
+ import { Button as z } from "./button.js";
6
+ import { Progress as ee } from "./progress.js";
7
+ import { Icon as M } from "./icon.js";
8
+ import { X as re } from "lucide-react";
9
+ import { Badge as te } from "./badge.js";
10
+ const j = d.createContext(
11
11
  null
12
- ), D = () => {
13
- const r = i.useContext(M);
12
+ ), I = () => {
13
+ const r = d.useContext(j);
14
14
  if (!r)
15
15
  throw new Error("useFileUpload must be used within a FileUpload component");
16
16
  return r;
17
17
  };
18
- function re(r) {
18
+ function oe(r) {
19
19
  return r instanceof File;
20
20
  }
21
- const j = /* @__PURE__ */ i.forwardRef(
21
+ const L = /* @__PURE__ */ d.forwardRef(
22
22
  ({
23
23
  className: r,
24
24
  files: e,
25
25
  onFilesChange: o,
26
26
  maxFiles: t = 10,
27
- maxSize: n = 10 * 1024 * 1024,
27
+ maxSize: a = 10 * 1024 * 1024,
28
28
  // 10MB
29
- accept: l,
30
- disabled: v = !1,
31
- multiple: p = !0,
29
+ accept: n,
30
+ disabled: p = !1,
31
+ multiple: m = !0,
32
32
  onUpload: c,
33
33
  onDelete: u,
34
+ uploadMode: h = "auto",
34
35
  children: w,
35
36
  ...N
36
- }, g) => {
37
- const F = i.useMemo(
37
+ }, U) => {
38
+ const b = d.useMemo(
38
39
  () => ({
39
40
  files: e,
40
41
  onFilesChange: o,
41
42
  maxFiles: t,
42
- maxSize: n,
43
- accept: l,
44
- disabled: v,
45
- multiple: p,
43
+ maxSize: a,
44
+ accept: n,
45
+ disabled: p,
46
+ multiple: m,
46
47
  onUpload: c,
47
- onDelete: u
48
+ onDelete: u,
49
+ uploadMode: h
48
50
  }),
49
51
  [
50
52
  e,
51
53
  o,
52
54
  t,
55
+ a,
53
56
  n,
54
- l,
55
- v,
56
57
  p,
58
+ m,
57
59
  c,
58
- u
60
+ u,
61
+ h
59
62
  ]
60
63
  );
61
- return /* @__PURE__ */ s(M.Provider, { value: F, children: /* @__PURE__ */ s("div", { ref: g, className: f("space-y-4", r), ...N, children: w }) });
64
+ return /* @__PURE__ */ i(j.Provider, { value: b, children: /* @__PURE__ */ i("div", { ref: U, className: v("space-y-4", r), ...N, children: w }) });
62
65
  }
63
66
  );
64
- j.displayName = "FileUploadRoot";
65
- const te = Y(
67
+ L.displayName = "FileUploadRoot";
68
+ const ae = _(
66
69
  "relative flex flex-col items-center justify-center rounded-[var(--border-radius-l)] border-2 border-dashed p-6 text-center transition-colors duration-200",
67
70
  {
68
71
  variants: {
@@ -80,92 +83,97 @@ const te = Y(
80
83
  disabled: !1
81
84
  }
82
85
  }
83
- ), k = /* @__PURE__ */ i.forwardRef(({ className: r, children: e, ...o }, t) => {
86
+ ), A = /* @__PURE__ */ d.forwardRef(({ className: r, children: e, ...o }, t) => {
84
87
  const {
85
- files: n,
86
- onFilesChange: l,
87
- maxFiles: v,
88
- maxSize: p,
88
+ files: a,
89
+ onFilesChange: n,
90
+ maxFiles: p,
91
+ maxSize: m,
89
92
  accept: c,
90
93
  disabled: u,
91
- multiple: w
92
- } = D(), [N, g] = i.useState(!1), F = i.useRef(null), O = (a) => {
93
- if (p && a.size > p)
94
- return `File size must be less than ${E(p)}`;
94
+ multiple: h,
95
+ onUpload: w,
96
+ uploadMode: N
97
+ } = I(), [U, b] = d.useState(!1), R = d.useRef(null), G = (s) => {
98
+ if (m && s.size > m)
99
+ return `File size must be less than ${S(m)}`;
95
100
  if (c && c.length > 0) {
96
- const d = a.type, U = `.${a.name.split(".").pop()?.toLowerCase()}`;
97
- if (!c.some((m) => {
98
- if (m.startsWith("."))
99
- return m.toLowerCase() === U;
100
- if (m.includes("*")) {
101
- const x = m.split("/")[0];
102
- return d.startsWith(x);
101
+ const l = s.type, y = `.${s.name.split(".").pop()?.toLowerCase()}`;
102
+ if (!c.some((f) => {
103
+ if (f.startsWith("."))
104
+ return f.toLowerCase() === y;
105
+ if (f.includes("*")) {
106
+ const x = f.split("/")[0];
107
+ return l.startsWith(x);
103
108
  }
104
- return d === m;
109
+ return l === f;
105
110
  }))
106
111
  return `File type not supported. Accepted types: ${c.join(", ")}`;
107
112
  }
108
113
  return null;
109
- }, I = (a) => {
110
- const d = [], U = n.length;
111
- Array.from(a).forEach((h, m) => {
112
- if (v && U + d.length >= v) return;
113
- const x = O(h), R = {
114
- id: `${Date.now()}-${m}`,
115
- file: h,
114
+ }, C = async (s) => {
115
+ const l = [], y = a.length;
116
+ if (Array.from(s).forEach((g, f) => {
117
+ if (p && y + l.length >= p) return;
118
+ const x = G(g), k = {
119
+ id: `${Date.now()}-${f}`,
120
+ file: g,
116
121
  status: x ? "error" : "pending",
117
122
  error: x || void 0,
118
123
  progress: 0
119
124
  };
120
- if (h.type.startsWith("image/")) {
121
- const C = new FileReader();
122
- C.onload = (H) => {
123
- const J = n.map(
124
- (y) => y.id === R.id ? { ...y, preview: H.target?.result } : y
125
+ if (g.type.startsWith("image/")) {
126
+ const P = new FileReader();
127
+ P.onload = (Q) => {
128
+ const Y = a.map(
129
+ (D) => D.id === k.id ? { ...D, preview: Q.target?.result } : D
125
130
  );
126
- l(J);
127
- }, C.readAsDataURL(h);
131
+ n(Y);
132
+ }, P.readAsDataURL(g);
128
133
  }
129
- d.push(R);
130
- }), d.length > 0 && l([...n, ...d]);
131
- }, S = (a) => {
132
- a.preventDefault(), a.stopPropagation(), u || g(!0);
133
- }, G = (a) => {
134
- a.preventDefault(), a.stopPropagation(), g(!1);
135
- }, K = (a) => {
136
- if (a.preventDefault(), a.stopPropagation(), g(!1), u) return;
137
- const d = a.dataTransfer.files;
138
- d.length > 0 && I(d);
139
- }, X = () => {
140
- u || F.current?.click();
141
- }, q = (a) => {
142
- const d = a.target.files;
143
- d && I(d), a.target.value = "";
134
+ l.push(k);
135
+ }), l.length > 0 && (n([...a, ...l]), N === "auto" && w)) {
136
+ const g = l.filter((f) => f.status !== "error");
137
+ g.length > 0 && await w(g);
138
+ }
139
+ }, K = (s) => {
140
+ s.preventDefault(), s.stopPropagation(), u || b(!0);
141
+ }, X = (s) => {
142
+ s.preventDefault(), s.stopPropagation(), b(!1);
143
+ }, q = (s) => {
144
+ if (s.preventDefault(), s.stopPropagation(), b(!1), u) return;
145
+ const l = s.dataTransfer.files;
146
+ l.length > 0 && C(l);
147
+ }, H = () => {
148
+ u || R.current?.click();
149
+ }, J = (s) => {
150
+ const l = s.target.files;
151
+ l && C(l), s.target.value = "";
144
152
  };
145
- return /* @__PURE__ */ b(
153
+ return /* @__PURE__ */ F(
146
154
  "div",
147
155
  {
148
156
  ref: t,
149
- className: f(te({ isDragActive: N, disabled: u }), r),
150
- onDragOver: S,
151
- onDragLeave: G,
152
- onDrop: K,
153
- onClick: X,
157
+ className: v(ae({ isDragActive: U, disabled: u }), r),
158
+ onDragOver: K,
159
+ onDragLeave: X,
160
+ onDrop: q,
161
+ onClick: H,
154
162
  role: "button",
155
163
  tabIndex: u ? -1 : 0,
156
164
  "aria-label": "Upload files",
157
165
  ...o,
158
166
  children: [
159
- /* @__PURE__ */ s(
167
+ /* @__PURE__ */ i(
160
168
  "input",
161
169
  {
162
- ref: F,
170
+ ref: R,
163
171
  type: "file",
164
- multiple: w,
172
+ multiple: h,
165
173
  accept: c?.join(","),
166
174
  disabled: u,
167
175
  className: "hidden",
168
- onChange: q
176
+ onChange: J
169
177
  }
170
178
  ),
171
179
  e
@@ -173,22 +181,27 @@ const te = Y(
173
181
  }
174
182
  );
175
183
  });
176
- k.displayName = "FileUploadDropzone";
177
- const L = /* @__PURE__ */ i.forwardRef(({ children: r, ...e }, o) => {
178
- const { disabled: t } = D();
179
- return /* @__PURE__ */ s(P, { ref: o, disabled: t, ...e, children: r });
184
+ A.displayName = "FileUploadDropzone";
185
+ const B = /* @__PURE__ */ d.forwardRef(({ children: r, onClick: e, ...o }, t) => {
186
+ const { disabled: a, files: n, onUpload: p } = I();
187
+ return /* @__PURE__ */ i(z, { ref: t, disabled: a, onClick: async (c) => {
188
+ if (e?.(c), p) {
189
+ const u = n.filter((h) => h.status === "pending");
190
+ u.length > 0 && await p(u);
191
+ }
192
+ }, ...o, children: r });
180
193
  });
181
- L.displayName = "FileUploadTrigger";
182
- const A = /* @__PURE__ */ i.forwardRef(
183
- ({ className: r, children: e, ...o }, t) => /* @__PURE__ */ s("div", { ref: t, className: f("space-y-2", r), ...o, children: e })
194
+ B.displayName = "FileUploadTrigger";
195
+ const W = /* @__PURE__ */ d.forwardRef(
196
+ ({ className: r, children: e, ...o }, t) => /* @__PURE__ */ i("div", { ref: t, className: v("space-y-2", r), ...o, children: e })
184
197
  );
185
- A.displayName = "FileUploadList";
186
- const B = /* @__PURE__ */ i.forwardRef(
187
- ({ className: r, file: e, children: o, ...t }, n) => /* @__PURE__ */ s(
198
+ W.displayName = "FileUploadList";
199
+ const $ = /* @__PURE__ */ d.forwardRef(
200
+ ({ className: r, file: e, children: o, ...t }, a) => /* @__PURE__ */ i(
188
201
  "div",
189
202
  {
190
- ref: n,
191
- className: f(
203
+ ref: a,
204
+ className: v(
192
205
  "flex items-center gap-3 rounded-[var(--border-radius-l)] border p-3",
193
206
  "border-[var(--color-border-primary)]",
194
207
  e.status === "error" && "border-[var(--color-border-error-bold)] bg-[var(--color-background-error-subtle)]",
@@ -200,104 +213,104 @@ const B = /* @__PURE__ */ i.forwardRef(
200
213
  }
201
214
  )
202
215
  );
203
- B.displayName = "FileUploadItem";
204
- const W = /* @__PURE__ */ i.forwardRef(({ className: r, file: e, ...o }, t) => {
205
- const n = (l) => l.startsWith("image/") ? "image" : l.startsWith("video/") ? "video" : l.startsWith("audio/") ? "volume-2" : l.includes("pdf") || l.includes("text/") || l.includes("application/json") ? "file-text" : "file";
206
- return /* @__PURE__ */ s(
216
+ $.displayName = "FileUploadItem";
217
+ const V = /* @__PURE__ */ d.forwardRef(({ className: r, file: e, ...o }, t) => {
218
+ const a = (n) => n.startsWith("image/") ? "image" : n.startsWith("video/") ? "video" : n.startsWith("audio/") ? "volume-2" : n.includes("pdf") || n.includes("text/") || n.includes("application/json") ? "file-text" : "file";
219
+ return /* @__PURE__ */ i(
207
220
  "div",
208
221
  {
209
222
  ref: t,
210
- className: f(
223
+ className: v(
211
224
  "flex h-10 w-10 items-center justify-center rounded",
212
225
  r
213
226
  ),
214
227
  ...o,
215
- children: e.preview && re(e.file) ? /* @__PURE__ */ s(
228
+ children: e.preview && oe(e.file) ? /* @__PURE__ */ i(
216
229
  "img",
217
230
  {
218
231
  src: e.preview,
219
232
  alt: e.file.name,
220
233
  className: "h-full w-full rounded object-cover"
221
234
  }
222
- ) : /* @__PURE__ */ s(
223
- z,
235
+ ) : /* @__PURE__ */ i(
236
+ M,
224
237
  {
225
- name: n(e.file.type ?? ""),
238
+ name: a(e.file.type ?? ""),
226
239
  className: "h-5 w-5 text-[var(--color-text-secondary)]"
227
240
  }
228
241
  )
229
242
  }
230
243
  );
231
244
  });
232
- W.displayName = "FileUploadItemPreview";
233
- const $ = /* @__PURE__ */ i.forwardRef(({ className: r, file: e, children: o, ...t }, n) => /* @__PURE__ */ s("div", { ref: n, className: f("flex-1 space-y-1", r), ...t, children: o ?? /* @__PURE__ */ b(Q, { children: [
234
- /* @__PURE__ */ b("div", { className: "flex items-center gap-2", children: [
235
- /* @__PURE__ */ s("p", { className: "text-body-sm truncate font-medium", children: e.file.name }),
236
- /* @__PURE__ */ s(ee, { children: e.status })
245
+ V.displayName = "FileUploadItemPreview";
246
+ const T = /* @__PURE__ */ d.forwardRef(({ className: r, file: e, children: o, ...t }, a) => /* @__PURE__ */ i("div", { ref: a, className: v("flex-1 space-y-1", r), ...t, children: o ?? /* @__PURE__ */ F(Z, { children: [
247
+ /* @__PURE__ */ F("div", { className: "flex items-center gap-2", children: [
248
+ /* @__PURE__ */ i("p", { className: "text-body-sm truncate font-medium", children: e.file.name }),
249
+ /* @__PURE__ */ i(te, { children: e.status })
237
250
  ] }),
238
- /* @__PURE__ */ s("p", { className: "text-caption-sm text-[var(--color-text-secondary)]", children: E(e.file.size) }),
239
- e.error && /* @__PURE__ */ s("p", { className: "text-caption-sm text-[var(--color-text-error-bold)]", children: e.error })
251
+ /* @__PURE__ */ i("p", { className: "text-caption-sm text-[var(--color-text-secondary)]", children: S(e.file.size) }),
252
+ e.error && /* @__PURE__ */ i("p", { className: "text-caption-sm text-[var(--color-text-error-bold)]", children: e.error })
240
253
  ] }) }));
241
- $.displayName = "FileUploadItemMetadata";
242
- const V = /* @__PURE__ */ i.forwardRef(({ className: r, file: e, variant: o = "linear", ...t }, n) => e.status !== "uploading" || e.progress === void 0 ? null : /* @__PURE__ */ b("div", { ref: n, className: f("w-full", r), ...t, children: [
243
- /* @__PURE__ */ s(Z, { value: e.progress, className: "h-2" }),
244
- /* @__PURE__ */ b("p", { className: "text-caption-sm mt-1 text-[var(--color-text-secondary)]", children: [
254
+ T.displayName = "FileUploadItemMetadata";
255
+ const E = /* @__PURE__ */ d.forwardRef(({ className: r, file: e, variant: o = "linear", ...t }, a) => e.status !== "uploading" || e.progress === void 0 ? null : /* @__PURE__ */ F("div", { ref: a, className: v("w-full", r), ...t, children: [
256
+ /* @__PURE__ */ i(ee, { value: e.progress, className: "h-2" }),
257
+ /* @__PURE__ */ F("p", { className: "text-caption-sm mt-1 text-[var(--color-text-secondary)]", children: [
245
258
  Math.round(e.progress),
246
259
  "%"
247
260
  ] })
248
261
  ] }));
249
- V.displayName = "FileUploadItemProgress";
250
- const T = /* @__PURE__ */ i.forwardRef(({ file: r, ...e }, o) => {
251
- const { files: t, onFilesChange: n, onDelete: l } = D();
252
- return /* @__PURE__ */ s(
253
- P,
262
+ E.displayName = "FileUploadItemProgress";
263
+ const O = /* @__PURE__ */ d.forwardRef(({ file: r, ...e }, o) => {
264
+ const { files: t, onFilesChange: a, onDelete: n } = I();
265
+ return /* @__PURE__ */ i(
266
+ z,
254
267
  {
255
268
  ref: o,
256
269
  variant: "ghost",
257
270
  size: "s",
258
271
  onClick: async () => {
259
- if (l)
272
+ if (n)
260
273
  try {
261
- await l(r);
274
+ await n(r);
262
275
  } catch {
263
276
  return;
264
277
  }
265
- const p = t.filter((c) => c.id !== r.id);
266
- n(p);
278
+ const m = t.filter((c) => c.id !== r.id);
279
+ a(m);
267
280
  },
268
281
  "aria-label": `Remove ${r.file.name}`,
269
282
  ...e,
270
- children: /* @__PURE__ */ s(z, { name: _, className: "h-4 w-4" })
283
+ children: /* @__PURE__ */ i(M, { name: re, className: "h-4 w-4" })
271
284
  }
272
285
  );
273
286
  });
274
- T.displayName = "FileUploadItemDelete";
275
- const E = (r) => {
287
+ O.displayName = "FileUploadItemDelete";
288
+ const S = (r) => {
276
289
  if (r === 0) return "0 Bytes";
277
290
  const e = 1024, o = ["Bytes", "KB", "MB", "GB"], t = Math.floor(Math.log(r) / Math.log(e));
278
291
  return parseFloat((r / Math.pow(e, t)).toFixed(2)) + " " + o[t];
279
- }, ue = {
280
- Root: j,
281
- Dropzone: k,
282
- Trigger: L,
283
- List: A,
284
- Item: B,
285
- ItemPreview: W,
286
- ItemMetadata: $,
287
- ItemProgress: V,
288
- ItemDelete: T
292
+ }, me = {
293
+ Root: L,
294
+ Dropzone: A,
295
+ Trigger: B,
296
+ List: W,
297
+ Item: $,
298
+ ItemPreview: V,
299
+ ItemMetadata: T,
300
+ ItemProgress: E,
301
+ ItemDelete: O
289
302
  };
290
303
  export {
291
- ue as FileUpload,
292
- k as FileUploadDropzone,
293
- B as FileUploadItem,
294
- T as FileUploadItemDelete,
295
- $ as FileUploadItemMetadata,
296
- W as FileUploadItemPreview,
297
- V as FileUploadItemProgress,
298
- A as FileUploadList,
299
- j as FileUploadRoot,
300
- L as FileUploadTrigger,
301
- E as formatFileSize,
302
- D as useFileUpload
304
+ me as FileUpload,
305
+ A as FileUploadDropzone,
306
+ $ as FileUploadItem,
307
+ O as FileUploadItemDelete,
308
+ T as FileUploadItemMetadata,
309
+ V as FileUploadItemPreview,
310
+ E as FileUploadItemProgress,
311
+ W as FileUploadList,
312
+ L as FileUploadRoot,
313
+ B as FileUploadTrigger,
314
+ S as formatFileSize,
315
+ I as useFileUpload
303
316
  };