@omnigrid/react 0.1.36 → 0.1.38

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/README.md CHANGED
@@ -22,16 +22,56 @@ const data = [...];
22
22
  <OmniGrid columns={columns} data={data} />;
23
23
  ```
24
24
 
25
+ `columns` is optional. When omitted, the core creates one leaf column for each
26
+ own property of the first data row. If there are no rows yet, it waits for data.
27
+ The inferred schema stays fixed; call `grid.setColumns` when the schema changes.
28
+ By default, each header is the property key. Set
29
+ `autoColumnHeaderMode="title-case"` to split camel-case keys into capitalized
30
+ words. For custom formatting, use the core's `getColumnHeader` option:
31
+
32
+ ```tsx
33
+ <OmniGrid
34
+ data={data}
35
+ autoColumnHeaderMode="title-case"
36
+ />
37
+ <OmniGrid
38
+ data={data}
39
+ getColumnHeader={(field) => field.toUpperCase()}
40
+ />
41
+ ```
42
+
43
+ The grid fills its parent's available height by default. Give the parent a
44
+ constrained height (for example, in a flex layout) and the adapter measures the
45
+ actual viewport with `ResizeObserver`. Use `height` for a fixed grid height, or
46
+ set `sizingMode="auto"` to grow the grid with its rows. Supplying `height`
47
+ without `sizingMode` selects fixed sizing; explicitly setting
48
+ `sizingMode="fixed"` requires `height`:
49
+
50
+ ```tsx
51
+ <OmniGrid columns={columns} data={data} height={480} />
52
+ <OmniGrid columns={columns} data={data} sizingMode="auto" />
53
+ ```
54
+
55
+ `minHeight` applies only to auto-height grids. Auto-height renders the full row
56
+ content and should be avoided for large datasets when vertical virtualization
57
+ is needed. `style` remains available for CSS customization; use `height` and
58
+ `sizingMode` to select the grid's sizing behavior. A legacy `style.height` can
59
+ still size the outer element in fill mode, but it no longer selects auto-height.
60
+
25
61
  Set `animateRows` to animate visible rows when filtering or sorting changes their
26
62
  positions. Row animation uses a separate CSS transform component, so virtualized
27
63
  positioning and scrolling remain independent. It is disabled by default; provide
28
64
  a stable `getRowId` to animate rows reliably across reorderings.
29
65
 
30
- During fast vertical scrolling, the React adapter keeps the current row pool
31
- visually in place while the native scrollbar continues moving. It repositions
32
- the existing row layers rather than creating a second copy of the rows, then
33
- updates the pool after scrolling has been idle for 150 ms. The core tracks the
34
- live scroll position without store notifications or scroll-driven React renders.
66
+ During vertical scrolling, a viewport-sized sticky row layer is kept separate
67
+ from the full-height spacer that provides the native scrollbar range. This
68
+ keeps the last painted rows visible until the adapter synchronizes the pool,
69
+ avoiding blank frames while the browser processes a fast scrollbar drag. Large
70
+ jumps and transitions into fast scrolling synchronize immediately; ordinary
71
+ scroll events remain frame-coalesced. `rowOverscan` controls the rendered row
72
+ buffer, but cannot pre-render an arbitrary destination on a 100,000-row grid.
73
+ The core tracks live scroll position without store notifications or
74
+ scroll-driven React renders.
35
75
 
36
76
  Column `headerRenderer` callbacks provide the header's primary content. The adapter owns the
37
77
  shared header layout and keeps sorting, filtering, and registered plugin extensions alongside it,
@@ -24,4 +24,4 @@ import type { GridProps } from "./types.js";
24
24
  * core's headless content; React components in slots are connected via
25
25
  * `slotComponents`.
26
26
  */
27
- export declare function OmniGrid<T>({ className, style, slotComponents, cellTooltipRenderer, cellTooltipClassName, ...options }: GridProps<T>): import("react").JSX.Element;
27
+ export declare function OmniGrid<T>({ className, style, height, sizingMode: sizingModeProp, slotComponents, cellTooltipRenderer, cellTooltipClassName, ...options }: GridProps<T>): import("react").JSX.Element;
package/dist/OmniGrid.js CHANGED
@@ -31,6 +31,8 @@ const SLOT_POSITIONS = ["start", "center", "end"];
31
31
  * deferred, so they cannot fight active user input.
32
32
  */
33
33
  const SCROLL_IDLE_WINDOW = 150;
34
+ /** Speed in pixels per millisecond at which scroll updates are committed synchronously. */
35
+ const FAST_SCROLL_VELOCITY = 2;
34
36
  const COLUMN_DRAG_TYPE = "application/x-omnigrid-column";
35
37
  function createRowLayerState() {
36
38
  return {
@@ -44,6 +46,7 @@ function createRowLayerState() {
44
46
  processed: null,
45
47
  originX: 0,
46
48
  rowWidth: 0,
49
+ scrollTop: 0,
47
50
  animationPositions: null,
48
51
  viewport: null,
49
52
  };
@@ -73,7 +76,7 @@ function createRowLayerState() {
73
76
  * core's headless content; React components in slots are connected via
74
77
  * `slotComponents`.
75
78
  */
76
- export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer, cellTooltipClassName, ...options }) {
79
+ export function OmniGrid({ className, style, height, sizingMode: sizingModeProp, slotComponents, cellTooltipRenderer, cellTooltipClassName, ...options }) {
77
80
  const { grid, state, viewportData } = useGrid(options);
78
81
  const columnTooltipOptions = useMemo(() => {
79
82
  const result = new Map();
@@ -103,6 +106,12 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
103
106
  top: 0,
104
107
  left: 0,
105
108
  });
109
+ const scrollSampleRef = useRef({
110
+ top: 0,
111
+ left: 0,
112
+ time: 0,
113
+ velocity: 0,
114
+ });
106
115
  /** Timestamp of the last scroll event, used to defer row-height anchoring until idle. */
107
116
  const lastScrollTimeRef = useRef(0);
108
117
  /** Anchoring correction accumulated while the user is scrolling. */
@@ -170,7 +179,15 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
170
179
  grid.refresh();
171
180
  }
172
181
  }, [grid, options.rowClassRules]);
173
- const autoHeight = style?.height === undefined;
182
+ const sizingMode = sizingModeProp ?? (height !== undefined ? "fixed" : "fill");
183
+ if (sizingMode === "fixed" && height === undefined) {
184
+ throw new Error('OmniGrid requires the "height" prop when sizingMode is "fixed".');
185
+ }
186
+ const autoHeight = sizingMode === "auto";
187
+ const styleHeight = style?.height;
188
+ const gridStyle = { ...style };
189
+ delete gridStyle.height;
190
+ const layoutHeight = sizingMode === "fixed" ? height : sizingMode === "fill" ? (styleHeight ?? "100%") : undefined;
174
191
  const rowHeight = grid.getState().rowHeight;
175
192
  // Column-group header stack: `headerRowCount` group rows (one per depth) above
176
193
  // the leaf header row. Without groups `headerRowCount` is 0 and `columnGroupDepth`
@@ -181,17 +198,11 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
181
198
  const minHeightRows = options.minHeight ?? 3;
182
199
  const minViewportHeight = autoHeight ? headerHeight + minHeightRows * rowHeight : 0;
183
200
  /**
184
- * Writes the scrollable content height (and the height of the row
185
- * layers) into the DOM.
186
- *
187
- * The total height changes with every measured row. Writing it through
188
- * React would re-render the whole grid per measurement — during a fast
189
- * scroll that starves the frame budget and the grid lags behind the
190
- * native scroll position. The imperative write keeps the scroll range
191
- * correct without a re-render.
201
+ * Keeps the spacer height and visible row-layer height in sync without
202
+ * re-rendering the grid for every measured row.
192
203
  */
193
204
  const applyContentHeight = (totalHeight) => {
194
- const layerHeight = `${totalHeight}px`;
205
+ const layerHeight = `${autoHeight ? totalHeight : Math.max(0, state.viewport.height - headerHeight)}px`;
195
206
  for (const ref of [rowsLayerRef, pinnedLeftRowsLayerRef, pinnedRightRowsLayerRef]) {
196
207
  const element = ref.current;
197
208
  if (element && element.style.height !== layerHeight)
@@ -238,8 +249,7 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
238
249
  const cellFlashRaw = options.cellFlash;
239
250
  const cellFlashDurationMs = cellFlashRaw === true ? 1000 : typeof cellFlashRaw === "number" && cellFlashRaw > 0 ? cellFlashRaw : false;
240
251
  const viewportStyle = {
241
- ...style,
242
- height: autoHeight ? measuredHeight : style?.height,
252
+ ...gridStyle,
243
253
  ...(cellFlashDurationMs !== false ? { "--omnigrid-flash-duration": `${cellFlashDurationMs}ms` } : {}),
244
254
  };
245
255
  const viewportClassName = ["omnigrid", className, suppressRowHoverHighlight ? "omnigrid-no-row-hover" : null, suppressColumnBorders ? "omnigrid-no-column-borders" : null]
@@ -784,7 +794,7 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
784
794
  transformRow: (host, offsetY) => {
785
795
  const pane = paneMap.get(host);
786
796
  if (pane)
787
- pane.setOffsetY(offsetY);
797
+ pane.setOffsetY(offsetY - layer.scrollTop);
788
798
  },
789
799
  recycleRow: (host) => {
790
800
  const pane = paneMap.get(host);
@@ -837,6 +847,14 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
837
847
  visibleRowRange: viewportData.visibleRowRange,
838
848
  rowWidth: layer.rowWidth,
839
849
  });
850
+ for (const pane of layer.panes) {
851
+ const row = pane.getRow();
852
+ if (!row)
853
+ continue;
854
+ const offsetY = Math.round(row.offsetY - layer.scrollTop);
855
+ if (pane.getOffsetY() !== offsetY)
856
+ pane.setOffsetY(offsetY);
857
+ }
840
858
  layer.animationPositions = null;
841
859
  };
842
860
  /**
@@ -859,18 +877,22 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
859
877
  const pinnedLeftWidth = currentViewportData.pinnedLeftColumns.reduce((sum, item) => sum + item.width, 0);
860
878
  const pinnedRightWidth = currentViewportData.pinnedRightColumns.reduce((sum, item) => sum + item.width, 0);
861
879
  const scrollableWidth = Math.max(0, currentViewportData.totalWidth - pinnedLeftWidth - pinnedRightWidth);
880
+ const scrollTop = grid.getScrollPosition().scrollTop;
862
881
  // Configure each layer: which columns it renders, the table-space origin
863
882
  // of its coordinate system (subtracted from core offsets), and the pixel
864
883
  // width of its row hosts.
865
884
  layers.left.columns = currentViewportData.pinnedLeftColumns;
866
885
  layers.left.originX = 0;
867
886
  layers.left.rowWidth = pinnedLeftWidth;
887
+ layers.left.scrollTop = scrollTop;
868
888
  layers.central.columns = currentViewportData.columns;
869
889
  layers.central.originX = pinnedLeftWidth;
870
890
  layers.central.rowWidth = Math.max(scrollableWidth, state.viewport.width - pinnedLeftWidth - pinnedRightWidth);
891
+ layers.central.scrollTop = scrollTop;
871
892
  layers.right.columns = currentViewportData.pinnedRightColumns;
872
893
  layers.right.originX = pinnedLeftWidth + scrollableWidth;
873
894
  layers.right.rowWidth = pinnedRightWidth;
895
+ layers.right.scrollTop = scrollTop;
874
896
  // Cell content rendering (root.render) is async under React concurrent
875
897
  // mode — without flushSync, rows are repositioned but their cell content
876
898
  // stays stale for one frame. Wrap only the DOM binding in flushSync so
@@ -1004,7 +1026,7 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
1004
1026
  // back to minWidth) and only snap to the correct size after the
1005
1027
  // ResizeObserver-driven useEffect runs — a visible flash on first load.
1006
1028
  measureViewport();
1007
- }, [autoHeight, hasVerticalOverflow, measureViewport, overlayScope]);
1029
+ }, [autoHeight, hasVerticalOverflow, height, measureViewport, overlayScope, sizingMode, styleHeight]);
1008
1030
  useEffect(() => {
1009
1031
  const unsubscribe = grid.on("scrollTo", (position) => {
1010
1032
  const element = viewportRef.current;
@@ -1075,12 +1097,27 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
1075
1097
  pinnedRightRowsLayerRef.current?.replaceChildren();
1076
1098
  };
1077
1099
  }, []);
1100
+ const syncScrollPosition = (scrollTop, scrollLeft) => {
1101
+ grid.setViewport({ scrollTop, scrollLeft });
1102
+ if (!isScrollActive() && pendingAnchorRef.current !== 0) {
1103
+ // The scroll settled — the deferred anchoring correction can be
1104
+ // applied now without fighting the user's input.
1105
+ flushPendingAnchor();
1106
+ }
1107
+ syncPoolRef.current?.(true);
1108
+ };
1078
1109
  const handleScroll = (event) => {
1079
1110
  const element = event.currentTarget;
1080
1111
  const frame = scrollFrameRef.current;
1081
1112
  frame.top = element.scrollTop;
1082
1113
  frame.left = element.scrollLeft;
1083
- lastScrollTimeRef.current = performance.now();
1114
+ const now = performance.now();
1115
+ const previousSample = scrollSampleRef.current;
1116
+ const elapsed = Math.max(1, now - previousSample.time);
1117
+ const velocity = Math.max(Math.abs(element.scrollTop - previousSample.top), Math.abs(element.scrollLeft - previousSample.left)) / elapsed;
1118
+ const enteredFastScroll = velocity >= FAST_SCROLL_VELOCITY && previousSample.velocity < FAST_SCROLL_VELOCITY;
1119
+ scrollSampleRef.current = { top: element.scrollTop, left: element.scrollLeft, time: now, velocity };
1120
+ lastScrollTimeRef.current = now;
1084
1121
  if (options.animateRows) {
1085
1122
  for (const layer of [layers.left, layers.central, layers.right]) {
1086
1123
  for (const pane of layer.panes)
@@ -1092,6 +1129,17 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
1092
1129
  // scroll event fires several times per frame during a thumb drag.
1093
1130
  // The ResizeObserver reports the content box size instead — it fires
1094
1131
  // exactly when the scrollbar appears or disappears.
1132
+ const scrollPosition = grid.getScrollPosition();
1133
+ const largeVerticalJump = element.clientHeight > 0 && Math.abs(element.scrollTop - scrollPosition.scrollTop) >= element.clientHeight;
1134
+ const largeHorizontalJump = element.clientWidth > 0 && Math.abs(element.scrollLeft - scrollPosition.scrollLeft) >= element.clientWidth;
1135
+ if (largeVerticalJump || largeHorizontalJump || enteredFastScroll) {
1136
+ if (frame.id) {
1137
+ window.cancelAnimationFrame(frame.id);
1138
+ frame.id = 0;
1139
+ }
1140
+ syncScrollPosition(element.scrollTop, element.scrollLeft);
1141
+ return;
1142
+ }
1095
1143
  if (frame.id)
1096
1144
  return;
1097
1145
  frame.id = window.requestAnimationFrame(() => {
@@ -1102,18 +1150,7 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
1102
1150
  // always reflects the most recent scrollTop, even if the last
1103
1151
  // onScroll event was at a different intermediate position.
1104
1152
  const element = viewportRef.current;
1105
- const scrollTop = element ? element.scrollTop : frame.top;
1106
- const scrollLeft = element ? element.scrollLeft : frame.left;
1107
- // setViewport with scroll-only updates does NOT notify the Store,
1108
- // so this does NOT trigger a React re-render. The DomPool is
1109
- // updated imperatively below.
1110
- grid.setViewport({ scrollTop, scrollLeft });
1111
- if (!isScrollActive() && pendingAnchorRef.current !== 0) {
1112
- // The scroll settled — the deferred anchoring correction can be
1113
- // applied now without fighting the user's input.
1114
- flushPendingAnchor();
1115
- }
1116
- syncPool(true);
1153
+ syncScrollPosition(element ? element.scrollTop : frame.top, element ? element.scrollLeft : frame.left);
1117
1154
  });
1118
1155
  };
1119
1156
  /** Resolves headless slot content into a ReactNode (see contentToReactNode). */
@@ -1410,6 +1447,10 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
1410
1447
  top: 0,
1411
1448
  zIndex: isPinned ? 4 : 2,
1412
1449
  width,
1450
+ // Force own compositing layer to fix Edge 1px sticky shift during scroll
1451
+ transform: "translateZ(0)",
1452
+ willChange: "transform",
1453
+ backfaceVisibility: "hidden",
1413
1454
  }, children: _jsxs("div", { role: "rowgroup", children: [Array.from({ length: headerRowCount }, (_, depth) => (_jsx("div", { role: "row", className: "omnigrid-header-row omnigrid-header-group-row", style: {
1414
1455
  height: rowHeight,
1415
1456
  left: 0,
@@ -1426,18 +1467,27 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
1426
1467
  width: "100%",
1427
1468
  }, children: renderHeaderCells(columns, originX) })] }) }));
1428
1469
  const renderRowsLayer = (ref, width) => (_jsx("div", { ref: ref, "data-pool-layer": "true", style: {
1429
- // The layer's height follows the measured row heights — it is
1430
- // updated imperatively (see `syncPool`) so a measurement does
1431
- // not re-render the whole grid.
1432
- height: viewportData.totalHeight,
1470
+ // This sticky paint layer stays viewport-sized; the scrollable
1471
+ // content wrapper provides the full height for the scrollbar.
1472
+ height: autoHeight ? viewportData.totalHeight : Math.max(0, state.viewport.height - headerHeight),
1433
1473
  left: 0,
1434
1474
  overflow: "hidden",
1435
- position: "absolute",
1475
+ position: "sticky",
1436
1476
  top: headerHeight,
1437
1477
  visibility: isViewportMeasured ? undefined : "hidden",
1438
1478
  width,
1479
+ // Force own compositing layer to fix Edge 1px sticky shift during scroll
1480
+ transform: "translateZ(0)",
1481
+ willChange: "transform",
1482
+ backfaceVisibility: "hidden",
1439
1483
  } }));
1440
- return (_jsxs("div", { className: "omnigrid-layout", ref: layoutRef, style: { ...style, position: "relative", ...(isFullOverlay ? { overflow: "hidden" } : {}) }, children: [renderSlot("top"), _jsxs("div", { className: "omnigrid-layout-body", children: [renderSlot("left"), _jsxs("div", { className: "omnigrid-viewport-container", style: {
1484
+ return (_jsxs("div", { className: "omnigrid-layout", ref: layoutRef, style: {
1485
+ ...gridStyle,
1486
+ height: layoutHeight,
1487
+ ...(sizingMode === "fill" ? { flex: "1 1 0", minHeight: 0 } : {}),
1488
+ position: "relative",
1489
+ ...(isFullOverlay ? { overflow: "hidden" } : {}),
1490
+ }, children: [renderSlot("top"), _jsxs("div", { className: "omnigrid-layout-body", children: [renderSlot("left"), _jsxs("div", { className: "omnigrid-viewport-container", style: {
1441
1491
  flex: "1 1 0",
1442
1492
  minHeight: 0,
1443
1493
  minWidth: 0,
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export type { CellTooltipRenderer, CellTooltipRenderContext, ReactCellRenderer,
5
5
  export { PooledRowPane } from "./pooledRow.js";
6
6
  export type { ContentBridge, PooledCellRenderInfo, PooledColumn, PooledRowCallbacks, PooledRowModifiers } from "./types.js";
7
7
  export { OmniGrid } from "./OmniGrid.js";
8
- export type { GridProps, OverlayComponent, OverlayComponentProps, SlotRendererRegistry } from "./types.js";
8
+ export type { GridProps, GridSizingMode, OverlayComponent, OverlayComponentProps, SlotRendererRegistry } from "./types.js";
9
9
  export { useGrid } from "./useGrid.js";
10
10
  export { DebouncedInput, isUIControlDescriptor, renderControl } from "./controls.js";
11
11
  export type { DebouncedInputProps } from "./controls.js";
@@ -65,6 +65,7 @@ export type ReactColumnDef<T> = ReactColumnLeafDef<T> | ReactColumnGroupDef<T>;
65
65
  * @api
66
66
  */
67
67
  export type ReactGridOptions<T> = Omit<GridOptions<T>, "columns" | "defaultColumnDef"> & {
68
- columns: ReactColumnDef<T>[];
68
+ /** Column definitions; inferred from the first data row when omitted. @api */
69
+ columns?: ReactColumnDef<T>[];
69
70
  defaultColumnDef?: Partial<Omit<ReactColumnLeafDef<T>, "id" | "header">>;
70
71
  };
package/dist/types.d.ts CHANGED
@@ -85,14 +85,28 @@ export interface SlotRendererRegistry<T> {
85
85
  }
86
86
  export type OverlayComponentProps<T> = import("@omnigrid/core").OverlayComponentProps<T>;
87
87
  export type OverlayComponent<T> = import("react").ComponentType<import("@omnigrid/core").OverlayComponentProps<T>> | import("@omnigrid/core").OverlayComponent<T>;
88
- export interface GridProps<T> extends Omit<ReactGridOptions<T>, "overlayComponents"> {
88
+ interface GridPropsBase<T> extends Omit<ReactGridOptions<T>, "overlayComponents"> {
89
89
  className?: string;
90
90
  style?: import("react").CSSProperties;
91
91
  /**
92
- * Minimum grid height in rows when `style.height` is not set (auto-height
93
- * mode). The viewport will be at least `headerHeight + minHeight *
94
- * rowHeight` tall so the grid does not collapse when it has few or no
95
- * rows. Set to `0` to disable.
92
+ * Height behavior. Defaults to `fill`, unless `height` is provided without
93
+ * an explicit mode, which selects `fixed`.
94
+ *
95
+ * @api
96
+ * @default "fill"
97
+ */
98
+ sizingMode?: GridSizingMode;
99
+ /**
100
+ * Explicit height for `fixed` sizing. When provided without `sizingMode`,
101
+ * the grid uses `fixed` sizing.
102
+ *
103
+ * @api
104
+ */
105
+ height?: import("react").CSSProperties["height"];
106
+ /**
107
+ * Minimum grid height in rows in `auto` sizing mode. The viewport will be
108
+ * at least `headerHeight + minHeight * rowHeight` tall so it does not
109
+ * collapse when it has few or no rows. Set to `0` to disable.
96
110
  *
97
111
  * @api
98
112
  * @default 3
@@ -113,6 +127,27 @@ export interface GridProps<T> extends Omit<ReactGridOptions<T>, "overlayComponen
113
127
  slotComponents?: SlotRendererRegistry<T>;
114
128
  overlayComponents?: Record<string, import("@omnigrid/core").OverlayComponent<T>>;
115
129
  }
130
+ /**
131
+ * Controls how the grid obtains its height.
132
+ *
133
+ * @api
134
+ */
135
+ export type GridSizingMode = "fixed" | "fill" | "auto";
136
+ /**
137
+ * React adapter props with explicit grid sizing.
138
+ *
139
+ * @api
140
+ */
141
+ export type GridProps<T> = GridPropsBase<T> & ({
142
+ sizingMode: "fixed";
143
+ height: NonNullable<import("react").CSSProperties["height"]>;
144
+ } | {
145
+ sizingMode: "fill" | "auto";
146
+ height?: never;
147
+ } | {
148
+ sizingMode?: undefined;
149
+ height?: import("react").CSSProperties["height"];
150
+ });
116
151
  /**
117
152
  * Pre-resolved presentation (classes and inline styles) for a column data body.
118
153
  * Evaluated once per column across the dataset, then applied to each rendered cell in O(1).
@@ -125,3 +160,4 @@ export interface ResolvedColumnPresentation {
125
160
  /** Combined inline styles for the column body. */
126
161
  style: import("@omnigrid/core").ColumnStyle;
127
162
  }
163
+ export {};
package/dist/useGrid.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isColumnGroup } from '@omnigrid/core';
1
+ import { inferColumns, isColumnGroup } from '@omnigrid/core';
2
2
  import { Grid } from '@omnigrid/core';
3
3
  import { useCallback, useEffect, useRef, useSyncExternalStore } from 'react';
4
4
  /**
@@ -29,6 +29,12 @@ export function useGrid(options) {
29
29
  const subscribe = useCallback((listener) => grid.subscribe(listener), [grid]);
30
30
  const getSnapshot = useCallback(() => grid.getState(), [grid]);
31
31
  const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
32
+ const autoColumnIdsRef = useRef(new Set());
33
+ useEffect(() => {
34
+ if (options.columns !== undefined || autoColumnIdsRef.current.size > 0 || state.data.length === 0)
35
+ return;
36
+ autoColumnIdsRef.current = new Set(inferColumns(state.data, options.getColumnHeader).map((column) => column.id));
37
+ }, [options.columns, options.getColumnHeader, state.data]);
32
38
  // Reconcile plugins when the plugin list changes. Plugins removed from the
33
39
  // list are unregistered (their cleanup restores default behavior); new ones
34
40
  // are registered. Declared before the data sync so that, on a switch, the
@@ -62,8 +68,8 @@ export function useGrid(options) {
62
68
  // Sync columns when the columns reference changes (e.g. dynamic column
63
69
  // definitions). Skip on the first run — plugins may have prepended
64
70
  // selection/sorting columns during registerPlugin() in the Grid
65
- // constructor, and calling setColumns here would overwrite those injected
66
- // columns. Only sync on subsequent changes to options.columns.
71
+ // constructor, and calling grid.setColumns here would overwrite those
72
+ // injected columns. Only sync on subsequent changes to options.columns.
67
73
  //
68
74
  // StrictMode note: `options.columns` is a new array reference on every
69
75
  // render, but its entries are usually the same column objects (a new
@@ -89,6 +95,7 @@ export function useGrid(options) {
89
95
  const columnsChanged = prev.length !== next.length || prev.some((column, index) => column !== next[index]);
90
96
  const defaultColumnDefChanged = lastSyncedDefaultColumnDefRef.current !== options.defaultColumnDef;
91
97
  if (columnsChanged || defaultColumnDefChanged) {
98
+ const previousColumnsWereAuto = lastSyncedColumnsRef.current === undefined;
92
99
  lastSyncedColumnsRef.current = options.columns;
93
100
  lastSyncedDefaultColumnDefRef.current = options.defaultColumnDef;
94
101
  // Merge the user's column definitions into the current (plugin-enriched)
@@ -97,6 +104,11 @@ export function useGrid(options) {
97
104
  // not part of the user's column definitions; replacing columns with the
98
105
  // raw definitions would strip them and break e.g. column resize handles.
99
106
  const current = grid.getState().columns;
107
+ const autoColumnIds = previousColumnsWereAuto && options.columns !== undefined
108
+ ? autoColumnIdsRef.current.size > 0
109
+ ? autoColumnIdsRef.current
110
+ : new Set(inferColumns(grid.getState().data, options.getColumnHeader).map((column) => column.id))
111
+ : new Set();
100
112
  // Merge the user's column definitions INTO the current (plugin-enriched)
101
113
  // columns instead of replacing them wholesale. Iterating over `current`
102
114
  // preserves plugin-injected columns (e.g. the selection column prepended
@@ -111,7 +123,8 @@ export function useGrid(options) {
111
123
  current.forEach((currentColumn) => {
112
124
  const userColumn = next.find((c) => c.id === currentColumn.id);
113
125
  if (!userColumn) {
114
- merged.push(currentColumn);
126
+ if (!autoColumnIds.has(currentColumn.id))
127
+ merged.push(currentColumn);
115
128
  return;
116
129
  }
117
130
  const enriched = { ...userColumn };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnigrid/react",
3
- "version": "0.1.36",
3
+ "version": "0.1.38",
4
4
  "description": "React adapter and renderer for OmniGrid",
5
5
  "keywords": [
6
6
  "omnigrid",