@omnigrid/react 0.1.16 → 0.1.18

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/OmniGrid.js CHANGED
@@ -2,7 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
2
2
  import { createElement, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
3
3
  import { flushSync } from "react-dom";
4
4
  import { DomPool } from "@omnigrid/core";
5
- import { contentToReactNode } from "./content";
5
+ import { contentToReactNode, hasRenderableContent } from "./content";
6
6
  import { computeGroupSegments } from "./groupHeader";
7
7
  import { PooledRowPane } from "./pooledRow";
8
8
  import { createCellRoot } from "./reactRoot";
@@ -64,6 +64,8 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
64
64
  const pinnedLeftRowsLayerRef = useRef(null);
65
65
  const pinnedRightRowsLayerRef = useRef(null);
66
66
  const [isMeasured, setIsMeasured] = useState(false);
67
+ /** Space taken by the viewport's native scrollbars; body slots stay clear of it. */
68
+ const [scrollbarInsets, setScrollbarInsets] = useState({ right: 0, bottom: 0 });
67
69
  const scrollFrameRef = useRef({ id: 0, top: 0, left: 0, lastTop: 0, lastTime: 0 });
68
70
  const scrollVelocityRef = useRef(0);
69
71
  const optionsRef = useRef(options);
@@ -427,10 +429,16 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
427
429
  * whether or not the scrollbar is visible. For browsers without support,
428
430
  * the scroll handler re-measures when the scrollbar appears/disappears.
429
431
  */
432
+ const syncScrollbarInsets = useCallback((element) => {
433
+ const right = Math.max(0, element.offsetWidth - element.clientWidth - element.clientLeft * 2);
434
+ const bottom = Math.max(0, element.offsetHeight - element.clientHeight - element.clientTop * 2);
435
+ setScrollbarInsets((previous) => (previous.right === right && previous.bottom === bottom ? previous : { right, bottom }));
436
+ }, []);
430
437
  const measureViewport = useCallback(() => {
431
438
  const element = viewportRef.current;
432
439
  if (!element)
433
440
  return;
441
+ syncScrollbarInsets(element);
434
442
  const scrollbarWidth = element.offsetWidth - element.clientWidth;
435
443
  scrollbarWidthRef.current = scrollbarWidth;
436
444
  const width = element.clientWidth;
@@ -439,7 +447,7 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
439
447
  return;
440
448
  grid.setViewport({ width, height });
441
449
  setIsMeasured(true);
442
- }, [grid]);
450
+ }, [grid, syncScrollbarInsets]);
443
451
  useEffect(() => {
444
452
  const element = viewportRef.current;
445
453
  if (!element)
@@ -448,6 +456,7 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
448
456
  const element = viewportRef.current;
449
457
  if (!element)
450
458
  return;
459
+ syncScrollbarInsets(element);
451
460
  const scrollbarWidth = element.offsetWidth - element.clientWidth;
452
461
  scrollbarWidthRef.current = scrollbarWidth;
453
462
  const width = element.clientWidth;
@@ -460,7 +469,12 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
460
469
  observer.observe(element, { box: "content-box" });
461
470
  measureViewport();
462
471
  return () => observer.disconnect();
463
- }, [grid, measureViewport]);
472
+ }, [grid, measureViewport, syncScrollbarInsets]);
473
+ // Scrollbars can appear or disappear after any render (content or column changes).
474
+ useLayoutEffect(() => {
475
+ if (viewportRef.current)
476
+ syncScrollbarInsets(viewportRef.current);
477
+ });
464
478
  useLayoutEffect(() => {
465
479
  if (!isMeasured || autoHeight)
466
480
  return;
@@ -468,6 +482,16 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
468
482
  // so columns remain aligned when an overlay locks vertical scrolling.
469
483
  measureViewport();
470
484
  }, [autoHeight, hasVerticalOverflow, isMeasured, measureViewport, overlayScope]);
485
+ useEffect(() => {
486
+ const unsubscribe = grid.on("scrollTo", (position) => {
487
+ const element = viewportRef.current;
488
+ if (!element)
489
+ return;
490
+ element.scrollTop = position.scrollTop;
491
+ element.scrollLeft = position.scrollLeft;
492
+ });
493
+ return unsubscribe;
494
+ }, [grid]);
471
495
  /**
472
496
  * Sync the DomPool after every structural re-render.
473
497
  *
@@ -519,6 +543,7 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
519
543
  const scrollbarWidth = element.offsetWidth - element.clientWidth;
520
544
  if (scrollbarWidth !== scrollbarWidthRef.current) {
521
545
  scrollbarWidthRef.current = scrollbarWidth;
546
+ syncScrollbarInsets(element);
522
547
  grid.setViewport({ width: element.clientWidth });
523
548
  }
524
549
  if (frame.id)
@@ -566,17 +591,42 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
566
591
  const raw = typeof mount.content === "function" ? mount.content(context) : mount.content;
567
592
  return contentToReactNode(raw, { registry: slotComponentsRef.current, context: () => context });
568
593
  };
594
+ const renderSlotGroups = (mounts, context) => SLOT_POSITIONS.map((position) => {
595
+ const content = mounts
596
+ .filter((mount) => mount.position === position)
597
+ .map((mount) => ({ mount, value: resolveSlotContent(mount, context) }))
598
+ .filter((item) => hasRenderableContent(item.value));
599
+ if (content.length === 0)
600
+ return null;
601
+ return (_jsx("div", { className: `omnigrid-slot-item-group omnigrid-slot-pos-${position}`, children: content.map(({ mount, value }) => (_jsx("div", { className: "omnigrid-slot-item", children: value }, mount.id))) }, position));
602
+ });
569
603
  const renderSlot = (slot) => {
570
604
  const mounts = grid.getSlotMounts(slot);
571
605
  if (mounts.length === 0)
572
606
  return null;
573
607
  const context = { api: grid, state: grid.getState(), slot };
574
- return (_jsx("div", { className: `omnigrid-slot omnigrid-slot-${slot}`, children: SLOT_POSITIONS.map((position) => {
575
- const group = mounts.filter((mount) => mount.position === position);
576
- if (group.length === 0)
577
- return null;
578
- return (_jsx("div", { className: `omnigrid-slot-item-group omnigrid-slot-pos-${position}`, children: group.map((mount) => (_jsx("div", { className: "omnigrid-slot-item", children: resolveSlotContent(mount, context) }, mount.id))) }, position));
579
- }) }));
608
+ const groups = renderSlotGroups(mounts, context);
609
+ if (groups.every((group) => group === null))
610
+ return null;
611
+ return (_jsx("div", { className: `omnigrid-slot omnigrid-slot-${slot}`, children: groups }));
612
+ };
613
+ /** Renders slots that overlay the table body (absolute positioning, no layout shift). */
614
+ const renderBodySlot = (slot) => {
615
+ const mounts = grid.getSlotMounts(slot);
616
+ if (mounts.length === 0)
617
+ return null;
618
+ const context = { api: grid, state: grid.getState(), slot };
619
+ const groups = renderSlotGroups(mounts, context);
620
+ if (groups.every((group) => group === null))
621
+ return null;
622
+ return (_jsx("div", { className: `omnigrid-slot omnigrid-slot-${slot}`, style: {
623
+ bottom: slot === "bodyBottom" ? scrollbarInsets.bottom : undefined,
624
+ left: 0,
625
+ position: "absolute",
626
+ right: scrollbarInsets.right,
627
+ top: slot === "bodyTop" ? headerHeight : undefined,
628
+ zIndex: 3,
629
+ }, children: groups }));
580
630
  };
581
631
  /**
582
632
  * Resolves a React component, legacy headless renderer, or built-in overlay
@@ -731,52 +781,53 @@ export function OmniGrid({ className, style, slotComponents, ...options }) {
731
781
  top: headerHeight,
732
782
  width,
733
783
  } }));
734
- return (_jsxs("div", { className: "omnigrid-layout", style: { ...style, position: "relative", ...(isFullOverlay ? { overflow: "hidden" } : {}) }, children: [renderSlot("top"), _jsxs("div", { className: "omnigrid-layout-body", children: [renderSlot("left"), _jsxs("div", { ref: viewportRef, className: viewportClassName, onScroll: handleScroll, style: {
735
- ...viewportStyle,
784
+ return (_jsxs("div", { className: "omnigrid-layout", 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: {
736
785
  flex: "1 1 0",
737
- height: autoHeight ? measuredHeight : "100%",
738
786
  minHeight: 0,
739
787
  minWidth: 0,
740
- overflowX: overlayBlocksHorizontalScroll ? "hidden" : needsHorizontalScroll ? "auto" : "hidden",
741
- overflowY: overlayScope || autoHeight ? "hidden" : "auto",
742
788
  position: "relative",
743
- scrollbarGutter: verticalScrollbarGutter,
744
789
  width: "100%",
745
- }, children: [_jsxs("div", { style: {
746
- display: "flex",
747
- height: rowTotalHeight,
790
+ }, children: [_jsx("div", { ref: viewportRef, className: viewportClassName, onScroll: handleScroll, style: {
791
+ ...viewportStyle,
792
+ flex: "1 1 0",
793
+ height: autoHeight ? measuredHeight : "100%",
794
+ minHeight: 0,
795
+ minWidth: 0,
796
+ overflowX: overlayBlocksHorizontalScroll ? "hidden" : needsHorizontalScroll ? "auto" : "hidden",
797
+ overflowY: overlayScope || autoHeight ? "hidden" : "auto",
748
798
  position: "relative",
749
- width: `max(100%, ${viewportData.totalWidth}px)`,
750
- }, children: [hasPinnedLeft && (_jsxs("div", { className: "omnigrid-pinned omnigrid-pinned-left", style: {
751
- height: rowTotalHeight,
752
- left: 0,
753
- position: "sticky",
754
- top: 0,
755
- width: `${pinnedLeftWidth}px`,
756
- zIndex: 3,
757
- }, children: [renderHeader(viewportData.pinnedLeftColumns, 0, `${pinnedLeftWidth}px`, true), renderRowsLayer(pinnedLeftRowsLayerRef, `${pinnedLeftWidth}px`)] })), _jsxs("div", { className: "omnigrid-scrollable", style: {
758
- flex: hasPins ? `1 0 ${scrollableWidth}px` : "1 1 auto",
759
- minWidth: 0,
760
- position: "relative",
761
- width: centralWidth,
762
- zIndex: 1,
763
- }, children: [renderHeader(viewportData.columns, pinnedLeftWidth, "100%", false), renderRowsLayer(rowsLayerRef, "100%")] }), hasPinnedRight && (_jsxs("div", { className: "omnigrid-pinned omnigrid-pinned-right", style: {
764
- height: rowTotalHeight,
765
- position: "sticky",
766
- right: 0,
767
- top: 0,
768
- width: `${pinnedRightWidth}px`,
769
- zIndex: 3,
770
- }, children: [renderHeader(viewportData.pinnedRightColumns, pinnedLeftWidth + scrollableWidth, `${pinnedRightWidth}px`, true), renderRowsLayer(pinnedRightRowsLayerRef, `${pinnedRightWidth}px`)] }))] }), overlayState.type && overlayScope === "bodyOnly" && (
771
- // Keep the sticky overlay anchored to the body viewport without adding
772
- // its height to the scrollable content's natural dimensions.
773
- _jsx("div", { className: "omnigrid-overlay omnigrid-overlay-body", role: "status", style: {
799
+ scrollbarGutter: verticalScrollbarGutter,
800
+ width: "100%",
801
+ }, children: _jsxs("div", { style: {
802
+ display: "flex",
803
+ height: rowTotalHeight,
804
+ position: "relative",
805
+ width: `max(100%, ${viewportData.totalWidth}px)`,
806
+ }, children: [hasPinnedLeft && (_jsxs("div", { className: "omnigrid-pinned omnigrid-pinned-left", style: {
807
+ height: rowTotalHeight,
808
+ left: 0,
809
+ position: "sticky",
810
+ top: 0,
811
+ width: `${pinnedLeftWidth}px`,
812
+ zIndex: 3,
813
+ }, children: [renderHeader(viewportData.pinnedLeftColumns, 0, `${pinnedLeftWidth}px`, true), renderRowsLayer(pinnedLeftRowsLayerRef, `${pinnedLeftWidth}px`)] })), _jsxs("div", { className: "omnigrid-scrollable", style: {
814
+ flex: hasPins ? `1 0 ${scrollableWidth}px` : "1 1 auto",
815
+ minWidth: 0,
816
+ position: "relative",
817
+ width: centralWidth,
818
+ zIndex: 1,
819
+ }, children: [renderHeader(viewportData.columns, pinnedLeftWidth, "100%", false), renderRowsLayer(rowsLayerRef, "100%")] }), hasPinnedRight && (_jsxs("div", { className: "omnigrid-pinned omnigrid-pinned-right", style: {
820
+ height: rowTotalHeight,
821
+ position: "sticky",
822
+ right: 0,
823
+ top: 0,
824
+ width: `${pinnedRightWidth}px`,
825
+ zIndex: 3,
826
+ }, children: [renderHeader(viewportData.pinnedRightColumns, pinnedLeftWidth + scrollableWidth, `${pinnedRightWidth}px`, true), renderRowsLayer(pinnedRightRowsLayerRef, `${pinnedRightWidth}px`)] }))] }) }), renderBodySlot("bodyTop"), renderBodySlot("bodyBottom"), overlayState.type && overlayScope === "bodyOnly" && (_jsx("div", { className: "omnigrid-overlay omnigrid-overlay-body", role: "status", style: {
774
827
  height: overlayHeight,
775
828
  left: 0,
776
- marginBottom: -overlayHeight,
777
- marginTop: -rowTotalHeight,
778
829
  pointerEvents: "auto",
779
- position: "sticky",
830
+ position: "absolute",
780
831
  right: 0,
781
832
  top: headerHeight,
782
833
  width: "100%",
package/dist/content.d.ts CHANGED
@@ -21,6 +21,7 @@ export declare function isSlotComponentContent(value: unknown): value is {
21
21
  kind: string;
22
22
  payload?: unknown;
23
23
  };
24
+ export declare function hasRenderableContent(content: ReactNode): boolean;
24
25
  /**
25
26
  * Materialises the core's headless content into a ReactNode:
26
27
  * - `string | number | boolean` → scalar;
package/dist/content.js CHANGED
@@ -11,6 +11,16 @@ export function isSlotNodeContent(value) {
11
11
  export function isSlotComponentContent(value) {
12
12
  return typeof value === "object" && value !== null && value.type === "component";
13
13
  }
14
+ export function hasRenderableContent(content) {
15
+ if (content === null || content === undefined || typeof content === "boolean" || content === "")
16
+ return false;
17
+ if (Array.isArray(content))
18
+ return content.some(hasRenderableContent);
19
+ if (isValidElement(content) && content.type === Fragment) {
20
+ return hasRenderableContent(content.props.children);
21
+ }
22
+ return true;
23
+ }
14
24
  function renderCheckboxControl(value) {
15
25
  return createElement("input", {
16
26
  type: "checkbox",
package/dist/useGrid.js CHANGED
@@ -28,6 +28,22 @@ export function useGrid(options) {
28
28
  const subscribe = useCallback((listener) => grid.subscribe(listener), [grid]);
29
29
  const getSnapshot = useCallback(() => grid.getState(), [grid]);
30
30
  const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
31
+ // Reconcile plugins when the plugin list changes. Plugins removed from the
32
+ // list are unregistered (their cleanup restores default behavior); new ones
33
+ // are registered. Declared before the data sync so that, on a switch, the
34
+ // old plugin is gone before data is replaced and the new one is attached
35
+ // before the new data arrives. The first run is skipped: the Grid
36
+ // constructor already registered the initial plugins.
37
+ const pluginsSyncedRef = useRef(false);
38
+ useEffect(() => {
39
+ if (grid.isDestroyed())
40
+ return;
41
+ if (!pluginsSyncedRef.current) {
42
+ pluginsSyncedRef.current = true;
43
+ return;
44
+ }
45
+ grid.setPlugins(options.plugins);
46
+ }, [grid, options.plugins]);
31
47
  // Sync data when the data reference changes (async load, pagination,
32
48
  // parent-side filtering). Skip on the first run — the Grid constructor
33
49
  // already initialised data from options.data. Only sync on subsequent
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnigrid/react",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "description": "React adapter and renderer for OmniGrid",
5
5
  "keywords": [
6
6
  "omnigrid",