@gridkitjs/react 0.14.0 → 0.16.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/dist/index.js CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  // src/DataGrid/DataGrid.tsx
4
4
  import {
5
- useEffect as useEffect7,
5
+ useEffect as useEffect8,
6
6
  useId as useId2,
7
7
  useImperativeHandle as useImperativeHandle3,
8
8
  useMemo as useMemo5,
9
9
  useRef as useRef21,
10
- useState as useState20
10
+ useState as useState18
11
11
  } from "react";
12
12
  import {
13
13
  moveColumnBefore,
@@ -122,6 +122,7 @@ function tabIndexFor(columnIndex, focusedColumnIndex) {
122
122
  return columnIndex === focusedColumnIndex ? 0 : -1;
123
123
  }
124
124
  function useGridNavigation({
125
+ gridRef,
125
126
  headerTableRef,
126
127
  bodyTableRef,
127
128
  rowCount,
@@ -146,7 +147,10 @@ function useGridNavigation({
146
147
  getHeaderRow: () => headerTableRef.current?.tHead?.rows[0] ?? null,
147
148
  getBodyRow: (rowIndex) => findBodyRow(bodyTableRef.current, rowIndex)
148
149
  }));
149
- useLayoutEffect(() => gridFocus.attach(rows), [gridFocus, rows]);
150
+ useLayoutEffect(
151
+ () => gridFocus.attach(rows, gridRef.current),
152
+ [gridFocus, rows, gridRef]
153
+ );
150
154
  useEffect(() => {
151
155
  gridFocus.focusDrawnCell(focus);
152
156
  });
@@ -161,9 +165,10 @@ function useGridNavigation({
161
165
  gridFocus.focusCell(rowIndex, columnIndex);
162
166
  gridFocus.focusDrawnCell(gridFocus.getFocus());
163
167
  },
164
- focusTabStop: () => {
165
- const cells = (focus.rowIndex === HEADER_ROW ? rows.getHeaderRow() : rows.getBodyRow(focus.rowIndex))?.cells;
166
- cells?.[Math.min(focus.columnIndex, cells.length - 1)]?.focus();
168
+ tabStopCell: () => {
169
+ const { rowIndex, columnIndex } = gridFocus.getFocus();
170
+ const cells = (rowIndex === HEADER_ROW ? rows.getHeaderRow() : rows.getBodyRow(rowIndex))?.cells;
171
+ return cells?.[Math.min(columnIndex, cells.length - 1)] ?? null;
167
172
  },
168
173
  onKeyDown: (event) => {
169
174
  if (gridFocus.handleKey(
@@ -236,7 +241,7 @@ function GridHeader({
236
241
  const beforeId = drag.dropTarget?.kind === "column-order" ? drag.dropTarget.beforeId : void 0;
237
242
  const draggedEntry = columns.find((entry) => entry.id === drag.draggedColumnId) ?? null;
238
243
  function toggleFilterMenu(columnId) {
239
- const { panel } = panels;
244
+ const panel = panels.current();
240
245
  if (panel?.kind === "filter" && panel.target.area === "header" && panel.target.columnId === columnId) {
241
246
  panels.close();
242
247
  } else {
@@ -286,7 +291,7 @@ function GridHeader({
286
291
  nav.focusCell(HEADER_ROW, index);
287
292
  },
288
293
  onClick: (event) => {
289
- if (drag.justDragged()) {
294
+ if (drag.justDragged(entry.id)) {
290
295
  return;
291
296
  }
292
297
  if (event.target instanceof Element && (event.target.closest(".header-resize-handle") !== null || event.target.closest(".header-sort-toggle") !== null || event.target.closest(".header-group-toggle") !== null || event.target.closest(".header-filter-toggle") !== null)) {
@@ -1297,35 +1302,73 @@ import {
1297
1302
 
1298
1303
  // src/DataGrid/useDragReorder.ts
1299
1304
  import {
1300
- useRef,
1305
+ useRef as useRef2,
1301
1306
  useState as useState3
1302
1307
  } from "react";
1303
1308
  import {
1304
1309
  NO_DRAG,
1305
1310
  startDragReorder
1306
1311
  } from "@gridkitjs/dom";
1312
+
1313
+ // src/DataGrid/useGestureSignal.ts
1314
+ import { useEffect as useEffect2, useRef } from "react";
1315
+ function useGestureSignal() {
1316
+ const gesture = useRef(null);
1317
+ useEffect2(() => {
1318
+ const current = gesture.current;
1319
+ if (current !== null && !current.element.isConnected) {
1320
+ gesture.current = null;
1321
+ current.controller.abort();
1322
+ }
1323
+ });
1324
+ useEffect2(
1325
+ () => () => {
1326
+ gesture.current?.controller.abort();
1327
+ },
1328
+ []
1329
+ );
1330
+ return function startGesture(element, start) {
1331
+ const controller = new AbortController();
1332
+ if (!start(controller.signal)) {
1333
+ return false;
1334
+ }
1335
+ gesture.current?.controller.abort();
1336
+ gesture.current = { element, controller };
1337
+ return true;
1338
+ };
1339
+ }
1340
+
1341
+ // src/DataGrid/useDragReorder.ts
1307
1342
  function useDragReorder({
1308
1343
  resolveTarget,
1309
1344
  onDrop
1310
1345
  }) {
1311
1346
  const [drag, setDrag] = useState3(NO_DRAG);
1312
- const dragged = useRef(false);
1313
- function justDragged() {
1314
- const answer = dragged.current;
1315
- dragged.current = false;
1347
+ const openedDragId = useRef2(null);
1348
+ const startGesture = useGestureSignal();
1349
+ function justDragged(id) {
1350
+ const answer = openedDragId.current === id;
1351
+ openedDragId.current = null;
1316
1352
  return answer;
1317
1353
  }
1318
1354
  function startDrag(id, grabElement, event) {
1319
- startDragReorder(id, grabElement, event, {
1320
- resolveTarget,
1321
- onState(state) {
1322
- if (state.draggedId !== null) {
1323
- dragged.current = true;
1324
- }
1325
- setDrag(state);
1326
- },
1327
- onDrop
1328
- });
1355
+ const started = startGesture(
1356
+ grabElement,
1357
+ (signal) => startDragReorder(id, grabElement, event, {
1358
+ resolveTarget,
1359
+ onState(state) {
1360
+ if (state.draggedId !== null) {
1361
+ openedDragId.current = state.draggedId;
1362
+ }
1363
+ setDrag(state);
1364
+ },
1365
+ onDrop,
1366
+ signal
1367
+ })
1368
+ );
1369
+ if (started) {
1370
+ openedDragId.current = null;
1371
+ }
1329
1372
  }
1330
1373
  return {
1331
1374
  draggedId: drag.draggedId,
@@ -1353,8 +1396,7 @@ function useGroupByDrag({
1353
1396
  ghostTransform: drag.ghostTransform,
1354
1397
  startDrag: (columnId, event) => {
1355
1398
  drag.startDrag(columnId, event.currentTarget, event);
1356
- },
1357
- justDragged: drag.justDragged
1399
+ }
1358
1400
  };
1359
1401
  }
1360
1402
 
@@ -1689,7 +1731,7 @@ function copyToClipboard(content, { messages, announce }) {
1689
1731
  }
1690
1732
 
1691
1733
  // src/DataGrid/export/useSaveFile.ts
1692
- import { useRef as useRef2 } from "react";
1734
+ import { useRef as useRef3 } from "react";
1693
1735
  import { downloadBlob } from "@gridkitjs/dom";
1694
1736
 
1695
1737
  // src/DataGrid/export/exportApi.ts
@@ -1725,32 +1767,42 @@ function exportApi({
1725
1767
  }
1726
1768
 
1727
1769
  // src/DataGrid/export/useSaveFile.ts
1728
- function download(file) {
1729
- downloadBlob(exportBlob(file), file.fileName);
1730
- }
1770
+ var IDLE = { kind: "idle" };
1771
+ var WRITING = { kind: "writing" };
1731
1772
  function useSaveFile({
1732
1773
  messages,
1733
1774
  announce
1734
1775
  }) {
1735
- const writingRef = useRef2(false);
1776
+ const state = useRef3(IDLE);
1777
+ function fail(reason, error) {
1778
+ console.error(`DataGrid: ${reason}`, error);
1779
+ announce(messages.exportFailed);
1780
+ }
1781
+ function download(file) {
1782
+ try {
1783
+ downloadBlob(exportBlob(file), file.fileName);
1784
+ } catch (error) {
1785
+ fail("the export could not be saved.", error);
1786
+ }
1787
+ }
1736
1788
  return (file) => {
1737
- if (writingRef.current) {
1789
+ if (state.current.kind === "writing") {
1738
1790
  console.error(
1739
1791
  "DataGrid: an export is still being written, so this one was dropped."
1740
1792
  );
1741
- return;
1793
+ return false;
1742
1794
  }
1743
1795
  if (!(file instanceof Promise)) {
1744
1796
  download(file);
1745
- return;
1797
+ return true;
1746
1798
  }
1747
- writingRef.current = true;
1799
+ state.current = WRITING;
1748
1800
  file.then(download, (error) => {
1749
- console.error("DataGrid: the export's writer failed.", error);
1750
- announce(messages.exportFailed);
1801
+ fail("the export's writer failed.", error);
1751
1802
  }).finally(() => {
1752
- writingRef.current = false;
1803
+ state.current = IDLE;
1753
1804
  });
1805
+ return true;
1754
1806
  };
1755
1807
  }
1756
1808
 
@@ -1813,6 +1865,7 @@ function useGridCommands({
1813
1865
  print: (document2, options) => {
1814
1866
  printGrid(document2, options).catch((error) => {
1815
1867
  console.error("DataGrid: the print failed.", error);
1868
+ announce(messages.printFailed);
1816
1869
  });
1817
1870
  }
1818
1871
  };
@@ -1843,13 +1896,13 @@ function useGridCommands({
1843
1896
  }
1844
1897
 
1845
1898
  // src/DataGrid/menu/GridMenu.tsx
1846
- import { useLayoutEffect as useLayoutEffect3, useRef as useRef4, useState as useState4 } from "react";
1899
+ import { useLayoutEffect as useLayoutEffect3, useRef as useRef5, useState as useState4 } from "react";
1847
1900
  import {
1848
1901
  attachMenu
1849
1902
  } from "@gridkitjs/dom";
1850
1903
 
1851
1904
  // src/DataGrid/overlay/Overlay.tsx
1852
- import { useLayoutEffect as useLayoutEffect2, useRef as useRef3 } from "react";
1905
+ import { useLayoutEffect as useLayoutEffect2, useRef as useRef4 } from "react";
1853
1906
  import { createPortal as createPortal3 } from "react-dom";
1854
1907
  import {
1855
1908
  attachOverlay
@@ -1873,16 +1926,19 @@ function Overlay({
1873
1926
  offset,
1874
1927
  initialFocus,
1875
1928
  returnFocusTo,
1929
+ returnFocusFallback,
1876
1930
  ignoreScrollAfterInput,
1877
1931
  trapFocus,
1878
1932
  themeClassName,
1879
1933
  onDismiss,
1880
1934
  children
1881
1935
  }) {
1882
- const panelRef = useRef3(null);
1883
- const onDismissRef = useRef3(onDismiss);
1936
+ const panelRef = useRef4(null);
1937
+ const onDismissRef = useRef4(onDismiss);
1938
+ const returnFocusFallbackRef = useRef4(returnFocusFallback);
1884
1939
  useLayoutEffect2(() => {
1885
1940
  onDismissRef.current = onDismiss;
1941
+ returnFocusFallbackRef.current = returnFocusFallback;
1886
1942
  });
1887
1943
  useLayoutEffect2(() => {
1888
1944
  const panel = panelRef.current;
@@ -1895,6 +1951,7 @@ function Overlay({
1895
1951
  offset,
1896
1952
  initialFocus: attachedFocus(initialFocus),
1897
1953
  returnFocusTo,
1954
+ returnFocusFallback: () => returnFocusFallbackRef.current?.() ?? null,
1898
1955
  ignoreScrollAfterInput,
1899
1956
  trapFocus,
1900
1957
  onDismiss: (reason) => {
@@ -2028,11 +2085,11 @@ function GridMenu({
2028
2085
  onCloseSubmenu,
2029
2086
  behaviourRef
2030
2087
  }) {
2031
- const menuRef = useRef4(null);
2032
- const submenuBehaviourRef = useRef4(null);
2088
+ const menuRef = useRef5(null);
2089
+ const submenuBehaviourRef = useRef5(null);
2033
2090
  const [submenu, setSubmenu] = useState4(null);
2034
2091
  const isSubmenu = onCloseSubmenu !== void 0;
2035
- const latest = useRef4({ entries, submenu, onRun, onClose, onCloseSubmenu });
2092
+ const latest = useRef5({ entries, submenu, onRun, onClose, onCloseSubmenu });
2036
2093
  useLayoutEffect3(() => {
2037
2094
  latest.current = { entries, submenu, onRun, onClose, onCloseSubmenu };
2038
2095
  });
@@ -2088,6 +2145,9 @@ function GridMenu({
2088
2145
  const openEntry = submenu === null ? void 0 : entries.find(
2089
2146
  (entry) => entry.type === "submenu" && entry.id === submenu.id
2090
2147
  );
2148
+ if (submenu !== null && openEntry === void 0) {
2149
+ setSubmenu(null);
2150
+ }
2091
2151
  return /* @__PURE__ */ jsxs9(Fragment2, { children: [
2092
2152
  /* @__PURE__ */ jsx13(
2093
2153
  "div",
@@ -2140,7 +2200,7 @@ import { jsx as jsx14 } from "react/jsx-runtime";
2140
2200
  function GridContextMenu({
2141
2201
  contextMenu
2142
2202
  }) {
2143
- const { menu, run, close } = contextMenu;
2203
+ const { menu, returnFocusFallback, run, close } = contextMenu;
2144
2204
  if (menu === null) {
2145
2205
  return null;
2146
2206
  }
@@ -2150,6 +2210,7 @@ function GridContextMenu({
2150
2210
  anchor: menu.anchor,
2151
2211
  gap: menu.anchor instanceof Element ? ANCHOR_GAP : 0,
2152
2212
  returnFocusTo: menu.cell ?? void 0,
2213
+ returnFocusFallback,
2153
2214
  themeClassName: menu.themeClassName,
2154
2215
  onDismiss: close,
2155
2216
  children: /* @__PURE__ */ jsx14(
@@ -2169,10 +2230,10 @@ function GridContextMenu({
2169
2230
 
2170
2231
  // src/DataGrid/contextMenu/useContextMenu.ts
2171
2232
  import {
2172
- useEffect as useEffect2,
2233
+ useCallback,
2234
+ useEffect as useEffect3,
2173
2235
  useLayoutEffect as useLayoutEffect4,
2174
- useRef as useRef5,
2175
- useState as useState5
2236
+ useRef as useRef6
2176
2237
  } from "react";
2177
2238
  import {
2178
2239
  contextMenuEntries,
@@ -2321,7 +2382,7 @@ function useContextMenu({
2321
2382
  bodyTableRef,
2322
2383
  bodyScrollRef,
2323
2384
  contextMenu,
2324
- onContextMenuOpenChange,
2385
+ overlay,
2325
2386
  controller,
2326
2387
  commands,
2327
2388
  nav,
@@ -2329,9 +2390,8 @@ function useContextMenu({
2329
2390
  messages,
2330
2391
  columnName
2331
2392
  }) {
2332
- const [menu, setMenu] = useState5(null);
2333
- const current = useRef5(null);
2334
- const openings = useRef5(0);
2393
+ const { drawn, close: closeOverlay } = overlay;
2394
+ const openings = useRef6(0);
2335
2395
  const enabled = contextMenu !== void 0 && contextMenu !== false;
2336
2396
  function parts() {
2337
2397
  const { columns, pageRows } = controller.getView();
@@ -2386,20 +2446,6 @@ function useContextMenu({
2386
2446
  context
2387
2447
  });
2388
2448
  }
2389
- function show(next) {
2390
- const previous = current.current;
2391
- if (previous === next) {
2392
- return;
2393
- }
2394
- current.current = next;
2395
- setMenu(next);
2396
- if (previous !== null) {
2397
- onContextMenuOpenChange?.({ open: false, target: previous.target });
2398
- }
2399
- if (next !== null) {
2400
- onContextMenuOpenChange?.({ open: true, target: next.target });
2401
- }
2402
- }
2403
2449
  function openAt({ target, cell, anchor }, point) {
2404
2450
  let resolved = resolveFor(target);
2405
2451
  if (resolved.length === 0) {
@@ -2413,14 +2459,17 @@ function useContextMenu({
2413
2459
  nav.focusCellNow(cell.rowIndex, cell.columnIndex);
2414
2460
  }
2415
2461
  openings.current += 1;
2416
- show({
2417
- key: openings.current,
2418
- target,
2419
- entries: resolved,
2420
- label: labelOf(target),
2421
- anchor: point ?? anchor,
2422
- cell: cell?.element ?? null,
2423
- themeClassName: overlayThemeClassName(rootRef.current)
2462
+ overlay.open({
2463
+ kind: "contextMenu",
2464
+ menu: {
2465
+ key: openings.current,
2466
+ target,
2467
+ entries: resolved,
2468
+ label: labelOf(target),
2469
+ anchor: point ?? anchor,
2470
+ cell: cell?.element ?? null,
2471
+ themeClassName: overlayThemeClassName(rootRef.current)
2472
+ }
2424
2473
  });
2425
2474
  return true;
2426
2475
  }
@@ -2428,11 +2477,11 @@ function useContextMenu({
2428
2477
  const place = placeAt(target, parts());
2429
2478
  return place !== null && openAt(place, point);
2430
2479
  }
2431
- const latestRequest = useRef5(handleRequest);
2480
+ const latestRequest = useRef6(handleRequest);
2432
2481
  useLayoutEffect4(() => {
2433
2482
  latestRequest.current = handleRequest;
2434
2483
  });
2435
- useEffect2(() => {
2484
+ useEffect3(() => {
2436
2485
  const root = rootRef.current;
2437
2486
  if (!enabled || root === null) {
2438
2487
  return void 0;
@@ -2441,28 +2490,22 @@ function useContextMenu({
2441
2490
  onRequest: (request) => latestRequest.current(request)
2442
2491
  });
2443
2492
  }, [rootRef, enabled]);
2444
- const shown = useRef5(null);
2445
- useLayoutEffect4(() => {
2446
- const closed = shown.current;
2447
- shown.current = menu;
2448
- if (menu === null && closed !== null && closed.cell !== null && !closed.cell.isConnected && document.activeElement === document.body) {
2449
- nav.focusTabStop();
2450
- }
2451
- });
2452
- function close() {
2453
- show(null);
2454
- }
2493
+ const close = useCallback(() => {
2494
+ closeOverlay("contextMenu");
2495
+ }, [closeOverlay]);
2496
+ useEffect3(() => enabled ? close : void 0, [enabled, close]);
2455
2497
  return {
2456
- menu,
2498
+ menu: drawn.kind === "contextMenu" ? drawn.menu : null,
2457
2499
  opens: (area) => hasContextMenu(contextMenu, area),
2458
2500
  open: (target, anchor) => {
2459
2501
  const place = placeOf(target, parts());
2460
2502
  return place !== null && openAt(place, anchor ?? null);
2461
2503
  },
2504
+ returnFocusFallback: overlay.returnFocusFallback,
2462
2505
  run: (item) => {
2463
- const open = current.current;
2464
- if (item.run() && open !== null) {
2465
- commands.report(item.id, open.target, "contextMenu");
2506
+ const open = overlay.current();
2507
+ if (item.run() && open.kind === "contextMenu") {
2508
+ commands.report(item.id, open.menu.target, "contextMenu");
2466
2509
  }
2467
2510
  close();
2468
2511
  },
@@ -2470,11 +2513,52 @@ function useContextMenu({
2470
2513
  };
2471
2514
  }
2472
2515
 
2516
+ // src/DataGrid/overlay/useGridOverlay.ts
2517
+ import { useCallback as useCallback2, useLayoutEffect as useLayoutEffect5, useRef as useRef7, useState as useState5 } from "react";
2518
+ var CLOSED = { kind: "closed" };
2519
+ function useGridOverlay({
2520
+ nav,
2521
+ onContextMenuOpenChange
2522
+ }) {
2523
+ const [drawn, setDrawn] = useState5(CLOSED);
2524
+ const current = useRef7(CLOSED);
2525
+ const reportOpenChange = useRef7(onContextMenuOpenChange);
2526
+ useLayoutEffect5(() => {
2527
+ reportOpenChange.current = onContextMenuOpenChange;
2528
+ });
2529
+ const show = useCallback2((next) => {
2530
+ const previous = current.current;
2531
+ current.current = next;
2532
+ setDrawn(next);
2533
+ if (previous.kind === "contextMenu") {
2534
+ reportOpenChange.current?.({ open: false, target: previous.menu.target });
2535
+ }
2536
+ if (next.kind === "contextMenu") {
2537
+ reportOpenChange.current?.({ open: true, target: next.menu.target });
2538
+ }
2539
+ }, []);
2540
+ const close = useCallback2(
2541
+ (kind) => {
2542
+ if (current.current.kind === kind) {
2543
+ show(CLOSED);
2544
+ }
2545
+ },
2546
+ [show]
2547
+ );
2548
+ return {
2549
+ drawn,
2550
+ current: () => current.current,
2551
+ open: show,
2552
+ close,
2553
+ returnFocusFallback: nav.tabStopCell
2554
+ };
2555
+ }
2556
+
2473
2557
  // src/DataGrid/panels/GridPanels.tsx
2474
- import { useRef as useRef13 } from "react";
2558
+ import { useRef as useRef14 } from "react";
2475
2559
 
2476
2560
  // src/DataGrid/columnChooser/ColumnChooser.tsx
2477
- import { useLayoutEffect as useLayoutEffect5, useRef as useRef6, useState as useState6 } from "react";
2561
+ import { useLayoutEffect as useLayoutEffect6, useRef as useRef8, useState as useState6 } from "react";
2478
2562
  import { resolveColumnChooser } from "@gridkitjs/core";
2479
2563
  import { Fragment as Fragment3, jsx as jsx15, jsxs as jsxs10 } from "react/jsx-runtime";
2480
2564
  function ColumnChooser({
@@ -2482,13 +2566,13 @@ function ColumnChooser({
2482
2566
  messages
2483
2567
  }) {
2484
2568
  const [search, setSearch] = useState6("");
2485
- const showAllRef = useRef6(null);
2569
+ const showAllRef = useRef8(null);
2486
2570
  const { entries, selectAll, toggleAll } = resolveColumnChooser({
2487
2571
  columns: controller.getView().dataColumns,
2488
2572
  visibility: controller.getState().columnVisibility,
2489
2573
  search
2490
2574
  });
2491
- useLayoutEffect5(() => {
2575
+ useLayoutEffect6(() => {
2492
2576
  if (showAllRef.current !== null) {
2493
2577
  showAllRef.current.indeterminate = selectAll === "mixed";
2494
2578
  }
@@ -2559,7 +2643,7 @@ function ColumnChooser({
2559
2643
  }
2560
2644
 
2561
2645
  // src/DataGrid/filterMenu/FilterMenu.tsx
2562
- import { useEffect as useEffect3, useRef as useRef11 } from "react";
2646
+ import { useEffect as useEffect4, useRef as useRef13 } from "react";
2563
2647
  import {
2564
2648
  resolveColumnFilterMenu as resolveColumnFilterMenu2,
2565
2649
  resolveColumnHeaderText,
@@ -2569,23 +2653,102 @@ import {
2569
2653
  // src/DataGrid/filterMenu/ExcelFilterEditor.tsx
2570
2654
  import {
2571
2655
  useImperativeHandle as useImperativeHandle2,
2572
- useLayoutEffect as useLayoutEffect9,
2656
+ useLayoutEffect as useLayoutEffect10,
2573
2657
  useMemo as useMemo4,
2574
- useReducer,
2575
- useRef as useRef10,
2576
- useState as useState10
2658
+ useReducer as useReducer2,
2659
+ useRef as useRef12,
2660
+ useState as useState9
2577
2661
  } from "react";
2578
2662
  import {
2579
- describeColumnFilter as describeColumnFilter2,
2580
2663
  draftToFilter as draftToFilter2,
2581
2664
  filterChecklist,
2582
- filterEditorReducer as filterEditorReducer2,
2583
- initialFilterEditorDraft as initialFilterEditorDraft2,
2665
+ filterDraftChanged as filterDraftChanged3,
2584
2666
  selectAllState
2585
2667
  } from "@gridkitjs/core";
2586
2668
 
2669
+ // src/DataGrid/filterMenu/excelEditorState.ts
2670
+ import {
2671
+ describeColumnFilter,
2672
+ filterDraftChanged,
2673
+ filterEditorReducer,
2674
+ initialFilterEditorDraft
2675
+ } from "@gridkitjs/core";
2676
+ function openValues(filter, columnId, values) {
2677
+ const shape = describeColumnFilter(filter, columnId);
2678
+ return {
2679
+ over: filter,
2680
+ shape,
2681
+ draft: initialFilterEditorDraft(shape, values)
2682
+ };
2683
+ }
2684
+ function initialExcelEditorState(opened) {
2685
+ return { opened, draft: opened.draft };
2686
+ }
2687
+ function showsAlike(a, b) {
2688
+ return a.shape.kind === b.shape.kind && a.draft.options === b.draft.options && !filterDraftChanged(a.draft, b.draft);
2689
+ }
2690
+ function excelEditorReducer(state, action) {
2691
+ if (action.type === "filterChanged") {
2692
+ return showsAlike(state.opened, action.opened) ? { ...state, opened: action.opened } : initialExcelEditorState(action.opened);
2693
+ }
2694
+ return { ...state, draft: filterEditorReducer(state.draft, action) };
2695
+ }
2696
+
2697
+ // src/DataGrid/menu/useMenuButton.ts
2698
+ import {
2699
+ useLayoutEffect as useLayoutEffect7,
2700
+ useRef as useRef9,
2701
+ useState as useState7
2702
+ } from "react";
2703
+ var CLOSED2 = { kind: "closed" };
2704
+ function useMenuButton({
2705
+ itemCount,
2706
+ gap = () => ANCHOR_GAP
2707
+ }) {
2708
+ const [state, setState] = useState7(CLOSED2);
2709
+ const behaviourRef = useRef9(null);
2710
+ useLayoutEffect7(() => {
2711
+ if (state.kind === "open" && state.focusItem > 0) {
2712
+ behaviourRef.current?.focusItem(state.focusItem);
2713
+ }
2714
+ }, [state]);
2715
+ function open(button, onLast) {
2716
+ setState({
2717
+ kind: "open",
2718
+ anchor: button,
2719
+ themeClassName: overlayThemeClassName(button),
2720
+ gap: gap(button),
2721
+ focusItem: onLast ? itemCount - 1 : 0
2722
+ });
2723
+ }
2724
+ function close() {
2725
+ setState(CLOSED2);
2726
+ }
2727
+ return {
2728
+ state,
2729
+ close,
2730
+ buttonProps: {
2731
+ "aria-haspopup": "menu",
2732
+ "aria-expanded": state.kind === "open",
2733
+ onClick: (event) => {
2734
+ if (state.kind === "closed") {
2735
+ open(event.currentTarget, false);
2736
+ } else {
2737
+ close();
2738
+ }
2739
+ },
2740
+ onKeyDown: (event) => {
2741
+ if ((event.key === "ArrowDown" || event.key === "ArrowUp") && state.kind === "closed") {
2742
+ event.preventDefault();
2743
+ open(event.currentTarget, event.key === "ArrowUp");
2744
+ }
2745
+ }
2746
+ },
2747
+ behaviourRef
2748
+ };
2749
+ }
2750
+
2587
2751
  // src/DataGrid/filterMenu/OperatorMenuButton.tsx
2588
- import { useLayoutEffect as useLayoutEffect6, useRef as useRef7, useState as useState7 } from "react";
2589
2752
  import { Fragment as Fragment4, jsx as jsx16, jsxs as jsxs11 } from "react/jsx-runtime";
2590
2753
  function gapBeside(button) {
2591
2754
  const dialog = button.closest('[role="dialog"]') ?? button;
@@ -2598,25 +2761,11 @@ function OperatorMenuButton({
2598
2761
  onChoose,
2599
2762
  ref
2600
2763
  }) {
2601
- const [open, setOpen] = useState7(null);
2602
- const menuBehaviourRef = useRef7(null);
2764
+ const { state, close, buttonProps, behaviourRef } = useMenuButton({
2765
+ itemCount: menu.operators.length,
2766
+ gap: gapBeside
2767
+ });
2603
2768
  const label = messages.filterOperatorMenu(menu.family);
2604
- useLayoutEffect6(() => {
2605
- if (open !== null && open.focusItem > 0) {
2606
- menuBehaviourRef.current?.focusItem(open.focusItem);
2607
- }
2608
- }, [open]);
2609
- function show(button, onLast = false) {
2610
- setOpen({
2611
- anchor: button,
2612
- themeClassName: overlayThemeClassName(button),
2613
- gap: gapBeside(button),
2614
- focusItem: onLast ? menu.operators.length - 1 : 0
2615
- });
2616
- }
2617
- function close() {
2618
- setOpen(null);
2619
- }
2620
2769
  const entries = menu.operators.map((operator) => ({
2621
2770
  type: "item",
2622
2771
  id: operator,
@@ -2636,21 +2785,7 @@ function OperatorMenuButton({
2636
2785
  ref,
2637
2786
  type: "button",
2638
2787
  className: "filter-menu-submenu",
2639
- "aria-haspopup": "menu",
2640
- "aria-expanded": open !== null,
2641
- onClick: (event) => {
2642
- if (open === null) {
2643
- show(event.currentTarget);
2644
- } else {
2645
- close();
2646
- }
2647
- },
2648
- onKeyDown: (event) => {
2649
- if ((event.key === "ArrowDown" || event.key === "ArrowUp") && open === null) {
2650
- event.preventDefault();
2651
- show(event.currentTarget, event.key === "ArrowUp");
2652
- }
2653
- },
2788
+ ...buttonProps,
2654
2789
  children: [
2655
2790
  /* @__PURE__ */ jsx16("span", { className: "filter-menu-submenu-icon", "aria-hidden": "true", children: active !== void 0 && /* @__PURE__ */ jsx16(CheckIcon, {}) }),
2656
2791
  label,
@@ -2658,22 +2793,22 @@ function OperatorMenuButton({
2658
2793
  ]
2659
2794
  }
2660
2795
  ),
2661
- open !== null && /* @__PURE__ */ jsx16(
2796
+ state.kind === "open" && /* @__PURE__ */ jsx16(
2662
2797
  Overlay,
2663
2798
  {
2664
- anchor: open.anchor,
2799
+ anchor: state.anchor,
2665
2800
  placement: "right-start",
2666
- gap: open.gap,
2667
- returnFocusTo: open.anchor,
2668
- themeClassName: open.themeClassName,
2801
+ gap: state.gap,
2802
+ returnFocusTo: state.anchor,
2803
+ themeClassName: state.themeClassName,
2669
2804
  onDismiss: close,
2670
2805
  children: /* @__PURE__ */ jsx16(
2671
2806
  GridMenu,
2672
2807
  {
2673
2808
  entries,
2674
2809
  label,
2675
- themeClassName: open.themeClassName,
2676
- behaviourRef: menuBehaviourRef,
2810
+ themeClassName: state.themeClassName,
2811
+ behaviourRef,
2677
2812
  onRun: (item) => {
2678
2813
  item.run();
2679
2814
  },
@@ -2690,17 +2825,15 @@ import {
2690
2825
  Fragment as Fragment5,
2691
2826
  useId,
2692
2827
  useImperativeHandle,
2693
- useLayoutEffect as useLayoutEffect7,
2694
- useRef as useRef8,
2695
- useState as useState8
2828
+ useLayoutEffect as useLayoutEffect8,
2829
+ useReducer,
2830
+ useRef as useRef10
2696
2831
  } from "react";
2697
2832
  import {
2698
- conditionRowsFrom,
2699
- conditionsFromRows,
2700
- describeColumnFilter,
2833
+ conditionsFromRows as conditionsFromRows2,
2701
2834
  draftToFilter,
2702
- filterEditorReducer,
2703
- initialFilterEditorDraft
2835
+ filterDraftChanged as filterDraftChanged2,
2836
+ filterEditorReducer as filterEditorReducer3
2704
2837
  } from "@gridkitjs/core";
2705
2838
 
2706
2839
  // src/DataGrid/filterMenu/ConditionRow.tsx
@@ -2821,9 +2954,14 @@ function ConditionRow({
2821
2954
  );
2822
2955
  }
2823
2956
 
2824
- // src/DataGrid/filterMenu/SimpleFilterEditor.tsx
2825
- import { jsx as jsx18, jsxs as jsxs13 } from "react/jsx-runtime";
2826
- var COMBINATORS = ["and", "or"];
2957
+ // src/DataGrid/filterMenu/simpleEditorState.ts
2958
+ import {
2959
+ conditionRowsFrom,
2960
+ conditionsFromRows,
2961
+ describeColumnFilter as describeColumnFilter2,
2962
+ filterEditorReducer as filterEditorReducer2,
2963
+ initialFilterEditorDraft as initialFilterEditorDraft2
2964
+ } from "@gridkitjs/core";
2827
2965
  var NO_ROWS = [];
2828
2966
  var NO_VALUES = {
2829
2967
  options: [],
@@ -2831,6 +2969,77 @@ var NO_VALUES = {
2831
2969
  truncated: false,
2832
2970
  blanks: 0
2833
2971
  };
2972
+ function filterDraftShown(shape, rows, columnId, type) {
2973
+ const draft = initialFilterEditorDraft2(shape, {
2974
+ ...NO_VALUES,
2975
+ columnType: type
2976
+ });
2977
+ if (shape.kind !== "conditions") {
2978
+ return draft;
2979
+ }
2980
+ const shown = conditionsFromRows(rows, columnId, type);
2981
+ return shown.invalid.length > 0 ? draft : filterEditorReducer2(draft, {
2982
+ type: "setConditions",
2983
+ conditions: shown.conditions
2984
+ });
2985
+ }
2986
+ function openConditions(filter, columnId, operators, type, operator) {
2987
+ const shape = describeColumnFilter2(filter, columnId);
2988
+ const rows = conditionRowsFrom(shape, operators, type);
2989
+ return {
2990
+ over: filter,
2991
+ custom: shape.kind === "custom" || shape.kind === "values" && operator === void 0,
2992
+ draft: {
2993
+ // Its typed values kept, as picking the operator in the row keeps them.
2994
+ rows: operator === void 0 ? rows : rows.map(
2995
+ (row, index) => index === 0 ? { ...row, operator } : row
2996
+ ),
2997
+ combinator: shape.kind === "conditions" ? shape.combinator : "and"
2998
+ },
2999
+ filterDraft: filterDraftShown(shape, rows, columnId, type)
3000
+ };
3001
+ }
3002
+ function initialSimpleEditorState(opened) {
3003
+ return { opened, draft: opened.draft, invalid: NO_ROWS };
3004
+ }
3005
+ function sameConditionsDraft(a, b) {
3006
+ return a.combinator === b.combinator && a.rows.length === b.rows.length && a.rows.every((row, index) => {
3007
+ const other = b.rows[index];
3008
+ return other !== void 0 && row.operator === other.operator && row.text === other.text && row.textTo === other.textTo;
3009
+ });
3010
+ }
3011
+ function showsAlike2(a, b) {
3012
+ return a.custom === b.custom && sameConditionsDraft(a.draft, b.draft);
3013
+ }
3014
+ function simpleEditorReducer(state, action) {
3015
+ switch (action.type) {
3016
+ case "editRow":
3017
+ return {
3018
+ ...state,
3019
+ draft: {
3020
+ ...state.draft,
3021
+ rows: state.draft.rows.map(
3022
+ (row, index) => index === action.index ? action.row : row
3023
+ )
3024
+ },
3025
+ invalid: NO_ROWS
3026
+ };
3027
+ case "setCombinator":
3028
+ return {
3029
+ ...state,
3030
+ draft: { ...state.draft, combinator: action.combinator },
3031
+ invalid: NO_ROWS
3032
+ };
3033
+ case "markInvalid":
3034
+ return { ...state, invalid: action.rows };
3035
+ case "filterChanged":
3036
+ return showsAlike2(state.opened, action.opened) ? { ...state, opened: action.opened } : initialSimpleEditorState(action.opened);
3037
+ }
3038
+ }
3039
+
3040
+ // src/DataGrid/filterMenu/SimpleFilterEditor.tsx
3041
+ import { jsx as jsx18, jsxs as jsxs13 } from "react/jsx-runtime";
3042
+ var COMBINATORS = ["and", "or"];
2834
3043
  function holdsUnreadable(row) {
2835
3044
  return Array.from(row?.querySelectorAll("input") ?? []).some(
2836
3045
  (input) => input.validity.badInput
@@ -2876,42 +3085,34 @@ function SimpleFilterEditor({
2876
3085
  ref
2877
3086
  }) {
2878
3087
  const type = column.column.type;
2879
- const [opened] = useState8(() => {
2880
- const shape = describeColumnFilter(controller.getState().filter, column.id);
2881
- const rows = conditionRowsFrom(shape, menu.operators, type);
2882
- const draft2 = {
2883
- // Its typed values kept, as picking the operator in the row keeps them.
2884
- rows: operator === void 0 ? rows : rows.map(
2885
- (row, index) => index === 0 ? { ...row, operator } : row
2886
- ),
2887
- combinator: shape.kind === "conditions" ? shape.combinator : "and"
2888
- };
2889
- return {
2890
- custom: shape.kind === "custom" || shape.kind === "values" && operator === void 0,
2891
- draft: draft2
2892
- };
2893
- });
2894
- const [draft, setDraft] = useState8(opened.draft);
2895
- const [invalid, setInvalid] = useState8(NO_ROWS);
2896
- const rowElements = useRef8([]);
3088
+ const filter = controller.getState().filter;
3089
+ const open = (over) => openConditions(over, column.id, menu.operators, type, operator);
3090
+ const [{ opened, draft, invalid }, dispatch] = useReducer(
3091
+ simpleEditorReducer,
3092
+ filter,
3093
+ (over) => initialSimpleEditorState(open(over))
3094
+ );
3095
+ if (filter !== opened.over) {
3096
+ dispatch({ type: "filterChanged", opened: open(filter) });
3097
+ }
3098
+ const rowElements = useRef10([]);
2897
3099
  const invalidMessageId = useId();
2898
3100
  const combinatorName = useId();
2899
- function update(next) {
2900
- setDraft(next);
2901
- setInvalid(NO_ROWS);
2902
- }
2903
3101
  function applyConditions(conditions) {
2904
- const applied = filterEditorReducer(
2905
- initialFilterEditorDraft({ kind: "none" }, NO_VALUES),
2906
- { type: "setConditions", conditions, combinator: draft.combinator }
2907
- );
2908
- applyFilter(
2909
- draftToFilter(applied, column.id, controller.getState().filter)
2910
- );
3102
+ const applied = filterEditorReducer3(opened.filterDraft, {
3103
+ type: "setConditions",
3104
+ conditions,
3105
+ combinator: draft.combinator
3106
+ });
3107
+ if (filterDraftChanged2(opened.filterDraft, applied)) {
3108
+ applyFilter(
3109
+ draftToFilter(applied, column.id, controller.getState().filter)
3110
+ );
3111
+ }
2911
3112
  }
2912
3113
  useImperativeHandle(ref, () => ({
2913
3114
  apply: () => {
2914
- const parsed = conditionsFromRows(draft.rows, column.id, type);
3115
+ const parsed = conditionsFromRows2(draft.rows, column.id, type);
2915
3116
  const unreadable = draft.rows.map(
2916
3117
  (_, index) => holdsUnreadable(rowElements.current[index])
2917
3118
  );
@@ -2922,14 +3123,14 @@ function SimpleFilterEditor({
2922
3123
  applyConditions(parsed.conditions);
2923
3124
  return true;
2924
3125
  }
2925
- if (draft === opened.draft && !unreadable.includes(true)) {
3126
+ if (sameConditionsDraft(draft, opened.draft) && !unreadable.includes(true)) {
2926
3127
  return true;
2927
3128
  }
2928
- setInvalid(marked);
3129
+ dispatch({ type: "markInvalid", rows: marked });
2929
3130
  return false;
2930
3131
  }
2931
3132
  }));
2932
- useLayoutEffect7(() => {
3133
+ useLayoutEffect8(() => {
2933
3134
  const [first] = invalid;
2934
3135
  if (first !== void 0) {
2935
3136
  rowElements.current[first]?.querySelector('[aria-invalid="true"]')?.focus();
@@ -2947,7 +3148,7 @@ function SimpleFilterEditor({
2947
3148
  combinator: draft.combinator,
2948
3149
  messages,
2949
3150
  onChange: (combinator) => {
2950
- update({ ...draft, combinator });
3151
+ dispatch({ type: "setCombinator", combinator });
2951
3152
  }
2952
3153
  }
2953
3154
  ),
@@ -2965,12 +3166,7 @@ function SimpleFilterEditor({
2965
3166
  invalidMessageId: invalid.includes(index) ? invalidMessageId : void 0,
2966
3167
  focusRef: index === 0 ? initialFocusRef : void 0,
2967
3168
  onChange: (next) => {
2968
- update({
2969
- ...draft,
2970
- rows: draft.rows.map(
2971
- (current, at) => at === index ? next : current
2972
- )
2973
- });
3169
+ dispatch({ type: "editRow", index, row: next });
2974
3170
  }
2975
3171
  }
2976
3172
  )
@@ -2981,7 +3177,7 @@ function SimpleFilterEditor({
2981
3177
  }
2982
3178
 
2983
3179
  // src/DataGrid/filterMenu/ValueChecklist.tsx
2984
- import { useLayoutEffect as useLayoutEffect8, useRef as useRef9, useState as useState9 } from "react";
3180
+ import { useLayoutEffect as useLayoutEffect9, useRef as useRef11, useState as useState8 } from "react";
2985
3181
  import {
2986
3182
  attachChecklist,
2987
3183
  createRowVirtualizer
@@ -3001,15 +3197,15 @@ function ValueChecklist({
3001
3197
  label,
3002
3198
  onToggle
3003
3199
  }) {
3004
- const listRef = useRef9(null);
3005
- const checklistRef = useRef9(null);
3006
- const [virtualizer, setVirtualizer] = useState9(null);
3007
- const [range, setRange] = useState9(NO_RANGE);
3008
- const latest = useRef9({ items, onToggle });
3009
- useLayoutEffect8(() => {
3200
+ const listRef = useRef11(null);
3201
+ const checklistRef = useRef11(null);
3202
+ const [virtualizer, setVirtualizer] = useState8(null);
3203
+ const [range, setRange] = useState8(NO_RANGE);
3204
+ const latest = useRef11({ items, onToggle });
3205
+ useLayoutEffect9(() => {
3010
3206
  latest.current = { items, onToggle };
3011
3207
  });
3012
- useLayoutEffect8(() => {
3208
+ useLayoutEffect9(() => {
3013
3209
  const list = listRef.current;
3014
3210
  if (list === null) {
3015
3211
  return void 0;
@@ -3038,7 +3234,7 @@ function ValueChecklist({
3038
3234
  rows.destroy();
3039
3235
  };
3040
3236
  }, []);
3041
- useLayoutEffect8(() => {
3237
+ useLayoutEffect9(() => {
3042
3238
  virtualizer?.setRowCount(items.length);
3043
3239
  checklistRef.current?.setCount(items.length);
3044
3240
  }, [virtualizer, items.length]);
@@ -3086,6 +3282,10 @@ function ValueChecklist({
3086
3282
 
3087
3283
  // src/DataGrid/filterMenu/ExcelFilterEditor.tsx
3088
3284
  import { Fragment as Fragment7, jsx as jsx20, jsxs as jsxs15 } from "react/jsx-runtime";
3285
+ var OPENED_ON_VALUES = {
3286
+ kind: "values",
3287
+ focusOnMount: false
3288
+ };
3089
3289
  function toggleOf(item) {
3090
3290
  return item.kind === "blanks" ? { type: "setIncludeBlanks", includeBlanks: !item.checked } : { type: "toggleValue", value: item.value };
3091
3291
  }
@@ -3099,20 +3299,21 @@ function ExcelFilterEditor({
3099
3299
  columnValues,
3100
3300
  ref
3101
3301
  }) {
3102
- const [opened] = useState10(() => {
3103
- const shape2 = describeColumnFilter2(controller.getState().filter, column.id);
3104
- return {
3105
- shape: shape2,
3106
- draft: initialFilterEditorDraft2(shape2, columnValues(column.id))
3107
- };
3108
- });
3109
- const [draft, dispatch] = useReducer(filterEditorReducer2, opened.draft);
3110
- const [operator, setOperator] = useState10(null);
3111
- const conditions = useRef10(null);
3112
- const conditionsFocus = useRef10(null);
3113
- const operatorButton = useRef10(null);
3114
- const switched = useRef10(false);
3115
- const selectAllRef = useRef10(null);
3302
+ const filter = controller.getState().filter;
3303
+ const open = (over) => openValues(over, column.id, columnValues(column.id));
3304
+ const [{ opened, draft }, dispatch] = useReducer2(
3305
+ excelEditorReducer,
3306
+ filter,
3307
+ (over) => initialExcelEditorState(open(over))
3308
+ );
3309
+ if (filter !== opened.over) {
3310
+ dispatch({ type: "filterChanged", opened: open(filter) });
3311
+ }
3312
+ const [mode, setMode] = useState9(OPENED_ON_VALUES);
3313
+ const conditions = useRef12(null);
3314
+ const conditionsFocus = useRef12(null);
3315
+ const operatorButton = useRef12(null);
3316
+ const selectAllRef = useRef12(null);
3116
3317
  const selectAll = useMemo4(() => selectAllState(draft), [draft]);
3117
3318
  const checklist = useMemo4(
3118
3319
  () => filterChecklist(draft, {
@@ -3121,28 +3322,24 @@ function ExcelFilterEditor({
3121
3322
  }),
3122
3323
  [draft, menu.valueLimit, messages.filterBlanks]
3123
3324
  );
3124
- useLayoutEffect9(() => {
3325
+ useLayoutEffect10(() => {
3125
3326
  if (selectAllRef.current !== null) {
3126
3327
  selectAllRef.current.indeterminate = selectAll === "mixed";
3127
3328
  }
3128
3329
  });
3129
- useLayoutEffect9(() => {
3130
- if (switched.current) {
3131
- switched.current = false;
3132
- (operator === null ? operatorButton.current : conditionsFocus.current)?.focus();
3330
+ useLayoutEffect10(() => {
3331
+ if (mode.kind === "condition") {
3332
+ conditionsFocus.current?.focus();
3333
+ } else if (mode.focusOnMount) {
3334
+ operatorButton.current?.focus();
3133
3335
  }
3134
- }, [operator]);
3135
- function switchTo(chosen) {
3136
- switched.current = true;
3137
- setOperator(chosen);
3138
- }
3336
+ }, [mode]);
3139
3337
  useImperativeHandle2(ref, () => ({
3140
3338
  apply: () => {
3141
- if (operator !== null) {
3339
+ if (mode.kind === "condition") {
3142
3340
  return conditions.current?.apply() ?? true;
3143
3341
  }
3144
- const picked = draft.checked !== opened.draft.checked || draft.includeBlanks !== opened.draft.includeBlanks;
3145
- if (picked) {
3342
+ if (filterDraftChanged3(opened.draft, draft)) {
3146
3343
  applyFilter(
3147
3344
  draftToFilter2(draft, column.id, controller.getState().filter)
3148
3345
  );
@@ -3150,7 +3347,7 @@ function ExcelFilterEditor({
3150
3347
  return true;
3151
3348
  }
3152
3349
  }));
3153
- if (operator !== null) {
3350
+ if (mode.kind === "condition") {
3154
3351
  return /* @__PURE__ */ jsxs15(Fragment7, { children: [
3155
3352
  /* @__PURE__ */ jsxs15(
3156
3353
  "button",
@@ -3158,7 +3355,7 @@ function ExcelFilterEditor({
3158
3355
  type: "button",
3159
3356
  className: "filter-menu-back",
3160
3357
  onClick: () => {
3161
- switchTo(null);
3358
+ setMode({ kind: "values", focusOnMount: true });
3162
3359
  },
3163
3360
  children: [
3164
3361
  /* @__PURE__ */ jsx20("span", { className: "filter-menu-back-icon", "aria-hidden": "true", children: /* @__PURE__ */ jsx20(ChevronLeftIcon2, {}) }),
@@ -3178,7 +3375,7 @@ function ExcelFilterEditor({
3178
3375
  conditionsFocus.current = element;
3179
3376
  },
3180
3377
  applyFilter,
3181
- operator
3378
+ operator: mode.operator
3182
3379
  }
3183
3380
  )
3184
3381
  ] });
@@ -3192,7 +3389,9 @@ function ExcelFilterEditor({
3192
3389
  menu,
3193
3390
  active: shape.kind === "conditions" ? shape.conditions.map((condition) => condition.operator) : void 0,
3194
3391
  messages,
3195
- onChoose: switchTo
3392
+ onChoose: (operator) => {
3393
+ setMode({ kind: "condition", operator });
3394
+ }
3196
3395
  }
3197
3396
  ),
3198
3397
  (shape.kind === "conditions" || shape.kind === "custom") && /* @__PURE__ */ jsx20("p", { className: "filter-menu-custom", children: shape.kind === "conditions" ? messages.filterByCondition : messages.customFilter }),
@@ -3260,10 +3459,10 @@ function FilterMenu({
3260
3459
  applyFilter,
3261
3460
  columnValues
3262
3461
  }) {
3263
- const editor = useRef11(null);
3462
+ const editor = useRef13(null);
3264
3463
  const column = target.area === "header" ? controller.getView().dataColumns.find((entry) => entry.id === target.columnId) : void 0;
3265
3464
  const menu = column === void 0 ? null : resolveColumnFilterMenu2(controller.getOptions(), column.column);
3266
- useEffect3(() => {
3465
+ useEffect4(() => {
3267
3466
  if (menu === null) {
3268
3467
  close();
3269
3468
  }
@@ -3364,23 +3563,29 @@ function FilterMenu({
3364
3563
  );
3365
3564
  }
3366
3565
 
3566
+ // src/DataGrid/useAnnounceWhenDrawn.ts
3567
+ import { useEffect as useEffect5, useState as useState10 } from "react";
3568
+ function useAnnounceWhenDrawn(announce) {
3569
+ const [requested, setRequested] = useState10(null);
3570
+ useEffect5(() => {
3571
+ if (requested?.drawn() === true) {
3572
+ announce(requested.text());
3573
+ }
3574
+ }, [requested, announce]);
3575
+ return setRequested;
3576
+ }
3577
+
3367
3578
  // src/DataGrid/filterMenu/useApplyFilter.ts
3368
- import { useEffect as useEffect4, useRef as useRef12 } from "react";
3369
3579
  function useApplyFilter(controller, messages, announce) {
3370
- const unannounced = useRef12(null);
3371
- useEffect4(() => {
3372
- if (unannounced.current === controller.getState().filter) {
3373
- unannounced.current = null;
3374
- announce(
3375
- messages.filterResults(
3376
- controller.getView().rows.length,
3377
- controller.getOptions().dataSource?.length ?? 0
3378
- )
3379
- );
3380
- }
3381
- });
3580
+ const announceWhenDrawn = useAnnounceWhenDrawn(announce);
3382
3581
  return (filter) => {
3383
- unannounced.current = filter;
3582
+ announceWhenDrawn({
3583
+ drawn: () => controller.getState().filter === filter,
3584
+ text: () => messages.filterResults(
3585
+ controller.getView().rows.length,
3586
+ controller.getOptions().dataSource?.length ?? 0
3587
+ )
3588
+ });
3384
3589
  controller.setFilter(filter);
3385
3590
  };
3386
3591
  }
@@ -3417,14 +3622,14 @@ function GridPanels({
3417
3622
  commands,
3418
3623
  messages
3419
3624
  }) {
3420
- const initialFocus = useRef13(null);
3625
+ const initialFocus = useRef14(null);
3421
3626
  const applyFilter = useApplyFilter(
3422
3627
  controller,
3423
3628
  messages,
3424
3629
  commands.host.announce
3425
3630
  );
3426
3631
  const columnValues = useFilterValueCache(controller, messages);
3427
- const { panel, close } = panels;
3632
+ const { panel, close, returnFocusFallback } = panels;
3428
3633
  if (panel === null) {
3429
3634
  return null;
3430
3635
  }
@@ -3437,6 +3642,7 @@ function GridPanels({
3437
3642
  gap: panel.anchor instanceof Element ? ANCHOR_GAP : 0,
3438
3643
  initialFocus,
3439
3644
  returnFocusTo: panel.returnFocusTo,
3645
+ returnFocusFallback,
3440
3646
  ignoreScrollAfterInput: true,
3441
3647
  trapFocus,
3442
3648
  themeClassName: panel.themeClassName,
@@ -3477,7 +3683,7 @@ function panelApi(panels, controller) {
3477
3683
  return true;
3478
3684
  },
3479
3685
  closeFilterMenu: () => {
3480
- if (panels.panel?.kind === "filter") {
3686
+ if (panels.current()?.kind === "filter") {
3481
3687
  panels.close();
3482
3688
  }
3483
3689
  }
@@ -3485,7 +3691,7 @@ function panelApi(panels, controller) {
3485
3691
  }
3486
3692
 
3487
3693
  // src/DataGrid/panels/useGridPanels.ts
3488
- import { useLayoutEffect as useLayoutEffect10, useRef as useRef14, useState as useState12 } from "react";
3694
+ import { useRef as useRef15 } from "react";
3489
3695
  function toolbarItem(root, itemId) {
3490
3696
  const items = root.querySelectorAll(
3491
3697
  ":scope > .gridkit-toolbar > [data-gridkit-toolbar-item]"
@@ -3497,6 +3703,9 @@ function toolbarItem(root, itemId) {
3497
3703
  (item) => item.hasAttribute("data-gridkit-toolbar-more")
3498
3704
  ) ?? null;
3499
3705
  }
3706
+ function panelOf(overlay) {
3707
+ return overlay.kind === "panel" ? overlay.panel : null;
3708
+ }
3500
3709
  function headerCell2(headerTable, columnId) {
3501
3710
  const cells = headerTable?.tHead?.rows[0]?.cells;
3502
3711
  return cells === void 0 ? null : Array.from(cells).find(
@@ -3506,10 +3715,9 @@ function headerCell2(headerTable, columnId) {
3506
3715
  function useGridPanels({
3507
3716
  rootRef,
3508
3717
  headerTableRef,
3509
- nav
3718
+ overlay
3510
3719
  }) {
3511
- const [panel, setPanel] = useState12(null);
3512
- const openings = useRef14(0);
3720
+ const openings = useRef15(0);
3513
3721
  function placeFor(root, target) {
3514
3722
  const element = target.area === "toolbar" ? toolbarItem(root, target.itemId) : target.area === "header" ? headerCell2(headerTableRef.current, target.columnId) : null;
3515
3723
  if (element !== null) {
@@ -3527,38 +3735,35 @@ function useGridPanels({
3527
3735
  returnFocusTo: void 0
3528
3736
  };
3529
3737
  }
3530
- const shown = useRef14(null);
3531
- useLayoutEffect10(() => {
3532
- const closed = shown.current;
3533
- shown.current = panel;
3534
- if (panel === null && closed?.returnFocusTo?.isConnected === false && document.activeElement === document.body) {
3535
- nav.focusTabStop();
3536
- }
3537
- });
3538
3738
  return {
3539
- panel,
3739
+ panel: panelOf(overlay.drawn),
3740
+ current: () => panelOf(overlay.current()),
3540
3741
  open: (kind, target) => {
3541
3742
  const root = rootRef.current;
3542
3743
  if (root === null) {
3543
3744
  return;
3544
3745
  }
3545
3746
  openings.current += 1;
3546
- setPanel({
3547
- key: openings.current,
3548
- kind,
3549
- target,
3550
- ...placeFor(root, target),
3551
- themeClassName: overlayThemeClassName(root)
3747
+ overlay.open({
3748
+ kind: "panel",
3749
+ panel: {
3750
+ key: openings.current,
3751
+ kind,
3752
+ target,
3753
+ ...placeFor(root, target),
3754
+ themeClassName: overlayThemeClassName(root)
3755
+ }
3552
3756
  });
3553
3757
  },
3554
3758
  close: () => {
3555
- setPanel(null);
3556
- }
3759
+ overlay.close("panel");
3760
+ },
3761
+ returnFocusFallback: overlay.returnFocusFallback
3557
3762
  };
3558
3763
  }
3559
3764
 
3560
3765
  // src/DataGrid/toolbar/GridToolbar.tsx
3561
- import { useLayoutEffect as useLayoutEffect13, useRef as useRef18 } from "react";
3766
+ import { useLayoutEffect as useLayoutEffect12, useRef as useRef18 } from "react";
3562
3767
  import {
3563
3768
  resolveToolbar,
3564
3769
  splitToolbarItems,
@@ -3567,57 +3772,29 @@ import {
3567
3772
  import { attachToolbar } from "@gridkitjs/dom";
3568
3773
 
3569
3774
  // src/DataGrid/toolbar/ToolbarMenuButton.tsx
3570
- import { useLayoutEffect as useLayoutEffect11, useRef as useRef15, useState as useState13 } from "react";
3571
3775
  import { Fragment as Fragment8, jsx as jsx23, jsxs as jsxs17 } from "react/jsx-runtime";
3572
3776
  function ToolbarMenuButton({
3573
3777
  itemKey,
3778
+ attributes,
3574
3779
  label,
3575
3780
  icon,
3576
3781
  entries,
3577
3782
  more = false,
3578
- onMenuClose
3783
+ returnFocusFallback
3579
3784
  }) {
3580
- const [menu, setMenu] = useState13(null);
3581
- const menuBehaviourRef = useRef15(null);
3582
- useLayoutEffect11(() => {
3583
- if (menu !== null && menu.focusItem > 0) {
3584
- menuBehaviourRef.current?.focusItem(menu.focusItem);
3585
- }
3586
- }, [menu]);
3587
- function open(button, onLast = false) {
3588
- setMenu({
3589
- anchor: button,
3590
- themeClassName: overlayThemeClassName(button),
3591
- focusItem: onLast ? entries.filter((entry) => entry.type !== "separator").length - 1 : 0
3592
- });
3593
- }
3594
- function close() {
3595
- setMenu(null);
3596
- onMenuClose();
3597
- }
3785
+ const { state, close, buttonProps, behaviourRef } = useMenuButton({
3786
+ itemCount: entries.filter((entry) => entry.type !== "separator").length
3787
+ });
3598
3788
  return /* @__PURE__ */ jsxs17(Fragment8, { children: [
3599
3789
  /* @__PURE__ */ jsxs17(
3600
3790
  "button",
3601
3791
  {
3792
+ ...attributes,
3602
3793
  type: "button",
3603
3794
  className: classNames("toolbar-button", more && "toolbar-more"),
3604
3795
  "data-gridkit-toolbar-item": itemKey,
3605
3796
  ...ariaAttr(more, "data-gridkit-toolbar-more", ""),
3606
- "aria-haspopup": "menu",
3607
- "aria-expanded": menu !== null,
3608
- onClick: (event) => {
3609
- if (menu === null) {
3610
- open(event.currentTarget);
3611
- } else {
3612
- close();
3613
- }
3614
- },
3615
- onKeyDown: (event) => {
3616
- if ((event.key === "ArrowDown" || event.key === "ArrowUp") && menu === null) {
3617
- event.preventDefault();
3618
- open(event.currentTarget, event.key === "ArrowUp");
3619
- }
3620
- },
3797
+ ...buttonProps,
3621
3798
  children: [
3622
3799
  icon !== void 0 && /* @__PURE__ */ jsx23("span", { className: "toolbar-button-icon", "aria-hidden": "true", children: /* @__PURE__ */ jsx23(MenuIcon, { name: icon }) }),
3623
3800
  label,
@@ -3625,22 +3802,23 @@ function ToolbarMenuButton({
3625
3802
  ]
3626
3803
  }
3627
3804
  ),
3628
- menu !== null && /* @__PURE__ */ jsx23(
3805
+ state.kind === "open" && /* @__PURE__ */ jsx23(
3629
3806
  Overlay,
3630
3807
  {
3631
- anchor: menu.anchor,
3808
+ anchor: state.anchor,
3632
3809
  placement: "bottom-start",
3633
- gap: ANCHOR_GAP,
3634
- returnFocusTo: menu.anchor,
3635
- themeClassName: menu.themeClassName,
3810
+ gap: state.gap,
3811
+ returnFocusTo: state.anchor,
3812
+ returnFocusFallback,
3813
+ themeClassName: state.themeClassName,
3636
3814
  onDismiss: close,
3637
3815
  children: /* @__PURE__ */ jsx23(
3638
3816
  GridMenu,
3639
3817
  {
3640
3818
  entries,
3641
3819
  label,
3642
- themeClassName: menu.themeClassName,
3643
- behaviourRef: menuBehaviourRef,
3820
+ themeClassName: state.themeClassName,
3821
+ behaviourRef,
3644
3822
  onRun: (item) => {
3645
3823
  item.run();
3646
3824
  close();
@@ -3654,7 +3832,7 @@ function ToolbarMenuButton({
3654
3832
  }
3655
3833
 
3656
3834
  // src/DataGrid/toolbar/ToolbarSearch.tsx
3657
- import { useEffect as useEffect5, useRef as useRef16, useState as useState14 } from "react";
3835
+ import { useEffect as useEffect6, useRef as useRef16, useState as useState12 } from "react";
3658
3836
  import {
3659
3837
  searchTextOf,
3660
3838
  setSearchText
@@ -3663,14 +3841,15 @@ import { jsx as jsx24 } from "react/jsx-runtime";
3663
3841
  var SEARCH_DELAY_MS = 250;
3664
3842
  function ToolbarSearch({
3665
3843
  itemKey,
3844
+ attributes,
3666
3845
  controller,
3667
3846
  messages,
3668
3847
  announce
3669
3848
  }) {
3670
3849
  const applied = searchTextOf(controller.getState().filter);
3671
- const [draft, setDraft] = useState14(null);
3850
+ const [draft, setDraft] = useState12(null);
3672
3851
  const timer = useRef16(void 0);
3673
- const unannounced = useRef16(null);
3852
+ const announceWhenDrawn = useAnnounceWhenDrawn(announce);
3674
3853
  const text = draft !== null && draft.over === applied ? draft.text : applied;
3675
3854
  function apply(next, over) {
3676
3855
  clearTimeout(timer.current);
@@ -3681,17 +3860,14 @@ function ToolbarSearch({
3681
3860
  }
3682
3861
  const searched = setSearchText(filter, next);
3683
3862
  if (searched !== filter) {
3684
- unannounced.current = next;
3863
+ announceWhenDrawn({
3864
+ drawn: () => searchTextOf(controller.getState().filter) === next,
3865
+ text: () => messages.searchResults(controller.getView().rows.length)
3866
+ });
3685
3867
  controller.setFilter(searched);
3686
3868
  }
3687
3869
  }
3688
- useEffect5(() => {
3689
- if (unannounced.current === applied) {
3690
- unannounced.current = null;
3691
- announce(messages.searchResults(controller.getView().rows.length));
3692
- }
3693
- });
3694
- useEffect5(
3870
+ useEffect6(
3695
3871
  () => () => {
3696
3872
  clearTimeout(timer.current);
3697
3873
  },
@@ -3700,6 +3876,7 @@ function ToolbarSearch({
3700
3876
  return /* @__PURE__ */ jsx24(
3701
3877
  "input",
3702
3878
  {
3879
+ ...attributes,
3703
3880
  type: "search",
3704
3881
  className: "toolbar-search",
3705
3882
  "data-gridkit-toolbar-item": itemKey,
@@ -3727,10 +3904,10 @@ function ToolbarSearch({
3727
3904
  }
3728
3905
 
3729
3906
  // src/DataGrid/toolbar/useToolbarOverflow.ts
3730
- import { useLayoutEffect as useLayoutEffect12, useRef as useRef17, useState as useState15 } from "react";
3907
+ import { useLayoutEffect as useLayoutEffect11, useRef as useRef17, useState as useState13 } from "react";
3731
3908
  import { observeToolbarOverflow } from "@gridkitjs/dom";
3732
3909
  function useToolbarOverflow(toolbarRef, items) {
3733
- const [fit, setFit] = useState15(items.length);
3910
+ const [fit, setFit] = useState13(items.length);
3734
3911
  const overflow = useRef17(null);
3735
3912
  const measuredLayout = useRef17("");
3736
3913
  const layout = JSON.stringify(
@@ -3740,7 +3917,7 @@ function useToolbarOverflow(toolbarRef, items) {
3740
3917
  "label" in item ? item.label : null
3741
3918
  ])
3742
3919
  );
3743
- useLayoutEffect12(() => {
3920
+ useLayoutEffect11(() => {
3744
3921
  const toolbar = toolbarRef.current;
3745
3922
  if (toolbar === null || layout === measuredLayout.current) {
3746
3923
  return;
@@ -3753,7 +3930,7 @@ function useToolbarOverflow(toolbarRef, items) {
3753
3930
  overflow.current.update(options);
3754
3931
  }
3755
3932
  }, [toolbarRef, layout, items]);
3756
- useLayoutEffect12(
3933
+ useLayoutEffect11(
3757
3934
  () => () => {
3758
3935
  overflow.current?.detach();
3759
3936
  overflow.current = null;
@@ -3817,12 +3994,21 @@ function GridToolbar(props) {
3817
3994
  if (entries.length === 0) {
3818
3995
  return null;
3819
3996
  }
3820
- const label = (typeof props.toolbar === "object" ? props.toolbar.label : void 0) ?? props.messages.toolbarLabel;
3821
- return /* @__PURE__ */ jsx25(Toolbar, { ...props, entries, label });
3997
+ const options = typeof props.toolbar === "object" ? props.toolbar : void 0;
3998
+ return /* @__PURE__ */ jsx25(
3999
+ Toolbar,
4000
+ {
4001
+ ...props,
4002
+ entries,
4003
+ label: options?.label ?? props.messages.toolbarLabel,
4004
+ itemAttributes: options?.itemAttributes
4005
+ }
4006
+ );
3822
4007
  }
3823
4008
  function Toolbar({
3824
4009
  entries,
3825
4010
  label,
4011
+ itemAttributes,
3826
4012
  renderItem,
3827
4013
  gridId,
3828
4014
  controller,
@@ -3831,10 +4017,10 @@ function Toolbar({
3831
4017
  announce
3832
4018
  }) {
3833
4019
  const toolbarRef = useRef18(null);
3834
- const menuClosed = useRef18(false);
3835
4020
  const items = resolveToolbar(entries, {
3836
4021
  commands: commands.commands,
3837
- contextFor: commands.contextFor
4022
+ contextFor: commands.contextFor,
4023
+ itemAttributes
3838
4024
  }).map(
3839
4025
  (item) => reportingItem(item, (commandId, itemId) => {
3840
4026
  commands.report(commandId, { area: "toolbar", itemId }, "toolbar");
@@ -3842,22 +4028,13 @@ function Toolbar({
3842
4028
  );
3843
4029
  const fit = useToolbarOverflow(toolbarRef, items);
3844
4030
  const { shown, overflow } = splitToolbarItems(items, fit);
3845
- useLayoutEffect13(() => {
4031
+ useLayoutEffect12(() => {
3846
4032
  const toolbar = toolbarRef.current;
3847
4033
  return toolbar === null ? void 0 : attachToolbar(toolbar);
3848
4034
  }, []);
3849
- useLayoutEffect13(() => {
3850
- if (!menuClosed.current) {
3851
- return;
3852
- }
3853
- menuClosed.current = false;
4035
+ function toolbarTabStop() {
3854
4036
  const toolbar = toolbarRef.current;
3855
- if (toolbar !== null && document.activeElement === document.body) {
3856
- (toolbar.querySelector('[tabindex="0"]') ?? toolbar.querySelector("button, input"))?.focus();
3857
- }
3858
- });
3859
- function onMenuClose() {
3860
- menuClosed.current = true;
4037
+ return toolbar?.querySelector('[tabindex="0"]') ?? toolbar?.querySelector("button, input") ?? null;
3861
4038
  }
3862
4039
  function draw(item) {
3863
4040
  switch (item.type) {
@@ -3865,6 +4042,7 @@ function Toolbar({
3865
4042
  return /* @__PURE__ */ jsxs18(
3866
4043
  "button",
3867
4044
  {
4045
+ ...item.attributes,
3868
4046
  type: "button",
3869
4047
  className: "toolbar-button",
3870
4048
  "data-gridkit-toolbar-item": item.key,
@@ -3894,10 +4072,11 @@ function Toolbar({
3894
4072
  ToolbarMenuButton,
3895
4073
  {
3896
4074
  itemKey: item.key,
4075
+ attributes: item.attributes,
3897
4076
  label: item.label,
3898
4077
  icon: item.icon,
3899
4078
  entries: item.items,
3900
- onMenuClose
4079
+ returnFocusFallback: toolbarTabStop
3901
4080
  },
3902
4081
  item.key
3903
4082
  );
@@ -3906,6 +4085,7 @@ function Toolbar({
3906
4085
  ToolbarSearch,
3907
4086
  {
3908
4087
  itemKey: item.key,
4088
+ attributes: item.attributes,
3909
4089
  controller,
3910
4090
  messages,
3911
4091
  announce
@@ -3936,6 +4116,7 @@ function Toolbar({
3936
4116
  return /* @__PURE__ */ jsx25(
3937
4117
  "div",
3938
4118
  {
4119
+ ...item.attributes,
3939
4120
  className: "toolbar-custom",
3940
4121
  "data-gridkit-toolbar-item": item.key,
3941
4122
  children: renderItem?.(item.id)
@@ -3961,7 +4142,7 @@ function Toolbar({
3961
4142
  label: messages.moreToolbarItems,
3962
4143
  entries: overflow,
3963
4144
  more: true,
3964
- onMenuClose
4145
+ returnFocusFallback: toolbarTabStop
3965
4146
  }
3966
4147
  )
3967
4148
  ]
@@ -4010,7 +4191,7 @@ function useColumnDrag({
4010
4191
 
4011
4192
  // src/DataGrid/useColumnResize.ts
4012
4193
  import {
4013
- useState as useState16
4194
+ useState as useState14
4014
4195
  } from "react";
4015
4196
  import {
4016
4197
  beginColumnResize,
@@ -4024,7 +4205,8 @@ function useColumnResize({
4024
4205
  controller,
4025
4206
  columnSizeDefaults
4026
4207
  }) {
4027
- const [activeColumnId, setActiveColumnId] = useState16(null);
4208
+ const [activeColumnId, setActiveColumnId] = useState14(null);
4209
+ const startGesture = useGestureSignal();
4028
4210
  function startResize(entry, event) {
4029
4211
  event.stopPropagation();
4030
4212
  const session = beginColumnResize(
@@ -4034,11 +4216,10 @@ function useColumnResize({
4034
4216
  columnSizeDefaults
4035
4217
  );
4036
4218
  const base = controller.getState().columnSizing;
4037
- const started = startColumnResizeGesture(
4038
- event.currentTarget,
4039
- event,
4040
- session,
4041
- {
4219
+ const handle = event.currentTarget;
4220
+ const started = startGesture(
4221
+ handle,
4222
+ (signal) => startColumnResizeGesture(handle, event, session, {
4042
4223
  onMove(width) {
4043
4224
  controller.resizeColumn(session.columnId, width, "move");
4044
4225
  },
@@ -4062,8 +4243,9 @@ function useColumnResize({
4062
4243
  base,
4063
4244
  session.startWidth
4064
4245
  );
4065
- }
4066
- }
4246
+ },
4247
+ signal
4248
+ })
4067
4249
  );
4068
4250
  if (started) {
4069
4251
  setActiveColumnId(session.columnId);
@@ -4102,11 +4284,11 @@ function useColumnResize({
4102
4284
  }
4103
4285
 
4104
4286
  // src/DataGrid/useElementWidth.ts
4105
- import { useLayoutEffect as useLayoutEffect14, useState as useState17 } from "react";
4287
+ import { useLayoutEffect as useLayoutEffect13, useState as useState15 } from "react";
4106
4288
  import { observeElementWidth } from "@gridkitjs/dom";
4107
4289
  function useElementWidth(ref, enabled) {
4108
- const [width, setWidth] = useState17(null);
4109
- useLayoutEffect14(() => {
4290
+ const [width, setWidth] = useState15(null);
4291
+ useLayoutEffect13(() => {
4110
4292
  const element = ref.current;
4111
4293
  if (element === null || !enabled) {
4112
4294
  return void 0;
@@ -4117,12 +4299,12 @@ function useElementWidth(ref, enabled) {
4117
4299
  }
4118
4300
 
4119
4301
  // src/DataGrid/useGridController.ts
4120
- import { useState as useState18, useSyncExternalStore as useSyncExternalStore2 } from "react";
4302
+ import { useState as useState16, useSyncExternalStore as useSyncExternalStore2 } from "react";
4121
4303
  import {
4122
4304
  createGridController
4123
4305
  } from "@gridkitjs/core";
4124
4306
  function useGridController(options) {
4125
- const [controller] = useState18(
4307
+ const [controller] = useState16(
4126
4308
  () => createGridController(options)
4127
4309
  );
4128
4310
  controller.setOptions(options);
@@ -4135,7 +4317,7 @@ function useGridController(options) {
4135
4317
  }
4136
4318
 
4137
4319
  // src/DataGrid/useInfiniteScroll.ts
4138
- import { useEffect as useEffect6, useRef as useRef19 } from "react";
4320
+ import { useEffect as useEffect7, useRef as useRef19 } from "react";
4139
4321
  import { attachInfiniteScroll } from "@gridkitjs/dom";
4140
4322
  function useInfiniteScroll({
4141
4323
  rootRef,
@@ -4143,7 +4325,7 @@ function useInfiniteScroll({
4143
4325
  config
4144
4326
  }) {
4145
4327
  const scroll = useRef19(null);
4146
- useEffect6(() => {
4328
+ useEffect7(() => {
4147
4329
  const root = rootRef.current;
4148
4330
  if (root === null) {
4149
4331
  return void 0;
@@ -4158,7 +4340,7 @@ function useInfiniteScroll({
4158
4340
  scroll.current = null;
4159
4341
  };
4160
4342
  }, [rootRef]);
4161
- useEffect6(() => {
4343
+ useEffect7(() => {
4162
4344
  scroll.current?.update({
4163
4345
  sentinel: sentinelRef.current,
4164
4346
  hasMore: config?.hasMore ?? false,
@@ -4170,11 +4352,11 @@ function useInfiniteScroll({
4170
4352
  }
4171
4353
 
4172
4354
  // src/DataGrid/useLiveAnnouncer.ts
4173
- import { useCallback, useLayoutEffect as useLayoutEffect15, useRef as useRef20 } from "react";
4355
+ import { useCallback as useCallback3, useLayoutEffect as useLayoutEffect14, useRef as useRef20 } from "react";
4174
4356
  import { createLiveAnnouncer } from "@gridkitjs/dom";
4175
4357
  function useLiveAnnouncer(containerRef) {
4176
4358
  const announcer = useRef20(null);
4177
- useLayoutEffect15(() => {
4359
+ useLayoutEffect14(() => {
4178
4360
  const container = containerRef.current;
4179
4361
  if (container === null) {
4180
4362
  return void 0;
@@ -4186,13 +4368,13 @@ function useLiveAnnouncer(containerRef) {
4186
4368
  announcer.current = null;
4187
4369
  };
4188
4370
  }, [containerRef]);
4189
- return useCallback((text) => {
4371
+ return useCallback3((text) => {
4190
4372
  announcer.current?.announce(text);
4191
4373
  }, []);
4192
4374
  }
4193
4375
 
4194
4376
  // src/DataGrid/useRowVirtualizer.ts
4195
- import { useLayoutEffect as useLayoutEffect16, useState as useState19 } from "react";
4377
+ import { useLayoutEffect as useLayoutEffect15, useState as useState17 } from "react";
4196
4378
  import { createRowVirtualizer as createRowVirtualizer2 } from "@gridkitjs/dom";
4197
4379
  var EMPTY_RANGE = {
4198
4380
  startIndex: 0,
@@ -4210,9 +4392,9 @@ function useRowVirtualizer({
4210
4392
  estimatedRowHeight,
4211
4393
  overscan
4212
4394
  }) {
4213
- const [virtualizer, setVirtualizer] = useState19(null);
4214
- const [range, setRange] = useState19(EMPTY_RANGE);
4215
- useLayoutEffect16(() => {
4395
+ const [virtualizer, setVirtualizer] = useState17(null);
4396
+ const [range, setRange] = useState17(EMPTY_RANGE);
4397
+ useLayoutEffect15(() => {
4216
4398
  const element = bodyScrollRef.current;
4217
4399
  if (!enabled || element === null) {
4218
4400
  return void 0;
@@ -4227,7 +4409,7 @@ function useRowVirtualizer({
4227
4409
  created.destroy();
4228
4410
  };
4229
4411
  }, [enabled, bodyScrollRef, estimatedRowHeight, overscan]);
4230
- useLayoutEffect16(() => {
4412
+ useLayoutEffect15(() => {
4231
4413
  virtualizer?.setRowCount(rows.length);
4232
4414
  virtualizer?.resetHeights();
4233
4415
  }, [virtualizer, rows]);
@@ -4302,6 +4484,7 @@ function DataGrid(props) {
4302
4484
  const hoverColumns = hoverable?.columns ?? true;
4303
4485
  const hoverCells = hoverable?.cells ?? true;
4304
4486
  const viewportRef = useRef21(null);
4487
+ const gridRef = useRef21(null);
4305
4488
  const headerTableRef = useRef21(null);
4306
4489
  const bodyTableRef = useRef21(null);
4307
4490
  const bodyScrollRef = useRef21(null);
@@ -4325,8 +4508,8 @@ function DataGrid(props) {
4325
4508
  columnOf
4326
4509
  })
4327
4510
  });
4328
- const [readPagination] = useState20(() => paginationReader(controller));
4329
- const [readSelectedRows] = useState20(() => selectedRowsReader(controller));
4511
+ const [readPagination] = useState18(() => paginationReader(controller));
4512
+ const [readSelectedRows] = useState18(() => selectedRowsReader(controller));
4330
4513
  const virtualizer = useRowVirtualizer({
4331
4514
  enabled: virtualized,
4332
4515
  rows: view.pageRows,
@@ -4334,7 +4517,7 @@ function DataGrid(props) {
4334
4517
  estimatedRowHeight,
4335
4518
  overscan
4336
4519
  });
4337
- useEffect7(() => {
4520
+ useEffect8(() => {
4338
4521
  if (virtualized && height === void 0) {
4339
4522
  console.error(
4340
4523
  "DataGrid: `virtualized` has no effect without `height` \u2014 a body with no bounded height has no viewport to window rows against."
@@ -4342,7 +4525,7 @@ function DataGrid(props) {
4342
4525
  }
4343
4526
  }, [virtualized, height]);
4344
4527
  const hasInfiniteScroll = infiniteScroll !== void 0;
4345
- useEffect7(() => {
4528
+ useEffect8(() => {
4346
4529
  if (paginated && hasInfiniteScroll) {
4347
4530
  console.error(
4348
4531
  "DataGrid: `paginated` and `infiniteScroll` are mutually exclusive \u2014 only `paginated` takes effect while both are set."
@@ -4371,6 +4554,7 @@ function DataGrid(props) {
4371
4554
  columnSizeDefaults
4372
4555
  });
4373
4556
  const nav = useGridNavigation({
4557
+ gridRef,
4374
4558
  headerTableRef,
4375
4559
  bodyTableRef,
4376
4560
  // `null` when virtualization is off, in which case behavior is
@@ -4393,7 +4577,15 @@ function DataGrid(props) {
4393
4577
  return entry !== void 0 && "kind" in entry && entry.kind === "group-summary";
4394
4578
  }
4395
4579
  });
4396
- const panels = useGridPanels({ rootRef: viewportRef, headerTableRef, nav });
4580
+ const overlay = useGridOverlay({
4581
+ nav,
4582
+ onContextMenuOpenChange: props.onContextMenuOpenChange
4583
+ });
4584
+ const panels = useGridPanels({
4585
+ rootRef: viewportRef,
4586
+ headerTableRef,
4587
+ overlay
4588
+ });
4397
4589
  const commands = useGridCommands({
4398
4590
  controller,
4399
4591
  commands: props.commands,
@@ -4467,7 +4659,7 @@ function DataGrid(props) {
4467
4659
  bodyTableRef,
4468
4660
  bodyScrollRef,
4469
4661
  contextMenu: props.contextMenu,
4470
- onContextMenuOpenChange: props.onContextMenuOpenChange,
4662
+ overlay,
4471
4663
  controller,
4472
4664
  commands,
4473
4665
  nav,
@@ -4475,7 +4667,7 @@ function DataGrid(props) {
4475
4667
  messages,
4476
4668
  columnName
4477
4669
  });
4478
- useEffect7(() => {
4670
+ useEffect8(() => {
4479
4671
  for (const listener of subscribersRef.current) {
4480
4672
  listener();
4481
4673
  }
@@ -4620,6 +4812,7 @@ function DataGrid(props) {
4620
4812
  /* @__PURE__ */ jsxs19(
4621
4813
  "div",
4622
4814
  {
4815
+ ref: gridRef,
4623
4816
  id: gridId,
4624
4817
  className: "gridkit-data-grid-tables",
4625
4818
  role: state.groupBy.length > 0 ? "treegrid" : "grid",
@@ -4770,23 +4963,41 @@ function DataGrid(props) {
4770
4963
  import { serializeGridState } from "@gridkitjs/core";
4771
4964
 
4772
4965
  // src/DataGrid/hooks/usePaginationState.ts
4773
- import { useCallback as useCallback3 } from "react";
4966
+ import { useCallback as useCallback5 } from "react";
4774
4967
 
4775
4968
  // src/DataGrid/hooks/useGridSnapshot.ts
4776
- import { useCallback as useCallback2, useSyncExternalStore as useSyncExternalStore3 } from "react";
4969
+ import {
4970
+ useCallback as useCallback4,
4971
+ useEffect as useEffect9,
4972
+ useRef as useRef22,
4973
+ useSyncExternalStore as useSyncExternalStore3
4974
+ } from "react";
4975
+ function subscribeTo(api, listener) {
4976
+ return api === null ? { kind: "waiting", listener } : {
4977
+ kind: "subscribed",
4978
+ listener,
4979
+ api,
4980
+ unsubscribe: api.subscribe(listener)
4981
+ };
4982
+ }
4983
+ function unsubscribe(subscription) {
4984
+ if (subscription?.kind === "subscribed") {
4985
+ subscription.unsubscribe();
4986
+ }
4987
+ }
4777
4988
  function useGridSnapshot(gridRef, getSnapshot, fallback) {
4778
- const subscribe = useCallback2(
4989
+ const subscription = useRef22(null);
4990
+ const subscribe = useCallback4(
4779
4991
  (listener) => {
4780
- const api = gridRef.current;
4781
- if (api === null) {
4782
- return () => {
4783
- };
4784
- }
4785
- return api.subscribe(listener);
4992
+ subscription.current = subscribeTo(gridRef.current, listener);
4993
+ return () => {
4994
+ unsubscribe(subscription.current);
4995
+ subscription.current = null;
4996
+ };
4786
4997
  },
4787
4998
  [gridRef]
4788
4999
  );
4789
- return useSyncExternalStore3(
5000
+ const snapshot = useSyncExternalStore3(
4790
5001
  subscribe,
4791
5002
  () => {
4792
5003
  const api = gridRef.current;
@@ -4794,6 +5005,20 @@ function useGridSnapshot(gridRef, getSnapshot, fallback) {
4794
5005
  },
4795
5006
  () => fallback
4796
5007
  );
5008
+ useEffect9(() => {
5009
+ const current = subscription.current;
5010
+ if (current === null) {
5011
+ return;
5012
+ }
5013
+ const api = gridRef.current;
5014
+ if (api === (current.kind === "subscribed" ? current.api : null)) {
5015
+ return;
5016
+ }
5017
+ unsubscribe(current);
5018
+ subscription.current = subscribeTo(api, current.listener);
5019
+ current.listener();
5020
+ });
5021
+ return snapshot;
4797
5022
  }
4798
5023
 
4799
5024
  // src/DataGrid/hooks/usePaginationState.ts
@@ -4801,12 +5026,12 @@ var FALLBACK_PAGINATION = { pageIndex: 0, pageSize: 0 };
4801
5026
  function usePaginationState(gridRef) {
4802
5027
  const pagination = useGridSnapshot(
4803
5028
  gridRef,
4804
- useCallback3((api) => api.getPagination(), []),
5029
+ useCallback5((api) => api.getPagination(), []),
4805
5030
  FALLBACK_PAGINATION
4806
5031
  );
4807
5032
  const pageCount = useGridSnapshot(
4808
5033
  gridRef,
4809
- useCallback3((api) => api.getPageCount(), []),
5034
+ useCallback5((api) => api.getPageCount(), []),
4810
5035
  0
4811
5036
  );
4812
5037
  return {
@@ -4828,18 +5053,18 @@ function usePaginationState(gridRef) {
4828
5053
  }
4829
5054
 
4830
5055
  // src/DataGrid/hooks/useGroupByState.ts
4831
- import { useCallback as useCallback4 } from "react";
5056
+ import { useCallback as useCallback6 } from "react";
4832
5057
  var FALLBACK_GROUP_BY = [];
4833
5058
  var FALLBACK_EXPANSION = [];
4834
5059
  function useGroupByState(gridRef) {
4835
5060
  const groupBy = useGridSnapshot(
4836
5061
  gridRef,
4837
- useCallback4((api) => api.getGroupBy(), []),
5062
+ useCallback6((api) => api.getGroupBy(), []),
4838
5063
  FALLBACK_GROUP_BY
4839
5064
  );
4840
5065
  const groupExpansion = useGridSnapshot(
4841
5066
  gridRef,
4842
- useCallback4((api) => api.getGroupExpansion(), []),
5067
+ useCallback6((api) => api.getGroupExpansion(), []),
4843
5068
  FALLBACK_EXPANSION
4844
5069
  );
4845
5070
  return {
@@ -4855,34 +5080,34 @@ function useGroupByState(gridRef) {
4855
5080
  }
4856
5081
 
4857
5082
  // src/DataGrid/hooks/useColumnSortState.ts
4858
- import { useCallback as useCallback5 } from "react";
5083
+ import { useCallback as useCallback7 } from "react";
4859
5084
  var FALLBACK_SORT = [];
4860
5085
  function useColumnSortState(gridRef) {
4861
5086
  return useGridSnapshot(
4862
5087
  gridRef,
4863
- useCallback5((api) => api.getColumnSort(), []),
5088
+ useCallback7((api) => api.getColumnSort(), []),
4864
5089
  FALLBACK_SORT
4865
5090
  );
4866
5091
  }
4867
5092
 
4868
5093
  // src/DataGrid/hooks/useSelectionState.ts
4869
- import { useCallback as useCallback6 } from "react";
5094
+ import { useCallback as useCallback8 } from "react";
4870
5095
  var FALLBACK_SELECTION = [];
4871
5096
  var FALLBACK_CELL_SELECTION = null;
4872
5097
  function useSelectionState(gridRef) {
4873
5098
  const rowSelection = useGridSnapshot(
4874
5099
  gridRef,
4875
- useCallback6((api) => api.getRowSelection(), []),
5100
+ useCallback8((api) => api.getRowSelection(), []),
4876
5101
  FALLBACK_SELECTION
4877
5102
  );
4878
5103
  const columnSelection = useGridSnapshot(
4879
5104
  gridRef,
4880
- useCallback6((api) => api.getColumnSelection(), []),
5105
+ useCallback8((api) => api.getColumnSelection(), []),
4881
5106
  FALLBACK_SELECTION
4882
5107
  );
4883
5108
  const cellSelection = useGridSnapshot(
4884
5109
  gridRef,
4885
- useCallback6((api) => api.getCellSelection(), []),
5110
+ useCallback8((api) => api.getCellSelection(), []),
4886
5111
  FALLBACK_CELL_SELECTION
4887
5112
  );
4888
5113
  return {
@@ -4899,56 +5124,56 @@ function useSelectionState(gridRef) {
4899
5124
  }
4900
5125
 
4901
5126
  // src/DataGrid/hooks/useAggregateState.ts
4902
- import { useCallback as useCallback7 } from "react";
5127
+ import { useCallback as useCallback9 } from "react";
4903
5128
  var FALLBACK_AGGREGATES = /* @__PURE__ */ new Map();
4904
5129
  function useAggregateState(gridRef) {
4905
5130
  return useGridSnapshot(
4906
5131
  gridRef,
4907
- useCallback7((api) => api.getAggregates(), []),
5132
+ useCallback9((api) => api.getAggregates(), []),
4908
5133
  FALLBACK_AGGREGATES
4909
5134
  );
4910
5135
  }
4911
5136
 
4912
5137
  // src/DataGrid/hooks/useColumnSizingState.ts
4913
- import { useCallback as useCallback8 } from "react";
5138
+ import { useCallback as useCallback10 } from "react";
4914
5139
  var FALLBACK_SIZING = {};
4915
5140
  function useColumnSizingState(gridRef) {
4916
5141
  return useGridSnapshot(
4917
5142
  gridRef,
4918
- useCallback8((api) => api.getColumnSizing(), []),
5143
+ useCallback10((api) => api.getColumnSizing(), []),
4919
5144
  FALLBACK_SIZING
4920
5145
  );
4921
5146
  }
4922
5147
 
4923
5148
  // src/DataGrid/hooks/useColumnOrderState.ts
4924
- import { useCallback as useCallback9 } from "react";
5149
+ import { useCallback as useCallback11 } from "react";
4925
5150
  var FALLBACK_ORDER = [];
4926
5151
  function useColumnOrderState(gridRef) {
4927
5152
  return useGridSnapshot(
4928
5153
  gridRef,
4929
- useCallback9((api) => api.getColumnOrder(), []),
5154
+ useCallback11((api) => api.getColumnOrder(), []),
4930
5155
  FALLBACK_ORDER
4931
5156
  );
4932
5157
  }
4933
5158
 
4934
5159
  // src/DataGrid/hooks/useColumnVisibilityState.ts
4935
- import { useCallback as useCallback10 } from "react";
5160
+ import { useCallback as useCallback12 } from "react";
4936
5161
  var FALLBACK_VISIBILITY = {};
4937
5162
  function useColumnVisibilityState(gridRef) {
4938
5163
  return useGridSnapshot(
4939
5164
  gridRef,
4940
- useCallback10((api) => api.getColumnVisibility(), []),
5165
+ useCallback12((api) => api.getColumnVisibility(), []),
4941
5166
  FALLBACK_VISIBILITY
4942
5167
  );
4943
5168
  }
4944
5169
 
4945
5170
  // src/DataGrid/hooks/useFilterState.ts
4946
- import { useCallback as useCallback11, useState as useState21 } from "react";
5171
+ import { useCallback as useCallback13, useState as useState19 } from "react";
4947
5172
  function useFilterState(gridRef) {
4948
- const [fallback] = useState21(() => []);
5173
+ const [fallback] = useState19(() => []);
4949
5174
  const filter = useGridSnapshot(
4950
5175
  gridRef,
4951
- useCallback11((api) => api.getFilter(), []),
5176
+ useCallback13((api) => api.getFilter(), []),
4952
5177
  fallback
4953
5178
  );
4954
5179
  return {