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] Add the areElementSizesEqual utility to improve code readability #13254

Merged
merged 3 commits into from
May 31, 2024
Merged
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 @@ -121,16 +121,14 @@ export function useGridDimensions(

const computedStyle = ownerWindow(element).getComputedStyle(element);

const height = parseFloat(computedStyle.height) || 0;
const width = parseFloat(computedStyle.width) || 0;

const hasHeightChanged = height !== previousSize.current?.height;
const hasWidthChanged = width !== previousSize.current?.width;
const newSize = {
width: parseFloat(computedStyle.width) || 0,
height: parseFloat(computedStyle.height) || 0,
};

if (!previousSize.current || hasHeightChanged || hasWidthChanged) {
const size = { width, height };
apiRef.current.publishEvent('resize', size);
previousSize.current = size;
if (!previousSize.current || !areElementSizesEqual(previousSize.current, newSize)) {
apiRef.current.publishEvent('resize', newSize);
previousSize.current = newSize;
}
}, [apiRef]);

Expand Down Expand Up @@ -263,10 +261,7 @@ export function useGridDimensions(
const prevDimensions = apiRef.current.state.dimensions;
setDimensions(newDimensions);

if (
newDimensions.viewportInnerSize.width !== prevDimensions.viewportInnerSize.width ||
newDimensions.viewportInnerSize.height !== prevDimensions.viewportInnerSize.height
) {
if (!areElementSizesEqual(newDimensions.viewportInnerSize, prevDimensions.viewportInnerSize)) {
apiRef.current.publishEvent('viewportInnerSizeChange', newDimensions.viewportInnerSize);
}

Expand Down Expand Up @@ -413,3 +408,7 @@ function measureScrollbarSize(
function roundToDecimalPlaces(value: number, decimals: number) {
return Math.round(value * 10 ** decimals) / 10 ** decimals;
}

function areElementSizesEqual(a: ElementSize, b: ElementSize) {
return a.width === b.width && a.height === b.height;
}