Skip to content

Commit 97ca6c4

Browse files
kenanyusufweb-flow
authored andcommitted
[DataGrid] Fix scrollbars overlapping cells on mount (#16639)
1 parent a6d6352 commit 97ca6c4

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

packages/x-data-grid/src/hooks/features/dimensions/useGridDimensions.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,10 @@ export function useGridDimensions(apiRef: RefObject<GridPrivateApiCommunity>, pr
203203
// All the floating point dimensions should be rounded to .1 decimal places to avoid subpixel rendering issues
204204
// https://github.com/mui/mui-x/issues/9550#issuecomment-1619020477
205205
// https://github.com/mui/mui-x/issues/15721
206-
const rootElement = apiRef.current.rootElementRef.current;
207-
208-
const scrollbarSize = measureScrollbarSize(rootElement, props.scrollbarSize);
206+
const scrollbarSize = measureScrollbarSize(
207+
apiRef.current.mainElementRef.current,
208+
props.scrollbarSize,
209+
);
209210

210211
const rowsMeta = gridRowsMetaSelector(apiRef.current.state);
211212
const topContainerHeight = headersTotalHeight + rowsMeta.pinnedTopRowsTotalHeight;
@@ -454,32 +455,32 @@ function getStaticDimensions(
454455
}
455456

456457
const scrollbarSizeCache = new WeakMap<Element, number>();
457-
function measureScrollbarSize(rootElement: Element | null, scrollbarSize: number | undefined) {
458+
function measureScrollbarSize(element: Element | null, scrollbarSize: number | undefined) {
458459
if (scrollbarSize !== undefined) {
459460
return scrollbarSize;
460461
}
461462

462-
if (rootElement === null) {
463+
if (element === null) {
463464
return 0;
464465
}
465466

466-
const cachedSize = scrollbarSizeCache.get(rootElement);
467+
const cachedSize = scrollbarSizeCache.get(element);
467468
if (cachedSize !== undefined) {
468469
return cachedSize;
469470
}
470471

471-
const doc = ownerDocument(rootElement);
472+
const doc = ownerDocument(element);
472473
const scrollDiv = doc.createElement('div');
473474
scrollDiv.style.width = '99px';
474475
scrollDiv.style.height = '99px';
475476
scrollDiv.style.position = 'absolute';
476477
scrollDiv.style.overflow = 'scroll';
477478
scrollDiv.className = 'scrollDiv';
478-
rootElement.appendChild(scrollDiv);
479+
element.appendChild(scrollDiv);
479480
const size = scrollDiv.offsetWidth - scrollDiv.clientWidth;
480-
rootElement.removeChild(scrollDiv);
481+
element.removeChild(scrollDiv);
481482

482-
scrollbarSizeCache.set(rootElement, size);
483+
scrollbarSizeCache.set(element, size);
483484

484485
return size;
485486
}

packages/x-data-grid/src/tests/rows.DataGrid.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ describe('<DataGrid /> - Rows', () => {
749749
expect(getRow(2)).toHaveInlineStyle({ minHeight: '100px' });
750750
});
751751

752-
it('should not virtualize columns if a row has auto height', () => {
752+
it('should not virtualize columns if a row has auto height', async () => {
753753
render(
754754
<TestCase
755755
rows={baselineProps.rows.slice(0, 1)}
@@ -759,7 +759,9 @@ describe('<DataGrid /> - Rows', () => {
759759
width={100}
760760
/>,
761761
);
762-
expect($$(`.${gridClasses.cell}:not(.${gridClasses.cellEmpty})`)).to.have.length(2);
762+
await waitFor(() => {
763+
expect($$(`.${gridClasses.cell}:not(.${gridClasses.cellEmpty})`)).to.have.length(2);
764+
});
763765
});
764766

765767
it('should measure rows while scrolling', async () => {

0 commit comments

Comments
 (0)