@gridkitjs/react 0.15.0 → 0.17.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,14 +2,16 @@
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
- useRef as useRef21,
10
- useState as useState20
9
+ useRef as useRef23,
10
+ useState as useState18
11
11
  } from "react";
12
12
  import {
13
+ bodyFocus as bodyFocus3,
14
+ headerFocus as headerFocus3,
13
15
  moveColumnBefore,
14
16
  resolveColumnHeaderText as resolveColumnHeaderText2,
15
17
  resolveMessages,
@@ -27,6 +29,7 @@ function GridColgroup({ columns }) {
27
29
  import { createPortal } from "react-dom";
28
30
  import {
29
31
  buildKeyShortcuts,
32
+ headerFocus,
30
33
  intentOf,
31
34
  isColumnFiltered,
32
35
  KEYBOARD_STEP,
@@ -105,78 +108,6 @@ function isOwnEvent(event) {
105
108
  return event.target instanceof Node && event.currentTarget.contains(event.target);
106
109
  }
107
110
 
108
- // src/DataGrid/useGridNavigation.ts
109
- import {
110
- useEffect,
111
- useLayoutEffect,
112
- useState,
113
- useSyncExternalStore
114
- } from "react";
115
- import { HEADER_ROW } from "@gridkitjs/core";
116
- import {
117
- createGridFocus,
118
- findBodyRow,
119
- measurePageSize
120
- } from "@gridkitjs/dom";
121
- function tabIndexFor(columnIndex, focusedColumnIndex) {
122
- return columnIndex === focusedColumnIndex ? 0 : -1;
123
- }
124
- function useGridNavigation({
125
- headerTableRef,
126
- bodyTableRef,
127
- rowCount,
128
- columnCount,
129
- isSkippableRow,
130
- virtualRange
131
- }) {
132
- const options = {
133
- rowCount,
134
- columnCount,
135
- isSkippableRow,
136
- virtualRange
137
- };
138
- const [gridFocus] = useState(() => createGridFocus(options));
139
- gridFocus.setOptions(options);
140
- const focus = useSyncExternalStore(
141
- gridFocus.subscribe,
142
- gridFocus.getFocus,
143
- gridFocus.getFocus
144
- );
145
- const [rows] = useState(() => ({
146
- getHeaderRow: () => headerTableRef.current?.tHead?.rows[0] ?? null,
147
- getBodyRow: (rowIndex) => findBodyRow(bodyTableRef.current, rowIndex)
148
- }));
149
- useLayoutEffect(() => gridFocus.attach(rows), [gridFocus, rows]);
150
- useEffect(() => {
151
- gridFocus.focusDrawnCell(focus);
152
- });
153
- return {
154
- focus,
155
- tabIndexFor: (rowIndex, columnIndex) => tabIndexFor(
156
- columnIndex,
157
- rowIndex === focus.rowIndex ? focus.columnIndex : null
158
- ),
159
- focusCell: gridFocus.focusCell,
160
- focusCellNow: (rowIndex, columnIndex) => {
161
- gridFocus.focusCell(rowIndex, columnIndex);
162
- gridFocus.focusDrawnCell(gridFocus.getFocus());
163
- },
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();
167
- },
168
- onKeyDown: (event) => {
169
- if (gridFocus.handleKey(
170
- event.key,
171
- event,
172
- measurePageSize(bodyTableRef.current)
173
- )) {
174
- event.preventDefault();
175
- }
176
- }
177
- };
178
- }
179
-
180
111
  // src/DataGrid/useGridSelection.ts
181
112
  import { useMemo } from "react";
182
113
  function keyboardSelectIntent(event) {
@@ -236,7 +167,7 @@ function GridHeader({
236
167
  const beforeId = drag.dropTarget?.kind === "column-order" ? drag.dropTarget.beforeId : void 0;
237
168
  const draggedEntry = columns.find((entry) => entry.id === drag.draggedColumnId) ?? null;
238
169
  function toggleFilterMenu(columnId) {
239
- const { panel } = panels;
170
+ const panel = panels.current();
240
171
  if (panel?.kind === "filter" && panel.target.area === "header" && panel.target.columnId === columnId) {
241
172
  panels.close();
242
173
  } else {
@@ -276,17 +207,17 @@ function GridHeader({
276
207
  role: "columnheader",
277
208
  "data-gridkit-column": entry.id,
278
209
  "aria-colindex": index + 1,
279
- tabIndex: nav.tabIndexFor(HEADER_ROW, index),
210
+ tabIndex: nav.tabIndexFor(headerFocus(index)),
280
211
  ...ariaAttr(
281
212
  selection.columnMode !== false,
282
213
  "aria-selected",
283
214
  selected
284
215
  ),
285
216
  onFocus: () => {
286
- nav.focusCell(HEADER_ROW, index);
217
+ nav.focusCell(headerFocus(index));
287
218
  },
288
219
  onClick: (event) => {
289
- if (drag.justDragged()) {
220
+ if (drag.justDragged(entry.id)) {
290
221
  return;
291
222
  }
292
223
  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)) {
@@ -503,6 +434,7 @@ import {
503
434
  useState as useState2
504
435
  } from "react";
505
436
  import {
437
+ bodyFocus,
506
438
  groupRowId,
507
439
  intentOf as intentOf2
508
440
  } from "@gridkitjs/core";
@@ -512,6 +444,88 @@ import {
512
444
  groupRowFrom
513
445
  } from "@gridkitjs/dom";
514
446
 
447
+ // src/DataGrid/useGridNavigation.ts
448
+ import {
449
+ useEffect,
450
+ useLayoutEffect,
451
+ useState,
452
+ useSyncExternalStore
453
+ } from "react";
454
+ import {
455
+ createGridFocus,
456
+ findBodyRow,
457
+ measurePageSize
458
+ } from "@gridkitjs/dom";
459
+ function tabIndexFor(columnIndex, focusedColumnIndex) {
460
+ return columnIndex === focusedColumnIndex ? 0 : -1;
461
+ }
462
+ function focusedColumnInRow(focus, rowIndex) {
463
+ return focus.area === "body" && focus.rowIndex === rowIndex ? focus.columnIndex : null;
464
+ }
465
+ function sameCell(a, b) {
466
+ if (a.columnIndex !== b.columnIndex) {
467
+ return false;
468
+ }
469
+ return a.area === "header" ? b.area === "header" : focusedColumnInRow(b, a.rowIndex) !== null;
470
+ }
471
+ function useGridNavigation({
472
+ gridRef,
473
+ headerTableRef,
474
+ bodyTableRef,
475
+ rowCount,
476
+ columnCount,
477
+ isSkippableRow,
478
+ virtualRange
479
+ }) {
480
+ const options = {
481
+ rowCount,
482
+ columnCount,
483
+ isSkippableRow,
484
+ virtualRange
485
+ };
486
+ const [gridFocus] = useState(() => createGridFocus(options));
487
+ gridFocus.setOptions(options);
488
+ const focus = useSyncExternalStore(
489
+ gridFocus.subscribe,
490
+ gridFocus.getFocus,
491
+ gridFocus.getFocus
492
+ );
493
+ const [rows] = useState(() => ({
494
+ getHeaderRow: () => headerTableRef.current?.tHead?.rows[0] ?? null,
495
+ getBodyRow: (rowIndex) => findBodyRow(bodyTableRef.current, rowIndex)
496
+ }));
497
+ useLayoutEffect(
498
+ () => gridFocus.attach(rows, gridRef.current),
499
+ [gridFocus, rows, gridRef]
500
+ );
501
+ useEffect(() => {
502
+ gridFocus.focusDrawnCell(focus);
503
+ });
504
+ return {
505
+ focus,
506
+ tabIndexFor: (cell) => sameCell(cell, focus) ? 0 : -1,
507
+ focusCell: gridFocus.focusCell,
508
+ focusCellNow: (next) => {
509
+ gridFocus.focusCell(next);
510
+ gridFocus.focusDrawnCell(gridFocus.getFocus());
511
+ },
512
+ tabStopCell: () => {
513
+ const stop = gridFocus.getFocus();
514
+ const cells = (stop.area === "header" ? rows.getHeaderRow() : rows.getBodyRow(stop.rowIndex))?.cells;
515
+ return cells?.[Math.min(stop.columnIndex, cells.length - 1)] ?? null;
516
+ },
517
+ onKeyDown: (event) => {
518
+ if (gridFocus.handleKey(
519
+ event.key,
520
+ event,
521
+ measurePageSize(bodyTableRef.current)
522
+ )) {
523
+ event.preventDefault();
524
+ }
525
+ }
526
+ };
527
+ }
528
+
515
529
  // src/DataGrid/rowProps.ts
516
530
  var NO_ATTRIBUTES = {};
517
531
  var NO_ROW_PROPS = {
@@ -931,12 +945,14 @@ function GridBody({
931
945
  onFocus: (event) => {
932
946
  const group = groupRowFrom(event.target);
933
947
  if (group !== null) {
934
- nav.focusCell(group.rowIndex, 0);
948
+ nav.focusCell(bodyFocus(group.rowIndex, 0));
935
949
  return;
936
950
  }
937
951
  const found = targetOf(event.target);
938
952
  if (found === null) return;
939
- nav.focusCell(found.position.rowIndex, found.position.columnIndex);
953
+ nav.focusCell(
954
+ bodyFocus(found.position.rowIndex, found.position.columnIndex)
955
+ );
940
956
  },
941
957
  onClick: (event) => {
942
958
  const group = groupRowFrom(event.target);
@@ -1015,7 +1031,7 @@ function GridBody({
1015
1031
  selectedColumnIds: selection.selectedColumnIds,
1016
1032
  selected: selection.selectedRowIds.has(entry.rowId),
1017
1033
  selectedColumnId: selectedCell?.rowId === entry.rowId ? selectedCell.columnId : null,
1018
- focusedColumnIndex: nav.focus.rowIndex === entry.rowIndex ? nav.focus.columnIndex : null,
1034
+ focusedColumnIndex: focusedColumnInRow(nav.focus, entry.rowIndex),
1019
1035
  rowsSelectable: rowMode !== false,
1020
1036
  cellsSelectable: cellMode !== false,
1021
1037
  contextMenu: cellMenu,
@@ -1064,7 +1080,7 @@ function GridBody({
1064
1080
  definitions,
1065
1081
  locale,
1066
1082
  cellMessages,
1067
- focused: nav.focus.rowIndex === entry.rowIndex,
1083
+ focused: focusedColumnInRow(nav.focus, entry.rowIndex) !== null,
1068
1084
  contextMenu: groupMenu
1069
1085
  },
1070
1086
  entry.groupId
@@ -1297,40 +1313,86 @@ import {
1297
1313
 
1298
1314
  // src/DataGrid/useDragReorder.ts
1299
1315
  import {
1300
- useRef,
1316
+ useLayoutEffect as useLayoutEffect2,
1317
+ useRef as useRef2,
1301
1318
  useState as useState3
1302
1319
  } from "react";
1303
1320
  import {
1304
1321
  NO_DRAG,
1305
1322
  startDragReorder
1306
1323
  } from "@gridkitjs/dom";
1324
+
1325
+ // src/DataGrid/useGestureSignal.ts
1326
+ import { useEffect as useEffect2, useRef } from "react";
1327
+ function useGestureSignal() {
1328
+ const gesture = useRef(null);
1329
+ useEffect2(() => {
1330
+ const current = gesture.current;
1331
+ if (current !== null && !current.element.isConnected) {
1332
+ gesture.current = null;
1333
+ current.controller.abort();
1334
+ }
1335
+ });
1336
+ useEffect2(
1337
+ () => () => {
1338
+ gesture.current?.controller.abort();
1339
+ },
1340
+ []
1341
+ );
1342
+ return function startGesture(element, start) {
1343
+ const controller = new AbortController();
1344
+ if (!start(controller.signal)) {
1345
+ return false;
1346
+ }
1347
+ gesture.current?.controller.abort();
1348
+ gesture.current = { element, controller };
1349
+ return true;
1350
+ };
1351
+ }
1352
+
1353
+ // src/DataGrid/useDragReorder.ts
1307
1354
  function useDragReorder({
1308
1355
  resolveTarget,
1309
1356
  onDrop
1310
1357
  }) {
1311
1358
  const [drag, setDrag] = useState3(NO_DRAG);
1312
- const dragged = useRef(false);
1313
- function justDragged() {
1314
- const answer = dragged.current;
1315
- dragged.current = false;
1359
+ const openedDragId = useRef2(null);
1360
+ const startGesture = useGestureSignal();
1361
+ const handlers = useRef2({ resolveTarget, onDrop });
1362
+ useLayoutEffect2(() => {
1363
+ handlers.current = { resolveTarget, onDrop };
1364
+ });
1365
+ function justDragged(id) {
1366
+ const answer = openedDragId.current === id;
1367
+ openedDragId.current = null;
1316
1368
  return answer;
1317
1369
  }
1318
1370
  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
- });
1371
+ const started = startGesture(
1372
+ grabElement,
1373
+ (signal) => startDragReorder(id, grabElement, event, {
1374
+ resolveTarget: (clientX, clientY, movedId) => handlers.current.resolveTarget(clientX, clientY, movedId),
1375
+ onState(state) {
1376
+ if (state.kind === "dragging") {
1377
+ openedDragId.current = state.draggedId;
1378
+ }
1379
+ setDrag(state);
1380
+ },
1381
+ onDrop: (target, movedId) => {
1382
+ handlers.current.onDrop(target, movedId);
1383
+ },
1384
+ signal
1385
+ })
1386
+ );
1387
+ if (started) {
1388
+ openedDragId.current = null;
1389
+ }
1329
1390
  }
1391
+ const dragging = drag.kind === "dragging" ? drag : null;
1330
1392
  return {
1331
- draggedId: drag.draggedId,
1332
- dropTarget: drag.dropTarget,
1333
- ghostTransform: drag.ghostTransform,
1393
+ draggedId: dragging?.draggedId ?? null,
1394
+ dropTarget: dragging?.dropTarget ?? null,
1395
+ ghostTransform: dragging?.ghostTransform ?? null,
1334
1396
  startDrag,
1335
1397
  justDragged
1336
1398
  };
@@ -1353,8 +1415,7 @@ function useGroupByDrag({
1353
1415
  ghostTransform: drag.ghostTransform,
1354
1416
  startDrag: (columnId, event) => {
1355
1417
  drag.startDrag(columnId, event.currentTarget, event);
1356
- },
1357
- justDragged: drag.justDragged
1418
+ }
1358
1419
  };
1359
1420
  }
1360
1421
 
@@ -1484,7 +1545,8 @@ function GroupByBar({
1484
1545
 
1485
1546
  // src/DataGrid/announcedCallbacks.ts
1486
1547
  import {
1487
- isColumnVisible
1548
+ isColumnVisible,
1549
+ isGroupExpanded
1488
1550
  } from "@gridkitjs/core";
1489
1551
  function announcedCallbacks({
1490
1552
  callbacks,
@@ -1575,20 +1637,21 @@ function announcedCallbacks({
1575
1637
  );
1576
1638
  },
1577
1639
  /**
1578
- * `groupId: null` marks `expandAll`/`collapseAll`, which touch every
1579
- * group in one call rather than one at a time — announced as a single
1580
- * summary instead of naming a group that doesn't apply.
1640
+ * `groupId: null` marks `expandAll`/`collapseAll` and
1641
+ * `setGroupExpansion`, which touch every group in one call rather than
1642
+ * one at a time — announced as a single summary of the mode the groups
1643
+ * now follow, instead of naming a group that doesn't apply.
1581
1644
  */
1582
1645
  onGroupExpansionChange: (event) => {
1583
1646
  callbacks.onGroupExpansionChange?.(event);
1584
1647
  if (event.groupId === null) {
1585
1648
  announce(
1586
- event.expansion.length === 0 ? messages.allGroupsExpanded : messages.allGroupsCollapsed
1649
+ event.expansion.mode === "allExpanded" ? messages.allGroupsExpanded : messages.allGroupsCollapsed
1587
1650
  );
1588
1651
  return;
1589
1652
  }
1590
1653
  announce(
1591
- event.expansion.includes(event.groupId) ? messages.groupCollapsed : messages.groupExpanded
1654
+ isGroupExpanded(event.expansion, event.groupId) ? messages.groupExpanded : messages.groupCollapsed
1592
1655
  );
1593
1656
  },
1594
1657
  onPaginationChange: (event) => {
@@ -1646,13 +1709,13 @@ function useGridCopy({
1646
1709
  return command !== void 0 && runCommand(command, commands.contextFor(target));
1647
1710
  }
1648
1711
  function focusedCell() {
1649
- const { rowIndex, columnIndex } = nav.focus;
1650
- if (rowIndex === HEADER_ROW) {
1712
+ const { focus } = nav;
1713
+ if (focus.area === "header") {
1651
1714
  return null;
1652
1715
  }
1653
1716
  const { pageRows, columns } = controller.getView();
1654
- const entry = pageRows[rowIndex];
1655
- const column = columns[columnIndex];
1717
+ const entry = pageRows[focus.rowIndex];
1718
+ const column = columns[focus.columnIndex];
1656
1719
  return entry === void 0 || "kind" in entry || column === void 0 ? null : { area: "cell", rowId: entry.rowId, columnId: column.id };
1657
1720
  }
1658
1721
  return (event) => {
@@ -1674,22 +1737,36 @@ import {
1674
1737
  runCommand as runCommand2
1675
1738
  } from "@gridkitjs/core";
1676
1739
 
1677
- // src/DataGrid/clipboard/copyToClipboard.ts
1740
+ // src/DataGrid/clipboard/useCopyToClipboard.ts
1741
+ import { useRef as useRef3 } from "react";
1678
1742
  import { writeClipboard } from "@gridkitjs/dom";
1679
- function copyToClipboard(content, { messages, announce }) {
1680
- writeClipboard(content).then(
1681
- () => {
1682
- announce(messages.copied(content.count, content.unit));
1683
- },
1684
- (error) => {
1685
- console.error("DataGrid: the clipboard could not be written.", error);
1686
- announce(messages.copyFailed);
1743
+ function useCopyToClipboard({
1744
+ messages,
1745
+ announce
1746
+ }) {
1747
+ const copies = useRef3(0);
1748
+ return (content) => {
1749
+ copies.current += 1;
1750
+ const copy = copies.current;
1751
+ function announceIfLatest(text) {
1752
+ if (copy === copies.current) {
1753
+ announce(text);
1754
+ }
1687
1755
  }
1688
- );
1756
+ writeClipboard(content).then(
1757
+ () => {
1758
+ announceIfLatest(messages.copied(content.count, content.unit));
1759
+ },
1760
+ (error) => {
1761
+ console.error("DataGrid: the clipboard could not be written.", error);
1762
+ announceIfLatest(messages.copyFailed);
1763
+ }
1764
+ );
1765
+ };
1689
1766
  }
1690
1767
 
1691
1768
  // src/DataGrid/export/useSaveFile.ts
1692
- import { useRef as useRef2 } from "react";
1769
+ import { useRef as useRef4 } from "react";
1693
1770
  import { downloadBlob } from "@gridkitjs/dom";
1694
1771
 
1695
1772
  // src/DataGrid/export/exportApi.ts
@@ -1725,33 +1802,50 @@ function exportApi({
1725
1802
  }
1726
1803
 
1727
1804
  // src/DataGrid/export/useSaveFile.ts
1728
- function download(file) {
1729
- downloadBlob(exportBlob(file), file.fileName);
1730
- }
1805
+ var IDLE = { kind: "idle" };
1806
+ var WRITING = { kind: "writing" };
1731
1807
  function useSaveFile({
1732
1808
  messages,
1733
1809
  announce
1734
1810
  }) {
1735
- const writingRef = useRef2(false);
1736
- return (file) => {
1737
- if (writingRef.current) {
1811
+ const state = useRef4(IDLE);
1812
+ function canSave() {
1813
+ if (state.current.kind === "writing") {
1738
1814
  console.error(
1739
1815
  "DataGrid: an export is still being written, so this one was dropped."
1740
1816
  );
1741
- return;
1817
+ return false;
1818
+ }
1819
+ return true;
1820
+ }
1821
+ function fail(reason, error) {
1822
+ console.error(`DataGrid: ${reason}`, error);
1823
+ announce(messages.exportFailed);
1824
+ }
1825
+ function download(file) {
1826
+ try {
1827
+ downloadBlob(exportBlob(file), file.fileName);
1828
+ } catch (error) {
1829
+ fail("the export could not be saved.", error);
1830
+ }
1831
+ }
1832
+ function saveFile(file) {
1833
+ if (!canSave()) {
1834
+ return false;
1742
1835
  }
1743
1836
  if (!(file instanceof Promise)) {
1744
1837
  download(file);
1745
- return;
1838
+ return true;
1746
1839
  }
1747
- writingRef.current = true;
1840
+ state.current = WRITING;
1748
1841
  file.then(download, (error) => {
1749
- console.error("DataGrid: the export's writer failed.", error);
1750
- announce(messages.exportFailed);
1842
+ fail("the export's writer failed.", error);
1751
1843
  }).finally(() => {
1752
- writingRef.current = false;
1844
+ state.current = IDLE;
1753
1845
  });
1754
- };
1846
+ return true;
1847
+ }
1848
+ return { saveFile, canSave };
1755
1849
  }
1756
1850
 
1757
1851
  // src/DataGrid/print/printApi.ts
@@ -1789,7 +1883,8 @@ function useGridCommands({
1789
1883
  openPanel
1790
1884
  }) {
1791
1885
  const commands = useMemo3(() => resolveCommands(appCommands), [appCommands]);
1792
- const saveFile = useSaveFile({ messages, announce });
1886
+ const { saveFile, canSave } = useSaveFile({ messages, announce });
1887
+ const writeClipboard2 = useCopyToClipboard({ messages, announce });
1793
1888
  const host = {
1794
1889
  autoFitColumns: (columnIds) => {
1795
1890
  const { columns } = controller.getView();
@@ -1807,12 +1902,12 @@ function useGridCommands({
1807
1902
  openPanel,
1808
1903
  announce,
1809
1904
  saveFile,
1810
- writeClipboard: (content) => {
1811
- copyToClipboard(content, { messages, announce });
1812
- },
1905
+ canSave,
1906
+ writeClipboard: writeClipboard2,
1813
1907
  print: (document2, options) => {
1814
1908
  printGrid(document2, options).catch((error) => {
1815
1909
  console.error("DataGrid: the print failed.", error);
1910
+ announce(messages.printFailed);
1816
1911
  });
1817
1912
  }
1818
1913
  };
@@ -1843,13 +1938,13 @@ function useGridCommands({
1843
1938
  }
1844
1939
 
1845
1940
  // src/DataGrid/menu/GridMenu.tsx
1846
- import { useLayoutEffect as useLayoutEffect3, useRef as useRef4, useState as useState4 } from "react";
1941
+ import { useLayoutEffect as useLayoutEffect4, useRef as useRef6, useState as useState4 } from "react";
1847
1942
  import {
1848
1943
  attachMenu
1849
1944
  } from "@gridkitjs/dom";
1850
1945
 
1851
1946
  // src/DataGrid/overlay/Overlay.tsx
1852
- import { useLayoutEffect as useLayoutEffect2, useRef as useRef3 } from "react";
1947
+ import { useLayoutEffect as useLayoutEffect3, useRef as useRef5 } from "react";
1853
1948
  import { createPortal as createPortal3 } from "react-dom";
1854
1949
  import {
1855
1950
  attachOverlay
@@ -1873,18 +1968,21 @@ function Overlay({
1873
1968
  offset,
1874
1969
  initialFocus,
1875
1970
  returnFocusTo,
1971
+ returnFocusFallback,
1876
1972
  ignoreScrollAfterInput,
1877
1973
  trapFocus,
1878
1974
  themeClassName,
1879
1975
  onDismiss,
1880
1976
  children
1881
1977
  }) {
1882
- const panelRef = useRef3(null);
1883
- const onDismissRef = useRef3(onDismiss);
1884
- useLayoutEffect2(() => {
1978
+ const panelRef = useRef5(null);
1979
+ const onDismissRef = useRef5(onDismiss);
1980
+ const returnFocusFallbackRef = useRef5(returnFocusFallback);
1981
+ useLayoutEffect3(() => {
1885
1982
  onDismissRef.current = onDismiss;
1983
+ returnFocusFallbackRef.current = returnFocusFallback;
1886
1984
  });
1887
- useLayoutEffect2(() => {
1985
+ useLayoutEffect3(() => {
1888
1986
  const panel = panelRef.current;
1889
1987
  if (panel === null) {
1890
1988
  return void 0;
@@ -1895,6 +1993,7 @@ function Overlay({
1895
1993
  offset,
1896
1994
  initialFocus: attachedFocus(initialFocus),
1897
1995
  returnFocusTo,
1996
+ returnFocusFallback: () => returnFocusFallbackRef.current?.() ?? null,
1898
1997
  ignoreScrollAfterInput,
1899
1998
  trapFocus,
1900
1999
  onDismiss: (reason) => {
@@ -1983,6 +2082,8 @@ function ChevronRightIcon2() {
1983
2082
 
1984
2083
  // src/DataGrid/menu/GridMenu.tsx
1985
2084
  import { Fragment as Fragment2, jsx as jsx13, jsxs as jsxs9 } from "react/jsx-runtime";
2085
+ var CLOSED = { kind: "closed" };
2086
+ var ENTRY_GONE = { kind: "entryGone" };
1986
2087
  function itemElement(menu, id) {
1987
2088
  return Array.from(
1988
2089
  menu.querySelectorAll("[data-gridkit-menu-item]")
@@ -2028,15 +2129,15 @@ function GridMenu({
2028
2129
  onCloseSubmenu,
2029
2130
  behaviourRef
2030
2131
  }) {
2031
- const menuRef = useRef4(null);
2032
- const submenuBehaviourRef = useRef4(null);
2033
- const [submenu, setSubmenu] = useState4(null);
2132
+ const menuRef = useRef6(null);
2133
+ const submenuBehaviourRef = useRef6(null);
2134
+ const [submenu, setSubmenu] = useState4(CLOSED);
2034
2135
  const isSubmenu = onCloseSubmenu !== void 0;
2035
- const latest = useRef4({ entries, submenu, onRun, onClose, onCloseSubmenu });
2036
- useLayoutEffect3(() => {
2136
+ const latest = useRef6({ entries, submenu, onRun, onClose, onCloseSubmenu });
2137
+ useLayoutEffect4(() => {
2037
2138
  latest.current = { entries, submenu, onRun, onClose, onCloseSubmenu };
2038
2139
  });
2039
- useLayoutEffect3(() => {
2140
+ useLayoutEffect4(() => {
2040
2141
  const menu = menuRef.current;
2041
2142
  if (menu === null) {
2042
2143
  return void 0;
@@ -2051,7 +2152,8 @@ function GridMenu({
2051
2152
  }
2052
2153
  },
2053
2154
  onOpenSubmenu: (id, { focus }) => {
2054
- if (latest.current.submenu?.id === id) {
2155
+ const open = latest.current.submenu;
2156
+ if (open.kind === "open" && open.id === id) {
2055
2157
  if (focus === "first") {
2056
2158
  submenuBehaviourRef.current?.focusItem(0);
2057
2159
  }
@@ -2059,11 +2161,17 @@ function GridMenu({
2059
2161
  }
2060
2162
  const anchor = itemElement(menu, id);
2061
2163
  if (anchor !== null) {
2062
- setSubmenu({ id, anchor, focus, offset: -topInset(menu) });
2164
+ setSubmenu({
2165
+ kind: "open",
2166
+ id,
2167
+ anchor,
2168
+ focus,
2169
+ offset: -topInset(menu)
2170
+ });
2063
2171
  }
2064
2172
  },
2065
2173
  onCollapseSubmenu: () => {
2066
- setSubmenu(null);
2174
+ setSubmenu(CLOSED);
2067
2175
  },
2068
2176
  onCloseSubmenu: isSubmenu ? () => {
2069
2177
  latest.current.onCloseSubmenu?.();
@@ -2082,12 +2190,25 @@ function GridMenu({
2082
2190
  behaviour.detach();
2083
2191
  };
2084
2192
  }, [isSubmenu, behaviourRef]);
2193
+ useLayoutEffect4(() => {
2194
+ const menu = menuRef.current;
2195
+ if (submenu.kind !== "entryGone" || menu === null || document.activeElement !== document.body) {
2196
+ return;
2197
+ }
2198
+ const item = menu.querySelector(
2199
+ '[data-gridkit-menu-item][tabindex="0"]'
2200
+ ) ?? menu.querySelector("[data-gridkit-menu-item]");
2201
+ item?.focus();
2202
+ }, [submenu]);
2085
2203
  function closeSubmenu() {
2086
- setSubmenu(null);
2204
+ setSubmenu(CLOSED);
2087
2205
  }
2088
- const openEntry = submenu === null ? void 0 : entries.find(
2206
+ const openEntry = submenu.kind === "open" ? entries.find(
2089
2207
  (entry) => entry.type === "submenu" && entry.id === submenu.id
2090
- );
2208
+ ) : void 0;
2209
+ if (submenu.kind === "open" && openEntry === void 0) {
2210
+ setSubmenu(ENTRY_GONE);
2211
+ }
2091
2212
  return /* @__PURE__ */ jsxs9(Fragment2, { children: [
2092
2213
  /* @__PURE__ */ jsx13(
2093
2214
  "div",
@@ -2101,14 +2222,14 @@ function GridMenu({
2101
2222
  MenuItem,
2102
2223
  {
2103
2224
  entry,
2104
- expanded: entry.id === submenu?.id
2225
+ expanded: submenu.kind === "open" && entry.id === submenu.id
2105
2226
  },
2106
2227
  entry.id
2107
2228
  )
2108
2229
  )
2109
2230
  }
2110
2231
  ),
2111
- submenu !== null && openEntry !== void 0 && /* @__PURE__ */ jsx13(
2232
+ submenu.kind === "open" && openEntry !== void 0 && /* @__PURE__ */ jsx13(
2112
2233
  Overlay,
2113
2234
  {
2114
2235
  anchor: submenu.anchor,
@@ -2140,7 +2261,7 @@ import { jsx as jsx14 } from "react/jsx-runtime";
2140
2261
  function GridContextMenu({
2141
2262
  contextMenu
2142
2263
  }) {
2143
- const { menu, run, close } = contextMenu;
2264
+ const { menu, returnFocusFallback, run, close } = contextMenu;
2144
2265
  if (menu === null) {
2145
2266
  return null;
2146
2267
  }
@@ -2150,6 +2271,7 @@ function GridContextMenu({
2150
2271
  anchor: menu.anchor,
2151
2272
  gap: menu.anchor instanceof Element ? ANCHOR_GAP : 0,
2152
2273
  returnFocusTo: menu.cell ?? void 0,
2274
+ returnFocusFallback,
2153
2275
  themeClassName: menu.themeClassName,
2154
2276
  onDismiss: close,
2155
2277
  children: /* @__PURE__ */ jsx14(
@@ -2169,10 +2291,10 @@ function GridContextMenu({
2169
2291
 
2170
2292
  // src/DataGrid/contextMenu/useContextMenu.ts
2171
2293
  import {
2172
- useEffect as useEffect2,
2173
- useLayoutEffect as useLayoutEffect4,
2174
- useRef as useRef5,
2175
- useState as useState5
2294
+ useCallback,
2295
+ useEffect as useEffect3,
2296
+ useLayoutEffect as useLayoutEffect5,
2297
+ useRef as useRef7
2176
2298
  } from "react";
2177
2299
  import {
2178
2300
  contextMenuEntries,
@@ -2186,7 +2308,8 @@ import {
2186
2308
 
2187
2309
  // src/DataGrid/contextMenu/contextMenuPlaces.ts
2188
2310
  import {
2189
- HEADER_ROW as HEADER_ROW2
2311
+ bodyFocus as bodyFocus2,
2312
+ headerFocus as headerFocus2
2190
2313
  } from "@gridkitjs/core";
2191
2314
  import {
2192
2315
  bodyCellFrom as bodyCellFrom2,
@@ -2196,11 +2319,11 @@ import {
2196
2319
  } from "@gridkitjs/dom";
2197
2320
  function headerCell(parts, columnIndex) {
2198
2321
  const element = parts.headerTable?.tHead?.rows[0]?.cells[columnIndex];
2199
- return element === void 0 ? null : { element, rowIndex: HEADER_ROW2, columnIndex };
2322
+ return element === void 0 ? null : { element, focus: headerFocus2(columnIndex) };
2200
2323
  }
2201
2324
  function bodyCell(parts, rowIndex, columnIndex) {
2202
2325
  const element = findBodyRow2(parts.bodyTable, rowIndex)?.cells[columnIndex];
2203
- return element === void 0 ? null : { element, rowIndex, columnIndex };
2326
+ return element === void 0 ? null : { element, focus: bodyFocus2(rowIndex, columnIndex) };
2204
2327
  }
2205
2328
  function viewOf(element, parts) {
2206
2329
  const root = parts.root?.getBoundingClientRect();
@@ -2321,7 +2444,7 @@ function useContextMenu({
2321
2444
  bodyTableRef,
2322
2445
  bodyScrollRef,
2323
2446
  contextMenu,
2324
- onContextMenuOpenChange,
2447
+ overlay,
2325
2448
  controller,
2326
2449
  commands,
2327
2450
  nav,
@@ -2329,9 +2452,8 @@ function useContextMenu({
2329
2452
  messages,
2330
2453
  columnName
2331
2454
  }) {
2332
- const [menu, setMenu] = useState5(null);
2333
- const current = useRef5(null);
2334
- const openings = useRef5(0);
2455
+ const { drawn, close: closeOverlay } = overlay;
2456
+ const openings = useRef7(0);
2335
2457
  const enabled = contextMenu !== void 0 && contextMenu !== false;
2336
2458
  function parts() {
2337
2459
  const { columns, pageRows } = controller.getView();
@@ -2386,20 +2508,6 @@ function useContextMenu({
2386
2508
  context
2387
2509
  });
2388
2510
  }
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
2511
  function openAt({ target, cell, anchor }, point) {
2404
2512
  let resolved = resolveFor(target);
2405
2513
  if (resolved.length === 0) {
@@ -2410,17 +2518,20 @@ function useContextMenu({
2410
2518
  }
2411
2519
  if (cell !== null && document.activeElement !== cell.element) {
2412
2520
  cell.element.focus({ preventScroll: true });
2413
- nav.focusCellNow(cell.rowIndex, cell.columnIndex);
2521
+ nav.focusCellNow(cell.focus);
2414
2522
  }
2415
2523
  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)
2524
+ overlay.open({
2525
+ kind: "contextMenu",
2526
+ menu: {
2527
+ key: openings.current,
2528
+ target,
2529
+ entries: resolved,
2530
+ label: labelOf(target),
2531
+ anchor: point ?? anchor,
2532
+ cell: cell?.element ?? null,
2533
+ themeClassName: overlayThemeClassName(rootRef.current)
2534
+ }
2424
2535
  });
2425
2536
  return true;
2426
2537
  }
@@ -2428,11 +2539,11 @@ function useContextMenu({
2428
2539
  const place = placeAt(target, parts());
2429
2540
  return place !== null && openAt(place, point);
2430
2541
  }
2431
- const latestRequest = useRef5(handleRequest);
2432
- useLayoutEffect4(() => {
2542
+ const latestRequest = useRef7(handleRequest);
2543
+ useLayoutEffect5(() => {
2433
2544
  latestRequest.current = handleRequest;
2434
2545
  });
2435
- useEffect2(() => {
2546
+ useEffect3(() => {
2436
2547
  const root = rootRef.current;
2437
2548
  if (!enabled || root === null) {
2438
2549
  return void 0;
@@ -2441,28 +2552,22 @@ function useContextMenu({
2441
2552
  onRequest: (request) => latestRequest.current(request)
2442
2553
  });
2443
2554
  }, [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
- }
2555
+ const close = useCallback(() => {
2556
+ closeOverlay("contextMenu");
2557
+ }, [closeOverlay]);
2558
+ useEffect3(() => enabled ? close : void 0, [enabled, close]);
2455
2559
  return {
2456
- menu,
2560
+ menu: drawn.kind === "contextMenu" ? drawn.menu : null,
2457
2561
  opens: (area) => hasContextMenu(contextMenu, area),
2458
2562
  open: (target, anchor) => {
2459
2563
  const place = placeOf(target, parts());
2460
2564
  return place !== null && openAt(place, anchor ?? null);
2461
2565
  },
2566
+ returnFocusFallback: overlay.returnFocusFallback,
2462
2567
  run: (item) => {
2463
- const open = current.current;
2464
- if (item.run() && open !== null) {
2465
- commands.report(item.id, open.target, "contextMenu");
2568
+ const open = overlay.current();
2569
+ if (item.run() && open.kind === "contextMenu") {
2570
+ commands.report(item.id, open.menu.target, "contextMenu");
2466
2571
  }
2467
2572
  close();
2468
2573
  },
@@ -2470,11 +2575,52 @@ function useContextMenu({
2470
2575
  };
2471
2576
  }
2472
2577
 
2578
+ // src/DataGrid/overlay/useGridOverlay.ts
2579
+ import { useCallback as useCallback2, useLayoutEffect as useLayoutEffect6, useRef as useRef8, useState as useState5 } from "react";
2580
+ var CLOSED2 = { kind: "closed" };
2581
+ function useGridOverlay({
2582
+ nav,
2583
+ onContextMenuOpenChange
2584
+ }) {
2585
+ const [drawn, setDrawn] = useState5(CLOSED2);
2586
+ const current = useRef8(CLOSED2);
2587
+ const reportOpenChange = useRef8(onContextMenuOpenChange);
2588
+ useLayoutEffect6(() => {
2589
+ reportOpenChange.current = onContextMenuOpenChange;
2590
+ });
2591
+ const show = useCallback2((next) => {
2592
+ const previous = current.current;
2593
+ current.current = next;
2594
+ setDrawn(next);
2595
+ if (previous.kind === "contextMenu") {
2596
+ reportOpenChange.current?.({ open: false, target: previous.menu.target });
2597
+ }
2598
+ if (next.kind === "contextMenu") {
2599
+ reportOpenChange.current?.({ open: true, target: next.menu.target });
2600
+ }
2601
+ }, []);
2602
+ const close = useCallback2(
2603
+ (kind) => {
2604
+ if (current.current.kind === kind) {
2605
+ show(CLOSED2);
2606
+ }
2607
+ },
2608
+ [show]
2609
+ );
2610
+ return {
2611
+ drawn,
2612
+ current: () => current.current,
2613
+ open: show,
2614
+ close,
2615
+ returnFocusFallback: nav.tabStopCell
2616
+ };
2617
+ }
2618
+
2473
2619
  // src/DataGrid/panels/GridPanels.tsx
2474
- import { useRef as useRef13 } from "react";
2620
+ import { useRef as useRef15 } from "react";
2475
2621
 
2476
2622
  // src/DataGrid/columnChooser/ColumnChooser.tsx
2477
- import { useLayoutEffect as useLayoutEffect5, useRef as useRef6, useState as useState6 } from "react";
2623
+ import { useLayoutEffect as useLayoutEffect7, useRef as useRef9, useState as useState6 } from "react";
2478
2624
  import { resolveColumnChooser } from "@gridkitjs/core";
2479
2625
  import { Fragment as Fragment3, jsx as jsx15, jsxs as jsxs10 } from "react/jsx-runtime";
2480
2626
  function ColumnChooser({
@@ -2482,13 +2628,13 @@ function ColumnChooser({
2482
2628
  messages
2483
2629
  }) {
2484
2630
  const [search, setSearch] = useState6("");
2485
- const showAllRef = useRef6(null);
2631
+ const showAllRef = useRef9(null);
2486
2632
  const { entries, selectAll, toggleAll } = resolveColumnChooser({
2487
2633
  columns: controller.getView().dataColumns,
2488
2634
  visibility: controller.getState().columnVisibility,
2489
2635
  search
2490
2636
  });
2491
- useLayoutEffect5(() => {
2637
+ useLayoutEffect7(() => {
2492
2638
  if (showAllRef.current !== null) {
2493
2639
  showAllRef.current.indeterminate = selectAll === "mixed";
2494
2640
  }
@@ -2559,7 +2705,7 @@ function ColumnChooser({
2559
2705
  }
2560
2706
 
2561
2707
  // src/DataGrid/filterMenu/FilterMenu.tsx
2562
- import { useEffect as useEffect3, useRef as useRef11 } from "react";
2708
+ import { useEffect as useEffect4, useRef as useRef14 } from "react";
2563
2709
  import {
2564
2710
  resolveColumnFilterMenu as resolveColumnFilterMenu2,
2565
2711
  resolveColumnHeaderText,
@@ -2569,23 +2715,106 @@ import {
2569
2715
  // src/DataGrid/filterMenu/ExcelFilterEditor.tsx
2570
2716
  import {
2571
2717
  useImperativeHandle as useImperativeHandle2,
2572
- useLayoutEffect as useLayoutEffect9,
2718
+ useLayoutEffect as useLayoutEffect11,
2573
2719
  useMemo as useMemo4,
2574
- useReducer,
2575
- useRef as useRef10,
2576
- useState as useState10
2720
+ useReducer as useReducer2,
2721
+ useRef as useRef13,
2722
+ useState as useState9
2577
2723
  } from "react";
2578
2724
  import {
2579
- describeColumnFilter as describeColumnFilter2,
2580
2725
  draftToFilter as draftToFilter2,
2581
2726
  filterChecklist,
2582
- filterEditorReducer as filterEditorReducer2,
2583
- initialFilterEditorDraft as initialFilterEditorDraft2,
2727
+ filterDraftChanged as filterDraftChanged3,
2584
2728
  selectAllState
2585
2729
  } from "@gridkitjs/core";
2586
2730
 
2731
+ // src/DataGrid/filterMenu/excelEditorState.ts
2732
+ import {
2733
+ describeColumnFilter,
2734
+ filterDraftChanged,
2735
+ filterEditorReducer,
2736
+ initialFilterEditorDraft,
2737
+ toValuesDraft
2738
+ } from "@gridkitjs/core";
2739
+ function openValues(filter, columnId, values) {
2740
+ const shape = describeColumnFilter(filter, columnId);
2741
+ return {
2742
+ over: filter,
2743
+ shape,
2744
+ draft: initialFilterEditorDraft(shape, values)
2745
+ };
2746
+ }
2747
+ function initialExcelEditorState(opened) {
2748
+ return { opened, draft: toValuesDraft(opened.draft) };
2749
+ }
2750
+ function showsAlike(a, b) {
2751
+ return a.shape.kind === b.shape.kind && a.draft.options === b.draft.options && !filterDraftChanged(a.draft, b.draft);
2752
+ }
2753
+ function excelEditorReducer(state, action) {
2754
+ if (action.type === "filterChanged") {
2755
+ return showsAlike(state.opened, action.opened) ? { ...state, opened: action.opened } : initialExcelEditorState(action.opened);
2756
+ }
2757
+ return {
2758
+ ...state,
2759
+ draft: toValuesDraft(filterEditorReducer(state.draft, action))
2760
+ };
2761
+ }
2762
+
2763
+ // src/DataGrid/menu/useMenuButton.ts
2764
+ import {
2765
+ useLayoutEffect as useLayoutEffect8,
2766
+ useRef as useRef10,
2767
+ useState as useState7
2768
+ } from "react";
2769
+ var CLOSED3 = { kind: "closed" };
2770
+ function useMenuButton({
2771
+ itemCount,
2772
+ gap = () => ANCHOR_GAP
2773
+ }) {
2774
+ const [state, setState] = useState7(CLOSED3);
2775
+ const behaviourRef = useRef10(null);
2776
+ useLayoutEffect8(() => {
2777
+ if (state.kind === "open" && state.focusItem > 0) {
2778
+ behaviourRef.current?.focusItem(state.focusItem);
2779
+ }
2780
+ }, [state]);
2781
+ function open(button, onLast) {
2782
+ setState({
2783
+ kind: "open",
2784
+ anchor: button,
2785
+ themeClassName: overlayThemeClassName(button),
2786
+ gap: gap(button),
2787
+ focusItem: onLast ? itemCount - 1 : 0
2788
+ });
2789
+ }
2790
+ function close() {
2791
+ setState(CLOSED3);
2792
+ }
2793
+ return {
2794
+ state,
2795
+ close,
2796
+ buttonProps: {
2797
+ "aria-haspopup": "menu",
2798
+ "aria-expanded": state.kind === "open",
2799
+ onClick: (event) => {
2800
+ if (state.kind === "closed") {
2801
+ open(event.currentTarget, false);
2802
+ } else {
2803
+ close();
2804
+ }
2805
+ },
2806
+ onKeyDown: (event) => {
2807
+ if ((event.key === "ArrowDown" || event.key === "ArrowUp") && state.kind === "closed") {
2808
+ event.preventDefault();
2809
+ open(event.currentTarget, event.key === "ArrowUp");
2810
+ }
2811
+ }
2812
+ },
2813
+ behaviourRef
2814
+ };
2815
+ }
2816
+
2587
2817
  // src/DataGrid/filterMenu/OperatorMenuButton.tsx
2588
- import { useLayoutEffect as useLayoutEffect6, useRef as useRef7, useState as useState7 } from "react";
2589
2818
  import { Fragment as Fragment4, jsx as jsx16, jsxs as jsxs11 } from "react/jsx-runtime";
2590
2819
  function gapBeside(button) {
2591
2820
  const dialog = button.closest('[role="dialog"]') ?? button;
@@ -2598,25 +2827,11 @@ function OperatorMenuButton({
2598
2827
  onChoose,
2599
2828
  ref
2600
2829
  }) {
2601
- const [open, setOpen] = useState7(null);
2602
- const menuBehaviourRef = useRef7(null);
2830
+ const { state, close, buttonProps, behaviourRef } = useMenuButton({
2831
+ itemCount: menu.operators.length,
2832
+ gap: gapBeside
2833
+ });
2603
2834
  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
2835
  const entries = menu.operators.map((operator) => ({
2621
2836
  type: "item",
2622
2837
  id: operator,
@@ -2636,21 +2851,7 @@ function OperatorMenuButton({
2636
2851
  ref,
2637
2852
  type: "button",
2638
2853
  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
- },
2854
+ ...buttonProps,
2654
2855
  children: [
2655
2856
  /* @__PURE__ */ jsx16("span", { className: "filter-menu-submenu-icon", "aria-hidden": "true", children: active !== void 0 && /* @__PURE__ */ jsx16(CheckIcon, {}) }),
2656
2857
  label,
@@ -2658,22 +2859,22 @@ function OperatorMenuButton({
2658
2859
  ]
2659
2860
  }
2660
2861
  ),
2661
- open !== null && /* @__PURE__ */ jsx16(
2862
+ state.kind === "open" && /* @__PURE__ */ jsx16(
2662
2863
  Overlay,
2663
2864
  {
2664
- anchor: open.anchor,
2865
+ anchor: state.anchor,
2665
2866
  placement: "right-start",
2666
- gap: open.gap,
2667
- returnFocusTo: open.anchor,
2668
- themeClassName: open.themeClassName,
2867
+ gap: state.gap,
2868
+ returnFocusTo: state.anchor,
2869
+ themeClassName: state.themeClassName,
2669
2870
  onDismiss: close,
2670
2871
  children: /* @__PURE__ */ jsx16(
2671
2872
  GridMenu,
2672
2873
  {
2673
2874
  entries,
2674
2875
  label,
2675
- themeClassName: open.themeClassName,
2676
- behaviourRef: menuBehaviourRef,
2876
+ themeClassName: state.themeClassName,
2877
+ behaviourRef,
2677
2878
  onRun: (item) => {
2678
2879
  item.run();
2679
2880
  },
@@ -2690,17 +2891,15 @@ import {
2690
2891
  Fragment as Fragment5,
2691
2892
  useId,
2692
2893
  useImperativeHandle,
2693
- useLayoutEffect as useLayoutEffect7,
2694
- useRef as useRef8,
2695
- useState as useState8
2894
+ useLayoutEffect as useLayoutEffect9,
2895
+ useReducer,
2896
+ useRef as useRef11
2696
2897
  } from "react";
2697
2898
  import {
2698
- conditionRowsFrom,
2699
- conditionsFromRows,
2700
- describeColumnFilter,
2899
+ conditionsFromRows as conditionsFromRows2,
2701
2900
  draftToFilter,
2702
- filterEditorReducer,
2703
- initialFilterEditorDraft
2901
+ filterDraftChanged as filterDraftChanged2,
2902
+ filterEditorReducer as filterEditorReducer2
2704
2903
  } from "@gridkitjs/core";
2705
2904
 
2706
2905
  // src/DataGrid/filterMenu/ConditionRow.tsx
@@ -2821,9 +3020,13 @@ function ConditionRow({
2821
3020
  );
2822
3021
  }
2823
3022
 
2824
- // src/DataGrid/filterMenu/SimpleFilterEditor.tsx
2825
- import { jsx as jsx18, jsxs as jsxs13 } from "react/jsx-runtime";
2826
- var COMBINATORS = ["and", "or"];
3023
+ // src/DataGrid/filterMenu/simpleEditorState.ts
3024
+ import {
3025
+ conditionRowsFrom,
3026
+ conditionsFromRows,
3027
+ describeColumnFilter as describeColumnFilter2,
3028
+ initialFilterEditorDraft as initialFilterEditorDraft2
3029
+ } from "@gridkitjs/core";
2827
3030
  var NO_ROWS = [];
2828
3031
  var NO_VALUES = {
2829
3032
  options: [],
@@ -2831,6 +3034,74 @@ var NO_VALUES = {
2831
3034
  truncated: false,
2832
3035
  blanks: 0
2833
3036
  };
3037
+ function filterDraftShown(shape, rows, columnId, type) {
3038
+ const draft = initialFilterEditorDraft2(shape, {
3039
+ ...NO_VALUES,
3040
+ columnType: type
3041
+ });
3042
+ if (draft.kind !== "conditions") {
3043
+ return draft;
3044
+ }
3045
+ const shown = conditionsFromRows(rows, columnId, type);
3046
+ return shown.invalid.length > 0 ? draft : { ...draft, conditions: shown.conditions };
3047
+ }
3048
+ function openConditions(filter, columnId, operators, type, operator) {
3049
+ const shape = describeColumnFilter2(filter, columnId);
3050
+ const rows = conditionRowsFrom(shape, operators, type);
3051
+ return {
3052
+ over: filter,
3053
+ custom: shape.kind === "custom" || shape.kind === "values" && operator === void 0,
3054
+ draft: {
3055
+ // Its typed values kept, as picking the operator in the row keeps them.
3056
+ rows: operator === void 0 ? rows : rows.map(
3057
+ (row, index) => index === 0 ? { ...row, operator } : row
3058
+ ),
3059
+ combinator: shape.kind === "conditions" ? shape.combinator : "and"
3060
+ },
3061
+ filterDraft: filterDraftShown(shape, rows, columnId, type)
3062
+ };
3063
+ }
3064
+ function initialSimpleEditorState(opened) {
3065
+ return { opened, draft: opened.draft, invalid: NO_ROWS };
3066
+ }
3067
+ function sameConditionRows(a, b) {
3068
+ return a.combinator === b.combinator && a.rows.length === b.rows.length && a.rows.every((row, index) => {
3069
+ const other = b.rows[index];
3070
+ return other !== void 0 && row.operator === other.operator && row.text === other.text && row.textTo === other.textTo;
3071
+ });
3072
+ }
3073
+ function showsAlike2(a, b) {
3074
+ return a.custom === b.custom && sameConditionRows(a.draft, b.draft);
3075
+ }
3076
+ function simpleEditorReducer(state, action) {
3077
+ switch (action.type) {
3078
+ case "editRow":
3079
+ return {
3080
+ ...state,
3081
+ draft: {
3082
+ ...state.draft,
3083
+ rows: state.draft.rows.map(
3084
+ (row, index) => index === action.index ? action.row : row
3085
+ )
3086
+ },
3087
+ invalid: NO_ROWS
3088
+ };
3089
+ case "setCombinator":
3090
+ return {
3091
+ ...state,
3092
+ draft: { ...state.draft, combinator: action.combinator },
3093
+ invalid: NO_ROWS
3094
+ };
3095
+ case "markInvalid":
3096
+ return { ...state, invalid: action.rows };
3097
+ case "filterChanged":
3098
+ return showsAlike2(state.opened, action.opened) ? { ...state, opened: action.opened } : initialSimpleEditorState(action.opened);
3099
+ }
3100
+ }
3101
+
3102
+ // src/DataGrid/filterMenu/SimpleFilterEditor.tsx
3103
+ import { jsx as jsx18, jsxs as jsxs13 } from "react/jsx-runtime";
3104
+ var COMBINATORS = ["and", "or"];
2834
3105
  function holdsUnreadable(row) {
2835
3106
  return Array.from(row?.querySelectorAll("input") ?? []).some(
2836
3107
  (input) => input.validity.badInput
@@ -2876,42 +3147,34 @@ function SimpleFilterEditor({
2876
3147
  ref
2877
3148
  }) {
2878
3149
  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([]);
3150
+ const filter = controller.getState().filter;
3151
+ const open = (over) => openConditions(over, column.id, menu.operators, type, operator);
3152
+ const [{ opened, draft, invalid }, dispatch] = useReducer(
3153
+ simpleEditorReducer,
3154
+ filter,
3155
+ (over) => initialSimpleEditorState(open(over))
3156
+ );
3157
+ if (filter !== opened.over) {
3158
+ dispatch({ type: "filterChanged", opened: open(filter) });
3159
+ }
3160
+ const rowElements = useRef11([]);
2897
3161
  const invalidMessageId = useId();
2898
3162
  const combinatorName = useId();
2899
- function update(next) {
2900
- setDraft(next);
2901
- setInvalid(NO_ROWS);
2902
- }
2903
3163
  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
- );
3164
+ const applied = filterEditorReducer2(opened.filterDraft, {
3165
+ type: "setConditions",
3166
+ conditions,
3167
+ combinator: draft.combinator
3168
+ });
3169
+ if (filterDraftChanged2(opened.filterDraft, applied)) {
3170
+ applyFilter(
3171
+ draftToFilter(applied, column.id, controller.getState().filter)
3172
+ );
3173
+ }
2911
3174
  }
2912
3175
  useImperativeHandle(ref, () => ({
2913
3176
  apply: () => {
2914
- const parsed = conditionsFromRows(draft.rows, column.id, type);
3177
+ const parsed = conditionsFromRows2(draft.rows, column.id, type);
2915
3178
  const unreadable = draft.rows.map(
2916
3179
  (_, index) => holdsUnreadable(rowElements.current[index])
2917
3180
  );
@@ -2922,14 +3185,14 @@ function SimpleFilterEditor({
2922
3185
  applyConditions(parsed.conditions);
2923
3186
  return true;
2924
3187
  }
2925
- if (draft === opened.draft && !unreadable.includes(true)) {
3188
+ if (sameConditionRows(draft, opened.draft) && !unreadable.includes(true)) {
2926
3189
  return true;
2927
3190
  }
2928
- setInvalid(marked);
3191
+ dispatch({ type: "markInvalid", rows: marked });
2929
3192
  return false;
2930
3193
  }
2931
3194
  }));
2932
- useLayoutEffect7(() => {
3195
+ useLayoutEffect9(() => {
2933
3196
  const [first] = invalid;
2934
3197
  if (first !== void 0) {
2935
3198
  rowElements.current[first]?.querySelector('[aria-invalid="true"]')?.focus();
@@ -2947,7 +3210,7 @@ function SimpleFilterEditor({
2947
3210
  combinator: draft.combinator,
2948
3211
  messages,
2949
3212
  onChange: (combinator) => {
2950
- update({ ...draft, combinator });
3213
+ dispatch({ type: "setCombinator", combinator });
2951
3214
  }
2952
3215
  }
2953
3216
  ),
@@ -2965,12 +3228,7 @@ function SimpleFilterEditor({
2965
3228
  invalidMessageId: invalid.includes(index) ? invalidMessageId : void 0,
2966
3229
  focusRef: index === 0 ? initialFocusRef : void 0,
2967
3230
  onChange: (next) => {
2968
- update({
2969
- ...draft,
2970
- rows: draft.rows.map(
2971
- (current, at) => at === index ? next : current
2972
- )
2973
- });
3231
+ dispatch({ type: "editRow", index, row: next });
2974
3232
  }
2975
3233
  }
2976
3234
  )
@@ -2981,7 +3239,7 @@ function SimpleFilterEditor({
2981
3239
  }
2982
3240
 
2983
3241
  // src/DataGrid/filterMenu/ValueChecklist.tsx
2984
- import { useLayoutEffect as useLayoutEffect8, useRef as useRef9, useState as useState9 } from "react";
3242
+ import { useLayoutEffect as useLayoutEffect10, useRef as useRef12, useState as useState8 } from "react";
2985
3243
  import {
2986
3244
  attachChecklist,
2987
3245
  createRowVirtualizer
@@ -3001,15 +3259,15 @@ function ValueChecklist({
3001
3259
  label,
3002
3260
  onToggle
3003
3261
  }) {
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(() => {
3262
+ const listRef = useRef12(null);
3263
+ const checklistRef = useRef12(null);
3264
+ const [virtualizer, setVirtualizer] = useState8(null);
3265
+ const [range, setRange] = useState8(NO_RANGE);
3266
+ const latest = useRef12({ items, onToggle });
3267
+ useLayoutEffect10(() => {
3010
3268
  latest.current = { items, onToggle };
3011
3269
  });
3012
- useLayoutEffect8(() => {
3270
+ useLayoutEffect10(() => {
3013
3271
  const list = listRef.current;
3014
3272
  if (list === null) {
3015
3273
  return void 0;
@@ -3038,7 +3296,7 @@ function ValueChecklist({
3038
3296
  rows.destroy();
3039
3297
  };
3040
3298
  }, []);
3041
- useLayoutEffect8(() => {
3299
+ useLayoutEffect10(() => {
3042
3300
  virtualizer?.setRowCount(items.length);
3043
3301
  checklistRef.current?.setCount(items.length);
3044
3302
  }, [virtualizer, items.length]);
@@ -3086,6 +3344,10 @@ function ValueChecklist({
3086
3344
 
3087
3345
  // src/DataGrid/filterMenu/ExcelFilterEditor.tsx
3088
3346
  import { Fragment as Fragment7, jsx as jsx20, jsxs as jsxs15 } from "react/jsx-runtime";
3347
+ var OPENED_ON_VALUES = {
3348
+ kind: "values",
3349
+ focusOnMount: false
3350
+ };
3089
3351
  function toggleOf(item) {
3090
3352
  return item.kind === "blanks" ? { type: "setIncludeBlanks", includeBlanks: !item.checked } : { type: "toggleValue", value: item.value };
3091
3353
  }
@@ -3099,20 +3361,21 @@ function ExcelFilterEditor({
3099
3361
  columnValues,
3100
3362
  ref
3101
3363
  }) {
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);
3364
+ const filter = controller.getState().filter;
3365
+ const open = (over) => openValues(over, column.id, columnValues(column.id));
3366
+ const [{ opened, draft }, dispatch] = useReducer2(
3367
+ excelEditorReducer,
3368
+ filter,
3369
+ (over) => initialExcelEditorState(open(over))
3370
+ );
3371
+ if (filter !== opened.over) {
3372
+ dispatch({ type: "filterChanged", opened: open(filter) });
3373
+ }
3374
+ const [mode, setMode] = useState9(OPENED_ON_VALUES);
3375
+ const conditions = useRef13(null);
3376
+ const conditionsFocus = useRef13(null);
3377
+ const operatorButton = useRef13(null);
3378
+ const selectAllRef = useRef13(null);
3116
3379
  const selectAll = useMemo4(() => selectAllState(draft), [draft]);
3117
3380
  const checklist = useMemo4(
3118
3381
  () => filterChecklist(draft, {
@@ -3121,28 +3384,24 @@ function ExcelFilterEditor({
3121
3384
  }),
3122
3385
  [draft, menu.valueLimit, messages.filterBlanks]
3123
3386
  );
3124
- useLayoutEffect9(() => {
3387
+ useLayoutEffect11(() => {
3125
3388
  if (selectAllRef.current !== null) {
3126
3389
  selectAllRef.current.indeterminate = selectAll === "mixed";
3127
3390
  }
3128
3391
  });
3129
- useLayoutEffect9(() => {
3130
- if (switched.current) {
3131
- switched.current = false;
3132
- (operator === null ? operatorButton.current : conditionsFocus.current)?.focus();
3392
+ useLayoutEffect11(() => {
3393
+ if (mode.kind === "condition") {
3394
+ conditionsFocus.current?.focus();
3395
+ } else if (mode.focusOnMount) {
3396
+ operatorButton.current?.focus();
3133
3397
  }
3134
- }, [operator]);
3135
- function switchTo(chosen) {
3136
- switched.current = true;
3137
- setOperator(chosen);
3138
- }
3398
+ }, [mode]);
3139
3399
  useImperativeHandle2(ref, () => ({
3140
3400
  apply: () => {
3141
- if (operator !== null) {
3401
+ if (mode.kind === "condition") {
3142
3402
  return conditions.current?.apply() ?? true;
3143
3403
  }
3144
- const picked = draft.checked !== opened.draft.checked || draft.includeBlanks !== opened.draft.includeBlanks;
3145
- if (picked) {
3404
+ if (filterDraftChanged3(opened.draft, draft)) {
3146
3405
  applyFilter(
3147
3406
  draftToFilter2(draft, column.id, controller.getState().filter)
3148
3407
  );
@@ -3150,7 +3409,7 @@ function ExcelFilterEditor({
3150
3409
  return true;
3151
3410
  }
3152
3411
  }));
3153
- if (operator !== null) {
3412
+ if (mode.kind === "condition") {
3154
3413
  return /* @__PURE__ */ jsxs15(Fragment7, { children: [
3155
3414
  /* @__PURE__ */ jsxs15(
3156
3415
  "button",
@@ -3158,7 +3417,7 @@ function ExcelFilterEditor({
3158
3417
  type: "button",
3159
3418
  className: "filter-menu-back",
3160
3419
  onClick: () => {
3161
- switchTo(null);
3420
+ setMode({ kind: "values", focusOnMount: true });
3162
3421
  },
3163
3422
  children: [
3164
3423
  /* @__PURE__ */ jsx20("span", { className: "filter-menu-back-icon", "aria-hidden": "true", children: /* @__PURE__ */ jsx20(ChevronLeftIcon2, {}) }),
@@ -3178,7 +3437,7 @@ function ExcelFilterEditor({
3178
3437
  conditionsFocus.current = element;
3179
3438
  },
3180
3439
  applyFilter,
3181
- operator
3440
+ operator: mode.operator
3182
3441
  }
3183
3442
  )
3184
3443
  ] });
@@ -3192,7 +3451,9 @@ function ExcelFilterEditor({
3192
3451
  menu,
3193
3452
  active: shape.kind === "conditions" ? shape.conditions.map((condition) => condition.operator) : void 0,
3194
3453
  messages,
3195
- onChoose: switchTo
3454
+ onChoose: (operator) => {
3455
+ setMode({ kind: "condition", operator });
3456
+ }
3196
3457
  }
3197
3458
  ),
3198
3459
  (shape.kind === "conditions" || shape.kind === "custom") && /* @__PURE__ */ jsx20("p", { className: "filter-menu-custom", children: shape.kind === "conditions" ? messages.filterByCondition : messages.customFilter }),
@@ -3260,10 +3521,10 @@ function FilterMenu({
3260
3521
  applyFilter,
3261
3522
  columnValues
3262
3523
  }) {
3263
- const editor = useRef11(null);
3524
+ const editor = useRef14(null);
3264
3525
  const column = target.area === "header" ? controller.getView().dataColumns.find((entry) => entry.id === target.columnId) : void 0;
3265
3526
  const menu = column === void 0 ? null : resolveColumnFilterMenu2(controller.getOptions(), column.column);
3266
- useEffect3(() => {
3527
+ useEffect4(() => {
3267
3528
  if (menu === null) {
3268
3529
  close();
3269
3530
  }
@@ -3364,23 +3625,29 @@ function FilterMenu({
3364
3625
  );
3365
3626
  }
3366
3627
 
3628
+ // src/DataGrid/useAnnounceWhenDrawn.ts
3629
+ import { useEffect as useEffect5, useState as useState10 } from "react";
3630
+ function useAnnounceWhenDrawn(announce) {
3631
+ const [requested, setRequested] = useState10(null);
3632
+ useEffect5(() => {
3633
+ if (requested?.drawn() === true) {
3634
+ announce(requested.text());
3635
+ }
3636
+ }, [requested, announce]);
3637
+ return setRequested;
3638
+ }
3639
+
3367
3640
  // src/DataGrid/filterMenu/useApplyFilter.ts
3368
- import { useEffect as useEffect4, useRef as useRef12 } from "react";
3369
3641
  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
- });
3642
+ const announceWhenDrawn = useAnnounceWhenDrawn(announce);
3382
3643
  return (filter) => {
3383
- unannounced.current = filter;
3644
+ announceWhenDrawn({
3645
+ drawn: () => controller.getState().filter === filter,
3646
+ text: () => messages.filterResults(
3647
+ controller.getView().rows.length,
3648
+ controller.getOptions().dataSource?.length ?? 0
3649
+ )
3650
+ });
3384
3651
  controller.setFilter(filter);
3385
3652
  };
3386
3653
  }
@@ -3417,14 +3684,14 @@ function GridPanels({
3417
3684
  commands,
3418
3685
  messages
3419
3686
  }) {
3420
- const initialFocus = useRef13(null);
3687
+ const initialFocus = useRef15(null);
3421
3688
  const applyFilter = useApplyFilter(
3422
3689
  controller,
3423
3690
  messages,
3424
3691
  commands.host.announce
3425
3692
  );
3426
3693
  const columnValues = useFilterValueCache(controller, messages);
3427
- const { panel, close } = panels;
3694
+ const { panel, close, returnFocusFallback } = panels;
3428
3695
  if (panel === null) {
3429
3696
  return null;
3430
3697
  }
@@ -3437,6 +3704,7 @@ function GridPanels({
3437
3704
  gap: panel.anchor instanceof Element ? ANCHOR_GAP : 0,
3438
3705
  initialFocus,
3439
3706
  returnFocusTo: panel.returnFocusTo,
3707
+ returnFocusFallback,
3440
3708
  ignoreScrollAfterInput: true,
3441
3709
  trapFocus,
3442
3710
  themeClassName: panel.themeClassName,
@@ -3477,7 +3745,7 @@ function panelApi(panels, controller) {
3477
3745
  return true;
3478
3746
  },
3479
3747
  closeFilterMenu: () => {
3480
- if (panels.panel?.kind === "filter") {
3748
+ if (panels.current()?.kind === "filter") {
3481
3749
  panels.close();
3482
3750
  }
3483
3751
  }
@@ -3485,7 +3753,7 @@ function panelApi(panels, controller) {
3485
3753
  }
3486
3754
 
3487
3755
  // src/DataGrid/panels/useGridPanels.ts
3488
- import { useLayoutEffect as useLayoutEffect10, useRef as useRef14, useState as useState12 } from "react";
3756
+ import { useRef as useRef16 } from "react";
3489
3757
  function toolbarItem(root, itemId) {
3490
3758
  const items = root.querySelectorAll(
3491
3759
  ":scope > .gridkit-toolbar > [data-gridkit-toolbar-item]"
@@ -3497,6 +3765,9 @@ function toolbarItem(root, itemId) {
3497
3765
  (item) => item.hasAttribute("data-gridkit-toolbar-more")
3498
3766
  ) ?? null;
3499
3767
  }
3768
+ function panelOf(overlay) {
3769
+ return overlay.kind === "panel" ? overlay.panel : null;
3770
+ }
3500
3771
  function headerCell2(headerTable, columnId) {
3501
3772
  const cells = headerTable?.tHead?.rows[0]?.cells;
3502
3773
  return cells === void 0 ? null : Array.from(cells).find(
@@ -3506,10 +3777,9 @@ function headerCell2(headerTable, columnId) {
3506
3777
  function useGridPanels({
3507
3778
  rootRef,
3508
3779
  headerTableRef,
3509
- nav
3780
+ overlay
3510
3781
  }) {
3511
- const [panel, setPanel] = useState12(null);
3512
- const openings = useRef14(0);
3782
+ const openings = useRef16(0);
3513
3783
  function placeFor(root, target) {
3514
3784
  const element = target.area === "toolbar" ? toolbarItem(root, target.itemId) : target.area === "header" ? headerCell2(headerTableRef.current, target.columnId) : null;
3515
3785
  if (element !== null) {
@@ -3527,38 +3797,35 @@ function useGridPanels({
3527
3797
  returnFocusTo: void 0
3528
3798
  };
3529
3799
  }
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
3800
  return {
3539
- panel,
3801
+ panel: panelOf(overlay.drawn),
3802
+ current: () => panelOf(overlay.current()),
3540
3803
  open: (kind, target) => {
3541
3804
  const root = rootRef.current;
3542
3805
  if (root === null) {
3543
3806
  return;
3544
3807
  }
3545
3808
  openings.current += 1;
3546
- setPanel({
3547
- key: openings.current,
3548
- kind,
3549
- target,
3550
- ...placeFor(root, target),
3551
- themeClassName: overlayThemeClassName(root)
3809
+ overlay.open({
3810
+ kind: "panel",
3811
+ panel: {
3812
+ key: openings.current,
3813
+ kind,
3814
+ target,
3815
+ ...placeFor(root, target),
3816
+ themeClassName: overlayThemeClassName(root)
3817
+ }
3552
3818
  });
3553
3819
  },
3554
3820
  close: () => {
3555
- setPanel(null);
3556
- }
3821
+ overlay.close("panel");
3822
+ },
3823
+ returnFocusFallback: overlay.returnFocusFallback
3557
3824
  };
3558
3825
  }
3559
3826
 
3560
3827
  // src/DataGrid/toolbar/GridToolbar.tsx
3561
- import { useLayoutEffect as useLayoutEffect13, useRef as useRef18 } from "react";
3828
+ import { useLayoutEffect as useLayoutEffect13, useRef as useRef19 } from "react";
3562
3829
  import {
3563
3830
  resolveToolbar,
3564
3831
  splitToolbarItems,
@@ -3567,7 +3834,6 @@ import {
3567
3834
  import { attachToolbar } from "@gridkitjs/dom";
3568
3835
 
3569
3836
  // src/DataGrid/toolbar/ToolbarMenuButton.tsx
3570
- import { useLayoutEffect as useLayoutEffect11, useRef as useRef15, useState as useState13 } from "react";
3571
3837
  import { Fragment as Fragment8, jsx as jsx23, jsxs as jsxs17 } from "react/jsx-runtime";
3572
3838
  function ToolbarMenuButton({
3573
3839
  itemKey,
@@ -3576,26 +3842,11 @@ function ToolbarMenuButton({
3576
3842
  icon,
3577
3843
  entries,
3578
3844
  more = false,
3579
- onMenuClose
3845
+ returnFocusFallback
3580
3846
  }) {
3581
- const [menu, setMenu] = useState13(null);
3582
- const menuBehaviourRef = useRef15(null);
3583
- useLayoutEffect11(() => {
3584
- if (menu !== null && menu.focusItem > 0) {
3585
- menuBehaviourRef.current?.focusItem(menu.focusItem);
3586
- }
3587
- }, [menu]);
3588
- function open(button, onLast = false) {
3589
- setMenu({
3590
- anchor: button,
3591
- themeClassName: overlayThemeClassName(button),
3592
- focusItem: onLast ? entries.filter((entry) => entry.type !== "separator").length - 1 : 0
3593
- });
3594
- }
3595
- function close() {
3596
- setMenu(null);
3597
- onMenuClose();
3598
- }
3847
+ const { state, close, buttonProps, behaviourRef } = useMenuButton({
3848
+ itemCount: entries.filter((entry) => entry.type !== "separator").length
3849
+ });
3599
3850
  return /* @__PURE__ */ jsxs17(Fragment8, { children: [
3600
3851
  /* @__PURE__ */ jsxs17(
3601
3852
  "button",
@@ -3605,21 +3856,7 @@ function ToolbarMenuButton({
3605
3856
  className: classNames("toolbar-button", more && "toolbar-more"),
3606
3857
  "data-gridkit-toolbar-item": itemKey,
3607
3858
  ...ariaAttr(more, "data-gridkit-toolbar-more", ""),
3608
- "aria-haspopup": "menu",
3609
- "aria-expanded": menu !== null,
3610
- onClick: (event) => {
3611
- if (menu === null) {
3612
- open(event.currentTarget);
3613
- } else {
3614
- close();
3615
- }
3616
- },
3617
- onKeyDown: (event) => {
3618
- if ((event.key === "ArrowDown" || event.key === "ArrowUp") && menu === null) {
3619
- event.preventDefault();
3620
- open(event.currentTarget, event.key === "ArrowUp");
3621
- }
3622
- },
3859
+ ...buttonProps,
3623
3860
  children: [
3624
3861
  icon !== void 0 && /* @__PURE__ */ jsx23("span", { className: "toolbar-button-icon", "aria-hidden": "true", children: /* @__PURE__ */ jsx23(MenuIcon, { name: icon }) }),
3625
3862
  label,
@@ -3627,22 +3864,23 @@ function ToolbarMenuButton({
3627
3864
  ]
3628
3865
  }
3629
3866
  ),
3630
- menu !== null && /* @__PURE__ */ jsx23(
3867
+ state.kind === "open" && /* @__PURE__ */ jsx23(
3631
3868
  Overlay,
3632
3869
  {
3633
- anchor: menu.anchor,
3870
+ anchor: state.anchor,
3634
3871
  placement: "bottom-start",
3635
- gap: ANCHOR_GAP,
3636
- returnFocusTo: menu.anchor,
3637
- themeClassName: menu.themeClassName,
3872
+ gap: state.gap,
3873
+ returnFocusTo: state.anchor,
3874
+ returnFocusFallback,
3875
+ themeClassName: state.themeClassName,
3638
3876
  onDismiss: close,
3639
3877
  children: /* @__PURE__ */ jsx23(
3640
3878
  GridMenu,
3641
3879
  {
3642
3880
  entries,
3643
3881
  label,
3644
- themeClassName: menu.themeClassName,
3645
- behaviourRef: menuBehaviourRef,
3882
+ themeClassName: state.themeClassName,
3883
+ behaviourRef,
3646
3884
  onRun: (item) => {
3647
3885
  item.run();
3648
3886
  close();
@@ -3656,7 +3894,7 @@ function ToolbarMenuButton({
3656
3894
  }
3657
3895
 
3658
3896
  // src/DataGrid/toolbar/ToolbarSearch.tsx
3659
- import { useEffect as useEffect5, useRef as useRef16, useState as useState14 } from "react";
3897
+ import { useEffect as useEffect6, useRef as useRef17, useState as useState12 } from "react";
3660
3898
  import {
3661
3899
  searchTextOf,
3662
3900
  setSearchText
@@ -3671,9 +3909,9 @@ function ToolbarSearch({
3671
3909
  announce
3672
3910
  }) {
3673
3911
  const applied = searchTextOf(controller.getState().filter);
3674
- const [draft, setDraft] = useState14(null);
3675
- const timer = useRef16(void 0);
3676
- const unannounced = useRef16(null);
3912
+ const [draft, setDraft] = useState12(null);
3913
+ const timer = useRef17(void 0);
3914
+ const announceWhenDrawn = useAnnounceWhenDrawn(announce);
3677
3915
  const text = draft !== null && draft.over === applied ? draft.text : applied;
3678
3916
  function apply(next, over) {
3679
3917
  clearTimeout(timer.current);
@@ -3684,17 +3922,14 @@ function ToolbarSearch({
3684
3922
  }
3685
3923
  const searched = setSearchText(filter, next);
3686
3924
  if (searched !== filter) {
3687
- unannounced.current = next;
3925
+ announceWhenDrawn({
3926
+ drawn: () => searchTextOf(controller.getState().filter) === next,
3927
+ text: () => messages.searchResults(controller.getView().rows.length)
3928
+ });
3688
3929
  controller.setFilter(searched);
3689
3930
  }
3690
3931
  }
3691
- useEffect5(() => {
3692
- if (unannounced.current === applied) {
3693
- unannounced.current = null;
3694
- announce(messages.searchResults(controller.getView().rows.length));
3695
- }
3696
- });
3697
- useEffect5(
3932
+ useEffect6(
3698
3933
  () => () => {
3699
3934
  clearTimeout(timer.current);
3700
3935
  },
@@ -3731,12 +3966,12 @@ function ToolbarSearch({
3731
3966
  }
3732
3967
 
3733
3968
  // src/DataGrid/toolbar/useToolbarOverflow.ts
3734
- import { useLayoutEffect as useLayoutEffect12, useRef as useRef17, useState as useState15 } from "react";
3969
+ import { useLayoutEffect as useLayoutEffect12, useRef as useRef18, useState as useState13 } from "react";
3735
3970
  import { observeToolbarOverflow } from "@gridkitjs/dom";
3736
3971
  function useToolbarOverflow(toolbarRef, items) {
3737
- const [fit, setFit] = useState15(items.length);
3738
- const overflow = useRef17(null);
3739
- const measuredLayout = useRef17("");
3972
+ const [fit, setFit] = useState13(items.length);
3973
+ const overflow = useRef18(null);
3974
+ const measuredLayout = useRef18("");
3740
3975
  const layout = JSON.stringify(
3741
3976
  items.map((item) => [
3742
3977
  item.type,
@@ -3843,8 +4078,7 @@ function Toolbar({
3843
4078
  messages,
3844
4079
  announce
3845
4080
  }) {
3846
- const toolbarRef = useRef18(null);
3847
- const menuClosed = useRef18(false);
4081
+ const toolbarRef = useRef19(null);
3848
4082
  const items = resolveToolbar(entries, {
3849
4083
  commands: commands.commands,
3850
4084
  contextFor: commands.contextFor,
@@ -3860,18 +4094,9 @@ function Toolbar({
3860
4094
  const toolbar = toolbarRef.current;
3861
4095
  return toolbar === null ? void 0 : attachToolbar(toolbar);
3862
4096
  }, []);
3863
- useLayoutEffect13(() => {
3864
- if (!menuClosed.current) {
3865
- return;
3866
- }
3867
- menuClosed.current = false;
4097
+ function toolbarTabStop() {
3868
4098
  const toolbar = toolbarRef.current;
3869
- if (toolbar !== null && document.activeElement === document.body) {
3870
- (toolbar.querySelector('[tabindex="0"]') ?? toolbar.querySelector("button, input"))?.focus();
3871
- }
3872
- });
3873
- function onMenuClose() {
3874
- menuClosed.current = true;
4099
+ return toolbar?.querySelector('[tabindex="0"]') ?? toolbar?.querySelector("button, input") ?? null;
3875
4100
  }
3876
4101
  function draw(item) {
3877
4102
  switch (item.type) {
@@ -3913,7 +4138,7 @@ function Toolbar({
3913
4138
  label: item.label,
3914
4139
  icon: item.icon,
3915
4140
  entries: item.items,
3916
- onMenuClose
4141
+ returnFocusFallback: toolbarTabStop
3917
4142
  },
3918
4143
  item.key
3919
4144
  );
@@ -3979,7 +4204,7 @@ function Toolbar({
3979
4204
  label: messages.moreToolbarItems,
3980
4205
  entries: overflow,
3981
4206
  more: true,
3982
- onMenuClose
4207
+ returnFocusFallback: toolbarTabStop
3983
4208
  }
3984
4209
  )
3985
4210
  ]
@@ -4028,7 +4253,7 @@ function useColumnDrag({
4028
4253
 
4029
4254
  // src/DataGrid/useColumnResize.ts
4030
4255
  import {
4031
- useState as useState16
4256
+ useState as useState14
4032
4257
  } from "react";
4033
4258
  import {
4034
4259
  beginColumnResize,
@@ -4042,7 +4267,8 @@ function useColumnResize({
4042
4267
  controller,
4043
4268
  columnSizeDefaults
4044
4269
  }) {
4045
- const [activeColumnId, setActiveColumnId] = useState16(null);
4270
+ const [activeColumnId, setActiveColumnId] = useState14(null);
4271
+ const startGesture = useGestureSignal();
4046
4272
  function startResize(entry, event) {
4047
4273
  event.stopPropagation();
4048
4274
  const session = beginColumnResize(
@@ -4052,11 +4278,10 @@ function useColumnResize({
4052
4278
  columnSizeDefaults
4053
4279
  );
4054
4280
  const base = controller.getState().columnSizing;
4055
- const started = startColumnResizeGesture(
4056
- event.currentTarget,
4057
- event,
4058
- session,
4059
- {
4281
+ const handle = event.currentTarget;
4282
+ const started = startGesture(
4283
+ handle,
4284
+ (signal) => startColumnResizeGesture(handle, event, session, {
4060
4285
  onMove(width) {
4061
4286
  controller.resizeColumn(session.columnId, width, "move");
4062
4287
  },
@@ -4080,8 +4305,9 @@ function useColumnResize({
4080
4305
  base,
4081
4306
  session.startWidth
4082
4307
  );
4083
- }
4084
- }
4308
+ },
4309
+ signal
4310
+ })
4085
4311
  );
4086
4312
  if (started) {
4087
4313
  setActiveColumnId(session.columnId);
@@ -4120,10 +4346,10 @@ function useColumnResize({
4120
4346
  }
4121
4347
 
4122
4348
  // src/DataGrid/useElementWidth.ts
4123
- import { useLayoutEffect as useLayoutEffect14, useState as useState17 } from "react";
4349
+ import { useLayoutEffect as useLayoutEffect14, useState as useState15 } from "react";
4124
4350
  import { observeElementWidth } from "@gridkitjs/dom";
4125
4351
  function useElementWidth(ref, enabled) {
4126
- const [width, setWidth] = useState17(null);
4352
+ const [width, setWidth] = useState15(null);
4127
4353
  useLayoutEffect14(() => {
4128
4354
  const element = ref.current;
4129
4355
  if (element === null || !enabled) {
@@ -4135,12 +4361,12 @@ function useElementWidth(ref, enabled) {
4135
4361
  }
4136
4362
 
4137
4363
  // src/DataGrid/useGridController.ts
4138
- import { useState as useState18, useSyncExternalStore as useSyncExternalStore2 } from "react";
4364
+ import { useState as useState16, useSyncExternalStore as useSyncExternalStore2 } from "react";
4139
4365
  import {
4140
4366
  createGridController
4141
4367
  } from "@gridkitjs/core";
4142
4368
  function useGridController(options) {
4143
- const [controller] = useState18(
4369
+ const [controller] = useState16(
4144
4370
  () => createGridController(options)
4145
4371
  );
4146
4372
  controller.setOptions(options);
@@ -4153,15 +4379,15 @@ function useGridController(options) {
4153
4379
  }
4154
4380
 
4155
4381
  // src/DataGrid/useInfiniteScroll.ts
4156
- import { useEffect as useEffect6, useRef as useRef19 } from "react";
4382
+ import { useEffect as useEffect7, useRef as useRef20 } from "react";
4157
4383
  import { attachInfiniteScroll } from "@gridkitjs/dom";
4158
4384
  function useInfiniteScroll({
4159
4385
  rootRef,
4160
4386
  sentinelRef,
4161
4387
  config
4162
4388
  }) {
4163
- const scroll = useRef19(null);
4164
- useEffect6(() => {
4389
+ const scroll = useRef20(null);
4390
+ useEffect7(() => {
4165
4391
  const root = rootRef.current;
4166
4392
  if (root === null) {
4167
4393
  return void 0;
@@ -4176,7 +4402,7 @@ function useInfiniteScroll({
4176
4402
  scroll.current = null;
4177
4403
  };
4178
4404
  }, [rootRef]);
4179
- useEffect6(() => {
4405
+ useEffect7(() => {
4180
4406
  scroll.current?.update({
4181
4407
  sentinel: sentinelRef.current,
4182
4408
  hasMore: config?.hasMore ?? false,
@@ -4188,10 +4414,10 @@ function useInfiniteScroll({
4188
4414
  }
4189
4415
 
4190
4416
  // src/DataGrid/useLiveAnnouncer.ts
4191
- import { useCallback, useLayoutEffect as useLayoutEffect15, useRef as useRef20 } from "react";
4417
+ import { useCallback as useCallback3, useLayoutEffect as useLayoutEffect15, useRef as useRef21 } from "react";
4192
4418
  import { createLiveAnnouncer } from "@gridkitjs/dom";
4193
4419
  function useLiveAnnouncer(containerRef) {
4194
- const announcer = useRef20(null);
4420
+ const announcer = useRef21(null);
4195
4421
  useLayoutEffect15(() => {
4196
4422
  const container = containerRef.current;
4197
4423
  if (container === null) {
@@ -4204,20 +4430,30 @@ function useLiveAnnouncer(containerRef) {
4204
4430
  announcer.current = null;
4205
4431
  };
4206
4432
  }, [containerRef]);
4207
- return useCallback((text) => {
4433
+ return useCallback3((text) => {
4208
4434
  announcer.current?.announce(text);
4209
4435
  }, []);
4210
4436
  }
4211
4437
 
4212
4438
  // src/DataGrid/useRowVirtualizer.ts
4213
- import { useLayoutEffect as useLayoutEffect16, useState as useState19 } from "react";
4439
+ import { useLayoutEffect as useLayoutEffect16, useRef as useRef22, useState as useState17 } from "react";
4214
4440
  import { createRowVirtualizer as createRowVirtualizer2 } from "@gridkitjs/dom";
4215
- var EMPTY_RANGE = {
4216
- startIndex: 0,
4217
- endIndex: -1,
4218
- offsetBefore: 0,
4219
- offsetAfter: 0
4441
+ var ALL_ROWS = { kind: "allRows" };
4442
+ var NO_ROWS3 = {
4443
+ kind: "range",
4444
+ range: { startIndex: 0, endIndex: -1, offsetBefore: 0, offsetAfter: 0 }
4220
4445
  };
4446
+ function rangeOf(rowWindow, rowCount) {
4447
+ if (rowWindow.kind === "range") {
4448
+ return rowWindow.range;
4449
+ }
4450
+ return {
4451
+ startIndex: 0,
4452
+ endIndex: rowCount - 1,
4453
+ offsetBefore: 0,
4454
+ offsetAfter: 0
4455
+ };
4456
+ }
4221
4457
  function ignoreRow() {
4222
4458
  return () => void 0;
4223
4459
  }
@@ -4228,8 +4464,18 @@ function useRowVirtualizer({
4228
4464
  estimatedRowHeight,
4229
4465
  overscan
4230
4466
  }) {
4231
- const [virtualizer, setVirtualizer] = useState19(null);
4232
- const [range, setRange] = useState19(EMPTY_RANGE);
4467
+ const [virtualizer, setVirtualizer] = useState17(null);
4468
+ const instance = useRef22(null);
4469
+ const rowCount = useRef22(rows.length);
4470
+ const [rowWindow, setRowWindow] = useState17(enabled ? NO_ROWS3 : ALL_ROWS);
4471
+ if (!enabled && rowWindow.kind !== "allRows") {
4472
+ setRowWindow(ALL_ROWS);
4473
+ }
4474
+ useLayoutEffect16(() => {
4475
+ rowCount.current = rows.length;
4476
+ instance.current?.setRowCount(rows.length);
4477
+ instance.current?.resetHeights();
4478
+ }, [rows]);
4233
4479
  useLayoutEffect16(() => {
4234
4480
  const element = bodyScrollRef.current;
4235
4481
  if (!enabled || element === null) {
@@ -4238,17 +4484,18 @@ function useRowVirtualizer({
4238
4484
  const created = createRowVirtualizer2(element, {
4239
4485
  estimatedRowHeight,
4240
4486
  overscan,
4241
- onRange: setRange
4487
+ onRange: (range2) => {
4488
+ setRowWindow({ kind: "range", range: range2 });
4489
+ }
4242
4490
  });
4491
+ created.setRowCount(rowCount.current);
4492
+ instance.current = created;
4243
4493
  setVirtualizer(created);
4244
4494
  return () => {
4245
4495
  created.destroy();
4246
4496
  };
4247
4497
  }, [enabled, bodyScrollRef, estimatedRowHeight, overscan]);
4248
- useLayoutEffect16(() => {
4249
- virtualizer?.setRowCount(rows.length);
4250
- virtualizer?.resetHeights();
4251
- }, [virtualizer, rows]);
4498
+ const range = rangeOf(rowWindow, rows.length);
4252
4499
  return {
4253
4500
  startIndex: range.startIndex,
4254
4501
  endIndex: range.endIndex,
@@ -4256,7 +4503,7 @@ function useRowVirtualizer({
4256
4503
  offsetAfter: range.offsetAfter,
4257
4504
  measureRow: virtualizer?.measureRow ?? ignoreRow,
4258
4505
  scrollToIndex: (index, options) => {
4259
- virtualizer?.scrollToIndex(index, options);
4506
+ instance.current?.scrollToIndex(index, options);
4260
4507
  }
4261
4508
  };
4262
4509
  }
@@ -4287,6 +4534,19 @@ function selectedRowsReader(controller) {
4287
4534
  return last.selected;
4288
4535
  };
4289
4536
  }
4537
+ function focusAt(rowIndex, columnIndex) {
4538
+ return rowIndex < 0 ? headerFocus3(columnIndex) : bodyFocus3(rowIndex, columnIndex);
4539
+ }
4540
+ function focusedCellReader() {
4541
+ let last;
4542
+ return (focus) => {
4543
+ if (last?.focus !== focus) {
4544
+ const rowIndex = focus.area === "header" ? -1 : focus.rowIndex;
4545
+ last = { focus, cell: { rowIndex, columnIndex: focus.columnIndex } };
4546
+ }
4547
+ return last.cell;
4548
+ };
4549
+ }
4290
4550
  function DataGrid(props) {
4291
4551
  const {
4292
4552
  borders,
@@ -4319,13 +4579,14 @@ function DataGrid(props) {
4319
4579
  const hoverRows = hoverable?.rows ?? true;
4320
4580
  const hoverColumns = hoverable?.columns ?? true;
4321
4581
  const hoverCells = hoverable?.cells ?? true;
4322
- const viewportRef = useRef21(null);
4323
- const headerTableRef = useRef21(null);
4324
- const bodyTableRef = useRef21(null);
4325
- const bodyScrollRef = useRef21(null);
4326
- const sentinelRef = useRef21(null);
4582
+ const viewportRef = useRef23(null);
4583
+ const gridRef = useRef23(null);
4584
+ const headerTableRef = useRef23(null);
4585
+ const bodyTableRef = useRef23(null);
4586
+ const bodyScrollRef = useRef23(null);
4587
+ const sentinelRef = useRef23(null);
4327
4588
  const viewportWidth = useElementWidth(viewportRef, resizeMode === "fit");
4328
- const subscribersRef = useRef21(/* @__PURE__ */ new Set());
4589
+ const subscribersRef = useRef23(/* @__PURE__ */ new Set());
4329
4590
  const gridId = useId2();
4330
4591
  const announce = useLiveAnnouncer(viewportRef);
4331
4592
  const messages = useMemo5(
@@ -4343,8 +4604,9 @@ function DataGrid(props) {
4343
4604
  columnOf
4344
4605
  })
4345
4606
  });
4346
- const [readPagination] = useState20(() => paginationReader(controller));
4347
- const [readSelectedRows] = useState20(() => selectedRowsReader(controller));
4607
+ const [readPagination] = useState18(() => paginationReader(controller));
4608
+ const [readSelectedRows] = useState18(() => selectedRowsReader(controller));
4609
+ const [readFocusedCell] = useState18(focusedCellReader);
4348
4610
  const virtualizer = useRowVirtualizer({
4349
4611
  enabled: virtualized,
4350
4612
  rows: view.pageRows,
@@ -4352,7 +4614,7 @@ function DataGrid(props) {
4352
4614
  estimatedRowHeight,
4353
4615
  overscan
4354
4616
  });
4355
- useEffect7(() => {
4617
+ useEffect8(() => {
4356
4618
  if (virtualized && height === void 0) {
4357
4619
  console.error(
4358
4620
  "DataGrid: `virtualized` has no effect without `height` \u2014 a body with no bounded height has no viewport to window rows against."
@@ -4360,7 +4622,7 @@ function DataGrid(props) {
4360
4622
  }
4361
4623
  }, [virtualized, height]);
4362
4624
  const hasInfiniteScroll = infiniteScroll !== void 0;
4363
- useEffect7(() => {
4625
+ useEffect8(() => {
4364
4626
  if (paginated && hasInfiniteScroll) {
4365
4627
  console.error(
4366
4628
  "DataGrid: `paginated` and `infiniteScroll` are mutually exclusive \u2014 only `paginated` takes effect while both are set."
@@ -4389,6 +4651,7 @@ function DataGrid(props) {
4389
4651
  columnSizeDefaults
4390
4652
  });
4391
4653
  const nav = useGridNavigation({
4654
+ gridRef,
4392
4655
  headerTableRef,
4393
4656
  bodyTableRef,
4394
4657
  // `null` when virtualization is off, in which case behavior is
@@ -4411,7 +4674,15 @@ function DataGrid(props) {
4411
4674
  return entry !== void 0 && "kind" in entry && entry.kind === "group-summary";
4412
4675
  }
4413
4676
  });
4414
- const panels = useGridPanels({ rootRef: viewportRef, headerTableRef, nav });
4677
+ const overlay = useGridOverlay({
4678
+ nav,
4679
+ onContextMenuOpenChange: props.onContextMenuOpenChange
4680
+ });
4681
+ const panels = useGridPanels({
4682
+ rootRef: viewportRef,
4683
+ headerTableRef,
4684
+ overlay
4685
+ });
4415
4686
  const commands = useGridCommands({
4416
4687
  controller,
4417
4688
  commands: props.commands,
@@ -4455,7 +4726,7 @@ function DataGrid(props) {
4455
4726
  announce(
4456
4727
  messages.columnMoved(columnName(movedId), position + 1, shown.length)
4457
4728
  );
4458
- nav.focusCell(HEADER_ROW, position);
4729
+ nav.focusCell(headerFocus3(position));
4459
4730
  }
4460
4731
  const drag = useColumnDrag({
4461
4732
  order: displayedIds,
@@ -4485,7 +4756,7 @@ function DataGrid(props) {
4485
4756
  bodyTableRef,
4486
4757
  bodyScrollRef,
4487
4758
  contextMenu: props.contextMenu,
4488
- onContextMenuOpenChange: props.onContextMenuOpenChange,
4759
+ overlay,
4489
4760
  controller,
4490
4761
  commands,
4491
4762
  nav,
@@ -4493,7 +4764,7 @@ function DataGrid(props) {
4493
4764
  messages,
4494
4765
  columnName
4495
4766
  });
4496
- useEffect7(() => {
4767
+ useEffect8(() => {
4497
4768
  for (const listener of subscribersRef.current) {
4498
4769
  listener();
4499
4770
  }
@@ -4527,10 +4798,12 @@ function DataGrid(props) {
4527
4798
  getSelectedRows: readSelectedRows,
4528
4799
  getColumnSelection: () => controller.getState().columnSelection,
4529
4800
  getCellSelection: () => controller.getState().cellSelection,
4530
- getFocusedCell: () => nav.focus,
4801
+ getFocusedCell: () => readFocusedCell(nav.focus),
4531
4802
  getState: controller.getState,
4532
4803
  restoreState: controller.restoreState,
4533
- focusCell: nav.focusCell,
4804
+ focusCell: (rowIndex, columnIndex) => {
4805
+ nav.focusCell(focusAt(rowIndex, columnIndex));
4806
+ },
4534
4807
  clearSelection: controller.clearSelection,
4535
4808
  selectAllRows: controller.selectAllRows,
4536
4809
  selectRows: controller.selectRows,
@@ -4638,6 +4911,7 @@ function DataGrid(props) {
4638
4911
  /* @__PURE__ */ jsxs19(
4639
4912
  "div",
4640
4913
  {
4914
+ ref: gridRef,
4641
4915
  id: gridId,
4642
4916
  className: "gridkit-data-grid-tables",
4643
4917
  role: state.groupBy.length > 0 ? "treegrid" : "grid",
@@ -4788,23 +5062,41 @@ function DataGrid(props) {
4788
5062
  import { serializeGridState } from "@gridkitjs/core";
4789
5063
 
4790
5064
  // src/DataGrid/hooks/usePaginationState.ts
4791
- import { useCallback as useCallback3 } from "react";
5065
+ import { useCallback as useCallback5 } from "react";
4792
5066
 
4793
5067
  // src/DataGrid/hooks/useGridSnapshot.ts
4794
- import { useCallback as useCallback2, useSyncExternalStore as useSyncExternalStore3 } from "react";
5068
+ import {
5069
+ useCallback as useCallback4,
5070
+ useEffect as useEffect9,
5071
+ useRef as useRef24,
5072
+ useSyncExternalStore as useSyncExternalStore3
5073
+ } from "react";
5074
+ function subscribeTo(api, listener) {
5075
+ return api === null ? { kind: "waiting", listener } : {
5076
+ kind: "subscribed",
5077
+ listener,
5078
+ api,
5079
+ unsubscribe: api.subscribe(listener)
5080
+ };
5081
+ }
5082
+ function unsubscribe(subscription) {
5083
+ if (subscription?.kind === "subscribed") {
5084
+ subscription.unsubscribe();
5085
+ }
5086
+ }
4795
5087
  function useGridSnapshot(gridRef, getSnapshot, fallback) {
4796
- const subscribe = useCallback2(
5088
+ const subscription = useRef24(null);
5089
+ const subscribe = useCallback4(
4797
5090
  (listener) => {
4798
- const api = gridRef.current;
4799
- if (api === null) {
4800
- return () => {
4801
- };
4802
- }
4803
- return api.subscribe(listener);
5091
+ subscription.current = subscribeTo(gridRef.current, listener);
5092
+ return () => {
5093
+ unsubscribe(subscription.current);
5094
+ subscription.current = null;
5095
+ };
4804
5096
  },
4805
5097
  [gridRef]
4806
5098
  );
4807
- return useSyncExternalStore3(
5099
+ const snapshot = useSyncExternalStore3(
4808
5100
  subscribe,
4809
5101
  () => {
4810
5102
  const api = gridRef.current;
@@ -4812,6 +5104,20 @@ function useGridSnapshot(gridRef, getSnapshot, fallback) {
4812
5104
  },
4813
5105
  () => fallback
4814
5106
  );
5107
+ useEffect9(() => {
5108
+ const current = subscription.current;
5109
+ if (current === null) {
5110
+ return;
5111
+ }
5112
+ const api = gridRef.current;
5113
+ if (api === (current.kind === "subscribed" ? current.api : null)) {
5114
+ return;
5115
+ }
5116
+ unsubscribe(current);
5117
+ subscription.current = subscribeTo(api, current.listener);
5118
+ current.listener();
5119
+ });
5120
+ return snapshot;
4815
5121
  }
4816
5122
 
4817
5123
  // src/DataGrid/hooks/usePaginationState.ts
@@ -4819,12 +5125,12 @@ var FALLBACK_PAGINATION = { pageIndex: 0, pageSize: 0 };
4819
5125
  function usePaginationState(gridRef) {
4820
5126
  const pagination = useGridSnapshot(
4821
5127
  gridRef,
4822
- useCallback3((api) => api.getPagination(), []),
5128
+ useCallback5((api) => api.getPagination(), []),
4823
5129
  FALLBACK_PAGINATION
4824
5130
  );
4825
5131
  const pageCount = useGridSnapshot(
4826
5132
  gridRef,
4827
- useCallback3((api) => api.getPageCount(), []),
5133
+ useCallback5((api) => api.getPageCount(), []),
4828
5134
  0
4829
5135
  );
4830
5136
  return {
@@ -4846,18 +5152,21 @@ function usePaginationState(gridRef) {
4846
5152
  }
4847
5153
 
4848
5154
  // src/DataGrid/hooks/useGroupByState.ts
4849
- import { useCallback as useCallback4 } from "react";
5155
+ import { useCallback as useCallback6 } from "react";
5156
+ import {
5157
+ ALL_EXPANDED
5158
+ } from "@gridkitjs/core";
4850
5159
  var FALLBACK_GROUP_BY = [];
4851
- var FALLBACK_EXPANSION = [];
5160
+ var FALLBACK_EXPANSION = ALL_EXPANDED;
4852
5161
  function useGroupByState(gridRef) {
4853
5162
  const groupBy = useGridSnapshot(
4854
5163
  gridRef,
4855
- useCallback4((api) => api.getGroupBy(), []),
5164
+ useCallback6((api) => api.getGroupBy(), []),
4856
5165
  FALLBACK_GROUP_BY
4857
5166
  );
4858
5167
  const groupExpansion = useGridSnapshot(
4859
5168
  gridRef,
4860
- useCallback4((api) => api.getGroupExpansion(), []),
5169
+ useCallback6((api) => api.getGroupExpansion(), []),
4861
5170
  FALLBACK_EXPANSION
4862
5171
  );
4863
5172
  return {
@@ -4873,34 +5182,34 @@ function useGroupByState(gridRef) {
4873
5182
  }
4874
5183
 
4875
5184
  // src/DataGrid/hooks/useColumnSortState.ts
4876
- import { useCallback as useCallback5 } from "react";
5185
+ import { useCallback as useCallback7 } from "react";
4877
5186
  var FALLBACK_SORT = [];
4878
5187
  function useColumnSortState(gridRef) {
4879
5188
  return useGridSnapshot(
4880
5189
  gridRef,
4881
- useCallback5((api) => api.getColumnSort(), []),
5190
+ useCallback7((api) => api.getColumnSort(), []),
4882
5191
  FALLBACK_SORT
4883
5192
  );
4884
5193
  }
4885
5194
 
4886
5195
  // src/DataGrid/hooks/useSelectionState.ts
4887
- import { useCallback as useCallback6 } from "react";
5196
+ import { useCallback as useCallback8 } from "react";
4888
5197
  var FALLBACK_SELECTION = [];
4889
5198
  var FALLBACK_CELL_SELECTION = null;
4890
5199
  function useSelectionState(gridRef) {
4891
5200
  const rowSelection = useGridSnapshot(
4892
5201
  gridRef,
4893
- useCallback6((api) => api.getRowSelection(), []),
5202
+ useCallback8((api) => api.getRowSelection(), []),
4894
5203
  FALLBACK_SELECTION
4895
5204
  );
4896
5205
  const columnSelection = useGridSnapshot(
4897
5206
  gridRef,
4898
- useCallback6((api) => api.getColumnSelection(), []),
5207
+ useCallback8((api) => api.getColumnSelection(), []),
4899
5208
  FALLBACK_SELECTION
4900
5209
  );
4901
5210
  const cellSelection = useGridSnapshot(
4902
5211
  gridRef,
4903
- useCallback6((api) => api.getCellSelection(), []),
5212
+ useCallback8((api) => api.getCellSelection(), []),
4904
5213
  FALLBACK_CELL_SELECTION
4905
5214
  );
4906
5215
  return {
@@ -4917,56 +5226,56 @@ function useSelectionState(gridRef) {
4917
5226
  }
4918
5227
 
4919
5228
  // src/DataGrid/hooks/useAggregateState.ts
4920
- import { useCallback as useCallback7 } from "react";
5229
+ import { useCallback as useCallback9 } from "react";
4921
5230
  var FALLBACK_AGGREGATES = /* @__PURE__ */ new Map();
4922
5231
  function useAggregateState(gridRef) {
4923
5232
  return useGridSnapshot(
4924
5233
  gridRef,
4925
- useCallback7((api) => api.getAggregates(), []),
5234
+ useCallback9((api) => api.getAggregates(), []),
4926
5235
  FALLBACK_AGGREGATES
4927
5236
  );
4928
5237
  }
4929
5238
 
4930
5239
  // src/DataGrid/hooks/useColumnSizingState.ts
4931
- import { useCallback as useCallback8 } from "react";
5240
+ import { useCallback as useCallback10 } from "react";
4932
5241
  var FALLBACK_SIZING = {};
4933
5242
  function useColumnSizingState(gridRef) {
4934
5243
  return useGridSnapshot(
4935
5244
  gridRef,
4936
- useCallback8((api) => api.getColumnSizing(), []),
5245
+ useCallback10((api) => api.getColumnSizing(), []),
4937
5246
  FALLBACK_SIZING
4938
5247
  );
4939
5248
  }
4940
5249
 
4941
5250
  // src/DataGrid/hooks/useColumnOrderState.ts
4942
- import { useCallback as useCallback9 } from "react";
5251
+ import { useCallback as useCallback11 } from "react";
4943
5252
  var FALLBACK_ORDER = [];
4944
5253
  function useColumnOrderState(gridRef) {
4945
5254
  return useGridSnapshot(
4946
5255
  gridRef,
4947
- useCallback9((api) => api.getColumnOrder(), []),
5256
+ useCallback11((api) => api.getColumnOrder(), []),
4948
5257
  FALLBACK_ORDER
4949
5258
  );
4950
5259
  }
4951
5260
 
4952
5261
  // src/DataGrid/hooks/useColumnVisibilityState.ts
4953
- import { useCallback as useCallback10 } from "react";
5262
+ import { useCallback as useCallback12 } from "react";
4954
5263
  var FALLBACK_VISIBILITY = {};
4955
5264
  function useColumnVisibilityState(gridRef) {
4956
5265
  return useGridSnapshot(
4957
5266
  gridRef,
4958
- useCallback10((api) => api.getColumnVisibility(), []),
5267
+ useCallback12((api) => api.getColumnVisibility(), []),
4959
5268
  FALLBACK_VISIBILITY
4960
5269
  );
4961
5270
  }
4962
5271
 
4963
5272
  // src/DataGrid/hooks/useFilterState.ts
4964
- import { useCallback as useCallback11, useState as useState21 } from "react";
5273
+ import { useCallback as useCallback13, useState as useState19 } from "react";
4965
5274
  function useFilterState(gridRef) {
4966
- const [fallback] = useState21(() => []);
5275
+ const [fallback] = useState19(() => []);
4967
5276
  const filter = useGridSnapshot(
4968
5277
  gridRef,
4969
- useCallback11((api) => api.getFilter(), []),
5278
+ useCallback13((api) => api.getFilter(), []),
4970
5279
  fallback
4971
5280
  );
4972
5281
  return {