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] Improve resize performance (@lauri865) #15592

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/x-data-grid/src/hooks/core/useGridRefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const useGridRefs = <PrivateApi extends GridPrivateApiCommon>(
apiRef: React.MutableRefObject<PrivateApi>,
) => {
const rootElementRef = React.useRef<HTMLDivElement>(null);
const mainElementRef = React.useRef<HTMLDivElement>(null);
const mainElementRef = React.useRef<HTMLDivElement | null>(null);
const virtualScrollerRef = React.useRef<HTMLDivElement>(null);
const virtualScrollbarVerticalRef = React.useRef<HTMLDivElement>(null);
const virtualScrollbarHorizontalRef = React.useRef<HTMLDivElement>(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
} from '@mui/utils';
import useLazyRef from '@mui/utils/useLazyRef';
import useTimeout from '@mui/utils/useTimeout';
import { useResizeObserver } from '@mui/x-internals/useResizeObserver';
import { useRtl } from '@mui/system/RtlProvider';
import reactMajor from '@mui/x-internals/reactMajor';
import type { GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity';
import { useGridPrivateApiContext } from '../../utils/useGridPrivateApiContext';
import { useGridRootProps } from '../../utils/useGridRootProps';
Expand Down Expand Up @@ -135,7 +135,57 @@ export const useGridVirtualScroller = () => {
const columnsTotalWidth = dimensions.columnsTotalWidth;
const hasColSpan = useGridSelector(apiRef, gridHasColSpanSelector);

useResizeObserver(mainRef, () => apiRef.current.resize());
const mainRefCallback = React.useCallback(
(node: HTMLDivElement | null) => {
mainRef.current = node;

if (!node) {
return undefined;
}

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

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

if (typeof ResizeObserver === 'undefined') {
return undefined;
}

const observer = new ResizeObserver((entries) => {
const entry = entries[0];
if (!entry) {
return;
}

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

if (newSize.width === lastSize.width && newSize.height === lastSize.height) {
return;
}

apiRef.current.publishEvent('resize', newSize);
lastSize = newSize;
});

observer.observe(node);

if (reactMajor >= 19) {
return () => {
mainRef.current = null;
observer.disconnect();
};
}
return undefined;
},
[apiRef, mainRef],
);

/*
* Scroll context logic
Expand Down Expand Up @@ -549,11 +599,6 @@ export const useGridVirtualScroller = () => {
apiRef.current.publishEvent('virtualScrollerContentSizeChange');
}, [apiRef, contentSize]);

useEnhancedEffect(() => {
// FIXME: Is this really necessary?
apiRef.current.resize();
}, [apiRef, rowsMeta.currentPageTotalHeight]);

useEnhancedEffect(() => {
// TODO a scroll reset should not be necessary
if (enabledForColumns) {
Expand Down Expand Up @@ -596,7 +641,7 @@ export const useGridVirtualScroller = () => {
setPanels,
getRows,
getContainerProps: () => ({
ref: mainRef,
ref: mainRefCallback,
}),
getScrollerProps: () => ({
ref: scrollerRef,
Expand Down
2 changes: 1 addition & 1 deletion packages/x-data-grid/src/models/api/gridCoreApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface GridCorePrivateApi<
/**
* The React ref of the grid main container div element.
*/
mainElementRef: React.RefObject<HTMLDivElement>;
mainElementRef: React.MutableRefObject<HTMLDivElement | null>;
/**
* The React ref of the grid's virtual scroller container element.
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/x-internals/src/reactMajor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as React from 'react';

export default parseInt(React.version, 10);