Skip to content

Commit

Permalink
ResizeObserver: Use contentBoxSize (#2882)
Browse files Browse the repository at this point in the history
* Use contentBoxSize

* Remove logic to set initial dimensions

* Fix tests

* Set width early

* Correctly set initial value

* Update src/hooks/useGridDimensions.ts

Co-authored-by: Nicolas Stepien <[email protected]>

* Update src/hooks/useGridDimensions.ts

Co-authored-by: Nicolas Stepien <[email protected]>

* Import ceil

* Revert

* Add back mock contentBoxSize

Co-authored-by: Nicolas Stepien <[email protected]>
  • Loading branch information
amanmahajan7 and nstepien authored Apr 6, 2022
1 parent fa2f98d commit 625fad5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
42 changes: 25 additions & 17 deletions src/hooks/useGridDimensions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { useRef, useState } from 'react';
import { useLayoutEffect } from './useLayoutEffect';

import { ceil } from '../utils';

export function useGridDimensions(): [
ref: React.RefObject<HTMLDivElement>,
width: number,
height: number
] {
const gridRef = useRef<HTMLDivElement>(null);
const [gridWidth, setGridWidth] = useState(1);
const [gridHeight, setGridHeight] = useState(1);
const [inlineSize, setInlineSize] = useState(1);
const [blockSize, setBlockSize] = useState(1);

useLayoutEffect(() => {
const { ResizeObserver } = window;
Expand All @@ -17,26 +19,32 @@ export function useGridDimensions(): [
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (ResizeObserver == null) return;

function saveDimensions() {
// Get dimensions without scrollbars.
// The dimensions given by the callback entries in Firefox do not substract the scrollbar sizes.
// https://bugzilla.mozilla.org/show_bug.cgi?id=1733042
const { clientWidth, clientHeight } = gridRef.current!;
// TODO: remove once fixed upstream
// we reduce width by 1px here to avoid layout issues in Chrome
// https://bugs.chromium.org/p/chromium/issues/detail?id=1206298
setGridWidth(clientWidth - (devicePixelRatio % 1 === 0 ? 0 : 1));
setGridHeight(clientHeight);
}

saveDimensions();
const resizeObserver = new ResizeObserver(saveDimensions);
const { clientWidth, clientHeight, offsetWidth, offsetHeight } = gridRef.current!;
const { width, height } = gridRef.current!.getBoundingClientRect();
const initialWidth = width - offsetWidth + clientWidth;
const initialHeight = height - offsetHeight + clientHeight;

setInlineSize(handleDevicePixelRatio(initialWidth));
setBlockSize(initialHeight);

const resizeObserver = new ResizeObserver((entries) => {
const size = entries[0].contentBoxSize[0];
setInlineSize(handleDevicePixelRatio(size.inlineSize));
setBlockSize(size.blockSize);
});
resizeObserver.observe(gridRef.current!);

return () => {
resizeObserver.disconnect();
};
}, []);

return [gridRef, gridWidth, gridHeight];
return [gridRef, inlineSize, blockSize];
}

// TODO: remove once fixed upstream
// we reduce width by 1px here to avoid layout issues in Chrome
// https://bugs.chromium.org/p/chromium/issues/detail?id=1206298
function handleDevicePixelRatio(size: number) {
return size - (devicePixelRatio === 1 ? 0 : ceil(devicePixelRatio));
}
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export * from './keyboardUtils';
export * from './selectedCellUtils';
export * from './styleUtils';

export const { min, max, round, floor, sign, abs } = Math;
export const { min, max, round, floor, sign, abs, ceil } = Math;

export function assertIsValidKeyGetter<R, K extends React.Key>(
keyGetter: unknown
Expand Down
4 changes: 3 additions & 1 deletion test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ if (typeof window !== 'undefined') {
}

observe() {
this.callback([], this);
// patch inlineSize/blockSize to pretend we're rendering DataGrid at 1920p/1080p
// @ts-expect-error
this.callback([{ contentBoxSize: [{ inlineSize: 1920, blockSize: 1080 }] }], this);
}

unobserve() {}
Expand Down

0 comments on commit 625fad5

Please sign in to comment.