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