@seanyeh77/ui 0.47.2 → 0.48.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.
@@ -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>;
@@ -28,6 +28,8 @@ function useWorkbench() {
28
28
  }
29
29
  const TOP = 48;
30
30
  const BAR = 36;
31
+ /** How far a border's hit area reaches over the main view beyond its 8 px handle: 8 + 16 = 24 px (WCAG 2.5.8). */
32
+ const REACH = 16;
31
33
  const LIM = {
32
34
  explorer: [180, 480],
33
35
  side: [240, 560],
@@ -47,8 +49,7 @@ const TOGGLE = {
47
49
  };
48
50
  const toggleIcon = (r, open) => FRAME + TOGGLE[r][open ? 0 : 1];
49
51
  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);
52
+ function defaults(ids, d) {
52
53
  const side = (d?.side ?? []).filter((x) => ids.includes(x));
53
54
  const panel = (d?.panel ?? []).filter((x) => ids.includes(x) && !side.includes(x));
54
55
  for (const x of ids) if (!side.includes(x) && !panel.includes(x)) panel.push(x);
@@ -99,6 +100,15 @@ function restore(raw, def) {
99
100
  return def;
100
101
  }
101
102
  }
103
+ function same(a, b) {
104
+ if (a === b) return true;
105
+ if (typeof a !== "object" || typeof b !== "object" || !a || !b || Array.isArray(a) !== Array.isArray(b)) return false;
106
+ const ka = Object.keys(a).filter((k) => a[k] !== void 0);
107
+ const kb = Object.keys(b).filter((k) => b[k] !== void 0);
108
+ return ka.length === kb.length && ka.every((k) => same(a[k], b[k]));
109
+ }
110
+ const SEP = "\0";
111
+ const splitIds = (sig) => sig ? sig.split(SEP) : [];
102
112
  const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, Math.round(v)));
103
113
  const inField = (t) => t instanceof HTMLElement && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName));
104
114
  /** A draggable border between two regions: role="separator", arrows 16 px, Shift 64 px, Home/End, double-click resets.
@@ -178,10 +188,12 @@ function Splitter(p) {
178
188
  }
179
189
  /** A region's tab row: droplet underline, overflow (更多 menu with a mouse, scrolling on phones), drag and drop. */
180
190
  function TabRow(p) {
191
+ const idsSig = p.ids.join(SEP);
192
+ const ids = React.useMemo(() => splitIds(idsSig), [idsSig]);
181
193
  const track = React.useRef(null);
182
194
  const tabs = React.useRef({});
183
195
  const widths = React.useRef({});
184
- const [fit, setFit] = React.useState(p.ids.length);
196
+ const [fit, setFit] = React.useState(ids.length);
185
197
  const [box, setBox] = React.useState(null);
186
198
  const [forward, setForward] = React.useState(true);
187
199
  const [edges, setEdges] = React.useState({
@@ -193,10 +205,10 @@ function TabRow(p) {
193
205
  const prevActive = React.useRef(p.active);
194
206
  const menu = p.overflow === "menu";
195
207
  const order = hold ? (() => {
196
- const a = p.ids.filter((x) => x !== hold.id);
208
+ const a = ids.filter((x) => x !== hold.id);
197
209
  a.splice(hold.at, 0, hold.id);
198
210
  return a;
199
- })() : p.ids;
211
+ })() : ids;
200
212
  let shown = order;
201
213
  let hidden = [];
202
214
  if (menu && fit < order.length) {
@@ -208,10 +220,11 @@ function TabRow(p) {
208
220
  const tr2 = track.current;
209
221
  if (!tr2) return;
210
222
  const el = p.active ? tabs.current[p.active] : null;
211
- setBox(el ? {
223
+ const b = el ? {
212
224
  left: el.offsetLeft + 10,
213
225
  right: tr2.clientWidth - el.offsetLeft - el.offsetWidth + 10
214
- } : null);
226
+ } : null;
227
+ setBox((m) => m === b || m && b && m.left === b.left && m.right === b.right ? m : b);
215
228
  const left = tr2.scrollLeft > 1;
216
229
  const right = tr2.scrollLeft + tr2.clientWidth < tr2.scrollWidth - 1;
217
230
  setEdges((m) => m.left === left && m.right === right ? m : {
@@ -222,9 +235,9 @@ function TabRow(p) {
222
235
  for (const [id, t] of Object.entries(tabs.current)) if (t) widths.current[id] = t.offsetWidth;
223
236
  let used = 0;
224
237
  let n = 0;
225
- const total = p.ids.reduce((a, id) => a + (widths.current[id] ?? 80), 0);
238
+ const total = ids.reduce((a, id) => a + (widths.current[id] ?? 80), 0);
226
239
  const room = tr2.clientWidth - (total > tr2.clientWidth ? 72 : 0);
227
- for (const id of p.ids) {
240
+ for (const id of ids) {
228
241
  used += widths.current[id] ?? 80;
229
242
  if (used > room) break;
230
243
  n++;
@@ -233,12 +246,12 @@ function TabRow(p) {
233
246
  }
234
247
  }, [
235
248
  p.active,
236
- p.ids,
249
+ ids,
237
250
  menu
238
251
  ]);
239
252
  React.useLayoutEffect(() => {
240
253
  if (prevActive.current !== p.active) {
241
- setForward(p.ids.indexOf(p.active ?? "") > p.ids.indexOf(prevActive.current ?? ""));
254
+ setForward(ids.indexOf(p.active ?? "") > ids.indexOf(prevActive.current ?? ""));
242
255
  prevActive.current = p.active;
243
256
  }
244
257
  measure();
@@ -254,21 +267,21 @@ function TabRow(p) {
254
267
  return () => ro?.disconnect();
255
268
  }, [
256
269
  p.active,
257
- p.ids,
270
+ ids,
258
271
  measure,
259
272
  menu,
260
273
  hold
261
274
  ]);
262
275
  const drag = p.drag;
263
276
  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));
277
+ const di = drag ? ids.indexOf(drag.id) : -1;
278
+ const noop = (before) => di >= 0 && (before === drag?.id || before === (ids[di + 1] ?? null));
266
279
  const tabEl = (id, i, list) => {
267
280
  const v = p.views[id];
268
281
  const on = id === p.active;
269
282
  const next = list[i + 1] ?? null;
270
283
  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;
284
+ const lineAfter = !!over && !over.noop && over.before === null && i === list.length - 1 && list.length === ids.length;
272
285
  return /* @__PURE__ */ jsxs("button", {
273
286
  ref: (n) => {
274
287
  tabs.current[id] = n;
@@ -343,7 +356,7 @@ function TabRow(p) {
343
356
  navigator.vibrate?.(10);
344
357
  setHold({
345
358
  id: pr.id,
346
- at: p.ids.indexOf(pr.id)
359
+ at: ids.indexOf(pr.id)
347
360
  });
348
361
  }
349
362
  }, 300);
@@ -383,7 +396,7 @@ function TabRow(p) {
383
396
  if (pr.mode === "drag" && hold) {
384
397
  const next = order;
385
398
  setHold(null);
386
- if (next.join("\n") !== p.ids.join("\n")) p.onReorder?.(next);
399
+ if (next.join(SEP) !== idsSig) p.onReorder?.(next);
387
400
  }
388
401
  },
389
402
  onPointerCancel: () => {
@@ -480,7 +493,9 @@ function Workbench({ ref, ...p }) {
480
493
  devWarn(p.id !== void 0 && p.storageKey === void 0, "Workbench", "`id` is deprecated. Pass storageKey instead: the same name keeps the stored layout.");
481
494
  const name = p.storageKey ?? p.id;
482
495
  const key = name ? "ui-workbench:" + name : void 0;
483
- const def = React.useMemo(() => defaults(views, p.defaultLayout), [views, p.defaultLayout]);
496
+ const idsSig = views.map((v) => v.id).join(SEP);
497
+ const defSig = JSON.stringify(p.defaultLayout ?? null);
498
+ const def = React.useMemo(() => defaults(splitIds(idsSig), JSON.parse(defSig)), [idsSig, defSig]);
484
499
  const parseLayout = React.useCallback((raw) => restore(raw, def), [def]);
485
500
  const [stored, saveLay] = useStored(key, parseLayout, def);
486
501
  const [mem, setMem] = React.useState(null);
@@ -516,7 +531,10 @@ function Workbench({ ref, ...p }) {
516
531
  return () => ro?.disconnect();
517
532
  }, []);
518
533
  const onLayoutChange = p.onLayoutChange;
534
+ const reported = React.useRef(null);
519
535
  React.useEffect(() => {
536
+ if (reported.current && same(reported.current, lay)) return;
537
+ reported.current = lay;
520
538
  onLayoutChange?.(lay);
521
539
  }, [lay]);
522
540
  const [ov, setOv] = React.useState({
@@ -665,7 +683,7 @@ function Workbench({ ref, ...p }) {
665
683
  }));
666
684
  };
667
685
  const resetLayout = () => {
668
- setLay(defaults(views, p.defaultLayout));
686
+ setLay(def);
669
687
  setOv({
670
688
  explorer: false,
671
689
  side: false
@@ -733,6 +751,11 @@ function Workbench({ ref, ...p }) {
733
751
  side: tr("右側欄"),
734
752
  panel: tr("下方面板")
735
753
  };
754
+ const regionIn = {
755
+ explorer: tr("句中::檔案總管"),
756
+ side: tr("句中::右側欄"),
757
+ panel: tr("句中::下方面板")
758
+ };
736
759
  const keys = {
737
760
  explorer: (IS_MAC ? "⌘" : "Ctrl ") + "B",
738
761
  side: (IS_MAC ? "⌘⌥" : "Ctrl Alt ") + "B",
@@ -759,7 +782,7 @@ function Workbench({ ref, ...p }) {
759
782
  const o = drag?.over;
760
783
  if (!o || o.region !== r) return null;
761
784
  const same = lay[r].includes(drag.id);
762
- const text = o.noop ? tr("位置不變") : same ? tr("放開以排在這裡") : tr("放開以移到{region}", { region: regionName[r] });
785
+ const text = o.noop ? tr("位置不變") : same ? tr("放開以排在這裡") : tr("放開以移到{region}", { region: regionIn[r] });
763
786
  return /* @__PURE__ */ jsx("div", {
764
787
  className: "ui-wb-dropframe",
765
788
  "aria-hidden": "true",
@@ -810,14 +833,14 @@ function Workbench({ ref, ...p }) {
810
833
  buttons: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(IconButton, {
811
834
  variant: "quiet",
812
835
  icon: MOVE_ICON,
813
- label: tr("把目前分頁移到{region}", { region: regionName[r === "side" ? "panel" : "side"] }),
836
+ label: tr("把目前分頁移到{region}", { region: regionIn[r === "side" ? "panel" : "side"] }),
814
837
  onClick: () => moveOver(r),
815
838
  disabled: !act
816
839
  }), /* @__PURE__ */ jsx(IconButton, {
817
840
  variant: "quiet",
818
841
  icon: r === "side" ? "caret-right" : "caret-down",
819
842
  label: tr("{v}({t})", {
820
- v: tr("收合{region}", { region: regionName[r] }),
843
+ v: tr("收合{region}", { region: regionIn[r] }),
821
844
  t: keys[r]
822
845
  }),
823
846
  onClick: () => toggle(r)
@@ -829,6 +852,10 @@ function Workbench({ ref, ...p }) {
829
852
  };
830
853
  const brand = p.brand;
831
854
  const hasBar = !!(p.crumbs?.length || p.mainBar);
855
+ const reach = {
856
+ "--ui-wb-reach-top": `${hasBar ? BAR : 0}px`,
857
+ "--ui-wb-reach-bottom": `${panelOpen ? panelH + REACH + 1 : 0}px`
858
+ };
832
859
  const filter = p.explorerFilter;
833
860
  const brandInner = brand ? /* @__PURE__ */ jsxs(Fragment, { children: [brand.mark ? /* @__PURE__ */ jsx("span", {
834
861
  className: "ui-wb-mark",
@@ -940,7 +967,7 @@ function Workbench({ ref, ...p }) {
940
967
  type: "button",
941
968
  tabIndex: -1,
942
969
  className: cx("ui-wb-scrim", exOpen && "is-open"),
943
- "aria-label": tr("關閉{region}", { region: regionName.explorer }),
970
+ "aria-label": tr("關閉{region}", { region: regionIn.explorer }),
944
971
  onClick: () => setOv((o) => ({
945
972
  ...o,
946
973
  explorer: false
@@ -995,7 +1022,10 @@ function Workbench({ ref, ...p }) {
995
1022
  }
996
1023
  })),
997
1024
  onDrag: setSizing,
998
- style: { left: exW }
1025
+ style: {
1026
+ left: exW,
1027
+ ...reach
1028
+ }
999
1029
  }) : null
1000
1030
  ]
1001
1031
  })] }) : null,
@@ -1043,7 +1073,7 @@ function Workbench({ ref, ...p }) {
1043
1073
  type: "button",
1044
1074
  tabIndex: -1,
1045
1075
  className: cx("ui-wb-scrim", sideOpen && "is-open"),
1046
- "aria-label": tr("關閉{region}", { region: regionName.side }),
1076
+ "aria-label": tr("關閉{region}", { region: regionIn.side }),
1047
1077
  onClick: () => setOv((o) => ({
1048
1078
  ...o,
1049
1079
  side: false
@@ -1077,14 +1107,17 @@ function Workbench({ ref, ...p }) {
1077
1107
  }
1078
1108
  })),
1079
1109
  onDrag: setSizing,
1080
- style: { left: -1 }
1110
+ style: {
1111
+ left: -1,
1112
+ ...reach
1113
+ }
1081
1114
  }) : null]
1082
1115
  })] }) : null,
1083
1116
  showDropSide ? /* @__PURE__ */ jsx("div", {
1084
1117
  className: cx("ui-wb-dropzone is-side", drag?.over?.region === "side" && "is-over"),
1085
1118
  style: { bottom: (panelOpen ? panelH : 0) + 8 },
1086
1119
  ...regionDrop("side"),
1087
- children: tr("放開以移到{region}", { region: regionName.side })
1120
+ children: tr("放開以移到{region}", { region: regionIn.side })
1088
1121
  }) : null,
1089
1122
  showDropPanel ? /* @__PURE__ */ jsx("div", {
1090
1123
  className: cx("ui-wb-dropzone is-panel", drag?.over?.region === "panel" && "is-over"),
@@ -1093,7 +1126,7 @@ function Workbench({ ref, ...p }) {
1093
1126
  right: mainR + (showDropSide ? 120 : 8)
1094
1127
  },
1095
1128
  ...regionDrop("panel"),
1096
- children: tr("放開以移到{region}", { region: regionName.panel })
1129
+ children: tr("放開以移到{region}", { region: regionIn.panel })
1097
1130
  }) : null,
1098
1131
  phone && merged.length ? /* @__PURE__ */ jsxs("section", {
1099
1132
  className: cx("ui-wb-sheet", sheetOpen ? "is-open" : "is-shut"),