Skip to content

Commit

Permalink
[data grid] Add the areElementSizesEqual utility to improve code read…
Browse files Browse the repository at this point in the history
…ability (#13254)
  • Loading branch information
layerok committed May 31, 2024
1 parent ebd0ff4 commit 70090bf
Showing 1 changed file with 12 additions and 13 deletions.
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;
}

0 comments on commit 70090bf

Please sign in to comment.