@omnigrid/react 0.1.35 → 0.1.37

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.
Files changed (3) hide show
  1. package/README.md +81 -77
  2. package/dist/OmniGrid.js +73 -44
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,77 +1,81 @@
1
- # @omnigrid/react
2
-
3
- React adapter and renderer for OmniGrid. Renders the headless core engine into the DOM using React.
4
-
5
- ## What it provides
6
-
7
- - **`<OmniGrid />`** — React component that accepts `GridOptions` and renders a fully virtualized, 2D scrolled data grid
8
- - **`useGrid()`** — hook that creates and manages a `GridCore` instance with full lifecycle handling
9
- - **Cell / Row pooling** — reusable React content via DOM-pool integration for minimal re-renders
10
- - **Styles and default theme** — required structural styles and light/dark default colors are included automatically
11
- - **`transpilePackages`-ready** — works out of the box with Next.js and other bundlers
12
-
13
- ## Usage
14
-
15
- ```tsx
16
- import { OmniGrid } from "@omnigrid/react";
17
- import { useMemo } from "react";
18
-
19
- const columns = useMemo(() => [...], []);
20
- const data = [...];
21
-
22
- <OmniGrid columns={columns} data={data} />;
23
- ```
24
-
25
- Set `animateRows` to animate visible rows when filtering or sorting changes their
26
- positions. Row animation uses a separate CSS transform component, so virtualized
27
- positioning and scrolling remain independent. It is disabled by default; provide
28
- a stable `getRowId` to animate rows reliably across reorderings.
29
-
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.
35
-
36
- Column `headerRenderer` callbacks provide the header's primary content. The adapter owns the
37
- shared header layout and keeps sorting, filtering, and registered plugin extensions alongside it,
38
- so plugins can add controls without replacing or duplicating the header renderer.
39
-
40
- ## Overlays
41
-
42
- Automatic loading, no-rows, and no-matching-rows overlays cover the body below the sticky
43
- header. They hide vertical scrolling while preserving its gutter, so columns do not shift;
44
- horizontal scrolling remains available. Automatic error overlays cover the full grid.
45
- Any active overlay hides vertical scrolling and preserves its gutter, regardless of scope.
46
- Manual overlays default to body-only, except errors default to full-grid; set `scope` to
47
- `"bodyOnly"` or `"full"` to choose the placement. `blockScroll: true` locks horizontal
48
- scrolling; vertical scrolling is always locked while an overlay is active.
49
-
50
- ```tsx
51
- import { OmniGrid, type OverlayComponentProps } from "@omnigrid/react";
52
-
53
- function LoadError<T>({ message }: OverlayComponentProps<T>) {
54
- return <div role="alert">{message ?? "Unable to load rows"}</div>;
55
- }
56
-
57
- <OmniGrid
58
- columns={columns}
59
- data={data}
60
- overlayComponents={{ error: LoadError }}
61
- onGridReady={(grid) => grid.showOverlay({ type: "error", message: "Please retry" })}
62
- />;
63
- ```
64
-
65
- To customize the table's color style, install a compatible `@omnigrid/*-theme`
66
- package and import it in the application after `@omnigrid/react`. Theme packages
67
- include their own light and dark variants; they are not light/dark mode toggles.
68
-
69
- ## Links
70
-
71
- - **Source:** https://github.com/KRZK-Apps/omnigrid/tree/main/adapters/react
72
- - **Demo:** https://omnigrid.cc/
73
- - **Issues:** https://github.com/KRZK-Apps/omnigrid/issues
74
-
75
- ## License
76
-
77
- MIT © KRZK Apps
1
+ # @omnigrid/react
2
+
3
+ React adapter and renderer for OmniGrid. Renders the headless core engine into the DOM using React.
4
+
5
+ ## What it provides
6
+
7
+ - **`<OmniGrid />`** — React component that accepts `GridOptions` and renders a fully virtualized, 2D scrolled data grid
8
+ - **`useGrid()`** — hook that creates and manages a `GridCore` instance with full lifecycle handling
9
+ - **Cell / Row pooling** — reusable React content via DOM-pool integration for minimal re-renders
10
+ - **Styles and default theme** — required structural styles and light/dark default colors are included automatically
11
+ - **`transpilePackages`-ready** — works out of the box with Next.js and other bundlers
12
+
13
+ ## Usage
14
+
15
+ ```tsx
16
+ import { OmniGrid } from "@omnigrid/react";
17
+ import { useMemo } from "react";
18
+
19
+ const columns = useMemo(() => [...], []);
20
+ const data = [...];
21
+
22
+ <OmniGrid columns={columns} data={data} />;
23
+ ```
24
+
25
+ Set `animateRows` to animate visible rows when filtering or sorting changes their
26
+ positions. Row animation uses a separate CSS transform component, so virtualized
27
+ positioning and scrolling remain independent. It is disabled by default; provide
28
+ a stable `getRowId` to animate rows reliably across reorderings.
29
+
30
+ During vertical scrolling, a viewport-sized sticky row layer is kept separate
31
+ from the full-height spacer that provides the native scrollbar range. This
32
+ keeps the last painted rows visible until the adapter synchronizes the pool,
33
+ avoiding blank frames while the browser processes a fast scrollbar drag. Large
34
+ jumps and transitions into fast scrolling synchronize immediately; ordinary
35
+ scroll events remain frame-coalesced. `rowOverscan` controls the rendered row
36
+ buffer, but cannot pre-render an arbitrary destination on a 100,000-row grid.
37
+ The core tracks live scroll position without store notifications or
38
+ scroll-driven React renders.
39
+
40
+ Column `headerRenderer` callbacks provide the header's primary content. The adapter owns the
41
+ shared header layout and keeps sorting, filtering, and registered plugin extensions alongside it,
42
+ so plugins can add controls without replacing or duplicating the header renderer.
43
+
44
+ ## Overlays
45
+
46
+ Automatic loading, no-rows, and no-matching-rows overlays cover the body below the sticky
47
+ header. They hide vertical scrolling while preserving its gutter, so columns do not shift;
48
+ horizontal scrolling remains available. Automatic error overlays cover the full grid.
49
+ Any active overlay hides vertical scrolling and preserves its gutter, regardless of scope.
50
+ Manual overlays default to body-only, except errors default to full-grid; set `scope` to
51
+ `"bodyOnly"` or `"full"` to choose the placement. `blockScroll: true` locks horizontal
52
+ scrolling; vertical scrolling is always locked while an overlay is active.
53
+
54
+ ```tsx
55
+ import { OmniGrid, type OverlayComponentProps } from "@omnigrid/react";
56
+
57
+ function LoadError<T>({ message }: OverlayComponentProps<T>) {
58
+ return <div role="alert">{message ?? "Unable to load rows"}</div>;
59
+ }
60
+
61
+ <OmniGrid
62
+ columns={columns}
63
+ data={data}
64
+ overlayComponents={{ error: LoadError }}
65
+ onGridReady={(grid) => grid.showOverlay({ type: "error", message: "Please retry" })}
66
+ />;
67
+ ```
68
+
69
+ To customize the table's color style, install a compatible `@omnigrid/*-theme`
70
+ package and import it in the application after `@omnigrid/react`. Theme packages
71
+ include their own light and dark variants; they are not light/dark mode toggles.
72
+
73
+ ## Links
74
+
75
+ - **Source:** https://github.com/KRZK-Apps/omnigrid/tree/main/adapters/react
76
+ - **Demo:** https://omnigrid.cc/demo/react/quick-start
77
+ - **Issues:** https://github.com/KRZK-Apps/omnigrid/issues
78
+
79
+ ## License
80
+
81
+ MIT © KRZK Apps
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
  };
@@ -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. */
@@ -181,17 +190,11 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
181
190
  const minHeightRows = options.minHeight ?? 3;
182
191
  const minViewportHeight = autoHeight ? headerHeight + minHeightRows * rowHeight : 0;
183
192
  /**
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.
193
+ * Keeps the spacer height and visible row-layer height in sync without
194
+ * re-rendering the grid for every measured row.
192
195
  */
193
196
  const applyContentHeight = (totalHeight) => {
194
- const layerHeight = `${totalHeight}px`;
197
+ const layerHeight = `${autoHeight ? totalHeight : Math.max(0, state.viewport.height - headerHeight)}px`;
195
198
  for (const ref of [rowsLayerRef, pinnedLeftRowsLayerRef, pinnedRightRowsLayerRef]) {
196
199
  const element = ref.current;
197
200
  if (element && element.style.height !== layerHeight)
@@ -784,7 +787,7 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
784
787
  transformRow: (host, offsetY) => {
785
788
  const pane = paneMap.get(host);
786
789
  if (pane)
787
- pane.setOffsetY(offsetY);
790
+ pane.setOffsetY(offsetY - layer.scrollTop);
788
791
  },
789
792
  recycleRow: (host) => {
790
793
  const pane = paneMap.get(host);
@@ -837,6 +840,14 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
837
840
  visibleRowRange: viewportData.visibleRowRange,
838
841
  rowWidth: layer.rowWidth,
839
842
  });
843
+ for (const pane of layer.panes) {
844
+ const row = pane.getRow();
845
+ if (!row)
846
+ continue;
847
+ const offsetY = Math.round(row.offsetY - layer.scrollTop);
848
+ if (pane.getOffsetY() !== offsetY)
849
+ pane.setOffsetY(offsetY);
850
+ }
840
851
  layer.animationPositions = null;
841
852
  };
842
853
  /**
@@ -859,18 +870,22 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
859
870
  const pinnedLeftWidth = currentViewportData.pinnedLeftColumns.reduce((sum, item) => sum + item.width, 0);
860
871
  const pinnedRightWidth = currentViewportData.pinnedRightColumns.reduce((sum, item) => sum + item.width, 0);
861
872
  const scrollableWidth = Math.max(0, currentViewportData.totalWidth - pinnedLeftWidth - pinnedRightWidth);
873
+ const scrollTop = grid.getScrollPosition().scrollTop;
862
874
  // Configure each layer: which columns it renders, the table-space origin
863
875
  // of its coordinate system (subtracted from core offsets), and the pixel
864
876
  // width of its row hosts.
865
877
  layers.left.columns = currentViewportData.pinnedLeftColumns;
866
878
  layers.left.originX = 0;
867
879
  layers.left.rowWidth = pinnedLeftWidth;
880
+ layers.left.scrollTop = scrollTop;
868
881
  layers.central.columns = currentViewportData.columns;
869
882
  layers.central.originX = pinnedLeftWidth;
870
883
  layers.central.rowWidth = Math.max(scrollableWidth, state.viewport.width - pinnedLeftWidth - pinnedRightWidth);
884
+ layers.central.scrollTop = scrollTop;
871
885
  layers.right.columns = currentViewportData.pinnedRightColumns;
872
886
  layers.right.originX = pinnedLeftWidth + scrollableWidth;
873
887
  layers.right.rowWidth = pinnedRightWidth;
888
+ layers.right.scrollTop = scrollTop;
874
889
  // Cell content rendering (root.render) is async under React concurrent
875
890
  // mode — without flushSync, rows are repositioned but their cell content
876
891
  // stays stale for one frame. Wrap only the DOM binding in flushSync so
@@ -1075,12 +1090,27 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
1075
1090
  pinnedRightRowsLayerRef.current?.replaceChildren();
1076
1091
  };
1077
1092
  }, []);
1093
+ const syncScrollPosition = (scrollTop, scrollLeft) => {
1094
+ grid.setViewport({ scrollTop, scrollLeft });
1095
+ if (!isScrollActive() && pendingAnchorRef.current !== 0) {
1096
+ // The scroll settled — the deferred anchoring correction can be
1097
+ // applied now without fighting the user's input.
1098
+ flushPendingAnchor();
1099
+ }
1100
+ syncPoolRef.current?.(true);
1101
+ };
1078
1102
  const handleScroll = (event) => {
1079
1103
  const element = event.currentTarget;
1080
1104
  const frame = scrollFrameRef.current;
1081
1105
  frame.top = element.scrollTop;
1082
1106
  frame.left = element.scrollLeft;
1083
- lastScrollTimeRef.current = performance.now();
1107
+ const now = performance.now();
1108
+ const previousSample = scrollSampleRef.current;
1109
+ const elapsed = Math.max(1, now - previousSample.time);
1110
+ const velocity = Math.max(Math.abs(element.scrollTop - previousSample.top), Math.abs(element.scrollLeft - previousSample.left)) / elapsed;
1111
+ const enteredFastScroll = velocity >= FAST_SCROLL_VELOCITY && previousSample.velocity < FAST_SCROLL_VELOCITY;
1112
+ scrollSampleRef.current = { top: element.scrollTop, left: element.scrollLeft, time: now, velocity };
1113
+ lastScrollTimeRef.current = now;
1084
1114
  if (options.animateRows) {
1085
1115
  for (const layer of [layers.left, layers.central, layers.right]) {
1086
1116
  for (const pane of layer.panes)
@@ -1092,6 +1122,17 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
1092
1122
  // scroll event fires several times per frame during a thumb drag.
1093
1123
  // The ResizeObserver reports the content box size instead — it fires
1094
1124
  // exactly when the scrollbar appears or disappears.
1125
+ const scrollPosition = grid.getScrollPosition();
1126
+ const largeVerticalJump = element.clientHeight > 0 && Math.abs(element.scrollTop - scrollPosition.scrollTop) >= element.clientHeight;
1127
+ const largeHorizontalJump = element.clientWidth > 0 && Math.abs(element.scrollLeft - scrollPosition.scrollLeft) >= element.clientWidth;
1128
+ if (largeVerticalJump || largeHorizontalJump || enteredFastScroll) {
1129
+ if (frame.id) {
1130
+ window.cancelAnimationFrame(frame.id);
1131
+ frame.id = 0;
1132
+ }
1133
+ syncScrollPosition(element.scrollTop, element.scrollLeft);
1134
+ return;
1135
+ }
1095
1136
  if (frame.id)
1096
1137
  return;
1097
1138
  frame.id = window.requestAnimationFrame(() => {
@@ -1102,18 +1143,7 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
1102
1143
  // always reflects the most recent scrollTop, even if the last
1103
1144
  // onScroll event was at a different intermediate position.
1104
1145
  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);
1146
+ syncScrollPosition(element ? element.scrollTop : frame.top, element ? element.scrollLeft : frame.left);
1117
1147
  });
1118
1148
  };
1119
1149
  /** Resolves headless slot content into a ReactNode (see contentToReactNode). */
@@ -1403,36 +1433,35 @@ export function OmniGrid({ className, style, slotComponents, cellTooltipRenderer
1403
1433
  * leaf header row. Each pane renders its own stack with the same geometry;
1404
1434
  * without groups this is exactly the previous single header row.
1405
1435
  */
1406
- const renderHeader = (columns, originX, width, isPinned = false) => (_jsxs("div", { className: "omnigrid-header", style: {
1436
+ const renderHeader = (columns, originX, width, isPinned = false) => (_jsx("div", { className: "omnigrid-header", style: {
1407
1437
  height: headerHeight,
1408
1438
  overflow: "visible",
1409
1439
  position: "sticky",
1410
1440
  top: 0,
1411
1441
  zIndex: isPinned ? 4 : 2,
1412
1442
  width,
1413
- }, children: [Array.from({ length: headerRowCount }, (_, depth) => (_jsx("div", { role: "row", className: "omnigrid-header-row omnigrid-header-group-row", style: {
1414
- height: rowHeight,
1415
- left: 0,
1416
- overflow: "hidden",
1417
- position: "absolute",
1418
- top: depth * rowHeight,
1419
- width: "100%",
1420
- }, children: renderGroupCells(depth, columns, originX) }, `group-row-${depth}`))), _jsx("div", { role: "row", className: "omnigrid-header-row", style: {
1421
- height: rowHeight,
1422
- left: 0,
1423
- overflow: "visible",
1424
- position: "absolute",
1425
- top: headerRowCount * rowHeight,
1426
- width: "100%",
1427
- }, children: renderHeaderCells(columns, originX) })] }));
1443
+ }, children: _jsxs("div", { role: "rowgroup", children: [Array.from({ length: headerRowCount }, (_, depth) => (_jsx("div", { role: "row", className: "omnigrid-header-row omnigrid-header-group-row", style: {
1444
+ height: rowHeight,
1445
+ left: 0,
1446
+ overflow: "hidden",
1447
+ position: "absolute",
1448
+ top: depth * rowHeight,
1449
+ width: "100%",
1450
+ }, children: renderGroupCells(depth, columns, originX) }, `group-row-${depth}`))), _jsx("div", { role: "row", className: "omnigrid-header-row", style: {
1451
+ height: rowHeight,
1452
+ left: 0,
1453
+ overflow: "visible",
1454
+ position: "absolute",
1455
+ top: headerRowCount * rowHeight,
1456
+ width: "100%",
1457
+ }, children: renderHeaderCells(columns, originX) })] }) }));
1428
1458
  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,
1459
+ // This sticky paint layer stays viewport-sized; the scrollable
1460
+ // content wrapper provides the full height for the scrollbar.
1461
+ height: autoHeight ? viewportData.totalHeight : Math.max(0, state.viewport.height - headerHeight),
1433
1462
  left: 0,
1434
1463
  overflow: "hidden",
1435
- position: "absolute",
1464
+ position: "sticky",
1436
1465
  top: headerHeight,
1437
1466
  visibility: isViewportMeasured ? undefined : "hidden",
1438
1467
  width,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnigrid/react",
3
- "version": "0.1.35",
3
+ "version": "0.1.37",
4
4
  "description": "React adapter and renderer for OmniGrid",
5
5
  "keywords": [
6
6
  "omnigrid",