@gridkitjs/react 0.0.0-dev-20261108231045 → 0.0.0-dev-20261109083020

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
@@ -6,7 +6,7 @@ import {
6
6
  useId as useId2,
7
7
  useImperativeHandle as useImperativeHandle3,
8
8
  useMemo as useMemo5,
9
- useRef as useRef22,
9
+ useRef as useRef21,
10
10
  useState as useState18
11
11
  } from "react";
12
12
  import {
@@ -1767,32 +1767,42 @@ function exportApi({
1767
1767
  }
1768
1768
 
1769
1769
  // src/DataGrid/export/useSaveFile.ts
1770
- function download(file) {
1771
- downloadBlob(exportBlob(file), file.fileName);
1772
- }
1770
+ var IDLE = { kind: "idle" };
1771
+ var WRITING = { kind: "writing" };
1773
1772
  function useSaveFile({
1774
1773
  messages,
1775
1774
  announce
1776
1775
  }) {
1777
- const writingRef = useRef3(false);
1776
+ const state = useRef3(IDLE);
1777
+ function fail(reason, error) {
1778
+ console.error(`DataGrid: ${reason}`, error);
1779
+ announce(messages.exportFailed);
1780
+ }
1781
+ function download(file) {
1782
+ try {
1783
+ downloadBlob(exportBlob(file), file.fileName);
1784
+ } catch (error) {
1785
+ fail("the export could not be saved.", error);
1786
+ }
1787
+ }
1778
1788
  return (file) => {
1779
- if (writingRef.current) {
1789
+ if (state.current.kind === "writing") {
1780
1790
  console.error(
1781
1791
  "DataGrid: an export is still being written, so this one was dropped."
1782
1792
  );
1783
- return;
1793
+ return false;
1784
1794
  }
1785
1795
  if (!(file instanceof Promise)) {
1786
1796
  download(file);
1787
- return;
1797
+ return true;
1788
1798
  }
1789
- writingRef.current = true;
1799
+ state.current = WRITING;
1790
1800
  file.then(download, (error) => {
1791
- console.error("DataGrid: the export's writer failed.", error);
1792
- announce(messages.exportFailed);
1801
+ fail("the export's writer failed.", error);
1793
1802
  }).finally(() => {
1794
- writingRef.current = false;
1803
+ state.current = IDLE;
1795
1804
  });
1805
+ return true;
1796
1806
  };
1797
1807
  }
1798
1808
 
@@ -1855,6 +1865,7 @@ function useGridCommands({
1855
1865
  print: (document2, options) => {
1856
1866
  printGrid(document2, options).catch((error) => {
1857
1867
  console.error("DataGrid: the print failed.", error);
1868
+ announce(messages.printFailed);
1858
1869
  });
1859
1870
  }
1860
1871
  };
@@ -2544,7 +2555,7 @@ function useGridOverlay({
2544
2555
  }
2545
2556
 
2546
2557
  // src/DataGrid/panels/GridPanels.tsx
2547
- import { useRef as useRef15 } from "react";
2558
+ import { useRef as useRef14 } from "react";
2548
2559
 
2549
2560
  // src/DataGrid/columnChooser/ColumnChooser.tsx
2550
2561
  import { useLayoutEffect as useLayoutEffect6, useRef as useRef8, useState as useState6 } from "react";
@@ -2644,19 +2655,45 @@ import {
2644
2655
  useImperativeHandle as useImperativeHandle2,
2645
2656
  useLayoutEffect as useLayoutEffect10,
2646
2657
  useMemo as useMemo4,
2647
- useReducer,
2658
+ useReducer as useReducer2,
2648
2659
  useRef as useRef12,
2649
- useState as useState10
2660
+ useState as useState9
2650
2661
  } from "react";
2651
2662
  import {
2652
- describeColumnFilter as describeColumnFilter2,
2653
2663
  draftToFilter as draftToFilter2,
2654
2664
  filterChecklist,
2655
- filterEditorReducer as filterEditorReducer2,
2656
- initialFilterEditorDraft as initialFilterEditorDraft2,
2665
+ filterDraftChanged as filterDraftChanged3,
2657
2666
  selectAllState
2658
2667
  } from "@gridkitjs/core";
2659
2668
 
2669
+ // src/DataGrid/filterMenu/excelEditorState.ts
2670
+ import {
2671
+ describeColumnFilter,
2672
+ filterDraftChanged,
2673
+ filterEditorReducer,
2674
+ initialFilterEditorDraft
2675
+ } from "@gridkitjs/core";
2676
+ function openValues(filter, columnId, values) {
2677
+ const shape = describeColumnFilter(filter, columnId);
2678
+ return {
2679
+ over: filter,
2680
+ shape,
2681
+ draft: initialFilterEditorDraft(shape, values)
2682
+ };
2683
+ }
2684
+ function initialExcelEditorState(opened) {
2685
+ return { opened, draft: opened.draft };
2686
+ }
2687
+ function showsAlike(a, b) {
2688
+ return a.shape.kind === b.shape.kind && a.draft.options === b.draft.options && !filterDraftChanged(a.draft, b.draft);
2689
+ }
2690
+ function excelEditorReducer(state, action) {
2691
+ if (action.type === "filterChanged") {
2692
+ return showsAlike(state.opened, action.opened) ? { ...state, opened: action.opened } : initialExcelEditorState(action.opened);
2693
+ }
2694
+ return { ...state, draft: filterEditorReducer(state.draft, action) };
2695
+ }
2696
+
2660
2697
  // src/DataGrid/menu/useMenuButton.ts
2661
2698
  import {
2662
2699
  useLayoutEffect as useLayoutEffect7,
@@ -2789,16 +2826,14 @@ import {
2789
2826
  useId,
2790
2827
  useImperativeHandle,
2791
2828
  useLayoutEffect as useLayoutEffect8,
2792
- useRef as useRef10,
2793
- useState as useState8
2829
+ useReducer,
2830
+ useRef as useRef10
2794
2831
  } from "react";
2795
2832
  import {
2796
- conditionRowsFrom,
2797
- conditionsFromRows,
2798
- describeColumnFilter,
2833
+ conditionsFromRows as conditionsFromRows2,
2799
2834
  draftToFilter,
2800
- filterEditorReducer,
2801
- initialFilterEditorDraft
2835
+ filterDraftChanged as filterDraftChanged2,
2836
+ filterEditorReducer as filterEditorReducer3
2802
2837
  } from "@gridkitjs/core";
2803
2838
 
2804
2839
  // src/DataGrid/filterMenu/ConditionRow.tsx
@@ -2919,9 +2954,14 @@ function ConditionRow({
2919
2954
  );
2920
2955
  }
2921
2956
 
2922
- // src/DataGrid/filterMenu/SimpleFilterEditor.tsx
2923
- import { jsx as jsx18, jsxs as jsxs13 } from "react/jsx-runtime";
2924
- var COMBINATORS = ["and", "or"];
2957
+ // src/DataGrid/filterMenu/simpleEditorState.ts
2958
+ import {
2959
+ conditionRowsFrom,
2960
+ conditionsFromRows,
2961
+ describeColumnFilter as describeColumnFilter2,
2962
+ filterEditorReducer as filterEditorReducer2,
2963
+ initialFilterEditorDraft as initialFilterEditorDraft2
2964
+ } from "@gridkitjs/core";
2925
2965
  var NO_ROWS = [];
2926
2966
  var NO_VALUES = {
2927
2967
  options: [],
@@ -2929,6 +2969,77 @@ var NO_VALUES = {
2929
2969
  truncated: false,
2930
2970
  blanks: 0
2931
2971
  };
2972
+ function filterDraftShown(shape, rows, columnId, type) {
2973
+ const draft = initialFilterEditorDraft2(shape, {
2974
+ ...NO_VALUES,
2975
+ columnType: type
2976
+ });
2977
+ if (shape.kind !== "conditions") {
2978
+ return draft;
2979
+ }
2980
+ const shown = conditionsFromRows(rows, columnId, type);
2981
+ return shown.invalid.length > 0 ? draft : filterEditorReducer2(draft, {
2982
+ type: "setConditions",
2983
+ conditions: shown.conditions
2984
+ });
2985
+ }
2986
+ function openConditions(filter, columnId, operators, type, operator) {
2987
+ const shape = describeColumnFilter2(filter, columnId);
2988
+ const rows = conditionRowsFrom(shape, operators, type);
2989
+ return {
2990
+ over: filter,
2991
+ custom: shape.kind === "custom" || shape.kind === "values" && operator === void 0,
2992
+ draft: {
2993
+ // Its typed values kept, as picking the operator in the row keeps them.
2994
+ rows: operator === void 0 ? rows : rows.map(
2995
+ (row, index) => index === 0 ? { ...row, operator } : row
2996
+ ),
2997
+ combinator: shape.kind === "conditions" ? shape.combinator : "and"
2998
+ },
2999
+ filterDraft: filterDraftShown(shape, rows, columnId, type)
3000
+ };
3001
+ }
3002
+ function initialSimpleEditorState(opened) {
3003
+ return { opened, draft: opened.draft, invalid: NO_ROWS };
3004
+ }
3005
+ function sameConditionsDraft(a, b) {
3006
+ return a.combinator === b.combinator && a.rows.length === b.rows.length && a.rows.every((row, index) => {
3007
+ const other = b.rows[index];
3008
+ return other !== void 0 && row.operator === other.operator && row.text === other.text && row.textTo === other.textTo;
3009
+ });
3010
+ }
3011
+ function showsAlike2(a, b) {
3012
+ return a.custom === b.custom && sameConditionsDraft(a.draft, b.draft);
3013
+ }
3014
+ function simpleEditorReducer(state, action) {
3015
+ switch (action.type) {
3016
+ case "editRow":
3017
+ return {
3018
+ ...state,
3019
+ draft: {
3020
+ ...state.draft,
3021
+ rows: state.draft.rows.map(
3022
+ (row, index) => index === action.index ? action.row : row
3023
+ )
3024
+ },
3025
+ invalid: NO_ROWS
3026
+ };
3027
+ case "setCombinator":
3028
+ return {
3029
+ ...state,
3030
+ draft: { ...state.draft, combinator: action.combinator },
3031
+ invalid: NO_ROWS
3032
+ };
3033
+ case "markInvalid":
3034
+ return { ...state, invalid: action.rows };
3035
+ case "filterChanged":
3036
+ return showsAlike2(state.opened, action.opened) ? { ...state, opened: action.opened } : initialSimpleEditorState(action.opened);
3037
+ }
3038
+ }
3039
+
3040
+ // src/DataGrid/filterMenu/SimpleFilterEditor.tsx
3041
+ import { jsx as jsx18, jsxs as jsxs13 } from "react/jsx-runtime";
3042
+ var COMBINATORS = ["and", "or"];
2932
3043
  function holdsUnreadable(row) {
2933
3044
  return Array.from(row?.querySelectorAll("input") ?? []).some(
2934
3045
  (input) => input.validity.badInput
@@ -2974,42 +3085,34 @@ function SimpleFilterEditor({
2974
3085
  ref
2975
3086
  }) {
2976
3087
  const type = column.column.type;
2977
- const [opened] = useState8(() => {
2978
- const shape = describeColumnFilter(controller.getState().filter, column.id);
2979
- const rows = conditionRowsFrom(shape, menu.operators, type);
2980
- const draft2 = {
2981
- // Its typed values kept, as picking the operator in the row keeps them.
2982
- rows: operator === void 0 ? rows : rows.map(
2983
- (row, index) => index === 0 ? { ...row, operator } : row
2984
- ),
2985
- combinator: shape.kind === "conditions" ? shape.combinator : "and"
2986
- };
2987
- return {
2988
- custom: shape.kind === "custom" || shape.kind === "values" && operator === void 0,
2989
- draft: draft2
2990
- };
2991
- });
2992
- const [draft, setDraft] = useState8(opened.draft);
2993
- const [invalid, setInvalid] = useState8(NO_ROWS);
3088
+ const filter = controller.getState().filter;
3089
+ const open = (over) => openConditions(over, column.id, menu.operators, type, operator);
3090
+ const [{ opened, draft, invalid }, dispatch] = useReducer(
3091
+ simpleEditorReducer,
3092
+ filter,
3093
+ (over) => initialSimpleEditorState(open(over))
3094
+ );
3095
+ if (filter !== opened.over) {
3096
+ dispatch({ type: "filterChanged", opened: open(filter) });
3097
+ }
2994
3098
  const rowElements = useRef10([]);
2995
3099
  const invalidMessageId = useId();
2996
3100
  const combinatorName = useId();
2997
- function update(next) {
2998
- setDraft(next);
2999
- setInvalid(NO_ROWS);
3000
- }
3001
3101
  function applyConditions(conditions) {
3002
- const applied = filterEditorReducer(
3003
- initialFilterEditorDraft({ kind: "none" }, NO_VALUES),
3004
- { type: "setConditions", conditions, combinator: draft.combinator }
3005
- );
3006
- applyFilter(
3007
- draftToFilter(applied, column.id, controller.getState().filter)
3008
- );
3102
+ const applied = filterEditorReducer3(opened.filterDraft, {
3103
+ type: "setConditions",
3104
+ conditions,
3105
+ combinator: draft.combinator
3106
+ });
3107
+ if (filterDraftChanged2(opened.filterDraft, applied)) {
3108
+ applyFilter(
3109
+ draftToFilter(applied, column.id, controller.getState().filter)
3110
+ );
3111
+ }
3009
3112
  }
3010
3113
  useImperativeHandle(ref, () => ({
3011
3114
  apply: () => {
3012
- const parsed = conditionsFromRows(draft.rows, column.id, type);
3115
+ const parsed = conditionsFromRows2(draft.rows, column.id, type);
3013
3116
  const unreadable = draft.rows.map(
3014
3117
  (_, index) => holdsUnreadable(rowElements.current[index])
3015
3118
  );
@@ -3020,10 +3123,10 @@ function SimpleFilterEditor({
3020
3123
  applyConditions(parsed.conditions);
3021
3124
  return true;
3022
3125
  }
3023
- if (draft === opened.draft && !unreadable.includes(true)) {
3126
+ if (sameConditionsDraft(draft, opened.draft) && !unreadable.includes(true)) {
3024
3127
  return true;
3025
3128
  }
3026
- setInvalid(marked);
3129
+ dispatch({ type: "markInvalid", rows: marked });
3027
3130
  return false;
3028
3131
  }
3029
3132
  }));
@@ -3045,7 +3148,7 @@ function SimpleFilterEditor({
3045
3148
  combinator: draft.combinator,
3046
3149
  messages,
3047
3150
  onChange: (combinator) => {
3048
- update({ ...draft, combinator });
3151
+ dispatch({ type: "setCombinator", combinator });
3049
3152
  }
3050
3153
  }
3051
3154
  ),
@@ -3063,12 +3166,7 @@ function SimpleFilterEditor({
3063
3166
  invalidMessageId: invalid.includes(index) ? invalidMessageId : void 0,
3064
3167
  focusRef: index === 0 ? initialFocusRef : void 0,
3065
3168
  onChange: (next) => {
3066
- update({
3067
- ...draft,
3068
- rows: draft.rows.map(
3069
- (current, at) => at === index ? next : current
3070
- )
3071
- });
3169
+ dispatch({ type: "editRow", index, row: next });
3072
3170
  }
3073
3171
  }
3074
3172
  )
@@ -3079,7 +3177,7 @@ function SimpleFilterEditor({
3079
3177
  }
3080
3178
 
3081
3179
  // src/DataGrid/filterMenu/ValueChecklist.tsx
3082
- import { useLayoutEffect as useLayoutEffect9, useRef as useRef11, useState as useState9 } from "react";
3180
+ import { useLayoutEffect as useLayoutEffect9, useRef as useRef11, useState as useState8 } from "react";
3083
3181
  import {
3084
3182
  attachChecklist,
3085
3183
  createRowVirtualizer
@@ -3101,8 +3199,8 @@ function ValueChecklist({
3101
3199
  }) {
3102
3200
  const listRef = useRef11(null);
3103
3201
  const checklistRef = useRef11(null);
3104
- const [virtualizer, setVirtualizer] = useState9(null);
3105
- const [range, setRange] = useState9(NO_RANGE);
3202
+ const [virtualizer, setVirtualizer] = useState8(null);
3203
+ const [range, setRange] = useState8(NO_RANGE);
3106
3204
  const latest = useRef11({ items, onToggle });
3107
3205
  useLayoutEffect9(() => {
3108
3206
  latest.current = { items, onToggle };
@@ -3184,6 +3282,10 @@ function ValueChecklist({
3184
3282
 
3185
3283
  // src/DataGrid/filterMenu/ExcelFilterEditor.tsx
3186
3284
  import { Fragment as Fragment7, jsx as jsx20, jsxs as jsxs15 } from "react/jsx-runtime";
3285
+ var OPENED_ON_VALUES = {
3286
+ kind: "values",
3287
+ focusOnMount: false
3288
+ };
3187
3289
  function toggleOf(item) {
3188
3290
  return item.kind === "blanks" ? { type: "setIncludeBlanks", includeBlanks: !item.checked } : { type: "toggleValue", value: item.value };
3189
3291
  }
@@ -3197,19 +3299,20 @@ function ExcelFilterEditor({
3197
3299
  columnValues,
3198
3300
  ref
3199
3301
  }) {
3200
- const [opened] = useState10(() => {
3201
- const shape2 = describeColumnFilter2(controller.getState().filter, column.id);
3202
- return {
3203
- shape: shape2,
3204
- draft: initialFilterEditorDraft2(shape2, columnValues(column.id))
3205
- };
3206
- });
3207
- const [draft, dispatch] = useReducer(filterEditorReducer2, opened.draft);
3208
- const [operator, setOperator] = useState10(null);
3302
+ const filter = controller.getState().filter;
3303
+ const open = (over) => openValues(over, column.id, columnValues(column.id));
3304
+ const [{ opened, draft }, dispatch] = useReducer2(
3305
+ excelEditorReducer,
3306
+ filter,
3307
+ (over) => initialExcelEditorState(open(over))
3308
+ );
3309
+ if (filter !== opened.over) {
3310
+ dispatch({ type: "filterChanged", opened: open(filter) });
3311
+ }
3312
+ const [mode, setMode] = useState9(OPENED_ON_VALUES);
3209
3313
  const conditions = useRef12(null);
3210
3314
  const conditionsFocus = useRef12(null);
3211
3315
  const operatorButton = useRef12(null);
3212
- const switched = useRef12(false);
3213
3316
  const selectAllRef = useRef12(null);
3214
3317
  const selectAll = useMemo4(() => selectAllState(draft), [draft]);
3215
3318
  const checklist = useMemo4(
@@ -3225,22 +3328,18 @@ function ExcelFilterEditor({
3225
3328
  }
3226
3329
  });
3227
3330
  useLayoutEffect10(() => {
3228
- if (switched.current) {
3229
- switched.current = false;
3230
- (operator === null ? operatorButton.current : conditionsFocus.current)?.focus();
3331
+ if (mode.kind === "condition") {
3332
+ conditionsFocus.current?.focus();
3333
+ } else if (mode.focusOnMount) {
3334
+ operatorButton.current?.focus();
3231
3335
  }
3232
- }, [operator]);
3233
- function switchTo(chosen) {
3234
- switched.current = true;
3235
- setOperator(chosen);
3236
- }
3336
+ }, [mode]);
3237
3337
  useImperativeHandle2(ref, () => ({
3238
3338
  apply: () => {
3239
- if (operator !== null) {
3339
+ if (mode.kind === "condition") {
3240
3340
  return conditions.current?.apply() ?? true;
3241
3341
  }
3242
- const picked = draft.checked !== opened.draft.checked || draft.includeBlanks !== opened.draft.includeBlanks;
3243
- if (picked) {
3342
+ if (filterDraftChanged3(opened.draft, draft)) {
3244
3343
  applyFilter(
3245
3344
  draftToFilter2(draft, column.id, controller.getState().filter)
3246
3345
  );
@@ -3248,7 +3347,7 @@ function ExcelFilterEditor({
3248
3347
  return true;
3249
3348
  }
3250
3349
  }));
3251
- if (operator !== null) {
3350
+ if (mode.kind === "condition") {
3252
3351
  return /* @__PURE__ */ jsxs15(Fragment7, { children: [
3253
3352
  /* @__PURE__ */ jsxs15(
3254
3353
  "button",
@@ -3256,7 +3355,7 @@ function ExcelFilterEditor({
3256
3355
  type: "button",
3257
3356
  className: "filter-menu-back",
3258
3357
  onClick: () => {
3259
- switchTo(null);
3358
+ setMode({ kind: "values", focusOnMount: true });
3260
3359
  },
3261
3360
  children: [
3262
3361
  /* @__PURE__ */ jsx20("span", { className: "filter-menu-back-icon", "aria-hidden": "true", children: /* @__PURE__ */ jsx20(ChevronLeftIcon2, {}) }),
@@ -3276,7 +3375,7 @@ function ExcelFilterEditor({
3276
3375
  conditionsFocus.current = element;
3277
3376
  },
3278
3377
  applyFilter,
3279
- operator
3378
+ operator: mode.operator
3280
3379
  }
3281
3380
  )
3282
3381
  ] });
@@ -3290,7 +3389,9 @@ function ExcelFilterEditor({
3290
3389
  menu,
3291
3390
  active: shape.kind === "conditions" ? shape.conditions.map((condition) => condition.operator) : void 0,
3292
3391
  messages,
3293
- onChoose: switchTo
3392
+ onChoose: (operator) => {
3393
+ setMode({ kind: "condition", operator });
3394
+ }
3294
3395
  }
3295
3396
  ),
3296
3397
  (shape.kind === "conditions" || shape.kind === "custom") && /* @__PURE__ */ jsx20("p", { className: "filter-menu-custom", children: shape.kind === "conditions" ? messages.filterByCondition : messages.customFilter }),
@@ -3462,23 +3563,29 @@ function FilterMenu({
3462
3563
  );
3463
3564
  }
3464
3565
 
3465
- // src/DataGrid/filterMenu/useApplyFilter.ts
3466
- import { useEffect as useEffect5, useRef as useRef14 } from "react";
3467
- function useApplyFilter(controller, messages, announce) {
3468
- const unannounced = useRef14(null);
3566
+ // src/DataGrid/useAnnounceWhenDrawn.ts
3567
+ import { useEffect as useEffect5, useState as useState10 } from "react";
3568
+ function useAnnounceWhenDrawn(announce) {
3569
+ const [requested, setRequested] = useState10(null);
3469
3570
  useEffect5(() => {
3470
- if (unannounced.current === controller.getState().filter) {
3471
- unannounced.current = null;
3472
- announce(
3473
- messages.filterResults(
3474
- controller.getView().rows.length,
3475
- controller.getOptions().dataSource?.length ?? 0
3476
- )
3477
- );
3571
+ if (requested?.drawn() === true) {
3572
+ announce(requested.text());
3478
3573
  }
3479
- });
3574
+ }, [requested, announce]);
3575
+ return setRequested;
3576
+ }
3577
+
3578
+ // src/DataGrid/filterMenu/useApplyFilter.ts
3579
+ function useApplyFilter(controller, messages, announce) {
3580
+ const announceWhenDrawn = useAnnounceWhenDrawn(announce);
3480
3581
  return (filter) => {
3481
- unannounced.current = filter;
3582
+ announceWhenDrawn({
3583
+ drawn: () => controller.getState().filter === filter,
3584
+ text: () => messages.filterResults(
3585
+ controller.getView().rows.length,
3586
+ controller.getOptions().dataSource?.length ?? 0
3587
+ )
3588
+ });
3482
3589
  controller.setFilter(filter);
3483
3590
  };
3484
3591
  }
@@ -3515,7 +3622,7 @@ function GridPanels({
3515
3622
  commands,
3516
3623
  messages
3517
3624
  }) {
3518
- const initialFocus = useRef15(null);
3625
+ const initialFocus = useRef14(null);
3519
3626
  const applyFilter = useApplyFilter(
3520
3627
  controller,
3521
3628
  messages,
@@ -3584,7 +3691,7 @@ function panelApi(panels, controller) {
3584
3691
  }
3585
3692
 
3586
3693
  // src/DataGrid/panels/useGridPanels.ts
3587
- import { useRef as useRef16 } from "react";
3694
+ import { useRef as useRef15 } from "react";
3588
3695
  function toolbarItem(root, itemId) {
3589
3696
  const items = root.querySelectorAll(
3590
3697
  ":scope > .gridkit-toolbar > [data-gridkit-toolbar-item]"
@@ -3610,7 +3717,7 @@ function useGridPanels({
3610
3717
  headerTableRef,
3611
3718
  overlay
3612
3719
  }) {
3613
- const openings = useRef16(0);
3720
+ const openings = useRef15(0);
3614
3721
  function placeFor(root, target) {
3615
3722
  const element = target.area === "toolbar" ? toolbarItem(root, target.itemId) : target.area === "header" ? headerCell2(headerTableRef.current, target.columnId) : null;
3616
3723
  if (element !== null) {
@@ -3656,7 +3763,7 @@ function useGridPanels({
3656
3763
  }
3657
3764
 
3658
3765
  // src/DataGrid/toolbar/GridToolbar.tsx
3659
- import { useLayoutEffect as useLayoutEffect12, useRef as useRef19 } from "react";
3766
+ import { useLayoutEffect as useLayoutEffect12, useRef as useRef18 } from "react";
3660
3767
  import {
3661
3768
  resolveToolbar,
3662
3769
  splitToolbarItems,
@@ -3725,7 +3832,7 @@ function ToolbarMenuButton({
3725
3832
  }
3726
3833
 
3727
3834
  // src/DataGrid/toolbar/ToolbarSearch.tsx
3728
- import { useEffect as useEffect6, useRef as useRef17, useState as useState12 } from "react";
3835
+ import { useEffect as useEffect6, useRef as useRef16, useState as useState12 } from "react";
3729
3836
  import {
3730
3837
  searchTextOf,
3731
3838
  setSearchText
@@ -3741,8 +3848,8 @@ function ToolbarSearch({
3741
3848
  }) {
3742
3849
  const applied = searchTextOf(controller.getState().filter);
3743
3850
  const [draft, setDraft] = useState12(null);
3744
- const timer = useRef17(void 0);
3745
- const unannounced = useRef17(null);
3851
+ const timer = useRef16(void 0);
3852
+ const announceWhenDrawn = useAnnounceWhenDrawn(announce);
3746
3853
  const text = draft !== null && draft.over === applied ? draft.text : applied;
3747
3854
  function apply(next, over) {
3748
3855
  clearTimeout(timer.current);
@@ -3753,16 +3860,13 @@ function ToolbarSearch({
3753
3860
  }
3754
3861
  const searched = setSearchText(filter, next);
3755
3862
  if (searched !== filter) {
3756
- unannounced.current = next;
3863
+ announceWhenDrawn({
3864
+ drawn: () => searchTextOf(controller.getState().filter) === next,
3865
+ text: () => messages.searchResults(controller.getView().rows.length)
3866
+ });
3757
3867
  controller.setFilter(searched);
3758
3868
  }
3759
3869
  }
3760
- useEffect6(() => {
3761
- if (unannounced.current === applied) {
3762
- unannounced.current = null;
3763
- announce(messages.searchResults(controller.getView().rows.length));
3764
- }
3765
- });
3766
3870
  useEffect6(
3767
3871
  () => () => {
3768
3872
  clearTimeout(timer.current);
@@ -3800,12 +3904,12 @@ function ToolbarSearch({
3800
3904
  }
3801
3905
 
3802
3906
  // src/DataGrid/toolbar/useToolbarOverflow.ts
3803
- import { useLayoutEffect as useLayoutEffect11, useRef as useRef18, useState as useState13 } from "react";
3907
+ import { useLayoutEffect as useLayoutEffect11, useRef as useRef17, useState as useState13 } from "react";
3804
3908
  import { observeToolbarOverflow } from "@gridkitjs/dom";
3805
3909
  function useToolbarOverflow(toolbarRef, items) {
3806
3910
  const [fit, setFit] = useState13(items.length);
3807
- const overflow = useRef18(null);
3808
- const measuredLayout = useRef18("");
3911
+ const overflow = useRef17(null);
3912
+ const measuredLayout = useRef17("");
3809
3913
  const layout = JSON.stringify(
3810
3914
  items.map((item) => [
3811
3915
  item.type,
@@ -3912,7 +4016,7 @@ function Toolbar({
3912
4016
  messages,
3913
4017
  announce
3914
4018
  }) {
3915
- const toolbarRef = useRef19(null);
4019
+ const toolbarRef = useRef18(null);
3916
4020
  const items = resolveToolbar(entries, {
3917
4021
  commands: commands.commands,
3918
4022
  contextFor: commands.contextFor,
@@ -4213,14 +4317,14 @@ function useGridController(options) {
4213
4317
  }
4214
4318
 
4215
4319
  // src/DataGrid/useInfiniteScroll.ts
4216
- import { useEffect as useEffect7, useRef as useRef20 } from "react";
4320
+ import { useEffect as useEffect7, useRef as useRef19 } from "react";
4217
4321
  import { attachInfiniteScroll } from "@gridkitjs/dom";
4218
4322
  function useInfiniteScroll({
4219
4323
  rootRef,
4220
4324
  sentinelRef,
4221
4325
  config
4222
4326
  }) {
4223
- const scroll = useRef20(null);
4327
+ const scroll = useRef19(null);
4224
4328
  useEffect7(() => {
4225
4329
  const root = rootRef.current;
4226
4330
  if (root === null) {
@@ -4248,10 +4352,10 @@ function useInfiniteScroll({
4248
4352
  }
4249
4353
 
4250
4354
  // src/DataGrid/useLiveAnnouncer.ts
4251
- import { useCallback as useCallback3, useLayoutEffect as useLayoutEffect14, useRef as useRef21 } from "react";
4355
+ import { useCallback as useCallback3, useLayoutEffect as useLayoutEffect14, useRef as useRef20 } from "react";
4252
4356
  import { createLiveAnnouncer } from "@gridkitjs/dom";
4253
4357
  function useLiveAnnouncer(containerRef) {
4254
- const announcer = useRef21(null);
4358
+ const announcer = useRef20(null);
4255
4359
  useLayoutEffect14(() => {
4256
4360
  const container = containerRef.current;
4257
4361
  if (container === null) {
@@ -4379,14 +4483,14 @@ function DataGrid(props) {
4379
4483
  const hoverRows = hoverable?.rows ?? true;
4380
4484
  const hoverColumns = hoverable?.columns ?? true;
4381
4485
  const hoverCells = hoverable?.cells ?? true;
4382
- const viewportRef = useRef22(null);
4383
- const gridRef = useRef22(null);
4384
- const headerTableRef = useRef22(null);
4385
- const bodyTableRef = useRef22(null);
4386
- const bodyScrollRef = useRef22(null);
4387
- const sentinelRef = useRef22(null);
4486
+ const viewportRef = useRef21(null);
4487
+ const gridRef = useRef21(null);
4488
+ const headerTableRef = useRef21(null);
4489
+ const bodyTableRef = useRef21(null);
4490
+ const bodyScrollRef = useRef21(null);
4491
+ const sentinelRef = useRef21(null);
4388
4492
  const viewportWidth = useElementWidth(viewportRef, resizeMode === "fit");
4389
- const subscribersRef = useRef22(/* @__PURE__ */ new Set());
4493
+ const subscribersRef = useRef21(/* @__PURE__ */ new Set());
4390
4494
  const gridId = useId2();
4391
4495
  const announce = useLiveAnnouncer(viewportRef);
4392
4496
  const messages = useMemo5(
@@ -4862,20 +4966,38 @@ import { serializeGridState } from "@gridkitjs/core";
4862
4966
  import { useCallback as useCallback5 } from "react";
4863
4967
 
4864
4968
  // src/DataGrid/hooks/useGridSnapshot.ts
4865
- import { useCallback as useCallback4, useSyncExternalStore as useSyncExternalStore3 } from "react";
4969
+ import {
4970
+ useCallback as useCallback4,
4971
+ useEffect as useEffect9,
4972
+ useRef as useRef22,
4973
+ useSyncExternalStore as useSyncExternalStore3
4974
+ } from "react";
4975
+ function subscribeTo(api, listener) {
4976
+ return api === null ? { kind: "waiting", listener } : {
4977
+ kind: "subscribed",
4978
+ listener,
4979
+ api,
4980
+ unsubscribe: api.subscribe(listener)
4981
+ };
4982
+ }
4983
+ function unsubscribe(subscription) {
4984
+ if (subscription?.kind === "subscribed") {
4985
+ subscription.unsubscribe();
4986
+ }
4987
+ }
4866
4988
  function useGridSnapshot(gridRef, getSnapshot, fallback) {
4989
+ const subscription = useRef22(null);
4867
4990
  const subscribe = useCallback4(
4868
4991
  (listener) => {
4869
- const api = gridRef.current;
4870
- if (api === null) {
4871
- return () => {
4872
- };
4873
- }
4874
- return api.subscribe(listener);
4992
+ subscription.current = subscribeTo(gridRef.current, listener);
4993
+ return () => {
4994
+ unsubscribe(subscription.current);
4995
+ subscription.current = null;
4996
+ };
4875
4997
  },
4876
4998
  [gridRef]
4877
4999
  );
4878
- return useSyncExternalStore3(
5000
+ const snapshot = useSyncExternalStore3(
4879
5001
  subscribe,
4880
5002
  () => {
4881
5003
  const api = gridRef.current;
@@ -4883,6 +5005,20 @@ function useGridSnapshot(gridRef, getSnapshot, fallback) {
4883
5005
  },
4884
5006
  () => fallback
4885
5007
  );
5008
+ useEffect9(() => {
5009
+ const current = subscription.current;
5010
+ if (current === null) {
5011
+ return;
5012
+ }
5013
+ const api = gridRef.current;
5014
+ if (api === (current.kind === "subscribed" ? current.api : null)) {
5015
+ return;
5016
+ }
5017
+ unsubscribe(current);
5018
+ subscription.current = subscribeTo(api, current.listener);
5019
+ current.listener();
5020
+ });
5021
+ return snapshot;
4886
5022
  }
4887
5023
 
4888
5024
  // src/DataGrid/hooks/usePaginationState.ts