@mui/x-data-grid 8.9.1 → 8.9.2

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 (126) hide show
  1. package/CHANGELOG.md +96 -2
  2. package/DataGrid/DataGrid.js +5 -0
  3. package/DataGrid/useDataGridComponent.js +2 -0
  4. package/colDef/gridCheckboxSelectionColDef.js +1 -0
  5. package/components/GridDetailPanels.d.ts +2 -2
  6. package/components/GridPinnedRows.d.ts +2 -2
  7. package/components/GridRow.js +5 -0
  8. package/components/cell/GridCell.js +7 -5
  9. package/components/cell/GridEditSingleSelectCell.js +1 -1
  10. package/components/columnsManagement/GridColumnsManagement.d.ts +6 -0
  11. package/components/columnsManagement/GridColumnsManagement.js +17 -5
  12. package/components/containers/GridRootStyles.d.ts +1 -1
  13. package/components/toolbarV8/Toolbar.d.ts +1 -1
  14. package/components/virtualization/GridVirtualScroller.js +5 -5
  15. package/constants/dataGridPropsDefaultValues.js +1 -0
  16. package/esm/DataGrid/DataGrid.js +5 -0
  17. package/esm/DataGrid/useDataGridComponent.js +2 -0
  18. package/esm/colDef/gridCheckboxSelectionColDef.js +1 -0
  19. package/esm/components/GridDetailPanels.d.ts +2 -2
  20. package/esm/components/GridPinnedRows.d.ts +2 -2
  21. package/esm/components/GridRow.js +5 -0
  22. package/esm/components/cell/GridCell.js +7 -5
  23. package/esm/components/cell/GridEditSingleSelectCell.js +1 -1
  24. package/esm/components/columnsManagement/GridColumnsManagement.d.ts +6 -0
  25. package/esm/components/columnsManagement/GridColumnsManagement.js +17 -5
  26. package/esm/components/containers/GridRootStyles.d.ts +1 -1
  27. package/esm/components/toolbarV8/Toolbar.d.ts +1 -1
  28. package/esm/components/virtualization/GridVirtualScroller.js +5 -5
  29. package/esm/constants/dataGridPropsDefaultValues.js +1 -0
  30. package/esm/hooks/core/useGridVirtualizer.d.ts +9 -0
  31. package/esm/hooks/core/useGridVirtualizer.js +223 -0
  32. package/esm/hooks/features/columnHeaders/useGridColumnHeaders.d.ts +1 -1
  33. package/esm/hooks/features/columnHeaders/useGridColumnHeaders.js +3 -2
  34. package/esm/hooks/features/columns/useGridColumnSpanning.js +5 -90
  35. package/esm/hooks/features/dimensions/useGridDimensions.js +18 -188
  36. package/esm/hooks/features/keyboardNavigation/useGridKeyboardNavigation.js +1 -1
  37. package/esm/hooks/features/keyboardNavigation/utils.d.ts +3 -3
  38. package/esm/hooks/features/keyboardNavigation/utils.js +5 -5
  39. package/esm/hooks/features/pivoting/gridPivotingInterfaces.d.ts +0 -1
  40. package/esm/hooks/features/pivoting/gridPivotingSelectors.d.ts +1 -4
  41. package/esm/hooks/features/pivoting/gridPivotingSelectors.js +1 -2
  42. package/esm/hooks/features/rows/gridRowsMetaInterfaces.d.ts +2 -8
  43. package/esm/hooks/features/rows/gridRowsMetaState.d.ts +2 -18
  44. package/esm/hooks/features/rows/useGridRowAriaAttributes.js +3 -1
  45. package/esm/hooks/features/rows/useGridRowSpanning.d.ts +2 -12
  46. package/esm/hooks/features/rows/useGridRowSpanning.js +54 -85
  47. package/esm/hooks/features/rows/useGridRowsMeta.d.ts +1 -1
  48. package/esm/hooks/features/rows/useGridRowsMeta.js +17 -187
  49. package/esm/hooks/features/virtualization/gridVirtualizationSelectors.d.ts +1 -1
  50. package/esm/hooks/features/virtualization/index.d.ts +1 -0
  51. package/esm/hooks/features/virtualization/index.js +1 -0
  52. package/esm/hooks/features/virtualization/useGridVirtualization.d.ts +5 -16
  53. package/esm/hooks/features/virtualization/useGridVirtualization.js +41 -22
  54. package/esm/hooks/utils/index.d.ts +1 -1
  55. package/esm/hooks/utils/index.js +1 -1
  56. package/esm/hooks/utils/useFirstRender.d.ts +1 -1
  57. package/esm/hooks/utils/useFirstRender.js +1 -8
  58. package/esm/index.js +1 -1
  59. package/esm/internals/index.d.ts +1 -1
  60. package/esm/internals/index.js +1 -1
  61. package/esm/internals/utils/getPinnedCellOffset.js +5 -0
  62. package/esm/locales/frFR.js +18 -21
  63. package/esm/locales/heIL.js +12 -12
  64. package/esm/locales/plPL.js +22 -24
  65. package/esm/models/api/gridApiCommon.d.ts +4 -1
  66. package/esm/models/api/gridColumnSpanning.d.ts +5 -11
  67. package/esm/models/props/DataGridProps.d.ts +5 -0
  68. package/esm/utils/roundToDecimalPlaces.d.ts +1 -1
  69. package/esm/utils/roundToDecimalPlaces.js +1 -3
  70. package/hooks/core/useGridVirtualizer.d.ts +9 -0
  71. package/hooks/core/useGridVirtualizer.js +231 -0
  72. package/hooks/features/columnHeaders/useGridColumnHeaders.d.ts +1 -1
  73. package/hooks/features/columnHeaders/useGridColumnHeaders.js +4 -3
  74. package/hooks/features/columns/useGridColumnSpanning.js +5 -91
  75. package/hooks/features/dimensions/useGridDimensions.js +18 -188
  76. package/hooks/features/keyboardNavigation/useGridKeyboardNavigation.js +1 -1
  77. package/hooks/features/keyboardNavigation/utils.d.ts +3 -3
  78. package/hooks/features/keyboardNavigation/utils.js +5 -5
  79. package/hooks/features/pivoting/gridPivotingInterfaces.d.ts +0 -1
  80. package/hooks/features/pivoting/gridPivotingSelectors.d.ts +1 -4
  81. package/hooks/features/pivoting/gridPivotingSelectors.js +2 -3
  82. package/hooks/features/rows/gridRowsMetaInterfaces.d.ts +2 -8
  83. package/hooks/features/rows/gridRowsMetaState.d.ts +2 -18
  84. package/hooks/features/rows/useGridRowAriaAttributes.js +3 -1
  85. package/hooks/features/rows/useGridRowSpanning.d.ts +2 -12
  86. package/hooks/features/rows/useGridRowSpanning.js +54 -85
  87. package/hooks/features/rows/useGridRowsMeta.d.ts +1 -1
  88. package/hooks/features/rows/useGridRowsMeta.js +15 -186
  89. package/hooks/features/virtualization/gridVirtualizationSelectors.d.ts +1 -1
  90. package/hooks/features/virtualization/index.d.ts +1 -0
  91. package/hooks/features/virtualization/index.js +12 -0
  92. package/hooks/features/virtualization/useGridVirtualization.d.ts +5 -16
  93. package/hooks/features/virtualization/useGridVirtualization.js +42 -24
  94. package/hooks/utils/index.d.ts +1 -1
  95. package/hooks/utils/index.js +12 -12
  96. package/hooks/utils/useFirstRender.d.ts +1 -1
  97. package/hooks/utils/useFirstRender.js +11 -11
  98. package/index.js +1 -1
  99. package/internals/index.d.ts +1 -1
  100. package/internals/index.js +4 -11
  101. package/internals/utils/getPinnedCellOffset.js +5 -0
  102. package/locales/frFR.js +18 -21
  103. package/locales/heIL.js +12 -12
  104. package/locales/plPL.js +22 -24
  105. package/models/api/gridApiCommon.d.ts +4 -1
  106. package/models/api/gridColumnSpanning.d.ts +5 -11
  107. package/models/props/DataGridProps.d.ts +5 -0
  108. package/package.json +5 -5
  109. package/utils/roundToDecimalPlaces.d.ts +1 -1
  110. package/utils/roundToDecimalPlaces.js +7 -4
  111. package/esm/hooks/features/rows/gridRowSpanningSelectors.d.ts +0 -10
  112. package/esm/hooks/features/rows/gridRowSpanningSelectors.js +0 -5
  113. package/esm/hooks/features/virtualization/useGridVirtualScroller.d.ts +0 -56
  114. package/esm/hooks/features/virtualization/useGridVirtualScroller.js +0 -843
  115. package/esm/hooks/utils/useRunOnce.d.ts +0 -5
  116. package/esm/hooks/utils/useRunOnce.js +0 -18
  117. package/esm/utils/platform.d.ts +0 -1
  118. package/esm/utils/platform.js +0 -2
  119. package/hooks/features/rows/gridRowSpanningSelectors.d.ts +0 -10
  120. package/hooks/features/rows/gridRowSpanningSelectors.js +0 -11
  121. package/hooks/features/virtualization/useGridVirtualScroller.d.ts +0 -56
  122. package/hooks/features/virtualization/useGridVirtualScroller.js +0 -854
  123. package/hooks/utils/useRunOnce.d.ts +0 -5
  124. package/hooks/utils/useRunOnce.js +0 -27
  125. package/utils/platform.d.ts +0 -1
  126. package/utils/platform.js +0 -8
@@ -1,854 +0,0 @@
1
- "use strict";
2
- 'use client';
3
-
4
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
5
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
6
- Object.defineProperty(exports, "__esModule", {
7
- value: true
8
- });
9
- exports.EMPTY_DETAIL_PANELS = void 0;
10
- exports.areRenderContextsEqual = areRenderContextsEqual;
11
- exports.computeOffsetLeft = computeOffsetLeft;
12
- exports.useGridVirtualScroller = void 0;
13
- var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
14
- var React = _interopRequireWildcard(require("react"));
15
- var ReactDOM = _interopRequireWildcard(require("react-dom"));
16
- var _useEventCallback = _interopRequireDefault(require("@mui/utils/useEventCallback"));
17
- var _useEnhancedEffect = _interopRequireDefault(require("@mui/utils/useEnhancedEffect"));
18
- var _useLazyRef = _interopRequireDefault(require("@mui/utils/useLazyRef"));
19
- var _useTimeout = _interopRequireDefault(require("@mui/utils/useTimeout"));
20
- var _RtlProvider = require("@mui/system/RtlProvider");
21
- var _reactMajor = _interopRequireDefault(require("@mui/x-internals/reactMajor"));
22
- var _gridDimensionsSelectors = require("../dimensions/gridDimensionsSelectors");
23
- var _useGridPrivateApiContext = require("../../utils/useGridPrivateApiContext");
24
- var _useGridRootProps = require("../../utils/useGridRootProps");
25
- var _useGridSelector = require("../../utils/useGridSelector");
26
- var _useRunOnce = require("../../utils/useRunOnce");
27
- var _gridColumnsSelector = require("../columns/gridColumnsSelector");
28
- var _gridRowsSelector = require("../rows/gridRowsSelector");
29
- var _useGridVisibleRows = require("../../utils/useGridVisibleRows");
30
- var _utils = require("../../utils");
31
- var platform = _interopRequireWildcard(require("../../../utils/platform"));
32
- var _utils2 = require("../../../utils/utils");
33
- var _gridRowsMetaSelector = require("../rows/gridRowsMetaSelector");
34
- var _gridColumnsUtils = require("../columns/gridColumnsUtils");
35
- var _gridVirtualizationSelectors = require("./gridVirtualizationSelectors");
36
- var _useGridVirtualization = require("./useGridVirtualization");
37
- var _gridRowSpanningSelectors = require("../rows/gridRowSpanningSelectors");
38
- var _gridRowsUtils = require("../rows/gridRowsUtils");
39
- var _gridFocusedVirtualCellSelector = require("./gridFocusedVirtualCellSelector");
40
- var _roundToDecimalPlaces = require("../../../utils/roundToDecimalPlaces");
41
- var _isJSDOM = require("../../../utils/isJSDOM");
42
- var _rowSelection = require("../rowSelection");
43
- var _jsxRuntime = require("react/jsx-runtime");
44
- const MINIMUM_COLUMN_WIDTH = 50;
45
- var ScrollDirection = /*#__PURE__*/function (ScrollDirection) {
46
- ScrollDirection[ScrollDirection["NONE"] = 0] = "NONE";
47
- ScrollDirection[ScrollDirection["UP"] = 1] = "UP";
48
- ScrollDirection[ScrollDirection["DOWN"] = 2] = "DOWN";
49
- ScrollDirection[ScrollDirection["LEFT"] = 3] = "LEFT";
50
- ScrollDirection[ScrollDirection["RIGHT"] = 4] = "RIGHT";
51
- return ScrollDirection;
52
- }(ScrollDirection || {});
53
- const EMPTY_SCROLL_POSITION = {
54
- top: 0,
55
- left: 0
56
- };
57
- const EMPTY_DETAIL_PANELS = exports.EMPTY_DETAIL_PANELS = Object.freeze(new Map());
58
- const createScrollCache = (isRtl, rowBufferPx, columnBufferPx, verticalBuffer, horizontalBuffer) => ({
59
- direction: ScrollDirection.NONE,
60
- buffer: bufferForDirection(isRtl, ScrollDirection.NONE, rowBufferPx, columnBufferPx, verticalBuffer, horizontalBuffer)
61
- });
62
- const useGridVirtualScroller = () => {
63
- const apiRef = (0, _useGridPrivateApiContext.useGridPrivateApiContext)();
64
- const rootProps = (0, _useGridRootProps.useGridRootProps)();
65
- const {
66
- listView
67
- } = rootProps;
68
- const visibleColumns = (0, _useGridSelector.useGridSelector)(apiRef, _gridColumnsSelector.gridVisibleColumnDefinitionsSelector);
69
- const enabledForRows = (0, _useGridSelector.useGridSelector)(apiRef, _gridVirtualizationSelectors.gridVirtualizationRowEnabledSelector) && !_isJSDOM.isJSDOM;
70
- const enabledForColumns = (0, _useGridSelector.useGridSelector)(apiRef, _gridVirtualizationSelectors.gridVirtualizationColumnEnabledSelector) && !_isJSDOM.isJSDOM;
71
- const pinnedRows = (0, _useGridSelector.useGridSelector)(apiRef, _gridRowsSelector.gridPinnedRowsSelector);
72
- const pinnedColumns = (0, _gridColumnsSelector.gridVisiblePinnedColumnDefinitionsSelector)(apiRef);
73
- const hasBottomPinnedRows = pinnedRows.bottom.length > 0;
74
- const [panels, setPanels] = React.useState(EMPTY_DETAIL_PANELS);
75
- const isRtl = (0, _RtlProvider.useRtl)();
76
- const rowSelectionManager = (0, _useGridSelector.useGridSelector)(apiRef, _rowSelection.gridRowSelectionManagerSelector);
77
- const currentPage = (0, _useGridVisibleRows.useGridVisibleRows)(apiRef);
78
- const mainRef = apiRef.current.mainElementRef;
79
- const scrollerRef = apiRef.current.virtualScrollerRef;
80
- const scrollbarVerticalRef = apiRef.current.virtualScrollbarVerticalRef;
81
- const scrollbarHorizontalRef = apiRef.current.virtualScrollbarHorizontalRef;
82
- const hasColSpan = (0, _useGridSelector.useGridSelector)(apiRef, _gridColumnsSelector.gridHasColSpanSelector);
83
- const isRenderContextReady = React.useRef(false);
84
- const rowHeight = (0, _useGridSelector.useGridSelector)(apiRef, _gridDimensionsSelectors.gridRowHeightSelector);
85
- const contentHeight = (0, _useGridSelector.useGridSelector)(apiRef, _gridDimensionsSelectors.gridContentHeightSelector);
86
- const columnsTotalWidth = (0, _useGridSelector.useGridSelector)(apiRef, _gridDimensionsSelectors.gridColumnsTotalWidthSelector);
87
- const needsHorizontalScrollbar = (0, _useGridSelector.useGridSelector)(apiRef, needsHorizontalScrollbarSelector);
88
- const verticalScrollbarWidth = (0, _useGridSelector.useGridSelector)(apiRef, _gridDimensionsSelectors.gridVerticalScrollbarWidthSelector);
89
- const gridHasFiller = (0, _useGridSelector.useGridSelector)(apiRef, _gridDimensionsSelectors.gridHasFillerSelector);
90
- const previousSize = React.useRef(null);
91
- const mainRefCallback = React.useCallback(node => {
92
- mainRef.current = node;
93
- if (!node) {
94
- return undefined;
95
- }
96
- const initialRect = node.getBoundingClientRect();
97
- let lastSize = {
98
- width: (0, _roundToDecimalPlaces.roundToDecimalPlaces)(initialRect.width, 1),
99
- height: (0, _roundToDecimalPlaces.roundToDecimalPlaces)(initialRect.height, 1)
100
- };
101
- if (!previousSize.current || lastSize.width !== previousSize.current.width && lastSize.height !== previousSize.current.height) {
102
- previousSize.current = lastSize;
103
- apiRef.current.publishEvent('resize', lastSize);
104
- }
105
- if (typeof ResizeObserver === 'undefined') {
106
- return undefined;
107
- }
108
- const observer = new ResizeObserver(entries => {
109
- const entry = entries[0];
110
- if (!entry) {
111
- return;
112
- }
113
- const newSize = {
114
- width: (0, _roundToDecimalPlaces.roundToDecimalPlaces)(entry.contentRect.width, 1),
115
- height: (0, _roundToDecimalPlaces.roundToDecimalPlaces)(entry.contentRect.height, 1)
116
- };
117
- if (newSize.width === lastSize.width && newSize.height === lastSize.height) {
118
- return;
119
- }
120
- apiRef.current.publishEvent('resize', newSize);
121
- lastSize = newSize;
122
- });
123
- observer.observe(node);
124
- if (_reactMajor.default >= 19) {
125
- return () => {
126
- mainRef.current = null;
127
- observer.disconnect();
128
- };
129
- }
130
- return undefined;
131
- }, [apiRef, mainRef]);
132
-
133
- /*
134
- * Scroll context logic
135
- * ====================
136
- * We only render the cells contained in the `renderContext`. However, when the user starts scrolling the grid
137
- * in a direction, we want to render as many cells as possible in that direction, as to avoid presenting white
138
- * areas if the user scrolls too fast/far and the viewport ends up in a region we haven't rendered yet. To render
139
- * more cells, we store some offsets to add to the viewport in `scrollCache.buffer`. Those offsets make the render
140
- * context wider in the direction the user is going, but also makes the buffer around the viewport `0` for the
141
- * dimension (horizontal or vertical) in which the user is not scrolling. So if the normal viewport is 8 columns
142
- * wide, with a 1 column buffer (10 columns total), then we want it to be exactly 8 columns wide during vertical
143
- * scroll.
144
- * However, we don't want the rows in the old context to re-render from e.g. 10 columns to 8 columns, because that's
145
- * work that's not necessary. Thus we store the context at the start of the scroll in `frozenContext`, and the rows
146
- * that are part of this old context will keep their same render context as to avoid re-rendering.
147
- */
148
- const scrollPosition = React.useRef(rootProps.initialState?.scroll ?? EMPTY_SCROLL_POSITION);
149
- const ignoreNextScrollEvent = React.useRef(false);
150
- const previousContextScrollPosition = React.useRef(EMPTY_SCROLL_POSITION);
151
- const previousRowContext = React.useRef(_useGridVirtualization.EMPTY_RENDER_CONTEXT);
152
- const renderContext = (0, _useGridSelector.useGridSelector)(apiRef, _gridVirtualizationSelectors.gridRenderContextSelector);
153
- const focusedVirtualCell = (0, _useGridSelector.useGridSelector)(apiRef, _gridFocusedVirtualCellSelector.gridFocusedVirtualCellSelector);
154
- const scrollTimeout = (0, _useTimeout.default)();
155
- const frozenContext = React.useRef(undefined);
156
- const scrollCache = (0, _useLazyRef.default)(() => createScrollCache(isRtl, rootProps.rowBufferPx, rootProps.columnBufferPx, rowHeight * 15, MINIMUM_COLUMN_WIDTH * 6)).current;
157
- const updateRenderContext = React.useCallback(nextRenderContext => {
158
- if (areRenderContextsEqual(nextRenderContext, apiRef.current.state.virtualization.renderContext)) {
159
- return;
160
- }
161
- const didRowsIntervalChange = nextRenderContext.firstRowIndex !== previousRowContext.current.firstRowIndex || nextRenderContext.lastRowIndex !== previousRowContext.current.lastRowIndex;
162
- apiRef.current.setState(state => {
163
- return (0, _extends2.default)({}, state, {
164
- virtualization: (0, _extends2.default)({}, state.virtualization, {
165
- renderContext: nextRenderContext
166
- })
167
- });
168
- });
169
-
170
- // The lazy-loading hook is listening to `renderedRowsIntervalChange`,
171
- // but only does something if we already have a render context, because
172
- // otherwise we would call an update directly on mount
173
- const isReady = (0, _gridDimensionsSelectors.gridDimensionsSelector)(apiRef).isReady;
174
- if (isReady && didRowsIntervalChange) {
175
- previousRowContext.current = nextRenderContext;
176
- apiRef.current.publishEvent('renderedRowsIntervalChange', nextRenderContext);
177
- }
178
- previousContextScrollPosition.current = scrollPosition.current;
179
- }, [apiRef]);
180
- const triggerUpdateRenderContext = (0, _useEventCallback.default)(() => {
181
- const scroller = scrollerRef.current;
182
- if (!scroller) {
183
- return undefined;
184
- }
185
- const dimensions = (0, _gridDimensionsSelectors.gridDimensionsSelector)(apiRef);
186
- const maxScrollTop = Math.ceil(dimensions.minimumSize.height - dimensions.viewportOuterSize.height);
187
- const maxScrollLeft = Math.ceil(dimensions.minimumSize.width - dimensions.viewportInnerSize.width);
188
-
189
- // Clamp the scroll position to the viewport to avoid re-calculating the render context for scroll bounce
190
- const newScroll = {
191
- top: (0, _utils2.clamp)(scroller.scrollTop, 0, maxScrollTop),
192
- left: isRtl ? (0, _utils2.clamp)(scroller.scrollLeft, -maxScrollLeft, 0) : (0, _utils2.clamp)(scroller.scrollLeft, 0, maxScrollLeft)
193
- };
194
- const dx = newScroll.left - scrollPosition.current.left;
195
- const dy = newScroll.top - scrollPosition.current.top;
196
- const isScrolling = dx !== 0 || dy !== 0;
197
- scrollPosition.current = newScroll;
198
- const direction = isScrolling ? directionForDelta(dx, dy) : ScrollDirection.NONE;
199
-
200
- // Since previous render, we have scrolled...
201
- const rowScroll = Math.abs(scrollPosition.current.top - previousContextScrollPosition.current.top);
202
- const columnScroll = Math.abs(scrollPosition.current.left - previousContextScrollPosition.current.left);
203
-
204
- // PERF: use the computed minimum column width instead of a static one
205
- const didCrossThreshold = rowScroll >= rowHeight || columnScroll >= MINIMUM_COLUMN_WIDTH;
206
- const didChangeDirection = scrollCache.direction !== direction;
207
- const shouldUpdate = didCrossThreshold || didChangeDirection;
208
- if (!shouldUpdate) {
209
- return renderContext;
210
- }
211
-
212
- // Render a new context
213
-
214
- if (didChangeDirection) {
215
- switch (direction) {
216
- case ScrollDirection.NONE:
217
- case ScrollDirection.LEFT:
218
- case ScrollDirection.RIGHT:
219
- frozenContext.current = undefined;
220
- break;
221
- default:
222
- frozenContext.current = renderContext;
223
- break;
224
- }
225
- }
226
- scrollCache.direction = direction;
227
- scrollCache.buffer = bufferForDirection(isRtl, direction, rootProps.rowBufferPx, rootProps.columnBufferPx, rowHeight * 15, MINIMUM_COLUMN_WIDTH * 6);
228
- const inputs = inputsSelector(apiRef, rootProps, enabledForRows, enabledForColumns);
229
- const nextRenderContext = computeRenderContext(inputs, scrollPosition.current, scrollCache);
230
- if (!areRenderContextsEqual(nextRenderContext, renderContext)) {
231
- // Prevents batching render context changes
232
- ReactDOM.flushSync(() => {
233
- updateRenderContext(nextRenderContext);
234
- });
235
- scrollTimeout.start(1000, triggerUpdateRenderContext);
236
- }
237
- return nextRenderContext;
238
- });
239
- const forceUpdateRenderContext = () => {
240
- // skip update if dimensions are not ready and virtualization is enabled
241
- if (!(0, _gridDimensionsSelectors.gridDimensionsSelector)(apiRef).isReady && (enabledForRows || enabledForColumns)) {
242
- return;
243
- }
244
- const inputs = inputsSelector(apiRef, rootProps, enabledForRows, enabledForColumns);
245
- const nextRenderContext = computeRenderContext(inputs, scrollPosition.current, scrollCache);
246
- // Reset the frozen context when the render context changes, see the illustration in https://github.com/mui/mui-x/pull/12353
247
- frozenContext.current = undefined;
248
- updateRenderContext(nextRenderContext);
249
- };
250
- const handleScroll = (0, _useEventCallback.default)(() => {
251
- if (ignoreNextScrollEvent.current) {
252
- ignoreNextScrollEvent.current = false;
253
- return;
254
- }
255
- const nextRenderContext = triggerUpdateRenderContext();
256
- apiRef.current.publishEvent('scrollPositionChange', {
257
- top: scrollPosition.current.top,
258
- left: scrollPosition.current.left,
259
- renderContext: nextRenderContext
260
- });
261
- });
262
- const handleWheel = (0, _useEventCallback.default)(event => {
263
- apiRef.current.publishEvent('virtualScrollerWheel', {}, event);
264
- });
265
- const handleTouchMove = (0, _useEventCallback.default)(event => {
266
- apiRef.current.publishEvent('virtualScrollerTouchMove', {}, event);
267
- });
268
- const getRows = (params = {}) => {
269
- if (!params.rows && !currentPage.range) {
270
- return [];
271
- }
272
- const rowTree = (0, _gridRowsSelector.gridRowTreeSelector)(apiRef);
273
- let baseRenderContext = renderContext;
274
- if (params.renderContext) {
275
- baseRenderContext = params.renderContext;
276
- baseRenderContext.firstColumnIndex = renderContext.firstColumnIndex;
277
- baseRenderContext.lastColumnIndex = renderContext.lastColumnIndex;
278
- }
279
- const isLastSection = !hasBottomPinnedRows && params.position === undefined || hasBottomPinnedRows && params.position === 'bottom';
280
- const isPinnedSection = params.position !== undefined;
281
- let rowIndexOffset;
282
- // FIXME: Why is the switch check exhaustiveness not validated with typescript-eslint?
283
- // eslint-disable-next-line default-case
284
- switch (params.position) {
285
- case 'top':
286
- rowIndexOffset = 0;
287
- break;
288
- case 'bottom':
289
- rowIndexOffset = pinnedRows.top.length + currentPage.rows.length;
290
- break;
291
- case undefined:
292
- rowIndexOffset = pinnedRows.top.length;
293
- break;
294
- }
295
- const rowModels = params.rows ?? currentPage.rows;
296
- const firstRowToRender = baseRenderContext.firstRowIndex;
297
- const lastRowToRender = Math.min(baseRenderContext.lastRowIndex, rowModels.length);
298
- const rowIndexes = params.rows ? (0, _utils2.range)(0, params.rows.length) : (0, _utils2.range)(firstRowToRender, lastRowToRender);
299
- let virtualRowIndex = -1;
300
- if (!isPinnedSection && focusedVirtualCell) {
301
- if (focusedVirtualCell.rowIndex < firstRowToRender) {
302
- rowIndexes.unshift(focusedVirtualCell.rowIndex);
303
- virtualRowIndex = focusedVirtualCell.rowIndex;
304
- }
305
- if (focusedVirtualCell.rowIndex > lastRowToRender) {
306
- rowIndexes.push(focusedVirtualCell.rowIndex);
307
- virtualRowIndex = focusedVirtualCell.rowIndex;
308
- }
309
- }
310
- const rows = [];
311
- const rowProps = rootProps.slotProps?.row;
312
- const columnPositions = (0, _gridColumnsSelector.gridColumnPositionsSelector)(apiRef);
313
- rowIndexes.forEach(rowIndexInPage => {
314
- const {
315
- id,
316
- model
317
- } = rowModels[rowIndexInPage];
318
-
319
- // In certain cases, the state might already be updated and `currentPage.rows` (which sets `rowModels`)
320
- // contains stale data.
321
- // In that case, skip any further row processing.
322
- // See:
323
- // - https://github.com/mui/mui-x/issues/16638
324
- // - https://github.com/mui/mui-x/issues/17022
325
- if (!rowTree[id]) {
326
- return;
327
- }
328
- const rowIndex = (currentPage?.range?.firstRowIndex || 0) + rowIndexOffset + rowIndexInPage;
329
-
330
- // NOTE: This is an expensive feature, the colSpan code could be optimized.
331
- if (hasColSpan) {
332
- const minFirstColumn = pinnedColumns.left.length;
333
- const maxLastColumn = visibleColumns.length - pinnedColumns.right.length;
334
- apiRef.current.calculateColSpan({
335
- rowId: id,
336
- minFirstColumn,
337
- maxLastColumn,
338
- columns: visibleColumns
339
- });
340
- if (pinnedColumns.left.length > 0) {
341
- apiRef.current.calculateColSpan({
342
- rowId: id,
343
- minFirstColumn: 0,
344
- maxLastColumn: pinnedColumns.left.length,
345
- columns: visibleColumns
346
- });
347
- }
348
- if (pinnedColumns.right.length > 0) {
349
- apiRef.current.calculateColSpan({
350
- rowId: id,
351
- minFirstColumn: visibleColumns.length - pinnedColumns.right.length,
352
- maxLastColumn: visibleColumns.length,
353
- columns: visibleColumns
354
- });
355
- }
356
- }
357
- const baseRowHeight = !apiRef.current.rowHasAutoHeight(id) ? apiRef.current.unstable_getRowHeight(id) : 'auto';
358
- const isSelected = rowSelectionManager.has(id) && apiRef.current.isRowSelectable(id);
359
- let isFirstVisible = false;
360
- if (params.position === undefined) {
361
- isFirstVisible = rowIndexInPage === 0;
362
- }
363
- let isLastVisible = false;
364
- const isLastVisibleInSection = rowIndexInPage === rowModels.length - 1;
365
- if (isLastSection) {
366
- if (!isPinnedSection) {
367
- const lastIndex = currentPage.rows.length - 1;
368
- const isLastVisibleRowIndex = rowIndexInPage === lastIndex;
369
- if (isLastVisibleRowIndex) {
370
- isLastVisible = true;
371
- }
372
- } else {
373
- isLastVisible = isLastVisibleInSection;
374
- }
375
- }
376
- let currentRenderContext = baseRenderContext;
377
- if (frozenContext.current && rowIndexInPage >= frozenContext.current.firstRowIndex && rowIndexInPage < frozenContext.current.lastRowIndex) {
378
- currentRenderContext = frozenContext.current;
379
- }
380
- const isVirtualFocusRow = rowIndexInPage === virtualRowIndex;
381
- const isVirtualFocusColumn = focusedVirtualCell?.rowIndex === rowIndex;
382
- const offsetLeft = computeOffsetLeft(columnPositions, currentRenderContext, pinnedColumns.left.length);
383
- const showBottomBorder = isLastVisibleInSection && params.position === 'top';
384
- const firstColumnIndex = currentRenderContext.firstColumnIndex;
385
- const lastColumnIndex = currentRenderContext.lastColumnIndex;
386
- rows.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(rootProps.slots.row, (0, _extends2.default)({
387
- row: model,
388
- rowId: id,
389
- index: rowIndex,
390
- selected: isSelected,
391
- offsetLeft: offsetLeft,
392
- columnsTotalWidth: columnsTotalWidth,
393
- rowHeight: baseRowHeight,
394
- pinnedColumns: pinnedColumns,
395
- visibleColumns: visibleColumns,
396
- firstColumnIndex: firstColumnIndex,
397
- lastColumnIndex: lastColumnIndex,
398
- focusedColumnIndex: isVirtualFocusColumn ? focusedVirtualCell.columnIndex : undefined,
399
- isFirstVisible: isFirstVisible,
400
- isLastVisible: isLastVisible,
401
- isNotVisible: isVirtualFocusRow,
402
- showBottomBorder: showBottomBorder,
403
- scrollbarWidth: verticalScrollbarWidth,
404
- gridHasFiller: gridHasFiller
405
- }, rowProps), id));
406
- if (isVirtualFocusRow) {
407
- return;
408
- }
409
- const panel = panels.get(id);
410
- if (panel) {
411
- rows.push(panel);
412
- }
413
- if (params.position === undefined && isLastVisibleInSection) {
414
- rows.push(apiRef.current.getInfiniteLoadingTriggerElement?.({
415
- lastRowId: id
416
- }));
417
- }
418
- });
419
- return rows;
420
- };
421
- const scrollerStyle = React.useMemo(() => ({
422
- overflowX: !needsHorizontalScrollbar || listView ? 'hidden' : undefined,
423
- overflowY: rootProps.autoHeight ? 'hidden' : undefined
424
- }), [needsHorizontalScrollbar, rootProps.autoHeight, listView]);
425
- const contentSize = React.useMemo(() => {
426
- const size = {
427
- width: needsHorizontalScrollbar ? columnsTotalWidth : 'auto',
428
- flexBasis: contentHeight,
429
- flexShrink: 0
430
- };
431
- if (size.flexBasis === 0) {
432
- size.flexBasis = _gridRowsUtils.minimalContentHeight; // Give room to show the overlay when there no rows.
433
- }
434
- return size;
435
- }, [columnsTotalWidth, contentHeight, needsHorizontalScrollbar]);
436
- const onContentSizeApplied = React.useCallback(node => {
437
- if (!node) {
438
- return;
439
- }
440
- apiRef.current.publishEvent('virtualScrollerContentSizeChange', {
441
- columnsTotalWidth,
442
- contentHeight
443
- });
444
- }, [apiRef, columnsTotalWidth, contentHeight]);
445
- (0, _useEnhancedEffect.default)(() => {
446
- if (!isRenderContextReady.current) {
447
- return;
448
- }
449
- apiRef.current.updateRenderContext?.();
450
- }, [apiRef, enabledForColumns, enabledForRows]);
451
- (0, _useEnhancedEffect.default)(() => {
452
- if (listView) {
453
- scrollerRef.current.scrollLeft = 0;
454
- }
455
- }, [listView, scrollerRef]);
456
- (0, _useRunOnce.useRunOnce)(renderContext !== _useGridVirtualization.EMPTY_RENDER_CONTEXT, () => {
457
- apiRef.current.publishEvent('scrollPositionChange', {
458
- top: scrollPosition.current.top,
459
- left: scrollPosition.current.left,
460
- renderContext
461
- });
462
- isRenderContextReady.current = true;
463
- if (rootProps.initialState?.scroll && scrollerRef.current) {
464
- const scroller = scrollerRef.current;
465
- const {
466
- top,
467
- left
468
- } = rootProps.initialState.scroll;
469
-
470
- // On initial mount, if we have columns available, we can restore the horizontal scroll immediately, but we need to skip the resulting scroll event, otherwise we would recalculate the render context at position top=0, left=restoredValue, but the initial render context is already calculated based on the initial value of scrollPosition ref.
471
- const isScrollRestored = {
472
- top: !(top > 0),
473
- left: !(left > 0)
474
- };
475
- if (!isScrollRestored.left && columnsTotalWidth) {
476
- scroller.scrollLeft = left;
477
- ignoreNextScrollEvent.current = true;
478
- isScrollRestored.left = true;
479
- }
480
-
481
- // For the sake of completeness, but I'm not sure if contentHeight is ever available at this point. Maybe when virtualisation is disabled?
482
- if (!isScrollRestored.top && contentHeight) {
483
- scroller.scrollTop = top;
484
- ignoreNextScrollEvent.current = true;
485
- isScrollRestored.top = true;
486
- }
487
-
488
- // To restore the vertical scroll, we need to wait until the rows are available in the DOM (otherwise there's nowhere to scroll), but before paint to avoid reflows
489
- if (!isScrollRestored.top || !isScrollRestored.left) {
490
- const unsubscribeContentSizeChange = apiRef.current.subscribeEvent('virtualScrollerContentSizeChange', params => {
491
- if (!isScrollRestored.left && params.columnsTotalWidth) {
492
- scroller.scrollLeft = left;
493
- ignoreNextScrollEvent.current = true;
494
- isScrollRestored.left = true;
495
- }
496
- if (!isScrollRestored.top && params.contentHeight) {
497
- scroller.scrollTop = top;
498
- ignoreNextScrollEvent.current = true;
499
- isScrollRestored.top = true;
500
- }
501
- if (isScrollRestored.left && isScrollRestored.top) {
502
- unsubscribeContentSizeChange();
503
- }
504
- });
505
- return unsubscribeContentSizeChange;
506
- }
507
- }
508
- return undefined;
509
- });
510
- apiRef.current.register('private', {
511
- updateRenderContext: forceUpdateRenderContext
512
- });
513
- (0, _utils.useGridEventPriority)(apiRef, 'sortedRowsSet', forceUpdateRenderContext);
514
- (0, _utils.useGridEventPriority)(apiRef, 'paginationModelChange', forceUpdateRenderContext);
515
- (0, _utils.useGridEventPriority)(apiRef, 'columnsChange', forceUpdateRenderContext);
516
- return {
517
- renderContext,
518
- setPanels,
519
- getRows,
520
- getContainerProps: () => ({
521
- ref: mainRefCallback
522
- }),
523
- getScrollerProps: () => ({
524
- ref: scrollerRef,
525
- onScroll: handleScroll,
526
- onWheel: handleWheel,
527
- onTouchMove: handleTouchMove,
528
- style: scrollerStyle,
529
- role: 'presentation',
530
- // `tabIndex` shouldn't be used along role=presentation, but it fixes a Firefox bug
531
- // https://github.com/mui/mui-x/pull/13891#discussion_r1683416024
532
- tabIndex: platform.isFirefox ? -1 : undefined
533
- }),
534
- getContentProps: () => ({
535
- style: contentSize,
536
- role: 'presentation',
537
- ref: onContentSizeApplied
538
- }),
539
- getRenderZoneProps: () => ({
540
- role: 'rowgroup'
541
- }),
542
- getScrollbarVerticalProps: () => ({
543
- ref: scrollbarVerticalRef,
544
- scrollPosition
545
- }),
546
- getScrollbarHorizontalProps: () => ({
547
- ref: scrollbarHorizontalRef,
548
- scrollPosition
549
- }),
550
- getScrollAreaProps: () => ({
551
- scrollPosition
552
- })
553
- };
554
- };
555
- exports.useGridVirtualScroller = useGridVirtualScroller;
556
- // dimension selectors
557
- function needsHorizontalScrollbarSelector(apiRef) {
558
- return apiRef.current.state.dimensions.viewportOuterSize.width > 0 && apiRef.current.state.dimensions.columnsTotalWidth > apiRef.current.state.dimensions.viewportOuterSize.width;
559
- }
560
- function inputsSelector(apiRef, rootProps, enabledForRows, enabledForColumns) {
561
- const dimensions = (0, _gridDimensionsSelectors.gridDimensionsSelector)(apiRef);
562
- const currentPage = (0, _useGridVisibleRows.getVisibleRows)(apiRef, rootProps);
563
- const visibleColumns = (0, _gridColumnsSelector.gridVisibleColumnDefinitionsSelector)(apiRef);
564
- const hiddenCellsOriginMap = (0, _gridRowSpanningSelectors.gridRowSpanningHiddenCellsOriginMapSelector)(apiRef);
565
- const lastRowId = apiRef.current.state.rows.dataRowIds.at(-1);
566
- const lastColumn = visibleColumns.at(-1);
567
- return {
568
- enabledForRows,
569
- enabledForColumns,
570
- apiRef,
571
- autoHeight: rootProps.autoHeight,
572
- rowBufferPx: rootProps.rowBufferPx,
573
- columnBufferPx: rootProps.columnBufferPx,
574
- leftPinnedWidth: dimensions.leftPinnedWidth,
575
- columnsTotalWidth: dimensions.columnsTotalWidth,
576
- viewportInnerWidth: dimensions.viewportInnerSize.width,
577
- viewportInnerHeight: dimensions.viewportInnerSize.height,
578
- lastRowHeight: lastRowId !== undefined ? apiRef.current.unstable_getRowHeight(lastRowId) : 0,
579
- lastColumnWidth: lastColumn?.computedWidth ?? 0,
580
- rowsMeta: (0, _gridRowsMetaSelector.gridRowsMetaSelector)(apiRef),
581
- columnPositions: (0, _gridColumnsSelector.gridColumnPositionsSelector)(apiRef),
582
- rows: currentPage.rows,
583
- range: currentPage.range,
584
- pinnedColumns: (0, _gridColumnsSelector.gridVisiblePinnedColumnDefinitionsSelector)(apiRef),
585
- visibleColumns,
586
- hiddenCellsOriginMap,
587
- listView: rootProps.listView ?? false,
588
- virtualizeColumnsWithAutoRowHeight: rootProps.virtualizeColumnsWithAutoRowHeight
589
- };
590
- }
591
- function computeRenderContext(inputs, scrollPosition, scrollCache) {
592
- const renderContext = {
593
- firstRowIndex: 0,
594
- lastRowIndex: inputs.rows.length,
595
- firstColumnIndex: 0,
596
- lastColumnIndex: inputs.visibleColumns.length
597
- };
598
- const {
599
- top,
600
- left
601
- } = scrollPosition;
602
- const realLeft = Math.abs(left) + inputs.leftPinnedWidth;
603
- if (inputs.enabledForRows) {
604
- // Clamp the value because the search may return an index out of bounds.
605
- // In the last index, this is not needed because Array.slice doesn't include it.
606
- let firstRowIndex = Math.min(getNearestIndexToRender(inputs, top, {
607
- atStart: true,
608
- lastPosition: inputs.rowsMeta.positions[inputs.rowsMeta.positions.length - 1] + inputs.lastRowHeight
609
- }), inputs.rowsMeta.positions.length - 1);
610
-
611
- // If any of the cells in the `firstRowIndex` is hidden due to an extended row span,
612
- // Make sure the row from where the rowSpan is originated is visible.
613
- const rowSpanHiddenCellOrigin = inputs.hiddenCellsOriginMap[firstRowIndex];
614
- if (rowSpanHiddenCellOrigin) {
615
- const minSpannedRowIndex = Math.min(...Object.values(rowSpanHiddenCellOrigin));
616
- firstRowIndex = Math.min(firstRowIndex, minSpannedRowIndex);
617
- }
618
- const lastRowIndex = inputs.autoHeight ? firstRowIndex + inputs.rows.length : getNearestIndexToRender(inputs, top + inputs.viewportInnerHeight);
619
- renderContext.firstRowIndex = firstRowIndex;
620
- renderContext.lastRowIndex = lastRowIndex;
621
- }
622
- if (inputs.listView) {
623
- return (0, _extends2.default)({}, renderContext, {
624
- lastColumnIndex: 1
625
- });
626
- }
627
- if (inputs.enabledForColumns) {
628
- let firstColumnIndex = 0;
629
- let lastColumnIndex = inputs.columnPositions.length;
630
- let hasRowWithAutoHeight = false;
631
- const [firstRowToRender, lastRowToRender] = getIndexesToRender({
632
- firstIndex: renderContext.firstRowIndex,
633
- lastIndex: renderContext.lastRowIndex,
634
- minFirstIndex: 0,
635
- maxLastIndex: inputs.rows.length,
636
- bufferBefore: scrollCache.buffer.rowBefore,
637
- bufferAfter: scrollCache.buffer.rowAfter,
638
- positions: inputs.rowsMeta.positions,
639
- lastSize: inputs.lastRowHeight
640
- });
641
- if (!inputs.virtualizeColumnsWithAutoRowHeight) {
642
- for (let i = firstRowToRender; i < lastRowToRender && !hasRowWithAutoHeight; i += 1) {
643
- const row = inputs.rows[i];
644
- hasRowWithAutoHeight = inputs.apiRef.current.rowHasAutoHeight(row.id);
645
- }
646
- }
647
- if (!hasRowWithAutoHeight || inputs.virtualizeColumnsWithAutoRowHeight) {
648
- firstColumnIndex = binarySearch(realLeft, inputs.columnPositions, {
649
- atStart: true,
650
- lastPosition: inputs.columnsTotalWidth
651
- });
652
- lastColumnIndex = binarySearch(realLeft + inputs.viewportInnerWidth, inputs.columnPositions);
653
- }
654
- renderContext.firstColumnIndex = firstColumnIndex;
655
- renderContext.lastColumnIndex = lastColumnIndex;
656
- }
657
- const actualRenderContext = deriveRenderContext(inputs, renderContext, scrollCache);
658
- return actualRenderContext;
659
- }
660
- function getNearestIndexToRender(inputs, offset, options) {
661
- const lastMeasuredIndexRelativeToAllRows = inputs.apiRef.current.getLastMeasuredRowIndex();
662
- let allRowsMeasured = lastMeasuredIndexRelativeToAllRows === Infinity;
663
- if (inputs.range?.lastRowIndex && !allRowsMeasured) {
664
- // Check if all rows in this page are already measured
665
- allRowsMeasured = lastMeasuredIndexRelativeToAllRows >= inputs.range.lastRowIndex;
666
- }
667
- const lastMeasuredIndexRelativeToCurrentPage = (0, _utils2.clamp)(lastMeasuredIndexRelativeToAllRows - (inputs.range?.firstRowIndex || 0), 0, inputs.rowsMeta.positions.length);
668
- if (allRowsMeasured || inputs.rowsMeta.positions[lastMeasuredIndexRelativeToCurrentPage] >= offset) {
669
- // If all rows were measured (when no row has "auto" as height) or all rows before the offset
670
- // were measured, then use a binary search because it's faster.
671
- return binarySearch(offset, inputs.rowsMeta.positions, options);
672
- }
673
-
674
- // Otherwise, use an exponential search.
675
- // If rows have "auto" as height, their positions will be based on estimated heights.
676
- // In this case, we can skip several steps until we find a position higher than the offset.
677
- // Inspired by https://github.com/bvaughn/react-virtualized/blob/master/source/Grid/utils/CellSizeAndPositionManager.js
678
- return exponentialSearch(offset, inputs.rowsMeta.positions, lastMeasuredIndexRelativeToCurrentPage, options);
679
- }
680
-
681
- /**
682
- * Accepts as input a raw render context (the area visible in the viewport) and adds
683
- * computes the actual render context based on pinned elements, buffer dimensions and
684
- * spanning.
685
- */
686
- function deriveRenderContext(inputs, nextRenderContext, scrollCache) {
687
- const [firstRowToRender, lastRowToRender] = getIndexesToRender({
688
- firstIndex: nextRenderContext.firstRowIndex,
689
- lastIndex: nextRenderContext.lastRowIndex,
690
- minFirstIndex: 0,
691
- maxLastIndex: inputs.rows.length,
692
- bufferBefore: scrollCache.buffer.rowBefore,
693
- bufferAfter: scrollCache.buffer.rowAfter,
694
- positions: inputs.rowsMeta.positions,
695
- lastSize: inputs.lastRowHeight
696
- });
697
- const [initialFirstColumnToRender, lastColumnToRender] = getIndexesToRender({
698
- firstIndex: nextRenderContext.firstColumnIndex,
699
- lastIndex: nextRenderContext.lastColumnIndex,
700
- minFirstIndex: inputs.pinnedColumns.left.length,
701
- maxLastIndex: inputs.visibleColumns.length - inputs.pinnedColumns.right.length,
702
- bufferBefore: scrollCache.buffer.columnBefore,
703
- bufferAfter: scrollCache.buffer.columnAfter,
704
- positions: inputs.columnPositions,
705
- lastSize: inputs.lastColumnWidth
706
- });
707
- const firstColumnToRender = (0, _gridColumnsUtils.getFirstNonSpannedColumnToRender)({
708
- firstColumnToRender: initialFirstColumnToRender,
709
- apiRef: inputs.apiRef,
710
- firstRowToRender,
711
- lastRowToRender,
712
- visibleRows: inputs.rows
713
- });
714
- return {
715
- firstRowIndex: firstRowToRender,
716
- lastRowIndex: lastRowToRender,
717
- firstColumnIndex: firstColumnToRender,
718
- lastColumnIndex: lastColumnToRender
719
- };
720
- }
721
- /**
722
- * Use binary search to avoid looping through all possible positions.
723
- * The `options.atStart` provides the possibility to match for the first element that
724
- * intersects the screen, even if said element's start position is before `offset`. In
725
- * other words, we search for `offset + width`.
726
- */
727
- function binarySearch(offset, positions, options = undefined, sliceStart = 0, sliceEnd = positions.length) {
728
- if (positions.length <= 0) {
729
- return -1;
730
- }
731
- if (sliceStart >= sliceEnd) {
732
- return sliceStart;
733
- }
734
- const pivot = sliceStart + Math.floor((sliceEnd - sliceStart) / 2);
735
- const position = positions[pivot];
736
- let isBefore;
737
- if (options?.atStart) {
738
- const width = (pivot === positions.length - 1 ? options.lastPosition : positions[pivot + 1]) - position;
739
- isBefore = offset - width < position;
740
- } else {
741
- isBefore = offset <= position;
742
- }
743
- return isBefore ? binarySearch(offset, positions, options, sliceStart, pivot) : binarySearch(offset, positions, options, pivot + 1, sliceEnd);
744
- }
745
- function exponentialSearch(offset, positions, index, options = undefined) {
746
- let interval = 1;
747
- while (index < positions.length && Math.abs(positions[index]) < offset) {
748
- index += interval;
749
- interval *= 2;
750
- }
751
- return binarySearch(offset, positions, options, Math.floor(index / 2), Math.min(index, positions.length));
752
- }
753
- function getIndexesToRender({
754
- firstIndex,
755
- lastIndex,
756
- bufferBefore,
757
- bufferAfter,
758
- minFirstIndex,
759
- maxLastIndex,
760
- positions,
761
- lastSize
762
- }) {
763
- const firstPosition = positions[firstIndex] - bufferBefore;
764
- const lastPosition = positions[lastIndex] + bufferAfter;
765
- const firstIndexPadded = binarySearch(firstPosition, positions, {
766
- atStart: true,
767
- lastPosition: positions[positions.length - 1] + lastSize
768
- });
769
- const lastIndexPadded = binarySearch(lastPosition, positions);
770
- return [(0, _utils2.clamp)(firstIndexPadded, minFirstIndex, maxLastIndex), (0, _utils2.clamp)(lastIndexPadded, minFirstIndex, maxLastIndex)];
771
- }
772
- function areRenderContextsEqual(context1, context2) {
773
- if (context1 === context2) {
774
- return true;
775
- }
776
- return context1.firstRowIndex === context2.firstRowIndex && context1.lastRowIndex === context2.lastRowIndex && context1.firstColumnIndex === context2.firstColumnIndex && context1.lastColumnIndex === context2.lastColumnIndex;
777
- }
778
- function computeOffsetLeft(columnPositions, renderContext, pinnedLeftLength) {
779
- const left = (columnPositions[renderContext.firstColumnIndex] ?? 0) - (columnPositions[pinnedLeftLength] ?? 0);
780
- return Math.abs(left);
781
- }
782
- function directionForDelta(dx, dy) {
783
- if (dx === 0 && dy === 0) {
784
- return ScrollDirection.NONE;
785
- }
786
- /* eslint-disable */
787
- if (Math.abs(dy) >= Math.abs(dx)) {
788
- if (dy > 0) {
789
- return ScrollDirection.DOWN;
790
- } else {
791
- return ScrollDirection.UP;
792
- }
793
- } else {
794
- if (dx > 0) {
795
- return ScrollDirection.RIGHT;
796
- } else {
797
- return ScrollDirection.LEFT;
798
- }
799
- }
800
- /* eslint-enable */
801
- }
802
- function bufferForDirection(isRtl, direction, rowBufferPx, columnBufferPx, verticalBuffer, horizontalBuffer) {
803
- if (isRtl) {
804
- switch (direction) {
805
- case ScrollDirection.LEFT:
806
- direction = ScrollDirection.RIGHT;
807
- break;
808
- case ScrollDirection.RIGHT:
809
- direction = ScrollDirection.LEFT;
810
- break;
811
- default:
812
- }
813
- }
814
- switch (direction) {
815
- case ScrollDirection.NONE:
816
- return {
817
- rowAfter: rowBufferPx,
818
- rowBefore: rowBufferPx,
819
- columnAfter: columnBufferPx,
820
- columnBefore: columnBufferPx
821
- };
822
- case ScrollDirection.LEFT:
823
- return {
824
- rowAfter: 0,
825
- rowBefore: 0,
826
- columnAfter: 0,
827
- columnBefore: horizontalBuffer
828
- };
829
- case ScrollDirection.RIGHT:
830
- return {
831
- rowAfter: 0,
832
- rowBefore: 0,
833
- columnAfter: horizontalBuffer,
834
- columnBefore: 0
835
- };
836
- case ScrollDirection.UP:
837
- return {
838
- rowAfter: 0,
839
- rowBefore: verticalBuffer,
840
- columnAfter: 0,
841
- columnBefore: 0
842
- };
843
- case ScrollDirection.DOWN:
844
- return {
845
- rowAfter: verticalBuffer,
846
- rowBefore: 0,
847
- columnAfter: 0,
848
- columnBefore: 0
849
- };
850
- default:
851
- // eslint unable to figure out enum exhaustiveness
852
- throw new Error('unreachable');
853
- }
854
- }