@seanyeh77/ui 0.7.1 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -520,6 +520,9 @@ interface ColumnBase<R extends Row> {
520
520
  mobile?: 'title' | 'status' | 'hide';
521
521
  /** Long text: 2 lines with 「顯示全部」 on phone cards, one line with an ellipsis on desktop. */
522
522
  long?: boolean;
523
+ /** Hidden at first; still listed in the 「欄位」 menu, where people can turn it on. Back to hidden on a new
524
+ * `resetKey`. Not for the first column, which is always shown. Default false. */
525
+ hidden?: boolean;
523
526
  }
524
527
  /** A column shows a field of the row (key is checked against the row type), or computes its cell with render (any key). */
525
528
  type DataTableColumn<R extends Row = Record<string, unknown>> = ColumnBase<R> & ({
package/dist/index.js CHANGED
@@ -1286,13 +1286,22 @@ function useReturnFocus(open) {
1286
1286
  if (!open && wasOpen.current) {
1287
1287
  const back = opener.current;
1288
1288
  opener.current = null;
1289
- const t = setTimeout(() => {
1290
- if (!lost()) return;
1291
- if (back?.isConnected) back.focus();
1292
- else focusMain();
1293
- }, 0);
1289
+ const held = document.activeElement;
1290
+ const until = performance.now() + 2e3;
1291
+ let frame = 0;
1292
+ const look = () => {
1293
+ if (lost()) {
1294
+ if (back?.isConnected) back.focus();
1295
+ else focusMain();
1296
+ return;
1297
+ }
1298
+ const at = document.activeElement;
1299
+ if (!(at === held || !!at?.closest("[data-ending-style]")) || performance.now() > until) return;
1300
+ frame = requestAnimationFrame(look);
1301
+ };
1302
+ frame = requestAnimationFrame(look);
1294
1303
  wasOpen.current = open;
1295
- return () => clearTimeout(t);
1304
+ return () => cancelAnimationFrame(frame);
1296
1305
  }
1297
1306
  wasOpen.current = open;
1298
1307
  }, [open]);
@@ -1316,13 +1325,24 @@ function useFocusRescue(ref, fallback) {
1316
1325
  last = e.target;
1317
1326
  index = Array.from(box.querySelectorAll(FOCUSABLE)).indexOf(last);
1318
1327
  };
1319
- const mo = new MutationObserver(() => {
1320
- if (!last || last.isConnected && !last.matches(":disabled") || !lost()) return;
1328
+ const rescue = () => {
1321
1329
  const now = list();
1322
1330
  const next = fb.current?.() ?? now[Math.min(index, now.length - 1)] ?? null;
1323
1331
  last = null;
1324
1332
  if (next) next.focus();
1325
1333
  else focusMain();
1334
+ };
1335
+ let frame = 0;
1336
+ const mo = new MutationObserver(() => {
1337
+ if (!last || last.isConnected && !last.matches(":disabled")) return;
1338
+ if (lost()) return rescue();
1339
+ const dead = last;
1340
+ cancelAnimationFrame(frame);
1341
+ frame = requestAnimationFrame(() => {
1342
+ frame = requestAnimationFrame(() => {
1343
+ if (last === dead && (lost() || document.activeElement === dead)) rescue();
1344
+ });
1345
+ });
1326
1346
  });
1327
1347
  box.addEventListener("focusin", onIn);
1328
1348
  mo.observe(box, {
@@ -1334,6 +1354,7 @@ function useFocusRescue(ref, fallback) {
1334
1354
  return () => {
1335
1355
  box.removeEventListener("focusin", onIn);
1336
1356
  mo.disconnect();
1357
+ cancelAnimationFrame(frame);
1337
1358
  };
1338
1359
  }, [ref]);
1339
1360
  }
@@ -2928,7 +2949,7 @@ function DataTable(p) {
2928
2949
  const [q, setQ] = React.useState(() => fromUrl().q);
2929
2950
  const [values, setValues] = React.useState(() => fromUrl().values);
2930
2951
  const [view, setView] = React.useState(p.views?.[0]?.id ?? null);
2931
- const [hidden, setHidden] = React.useState({});
2952
+ const [toggled, setToggled] = React.useState({});
2932
2953
  const [sel, setSel] = React.useState({});
2933
2954
  const [sumVal, setSumVal] = React.useState("all");
2934
2955
  const [resetKey, setResetKey] = React.useState(p.resetKey);
@@ -2944,6 +2965,7 @@ function DataTable(p) {
2944
2965
  key: null,
2945
2966
  dir: "desc"
2946
2967
  });
2968
+ setToggled({});
2947
2969
  }
2948
2970
  const valuesKey = JSON.stringify(values);
2949
2971
  React.useEffect(() => {
@@ -2951,7 +2973,11 @@ function DataTable(p) {
2951
2973
  }, [valuesKey, q]);
2952
2974
  devWarn(cols.some((c) => c.format === "percent" && !c.unit), "DataTable", "format: 'percent' sets 1 decimal and shows no % sign; add unit: '%' to that column.");
2953
2975
  devWarn(rows.length > 1 && rows.every((r) => get(r, "id") == null) && selectable, "DataTable", "rows have no id, so a selection follows the position, not the row. Add an id to each row.");
2954
- const shownCols = cols.filter((c, i) => i === 0 || !hidden[c.key]);
2976
+ devWarn(!!cols[0]?.hidden, "DataTable", "the first column is always shown, so its hidden: true does nothing. Move the column, or drop hidden.");
2977
+ devWarn((p.columnMenu === false || cols.length <= 2) && cols.some((c, i) => i > 0 && c.hidden), "DataTable", "a column has hidden: true but there is no 「欄位」 menu (columnMenu is false, or the table has 2 columns or fewer), so nobody can turn it on. Turn the menu on, or leave the column out.");
2978
+ const hidden = {};
2979
+ cols.forEach((c, i) => hidden[c.key] = i > 0 && (toggled[c.key] ?? !!c.hidden));
2980
+ const shownCols = cols.filter((c) => !hidden[c.key]);
2955
2981
  const { ref: scrollRef, more: scrollMore, overflows, scrolled, onScroll } = useScrollHint();
2956
2982
  const bodyRef = React.useRef(null);
2957
2983
  const rowEls = React.useRef({});
@@ -3347,8 +3373,8 @@ function DataTable(p) {
3347
3373
  p.columnMenu !== false && cols.length > 2 ? /* @__PURE__ */ jsx(ColumnMenu, {
3348
3374
  cols,
3349
3375
  hidden,
3350
- onToggle: (k) => setHidden({
3351
- ...hidden,
3376
+ onToggle: (k) => setToggled({
3377
+ ...toggled,
3352
3378
  [k]: !hidden[k]
3353
3379
  })
3354
3380
  }) : null