@gridkitjs/react 0.0.0-dev-20261118111459 → 0.0.0-dev-20261118134132

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.d.ts CHANGED
@@ -89,13 +89,16 @@ type SelectionCallbacks<Row> = SelectionCallbacks$1<Row, ReactNode>;
89
89
  * `packages/react/src/DataGrid/hooks/` for the public hooks built on it.
90
90
  */
91
91
  interface DataGridApi<Row> extends GridExportApi, GridPrintApi, GridPanelApi {
92
- /** The grid's scrollable viewport element. */
92
+ /**
93
+ * The grid's root element, `.gridkit-data-grid-viewport`. The tables
94
+ * scroll inside it, in `.gridkit-data-grid-tables`, not on it.
95
+ */
93
96
  readonly element: HTMLDivElement | null;
94
97
  /**
95
98
  * The grid's row-area `<table>` element — the one holding `<tbody>`. The
96
99
  * header and, when aggregates are active, footer render as their own
97
100
  * sibling `<table>`s outside this one; use `element` to reach the whole
98
- * scrollable viewport instead.
101
+ * grid instead.
99
102
  */
100
103
  readonly table: HTMLTableElement | null;
101
104
  /** Rows as currently filtered and sorted — what's rendered, ungrouped. */
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  useImperativeHandle as useImperativeHandle3,
8
8
  useMemo as useMemo5,
9
9
  useRef as useRef23,
10
- useState as useState18
10
+ useState as useState19
11
11
  } from "react";
12
12
  import {
13
13
  headerFocus as headerFocus3,
@@ -523,7 +523,7 @@ function useGridNavigation({
523
523
  if (gridFocus.handleKey(
524
524
  event.key,
525
525
  event,
526
- measurePageSize(bodyTableRef.current),
526
+ measurePageSize(bodyTableRef.current, gridRef.current),
527
527
  writingDirection2(event.currentTarget)
528
528
  )) {
529
529
  event.preventDefault();
@@ -2386,19 +2386,22 @@ function bodyCell(parts, rowIndex, columnIndex) {
2386
2386
  return element === void 0 ? null : { element, focus: bodyFocus2(rowIndex, columnIndex) };
2387
2387
  }
2388
2388
  function viewOf(element, parts) {
2389
- const root = parts.root?.getBoundingClientRect();
2390
- if (root === void 0) {
2389
+ const view = parts.scroller?.getBoundingClientRect();
2390
+ if (view === void 0) {
2391
2391
  return null;
2392
2392
  }
2393
- const body = parts.body?.contains(element) === true ? parts.body.getBoundingClientRect() : root;
2394
- const left = Math.max(root.left, body.left);
2395
- const top = Math.max(root.top, body.top);
2396
- return new DOMRect(
2397
- left,
2398
- top,
2399
- Math.max(0, Math.min(root.right, body.right) - left),
2400
- Math.max(0, Math.min(root.bottom, body.bottom) - top)
2401
- );
2393
+ let { top, bottom } = view;
2394
+ if (parts.body?.contains(element) === true) {
2395
+ top = Math.max(
2396
+ top,
2397
+ parts.headerTable?.getBoundingClientRect().bottom ?? top
2398
+ );
2399
+ bottom = Math.min(
2400
+ bottom,
2401
+ parts.footerTable?.getBoundingClientRect().top ?? bottom
2402
+ );
2403
+ }
2404
+ return new DOMRect(view.left, top, view.width, Math.max(0, bottom - top));
2402
2405
  }
2403
2406
  function clamp(value, min, max) {
2404
2407
  return Math.min(Math.max(value, min), max);
@@ -2424,12 +2427,16 @@ function onCell(target, cell, parts) {
2424
2427
  }
2425
2428
  };
2426
2429
  }
2427
- function onBody(body) {
2430
+ function onBody(body, parts) {
2428
2431
  const box = body.getBoundingClientRect();
2432
+ const view = viewOf(body, parts) ?? box;
2429
2433
  return {
2430
2434
  target: { area: "body" },
2431
2435
  cell: null,
2432
- anchor: { x: inlineStartOf(body, box), y: box.top }
2436
+ anchor: {
2437
+ x: inlineStartOf(body, view),
2438
+ y: clamp(box.top, view.top, view.bottom)
2439
+ }
2433
2440
  };
2434
2441
  }
2435
2442
  function headerPlaceAt(element, parts) {
@@ -2469,7 +2476,7 @@ function bodyPlaceAt(element, parts) {
2469
2476
  parts
2470
2477
  ) : null;
2471
2478
  }
2472
- return bodyRowFrom2(element) === null ? onBody(body) : null;
2479
+ return bodyRowFrom2(element) === null ? onBody(body, parts) : null;
2473
2480
  }
2474
2481
  function placeAt(element, parts) {
2475
2482
  return headerPlaceAt(element, parts) ?? bodyPlaceAt(element, parts);
@@ -2500,16 +2507,18 @@ function placeOf(target, parts) {
2500
2507
  return onCell(target, bodyCell(parts, rowIndex, 0), parts);
2501
2508
  }
2502
2509
  case "body":
2503
- return parts.body === null ? null : onBody(parts.body);
2510
+ return parts.body === null ? null : onBody(parts.body, parts);
2504
2511
  }
2505
2512
  }
2506
2513
 
2507
2514
  // src/DataGrid/contextMenu/useContextMenu.ts
2508
2515
  function useContextMenu({
2509
2516
  rootRef,
2517
+ scrollRef,
2510
2518
  headerTableRef,
2511
2519
  bodyTableRef,
2512
- bodyScrollRef,
2520
+ bodyRef,
2521
+ footerTableRef,
2513
2522
  contextMenu,
2514
2523
  overlay,
2515
2524
  controller,
@@ -2525,10 +2534,11 @@ function useContextMenu({
2525
2534
  function parts() {
2526
2535
  const { columns, pageRows } = controller.getView();
2527
2536
  return {
2528
- root: rootRef.current,
2537
+ scroller: scrollRef.current,
2529
2538
  headerTable: headerTableRef.current,
2530
2539
  bodyTable: bodyTableRef.current,
2531
- body: bodyScrollRef.current,
2540
+ body: bodyRef.current,
2541
+ footerTable: footerTableRef.current,
2532
2542
  columns,
2533
2543
  pageRows
2534
2544
  };
@@ -4446,12 +4456,31 @@ function useColumnResize({
4446
4456
  return { activeColumnId, startResize, sizeToContent, nudge };
4447
4457
  }
4448
4458
 
4449
- // src/DataGrid/useElementWidth.ts
4459
+ // src/DataGrid/useElementHeight.ts
4450
4460
  import { useLayoutEffect as useLayoutEffect14, useState as useState15 } from "react";
4461
+ import { observeElementHeight } from "@gridkitjs/dom";
4462
+ function useElementHeight(ref, enabled) {
4463
+ const [height, setHeight] = useState15(null);
4464
+ useLayoutEffect14(() => {
4465
+ const element = ref.current;
4466
+ if (element === null || !enabled) {
4467
+ return void 0;
4468
+ }
4469
+ const disconnect = observeElementHeight(element, setHeight);
4470
+ return () => {
4471
+ disconnect();
4472
+ setHeight(null);
4473
+ };
4474
+ }, [ref, enabled]);
4475
+ return enabled ? height : null;
4476
+ }
4477
+
4478
+ // src/DataGrid/useElementWidth.ts
4479
+ import { useLayoutEffect as useLayoutEffect15, useState as useState16 } from "react";
4451
4480
  import { observeElementWidth } from "@gridkitjs/dom";
4452
4481
  function useElementWidth(ref, enabled) {
4453
- const [width, setWidth] = useState15(null);
4454
- useLayoutEffect14(() => {
4482
+ const [width, setWidth] = useState16(null);
4483
+ useLayoutEffect15(() => {
4455
4484
  const element = ref.current;
4456
4485
  if (element === null || !enabled) {
4457
4486
  return void 0;
@@ -4462,12 +4491,12 @@ function useElementWidth(ref, enabled) {
4462
4491
  }
4463
4492
 
4464
4493
  // src/DataGrid/useGridController.ts
4465
- import { useState as useState16, useSyncExternalStore as useSyncExternalStore2 } from "react";
4494
+ import { useState as useState17, useSyncExternalStore as useSyncExternalStore2 } from "react";
4466
4495
  import {
4467
4496
  createGridController
4468
4497
  } from "@gridkitjs/core";
4469
4498
  function useGridController(options) {
4470
- const [controller] = useState16(
4499
+ const [controller] = useState17(
4471
4500
  () => createGridController(options)
4472
4501
  );
4473
4502
  controller.setOptions(options);
@@ -4515,11 +4544,11 @@ function useInfiniteScroll({
4515
4544
  }
4516
4545
 
4517
4546
  // src/DataGrid/useLiveAnnouncer.ts
4518
- import { useCallback as useCallback3, useLayoutEffect as useLayoutEffect15, useRef as useRef21 } from "react";
4547
+ import { useCallback as useCallback3, useLayoutEffect as useLayoutEffect16, useRef as useRef21 } from "react";
4519
4548
  import { createLiveAnnouncer } from "@gridkitjs/dom";
4520
4549
  function useLiveAnnouncer(containerRef) {
4521
4550
  const announcer = useRef21(null);
4522
- useLayoutEffect15(() => {
4551
+ useLayoutEffect16(() => {
4523
4552
  const container = containerRef.current;
4524
4553
  if (container === null) {
4525
4554
  return void 0;
@@ -4537,7 +4566,7 @@ function useLiveAnnouncer(containerRef) {
4537
4566
  }
4538
4567
 
4539
4568
  // src/DataGrid/useRowVirtualizer.ts
4540
- import { useLayoutEffect as useLayoutEffect16, useRef as useRef22, useState as useState17 } from "react";
4569
+ import { useLayoutEffect as useLayoutEffect17, useRef as useRef22, useState as useState18 } from "react";
4541
4570
  import {
4542
4571
  bodyRowFrom as bodyRowFrom3,
4543
4572
  createRowVirtualizer as createRowVirtualizer2
@@ -4572,25 +4601,25 @@ function ignoreRow() {
4572
4601
  function useRowVirtualizer({
4573
4602
  enabled,
4574
4603
  rows,
4575
- bodyScrollRef,
4604
+ scrollRef,
4576
4605
  bodyTableRef,
4577
4606
  estimatedRowHeight,
4578
4607
  overscan
4579
4608
  }) {
4580
- const [virtualizer, setVirtualizer] = useState17(null);
4609
+ const [virtualizer, setVirtualizer] = useState18(null);
4581
4610
  const instance = useRef22(null);
4582
4611
  const rowCount = useRef22(rows.length);
4583
- const [rowWindow, setRowWindow] = useState17(enabled ? NO_ROWS3 : ALL_ROWS);
4612
+ const [rowWindow, setRowWindow] = useState18(enabled ? NO_ROWS3 : ALL_ROWS);
4584
4613
  if (!enabled && rowWindow.kind !== "allRows") {
4585
4614
  setRowWindow(ALL_ROWS);
4586
4615
  }
4587
- useLayoutEffect16(() => {
4616
+ useLayoutEffect17(() => {
4588
4617
  rowCount.current = rows.length;
4589
4618
  instance.current?.setRowCount(rows.length);
4590
4619
  instance.current?.resetHeights();
4591
4620
  }, [rows]);
4592
- useLayoutEffect16(() => {
4593
- const element = bodyScrollRef.current;
4621
+ useLayoutEffect17(() => {
4622
+ const element = scrollRef.current;
4594
4623
  if (!enabled || element === null) {
4595
4624
  return void 0;
4596
4625
  }
@@ -4608,7 +4637,7 @@ function useRowVirtualizer({
4608
4637
  return () => {
4609
4638
  created.destroy();
4610
4639
  };
4611
- }, [enabled, bodyScrollRef, bodyTableRef, estimatedRowHeight, overscan]);
4640
+ }, [enabled, scrollRef, bodyTableRef, estimatedRowHeight, overscan]);
4612
4641
  const range = rangeOf(rowWindow, rows.length);
4613
4642
  return {
4614
4643
  startIndex: range.startIndex,
@@ -4684,9 +4713,10 @@ function DataGrid(props) {
4684
4713
  const gridRef = useRef23(null);
4685
4714
  const headerTableRef = useRef23(null);
4686
4715
  const bodyTableRef = useRef23(null);
4687
- const bodyScrollRef = useRef23(null);
4716
+ const bodyRef = useRef23(null);
4717
+ const footerTableRef = useRef23(null);
4688
4718
  const sentinelRef = useRef23(null);
4689
- const viewportWidth = useElementWidth(viewportRef, resizeMode === "fit");
4719
+ const viewportWidth = useElementWidth(gridRef, resizeMode === "fit");
4690
4720
  const subscribersRef = useRef23(/* @__PURE__ */ new Set());
4691
4721
  const gridId = useId2();
4692
4722
  const announce = useLiveAnnouncer(viewportRef);
@@ -4705,12 +4735,12 @@ function DataGrid(props) {
4705
4735
  columnOf
4706
4736
  })
4707
4737
  });
4708
- const [readPagination] = useState18(() => paginationReader(controller));
4709
- const [readSelectedRows] = useState18(() => selectedRowsReader(controller));
4738
+ const [readPagination] = useState19(() => paginationReader(controller));
4739
+ const [readSelectedRows] = useState19(() => selectedRowsReader(controller));
4710
4740
  const virtualizer = useRowVirtualizer({
4711
4741
  enabled: virtualized,
4712
4742
  rows: view.pageRows,
4713
- bodyScrollRef,
4743
+ scrollRef: gridRef,
4714
4744
  bodyTableRef,
4715
4745
  estimatedRowHeight,
4716
4746
  overscan
@@ -4731,7 +4761,7 @@ function DataGrid(props) {
4731
4761
  }
4732
4762
  }, [paginated, hasInfiniteScroll]);
4733
4763
  useInfiniteScroll({
4734
- rootRef: bodyScrollRef,
4764
+ rootRef: gridRef,
4735
4765
  sentinelRef,
4736
4766
  config: infiniteScroll
4737
4767
  });
@@ -4853,9 +4883,11 @@ function DataGrid(props) {
4853
4883
  const multiselectable = selection.rowMode === "multiple" || selection.columnMode === "multiple";
4854
4884
  const contextMenu = useContextMenu({
4855
4885
  rootRef: viewportRef,
4886
+ scrollRef: gridRef,
4856
4887
  headerTableRef,
4857
4888
  bodyTableRef,
4858
- bodyScrollRef,
4889
+ bodyRef,
4890
+ footerTableRef,
4859
4891
  contextMenu: props.contextMenu,
4860
4892
  overlay,
4861
4893
  controller,
@@ -4980,6 +5012,11 @@ function DataGrid(props) {
4980
5012
  selection.cellMode === false ? "" : "gridkit-selectable-cells"
4981
5013
  );
4982
5014
  const hasFooter = aggregates.length > 0;
5015
+ const headerHeight = useElementHeight(headerTableRef, height !== void 0);
5016
+ const footerHeight = useElementHeight(
5017
+ footerTableRef,
5018
+ height !== void 0 && hasFooter
5019
+ );
4983
5020
  return /* @__PURE__ */ jsxs19(
4984
5021
  "div",
4985
5022
  {
@@ -5022,6 +5059,12 @@ function DataGrid(props) {
5022
5059
  role: state.groupBy.length > 0 ? "treegrid" : "grid",
5023
5060
  "aria-rowcount": view.displayRows.length + (hasFooter ? 2 : 1),
5024
5061
  "aria-colcount": view.columns.length,
5062
+ tabIndex: -1,
5063
+ style: height === void 0 ? void 0 : {
5064
+ "--gridkit-body-height": typeof height === "number" ? `${String(height)}px` : height,
5065
+ ...headerHeight === null ? {} : { "--gridkit-header-height": `${String(headerHeight)}px` },
5066
+ ...footerHeight === null ? {} : { "--gridkit-footer-height": `${String(footerHeight)}px` }
5067
+ },
5025
5068
  ...ariaAttr(multiselectable, "aria-multiselectable", true),
5026
5069
  ...ariaAttr(labelledBy !== void 0, "aria-labelledby", labelledBy),
5027
5070
  ...ariaAttr(
@@ -5079,12 +5122,9 @@ function DataGrid(props) {
5079
5122
  "div",
5080
5123
  {
5081
5124
  className: "gridkit-data-grid-body",
5082
- ref: bodyScrollRef,
5125
+ ref: bodyRef,
5083
5126
  role: "rowgroup",
5084
5127
  tabIndex: -1,
5085
- style: height === void 0 ? void 0 : {
5086
- "--gridkit-body-height": typeof height === "number" ? `${String(height)}px` : height
5087
- },
5088
5128
  children: /* @__PURE__ */ jsxs19(
5089
5129
  "table",
5090
5130
  {
@@ -5126,6 +5166,7 @@ function DataGrid(props) {
5126
5166
  hasFooter && /* @__PURE__ */ jsxs19(
5127
5167
  "table",
5128
5168
  {
5169
+ ref: footerTableRef,
5129
5170
  role: "presentation",
5130
5171
  style: { width: totalColumnWidth(view.columns) },
5131
5172
  className: classNames(
@@ -5379,9 +5420,9 @@ function useColumnVisibilityState(gridRef) {
5379
5420
  }
5380
5421
 
5381
5422
  // src/DataGrid/hooks/useFilterState.ts
5382
- import { useCallback as useCallback13, useState as useState19 } from "react";
5423
+ import { useCallback as useCallback13, useState as useState20 } from "react";
5383
5424
  function useFilterState(gridRef) {
5384
- const [fallback] = useState19(() => []);
5425
+ const [fallback] = useState20(() => []);
5385
5426
  const filter = useGridSnapshot(
5386
5427
  gridRef,
5387
5428
  useCallback13((api) => api.getFilter(), []),