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

ResizeObserver: Use contentBoxSize #2882

Merged
merged 10 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
37 changes: 23 additions & 14 deletions src/hooks/useGridDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export function useGridDimensions(): [
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 +17,35 @@ 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!;
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];
// 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
amanmahajan7 marked this conversation as resolved.
Show resolved Hide resolved
setGridWidth(clientWidth - (devicePixelRatio % 1 === 0 ? 0 : 1));
setGridHeight(clientHeight);
}

saveDimensions();
amanmahajan7 marked this conversation as resolved.
Show resolved Hide resolved
const resizeObserver = new ResizeObserver(saveDimensions);
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 ? 0 : 1);
amanmahajan7 marked this conversation as resolved.
Show resolved Hide resolved
}
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the clientWidth/clientHeight patch below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still needed. We can also change the window.ResizeObserver to not call callback

}

unobserve() {}
Expand Down