Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Fix scrollbars overlapping cells on mount (@KenanYusuf) #16653

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ export function useGridDimensions(apiRef: RefObject<GridPrivateApiCommunity>, pr
// All the floating point dimensions should be rounded to .1 decimal places to avoid subpixel rendering issues
// https://github.com/mui/mui-x/issues/9550#issuecomment-1619020477
// https://github.com/mui/mui-x/issues/15721
const rootElement = apiRef.current.rootElementRef.current;

const scrollbarSize = measureScrollbarSize(rootElement, props.scrollbarSize);
const scrollbarSize = measureScrollbarSize(
apiRef.current.mainElementRef.current,
props.scrollbarSize,
);

const rowsMeta = gridRowsMetaSelector(apiRef.current.state);
const topContainerHeight = headersTotalHeight + rowsMeta.pinnedTopRowsTotalHeight;
Expand Down Expand Up @@ -454,32 +455,32 @@ function getStaticDimensions(
}

const scrollbarSizeCache = new WeakMap<Element, number>();
function measureScrollbarSize(rootElement: Element | null, scrollbarSize: number | undefined) {
function measureScrollbarSize(element: Element | null, scrollbarSize: number | undefined) {
if (scrollbarSize !== undefined) {
return scrollbarSize;
}

if (rootElement === null) {
if (element === null) {
return 0;
}

const cachedSize = scrollbarSizeCache.get(rootElement);
const cachedSize = scrollbarSizeCache.get(element);
if (cachedSize !== undefined) {
return cachedSize;
}

const doc = ownerDocument(rootElement);
const doc = ownerDocument(element);
const scrollDiv = doc.createElement('div');
scrollDiv.style.width = '99px';
scrollDiv.style.height = '99px';
scrollDiv.style.position = 'absolute';
scrollDiv.style.overflow = 'scroll';
scrollDiv.className = 'scrollDiv';
rootElement.appendChild(scrollDiv);
element.appendChild(scrollDiv);
const size = scrollDiv.offsetWidth - scrollDiv.clientWidth;
rootElement.removeChild(scrollDiv);
element.removeChild(scrollDiv);

scrollbarSizeCache.set(rootElement, size);
scrollbarSizeCache.set(element, size);

return size;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/x-data-grid/src/tests/rows.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ describe('<DataGrid /> - Rows', () => {
expect(getRow(2)).toHaveInlineStyle({ minHeight: '100px' });
});

it('should not virtualize columns if a row has auto height', () => {
it('should not virtualize columns if a row has auto height', async () => {
render(
<TestCase
rows={baselineProps.rows.slice(0, 1)}
Expand All @@ -759,7 +759,9 @@ describe('<DataGrid /> - Rows', () => {
width={100}
/>,
);
expect($$(`.${gridClasses.cell}:not(.${gridClasses.cellEmpty})`)).to.have.length(2);
await waitFor(() => {
expect($$(`.${gridClasses.cell}:not(.${gridClasses.cellEmpty})`)).to.have.length(2);
});
});

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