Skip to content

Commit

Permalink
[DataGrid] Round dimensions to avoid subpixel rendering error (mui#15850
Browse files Browse the repository at this point in the history
)
  • Loading branch information
KenanYusuf authored and lauri865 committed Dec 19, 2024
1 parent a0716b4 commit 4dd3ddb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,8 @@ export const useGridVirtualScroller = () => {
}

const initialRect = node.getBoundingClientRect();
let lastSize = {
width: initialRect.width,
height: initialRect.height,
};

let lastSize = roundDimensions(initialRect);

apiRef.current.publishEvent('resize', lastSize);

Expand All @@ -162,10 +160,7 @@ export const useGridVirtualScroller = () => {
return;
}

const newSize = {
width: entry.contentRect.width,
height: entry.contentRect.height,
};
const newSize = roundDimensions(entry.contentRect);

if (newSize.width === lastSize.width && newSize.height === lastSize.height) {
return;
Expand Down Expand Up @@ -1109,3 +1104,12 @@ function bufferForDirection(
throw new Error('unreachable');
}
}

// Round to avoid issues with subpixel rendering
// https://github.com/mui/mui-x/issues/15721
function roundDimensions(dimensions: { width: number; height: number }) {
return {
width: Math.round(dimensions.width * 10) / 10,
height: Math.round(dimensions.height * 10) / 10,
};
}
20 changes: 20 additions & 0 deletions packages/x-data-grid/src/tests/layout.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1318,4 +1318,24 @@ describe('<DataGrid /> - Layout & warnings', () => {
</div>,
);
});

// See https://github.com/mui/mui-x/issues/15721
it('should not exceed maximum call stack size caused by subpixel rendering', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
// Need layouting
this.skip();
}

render(
<div style={{ width: 702.37 }}>
<DataGrid
columns={[
{ field: '1', flex: 1 },
{ field: '2', flex: 1 },
]}
rows={[]}
/>
</div>,
);
});
});

0 comments on commit 4dd3ddb

Please sign in to comment.