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 custom overlay slots positioning #3832

Merged
merged 20 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
8 changes: 6 additions & 2 deletions docs/data/data-grid/components/CustomEmptyOverlayGrid.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import { GridOverlay, DataGrid } from '@mui/x-data-grid';
import { DataGrid } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';
import { styled } from '@mui/material/styles';

const StyledGridOverlay = styled(GridOverlay)(({ theme }) => ({
const StyledGridOverlay = styled('div')(({ theme }) => ({
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
height: '100%',
'& .ant-empty-img-1': {
fill: theme.palette.mode === 'light' ? '#aeb8c2' : '#262626',
},
Expand Down
8 changes: 6 additions & 2 deletions docs/data/data-grid/components/CustomEmptyOverlayGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import { GridOverlay, DataGrid } from '@mui/x-data-grid';
import { DataGrid } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';
import { styled } from '@mui/material/styles';

const StyledGridOverlay = styled(GridOverlay)(({ theme }) => ({
const StyledGridOverlay = styled('div')(({ theme }) => ({
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
height: '100%',
'& .ant-empty-img-1': {
fill: theme.palette.mode === 'light' ? '#aeb8c2' : '#262626',
},
Expand Down
14 changes: 2 additions & 12 deletions docs/data/data-grid/components/CustomLoadingOverlayGrid.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import * as React from 'react';
import { GridOverlay, DataGrid } from '@mui/x-data-grid';
import { DataGrid } from '@mui/x-data-grid';
import LinearProgress from '@mui/material/LinearProgress';
import { useDemoData } from '@mui/x-data-grid-generator';

function CustomLoadingOverlay() {
return (
<GridOverlay>
<div style={{ position: 'absolute', top: 0, width: '100%' }}>
<LinearProgress />
</div>
</GridOverlay>
);
}

export default function CustomLoadingOverlayGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
Expand All @@ -24,7 +14,7 @@ export default function CustomLoadingOverlayGrid() {
<div style={{ height: 400, width: '100%' }}>
<DataGrid
components={{
LoadingOverlay: CustomLoadingOverlay,
LoadingOverlay: LinearProgress,
}}
loading
{...data}
Expand Down
14 changes: 2 additions & 12 deletions docs/data/data-grid/components/CustomLoadingOverlayGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import * as React from 'react';
import { GridOverlay, DataGrid } from '@mui/x-data-grid';
import { DataGrid } from '@mui/x-data-grid';
import LinearProgress from '@mui/material/LinearProgress';
import { useDemoData } from '@mui/x-data-grid-generator';

function CustomLoadingOverlay() {
return (
<GridOverlay>
<div style={{ position: 'absolute', top: 0, width: '100%' }}>
<LinearProgress />
</div>
</GridOverlay>
);
}

export default function CustomLoadingOverlayGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
Expand All @@ -24,7 +14,7 @@ export default function CustomLoadingOverlayGrid() {
<div style={{ height: 400, width: '100%' }}>
<DataGrid
components={{
LoadingOverlay: CustomLoadingOverlay,
LoadingOverlay: LinearProgress,
}}
loading
{...data}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<DataGrid
components={{
LoadingOverlay: CustomLoadingOverlay,
LoadingOverlay: LinearProgress,
}}
loading
{...data}
Expand Down
14 changes: 2 additions & 12 deletions docs/data/data-grid/rows/InfiniteLoadingGrid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { DataGridPro, GridOverlay } from '@mui/x-data-grid-pro';
import { DataGridPro } from '@mui/x-data-grid-pro';
import {
useDemoData,
getRealGridData,
Expand All @@ -17,16 +17,6 @@ function sleep(duration) {
});
}

function CustomLoadingOverlay() {
return (
<GridOverlay>
<div style={{ position: 'absolute', top: 0, width: '100%' }}>
<LinearProgress />
</div>
</GridOverlay>
);
}

export default function InfiniteLoadingGrid() {
const [loading, setLoading] = React.useState(false);
const [loadedRows, setLoadedRows] = React.useState([]);
Expand Down Expand Up @@ -70,7 +60,7 @@ export default function InfiniteLoadingGrid() {
hideFooterPagination
onRowsScrollEnd={handleOnRowsScrollEnd}
components={{
LoadingOverlay: CustomLoadingOverlay,
LoadingOverlay: LinearProgress,
}}
/>
</div>
Expand Down
14 changes: 2 additions & 12 deletions docs/data/data-grid/rows/InfiniteLoadingGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { DataGridPro, GridOverlay } from '@mui/x-data-grid-pro';
import { DataGridPro } from '@mui/x-data-grid-pro';
import {
useDemoData,
getRealGridData,
Expand All @@ -17,16 +17,6 @@ function sleep(duration: number) {
});
}

function CustomLoadingOverlay() {
return (
<GridOverlay>
<div style={{ position: 'absolute', top: 0, width: '100%' }}>
<LinearProgress />
</div>
</GridOverlay>
);
}

export default function InfiniteLoadingGrid() {
const [loading, setLoading] = React.useState(false);
const [loadedRows, setLoadedRows] = React.useState<any>([]);
Expand Down Expand Up @@ -70,7 +60,7 @@ export default function InfiniteLoadingGrid() {
hideFooterPagination
onRowsScrollEnd={handleOnRowsScrollEnd}
components={{
LoadingOverlay: CustomLoadingOverlay,
LoadingOverlay: LinearProgress,
}}
/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/data/data-grid/rows/InfiniteLoadingGrid.tsx.preview
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
hideFooterPagination
onRowsScrollEnd={handleOnRowsScrollEnd}
components={{
LoadingOverlay: CustomLoadingOverlay,
LoadingOverlay: LinearProgress,
}}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GridOverlays } from './GridOverlays';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import { useGridSelector } from '../../hooks/utils/useGridSelector';
import { gridDensityHeaderHeightSelector } from '../../hooks/features/density/densitySelector';
import { GridOverlaysRoot } from './GridOverlaysRoot';

interface GridBodyProps {
children?: React.ReactNode;
Expand Down Expand Up @@ -72,7 +73,9 @@ function GridBody(props: GridBodyProps) {

return (
<GridMainContainer>
<GridOverlays />
<GridOverlaysRoot>
<GridOverlays />
Copy link
Member

Choose a reason for hiding this comment

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

Could GridOverlaysRoot be the root element of GridOverlays? This approach kinda breaks the internal convention for styled components:

<GridOverlays>
  <GridOverlaysRoot>
    {currentOverlay}
  </GridOverlaysRoot>
</GridOverlays>

For the error overlay we can make an exception and export GridOverlaysRoot.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree!

Copy link
Member Author

Choose a reason for hiding this comment

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

I've kept GridOverlayWrapper in separate file though to avoid exporting it in the package

</GridOverlaysRoot>
<ColumnHeadersComponent ref={columnsContainerRef} innerRef={columnHeadersRef} />
<GridAutoSizer
nonce={rootProps.nonce}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ErrorBoundary } from '../ErrorBoundary';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import { GridAutoSizer, AutoSizerSize } from '../GridAutoSizer';
import { GridEvents } from '../../models/events/gridEvents';
import { GridOverlaysRoot } from './GridOverlaysRoot';

export function GridErrorHandler(props) {
const { children } = props;
Expand Down Expand Up @@ -35,10 +36,12 @@ export function GridErrorHandler(props) {
onResize={handleResize}
>
{() => (
<rootProps.components.ErrorOverlay
{...errorProps}
{...rootProps.componentsProps?.errorOverlay}
/>
<GridOverlaysRoot>
<rootProps.components.ErrorOverlay
{...errorProps}
{...rootProps.componentsProps?.errorOverlay}
/>
</GridOverlaysRoot>
)}
</GridAutoSizer>
</GridMainContainer>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as React from 'react';
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/material/utils';
import { useGridSelector } from '../../hooks/utils/useGridSelector';
import { gridDensityHeaderHeightSelector } from '../../hooks/features/density/densitySelector';
import { GridEvents } from '../../models/events';
import { useGridApiContext } from '../../hooks/utils/useGridApiContext';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';

export function GridOverlaysRoot(props: React.PropsWithChildren<{}>) {
const apiRef = useGridApiContext();
const rootProps = useGridRootProps();
const headerHeight = useGridSelector(apiRef, gridDensityHeaderHeightSelector);

const [viewportInnerSize, setViewportInnerSize] = React.useState(
() => apiRef.current.getRootDimensions()?.viewportInnerSize ?? null,
);

const handleViewportSizeChange = React.useCallback(() => {
setViewportInnerSize(apiRef.current.getRootDimensions()?.viewportInnerSize ?? null);
}, [apiRef]);

useEnhancedEffect(() => {
return apiRef.current.subscribeEvent(
GridEvents.viewportInnerSizeChange,
handleViewportSizeChange,
);
}, [apiRef, handleViewportSizeChange]);

let height: React.CSSProperties['height'] = viewportInnerSize?.height ?? 0;
if (rootProps.autoHeight && height === 0) {
height = 'auto';
}

if (!viewportInnerSize) {
return null;
}

return (
<div
style={{
height,
width: viewportInnerSize?.width ?? 0,
position: 'absolute',
top: headerHeight,
left: 0,
right: 0,
bottom: 0,
Copy link
Member

Choose a reason for hiding this comment

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

bottom and right are not necessary if we also define the height and width.

Copy link
Member Author

Choose a reason for hiding this comment

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

turns out bottom: 0 is needed here when height: auto, otherwise overlay won't take the whole height

}}
{...props}
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import * as React from 'react';
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/material/utils';
import clsx from 'clsx';
import { unstable_composeClasses as composeClasses } from '@mui/material';
import { alpha, styled } from '@mui/material/styles';
import { useGridSelector } from '../../hooks/utils/useGridSelector';
import { gridDensityHeaderHeightSelector } from '../../hooks/features/density/densitySelector';
import { useGridApiContext } from '../../hooks/utils/useGridApiContext';
import { getDataGridUtilityClass } from '../../gridClasses';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import { DataGridProcessedProps } from '../../models/props/DataGridProps';
import { GridEvents } from '../../models/events';

export type GridOverlayProps = React.HTMLAttributes<HTMLDivElement>;

Expand All @@ -31,11 +26,7 @@ const GridOverlayRoot = styled('div', {
overridesResolver: (props, styles) => styles.overlay,
})(({ theme }) => ({
display: 'flex',
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
height: '100%',
alignSelf: 'center',
alignItems: 'center',
justifyContent: 'center',
Expand All @@ -46,50 +37,10 @@ export const GridOverlay = React.forwardRef<HTMLDivElement, GridOverlayProps>(fu
props: GridOverlayProps,
ref,
) {
const { className, style, ...other } = props;
const apiRef = useGridApiContext();
const { className, ...other } = props;
const rootProps = useGridRootProps();
const ownerState = { classes: rootProps.classes };
const classes = useUtilityClasses(ownerState);
const headerHeight = useGridSelector(apiRef, gridDensityHeaderHeightSelector);

const [viewportInnerSize, setViewportInnerSize] = React.useState(
() => apiRef.current.getRootDimensions()?.viewportInnerSize ?? null,
);

const handleViewportSizeChange = React.useCallback(() => {
setViewportInnerSize(apiRef.current.getRootDimensions()?.viewportInnerSize ?? null);
}, [apiRef]);

useEnhancedEffect(() => {
return apiRef.current.subscribeEvent(
GridEvents.viewportInnerSizeChange,
handleViewportSizeChange,
);
}, [apiRef, handleViewportSizeChange]);

let height: React.CSSProperties['height'] = viewportInnerSize?.height ?? 0;
if (rootProps.autoHeight && height === 0) {
height = 'auto';
}

if (!viewportInnerSize) {
return null;
}

return (
<GridOverlayRoot
ref={ref}
className={clsx(classes.root, className)}
style={{
height,
width: viewportInnerSize?.width ?? 0,
top: headerHeight,
position: 'absolute',
left: 0,
...style,
}}
{...other}
/>
);
return <GridOverlayRoot ref={ref} className={clsx(classes.root, className)} {...other} />;
});
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('<DataGrid /> - Components', () => {
</ErrorBoundary>,
);
}).toErrorDev([
'MUI: Could not find the data grid context.',
'MUI: useGridRootProps should only be used inside the DataGrid/DataGridPro component.',
'The above error occurred in the <ForwardRef(GridOverlay)> component',
]);
});
Expand Down
21 changes: 14 additions & 7 deletions packages/storybook/src/stories/grid-error.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { DataGridPro, GridColDef, useGridApiRef, GridOverlay } from '@mui/x-data-grid-pro';
import { DataGridPro, GridColDef, useGridApiRef } from '@mui/x-data-grid-pro';
import '../style/grid-stories.css';

export default {
Expand Down Expand Up @@ -145,12 +145,19 @@ export const ErrorProp = () => {
};
function CustomErrorOverlay(props) {
return (
<GridOverlay className="custom-overlay">
<div style={{ textAlign: 'center' }}>
<h1>{props.title}</h1>
<p>{typeof props.error === 'string' ? props.error : props.error.message}</p>
</div>
</GridOverlay>
<div
style={{
textAlign: 'center',
height: '100%',
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
}}
className="custom-overlay"
>
<h1>{props.title}</h1>
<p>{typeof props.error === 'string' ? props.error : props.error.message}</p>
</div>
);
}

Expand Down
Loading