@seanyeh77/ui 0.47.1 → 0.48.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.
@@ -0,0 +1,17 @@
1
+ export interface FilePickerOptions {
2
+ /** File types the picker offers, as in <input accept>, e.g. ".json,application/json". */
3
+ accept?: string;
4
+ /** Lets the user pick several files at once. */
5
+ multiple?: boolean;
6
+ /** Called with the picked files; not called when the user cancels. */
7
+ onPick: (files: File[]) => void;
8
+ }
9
+ export interface FilePicker {
10
+ /** Opens the browser's file picker. Call it straight from a click handler (Button onClick, Menu item onSelect) so
11
+ * the browser sees the user's gesture. */
12
+ open: () => void;
13
+ }
14
+ /** Opens the browser's file picker from any button or menu item, without a drop area: { open }. The hook keeps its
15
+ * own hidden <input type="file"> (created on the first open, removed on unmount), so the page renders no native
16
+ * control. Safe on the server: nothing touches the document until open() is called. */
17
+ export declare function useFilePicker({ accept, multiple, onPick }: FilePickerOptions): FilePicker;
@@ -0,0 +1,53 @@
1
+ "use client";
2
+ import * as React from "react";
3
+ // src/components/FileUpload/useFilePicker.ts
4
+ /** Opens the browser's file picker from any button or menu item, without a drop area: { open }. The hook keeps its
5
+ * own hidden <input type="file"> (created on the first open, removed on unmount), so the page renders no native
6
+ * control. Safe on the server: nothing touches the document until open() is called. */
7
+ function useFilePicker({ accept, multiple, onPick }) {
8
+ const input = React.useRef(null);
9
+ const opts = React.useRef({
10
+ accept,
11
+ multiple,
12
+ onPick
13
+ });
14
+ React.useLayoutEffect(() => {
15
+ opts.current = {
16
+ accept,
17
+ multiple,
18
+ onPick
19
+ };
20
+ });
21
+ React.useEffect(() => () => {
22
+ input.current?.remove();
23
+ input.current = null;
24
+ }, []);
25
+ const open = React.useCallback(() => {
26
+ let el = input.current;
27
+ if (!el) {
28
+ el = document.createElement("input");
29
+ el.type = "file";
30
+ el.hidden = true;
31
+ el.tabIndex = -1;
32
+ el.setAttribute("aria-hidden", "true");
33
+ el.addEventListener("change", () => {
34
+ const target = input.current;
35
+ if (!target) return;
36
+ const files = Array.from(target.files ?? []);
37
+ target.value = "";
38
+ if (files.length) opts.current.onPick(files);
39
+ });
40
+ document.body.appendChild(el);
41
+ input.current = el;
42
+ }
43
+ const { accept: a, multiple: m } = opts.current;
44
+ if (a) el.setAttribute("accept", a);
45
+ else el.removeAttribute("accept");
46
+ el.toggleAttribute("multiple", !!m);
47
+ el.click();
48
+ }, []);
49
+ return React.useMemo(() => ({ open }), [open]);
50
+ }
51
+ export { useFilePicker };
52
+
53
+ //# sourceMappingURL=useFilePicker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFilePicker.js","names":[],"sources":["../../../src/components/FileUpload/useFilePicker.ts"],"sourcesContent":["import * as React from 'react';\n\nexport interface FilePickerOptions {\n /** File types the picker offers, as in <input accept>, e.g. \".json,application/json\". */\n accept?: string;\n /** Lets the user pick several files at once. */\n multiple?: boolean;\n /** Called with the picked files; not called when the user cancels. */\n onPick: (files: File[]) => void;\n}\n\nexport interface FilePicker {\n /** Opens the browser's file picker. Call it straight from a click handler (Button onClick, Menu item onSelect) so\n * the browser sees the user's gesture. */\n open: () => void;\n}\n\n/** Opens the browser's file picker from any button or menu item, without a drop area: { open }. The hook keeps its\n * own hidden <input type=\"file\"> (created on the first open, removed on unmount), so the page renders no native\n * control. Safe on the server: nothing touches the document until open() is called. */\nexport function useFilePicker({ accept, multiple, onPick }: FilePickerOptions): FilePicker {\n const input = React.useRef<HTMLInputElement | null>(null);\n // The latest options, read when the picker opens and when files arrive.\n const opts = React.useRef({ accept, multiple, onPick });\n React.useLayoutEffect(() => {\n opts.current = { accept, multiple, onPick };\n });\n React.useEffect(\n () => () => {\n input.current?.remove();\n input.current = null;\n },\n [],\n );\n const open = React.useCallback(() => {\n let el = input.current;\n if (!el) {\n el = document.createElement('input');\n el.type = 'file';\n el.hidden = true;\n el.tabIndex = -1;\n el.setAttribute('aria-hidden', 'true');\n el.addEventListener('change', () => {\n const target = input.current;\n if (!target) return;\n const files = Array.from(target.files ?? []);\n // Cleared so picking the same file again still reports it.\n target.value = '';\n if (files.length) opts.current.onPick(files);\n });\n // In the document: some browsers open the picker only for an input that is attached.\n document.body.appendChild(el);\n input.current = el;\n }\n const { accept: a, multiple: m } = opts.current;\n if (a) el.setAttribute('accept', a);\n else el.removeAttribute('accept');\n el.toggleAttribute('multiple', !!m);\n el.click();\n }, []);\n return React.useMemo(() => ({ open }), [open]);\n}\n"],"mappings":";;;;;;AAoBA,SAAgB,cAAc,EAAE,QAAQ,UAAU,UAAyC;CACzF,MAAM,QAAQ,MAAM,OAAgC,IAAI;CAExD,MAAM,OAAO,MAAM,OAAO;EAAE;EAAQ;EAAU;CAAO,CAAC;CACtD,MAAM,sBAAsB;EAC1B,KAAK,UAAU;GAAE;GAAQ;GAAU;EAAO;CAC5C,CAAC;CACD,MAAM,sBACQ;EACV,MAAM,SAAS,OAAO;EACtB,MAAM,UAAU;CAClB,GACA,CAAC,CACH;CACA,MAAM,OAAO,MAAM,kBAAkB;EACnC,IAAI,KAAK,MAAM;EACf,IAAI,CAAC,IAAI;GACP,KAAK,SAAS,cAAc,OAAO;GACnC,GAAG,OAAO;GACV,GAAG,SAAS;GACZ,GAAG,WAAW;GACd,GAAG,aAAa,eAAe,MAAM;GACrC,GAAG,iBAAiB,gBAAgB;IAClC,MAAM,SAAS,MAAM;IACrB,IAAI,CAAC,QAAQ;IACb,MAAM,QAAQ,MAAM,KAAK,OAAO,SAAS,CAAC,CAAC;IAE3C,OAAO,QAAQ;IACf,IAAI,MAAM,QAAQ,KAAK,QAAQ,OAAO,KAAK;GAC7C,CAAC;GAED,SAAS,KAAK,YAAY,EAAE;GAC5B,MAAM,UAAU;EAClB;EACA,MAAM,EAAE,QAAQ,GAAG,UAAU,MAAM,KAAK;EACxC,IAAI,GAAG,GAAG,aAAa,UAAU,CAAC;OAC7B,GAAG,gBAAgB,QAAQ;EAChC,GAAG,gBAAgB,YAAY,CAAC,CAAC,CAAC;EAClC,GAAG,MAAM;CACX,GAAG,CAAC,CAAC;CACL,OAAO,MAAM,eAAe,EAAE,KAAK,IAAI,CAAC,IAAI,CAAC;AAC/C"}
@@ -55,7 +55,7 @@ export interface WorkbenchProps {
55
55
  explorerHead?: React.ReactNode;
56
56
  /** Below the explorer, fixed: 「設定」 and the like. */
57
57
  explorerFoot?: React.ReactNode;
58
- /** Tabs of the side bar and the bottom panel. Where each one sits is the user's choice, kept per browser. */
58
+ /** Tabs of the side bar and the bottom panel, kept per browser where the user put them; may be a new array on every render (only the ids count for the layout). */
59
59
  views?: WorkbenchView[];
60
60
  /** The layout before the user changes it, and after 「還原預設版面」. Views in neither list go to the panel. */
61
61
  defaultLayout?: {
@@ -70,8 +70,7 @@ export interface WorkbenchProps {
70
70
  label?: string;
71
71
  /** Extra class on the root element. */
72
72
  className?: string;
73
- /** Called with the layout on the first render and after every change (a tab picked or moved, a region opened or
74
- * closed, a size changed), for example to put the panel's current tab in the URL. */
73
+ /** Called with the layout on the first render and after every change of its content (a tab picked or moved, a region opened or closed, a size changed), never for an equal layout, so it can be a state setter. */
75
74
  onLayoutChange?: (layout: WorkbenchLayout) => void;
76
75
  /** show, toggle, resetLayout and getLayout from outside, such as URL handling in the page around the Workbench. */
77
76
  ref?: React.Ref<WorkbenchHandle>;
@@ -47,8 +47,7 @@ const TOGGLE = {
47
47
  };
48
48
  const toggleIcon = (r, open) => FRAME + TOGGLE[r][open ? 0 : 1];
49
49
  const MOVE_ICON = "M4 8h15m0 0-3.5-3.5M19 8l-3.5 3.5M20 16H5m0 0 3.5-3.5M5 16l3.5 3.5";
50
- function defaults(views, d) {
51
- const ids = views.map((v) => v.id);
50
+ function defaults(ids, d) {
52
51
  const side = (d?.side ?? []).filter((x) => ids.includes(x));
53
52
  const panel = (d?.panel ?? []).filter((x) => ids.includes(x) && !side.includes(x));
54
53
  for (const x of ids) if (!side.includes(x) && !panel.includes(x)) panel.push(x);
@@ -99,6 +98,15 @@ function restore(raw, def) {
99
98
  return def;
100
99
  }
101
100
  }
101
+ function same(a, b) {
102
+ if (a === b) return true;
103
+ if (typeof a !== "object" || typeof b !== "object" || !a || !b || Array.isArray(a) !== Array.isArray(b)) return false;
104
+ const ka = Object.keys(a).filter((k) => a[k] !== void 0);
105
+ const kb = Object.keys(b).filter((k) => b[k] !== void 0);
106
+ return ka.length === kb.length && ka.every((k) => same(a[k], b[k]));
107
+ }
108
+ const SEP = "\0";
109
+ const splitIds = (sig) => sig ? sig.split(SEP) : [];
102
110
  const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, Math.round(v)));
103
111
  const inField = (t) => t instanceof HTMLElement && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName));
104
112
  /** A draggable border between two regions: role="separator", arrows 16 px, Shift 64 px, Home/End, double-click resets.
@@ -178,10 +186,12 @@ function Splitter(p) {
178
186
  }
179
187
  /** A region's tab row: droplet underline, overflow (更多 menu with a mouse, scrolling on phones), drag and drop. */
180
188
  function TabRow(p) {
189
+ const idsSig = p.ids.join(SEP);
190
+ const ids = React.useMemo(() => splitIds(idsSig), [idsSig]);
181
191
  const track = React.useRef(null);
182
192
  const tabs = React.useRef({});
183
193
  const widths = React.useRef({});
184
- const [fit, setFit] = React.useState(p.ids.length);
194
+ const [fit, setFit] = React.useState(ids.length);
185
195
  const [box, setBox] = React.useState(null);
186
196
  const [forward, setForward] = React.useState(true);
187
197
  const [edges, setEdges] = React.useState({
@@ -193,10 +203,10 @@ function TabRow(p) {
193
203
  const prevActive = React.useRef(p.active);
194
204
  const menu = p.overflow === "menu";
195
205
  const order = hold ? (() => {
196
- const a = p.ids.filter((x) => x !== hold.id);
206
+ const a = ids.filter((x) => x !== hold.id);
197
207
  a.splice(hold.at, 0, hold.id);
198
208
  return a;
199
- })() : p.ids;
209
+ })() : ids;
200
210
  let shown = order;
201
211
  let hidden = [];
202
212
  if (menu && fit < order.length) {
@@ -208,10 +218,11 @@ function TabRow(p) {
208
218
  const tr2 = track.current;
209
219
  if (!tr2) return;
210
220
  const el = p.active ? tabs.current[p.active] : null;
211
- setBox(el ? {
221
+ const b = el ? {
212
222
  left: el.offsetLeft + 10,
213
223
  right: tr2.clientWidth - el.offsetLeft - el.offsetWidth + 10
214
- } : null);
224
+ } : null;
225
+ setBox((m) => m === b || m && b && m.left === b.left && m.right === b.right ? m : b);
215
226
  const left = tr2.scrollLeft > 1;
216
227
  const right = tr2.scrollLeft + tr2.clientWidth < tr2.scrollWidth - 1;
217
228
  setEdges((m) => m.left === left && m.right === right ? m : {
@@ -222,9 +233,9 @@ function TabRow(p) {
222
233
  for (const [id, t] of Object.entries(tabs.current)) if (t) widths.current[id] = t.offsetWidth;
223
234
  let used = 0;
224
235
  let n = 0;
225
- const total = p.ids.reduce((a, id) => a + (widths.current[id] ?? 80), 0);
236
+ const total = ids.reduce((a, id) => a + (widths.current[id] ?? 80), 0);
226
237
  const room = tr2.clientWidth - (total > tr2.clientWidth ? 72 : 0);
227
- for (const id of p.ids) {
238
+ for (const id of ids) {
228
239
  used += widths.current[id] ?? 80;
229
240
  if (used > room) break;
230
241
  n++;
@@ -233,12 +244,12 @@ function TabRow(p) {
233
244
  }
234
245
  }, [
235
246
  p.active,
236
- p.ids,
247
+ ids,
237
248
  menu
238
249
  ]);
239
250
  React.useLayoutEffect(() => {
240
251
  if (prevActive.current !== p.active) {
241
- setForward(p.ids.indexOf(p.active ?? "") > p.ids.indexOf(prevActive.current ?? ""));
252
+ setForward(ids.indexOf(p.active ?? "") > ids.indexOf(prevActive.current ?? ""));
242
253
  prevActive.current = p.active;
243
254
  }
244
255
  measure();
@@ -254,21 +265,21 @@ function TabRow(p) {
254
265
  return () => ro?.disconnect();
255
266
  }, [
256
267
  p.active,
257
- p.ids,
268
+ ids,
258
269
  measure,
259
270
  menu,
260
271
  hold
261
272
  ]);
262
273
  const drag = p.drag;
263
274
  const over = drag?.over?.region === p.region ? drag.over : void 0;
264
- const di = drag ? p.ids.indexOf(drag.id) : -1;
265
- const noop = (before) => di >= 0 && (before === drag?.id || before === (p.ids[di + 1] ?? null));
275
+ const di = drag ? ids.indexOf(drag.id) : -1;
276
+ const noop = (before) => di >= 0 && (before === drag?.id || before === (ids[di + 1] ?? null));
266
277
  const tabEl = (id, i, list) => {
267
278
  const v = p.views[id];
268
279
  const on = id === p.active;
269
280
  const next = list[i + 1] ?? null;
270
281
  const lineBefore = !!over && !over.noop && over.before === id;
271
- const lineAfter = !!over && !over.noop && over.before === null && i === list.length - 1 && list.length === p.ids.length;
282
+ const lineAfter = !!over && !over.noop && over.before === null && i === list.length - 1 && list.length === ids.length;
272
283
  return /* @__PURE__ */ jsxs("button", {
273
284
  ref: (n) => {
274
285
  tabs.current[id] = n;
@@ -343,7 +354,7 @@ function TabRow(p) {
343
354
  navigator.vibrate?.(10);
344
355
  setHold({
345
356
  id: pr.id,
346
- at: p.ids.indexOf(pr.id)
357
+ at: ids.indexOf(pr.id)
347
358
  });
348
359
  }
349
360
  }, 300);
@@ -383,7 +394,7 @@ function TabRow(p) {
383
394
  if (pr.mode === "drag" && hold) {
384
395
  const next = order;
385
396
  setHold(null);
386
- if (next.join("\n") !== p.ids.join("\n")) p.onReorder?.(next);
397
+ if (next.join(SEP) !== idsSig) p.onReorder?.(next);
387
398
  }
388
399
  },
389
400
  onPointerCancel: () => {
@@ -480,7 +491,9 @@ function Workbench({ ref, ...p }) {
480
491
  devWarn(p.id !== void 0 && p.storageKey === void 0, "Workbench", "`id` is deprecated. Pass storageKey instead: the same name keeps the stored layout.");
481
492
  const name = p.storageKey ?? p.id;
482
493
  const key = name ? "ui-workbench:" + name : void 0;
483
- const def = React.useMemo(() => defaults(views, p.defaultLayout), [views, p.defaultLayout]);
494
+ const idsSig = views.map((v) => v.id).join(SEP);
495
+ const defSig = JSON.stringify(p.defaultLayout ?? null);
496
+ const def = React.useMemo(() => defaults(splitIds(idsSig), JSON.parse(defSig)), [idsSig, defSig]);
484
497
  const parseLayout = React.useCallback((raw) => restore(raw, def), [def]);
485
498
  const [stored, saveLay] = useStored(key, parseLayout, def);
486
499
  const [mem, setMem] = React.useState(null);
@@ -516,7 +529,10 @@ function Workbench({ ref, ...p }) {
516
529
  return () => ro?.disconnect();
517
530
  }, []);
518
531
  const onLayoutChange = p.onLayoutChange;
532
+ const reported = React.useRef(null);
519
533
  React.useEffect(() => {
534
+ if (reported.current && same(reported.current, lay)) return;
535
+ reported.current = lay;
520
536
  onLayoutChange?.(lay);
521
537
  }, [lay]);
522
538
  const [ov, setOv] = React.useState({
@@ -665,7 +681,7 @@ function Workbench({ ref, ...p }) {
665
681
  }));
666
682
  };
667
683
  const resetLayout = () => {
668
- setLay(defaults(views, p.defaultLayout));
684
+ setLay(def);
669
685
  setOv({
670
686
  explorer: false,
671
687
  side: false