-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[charts] Take responsive container from data grid (#9497)
- Loading branch information
1 parent
575be07
commit 7f90a1b
Showing
4 changed files
with
98 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 89 additions & 21 deletions
110
packages/x-charts/src/ResponsiveChartContainer/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,121 @@ | ||
import * as React from 'react'; | ||
import { ResizeObserver } from '@juggle/resize-observer'; | ||
import useEnhancedEffect from '@mui/utils/useEnhancedEffect'; | ||
import ownerWindow from '@mui/utils/ownerWindow'; | ||
import { styled } from '@mui/material/styles'; | ||
import { ChartContainer, ChartContainerProps } from '../ChartContainer'; | ||
import { MakeOptional } from '../models/helpers'; | ||
|
||
const useChartDimensions = ( | ||
inWidth?: number, | ||
inHeight?: number, | ||
): [React.RefObject<HTMLDivElement>, number, number] => { | ||
const ref = React.useRef<HTMLDivElement>(null); | ||
const rootRef = React.useRef<HTMLDivElement>(null); | ||
const displayError = React.useRef<boolean>(false); | ||
|
||
const [width, setWidth] = React.useState(0); | ||
const [height, setHeight] = React.useState(0); | ||
|
||
// Adaptation of the `computeSizeAndPublishResizeEvent` from the grid. | ||
const computeSize = React.useCallback(() => { | ||
const mainEl = rootRef?.current; | ||
|
||
if (!mainEl) { | ||
return; | ||
} | ||
|
||
const win = ownerWindow(mainEl); | ||
const computedStyle = win.getComputedStyle(mainEl); | ||
|
||
const newHeight = parseFloat(computedStyle.height) || 0; | ||
const newWidth = parseFloat(computedStyle.width) || 0; | ||
|
||
setWidth(newWidth); | ||
setHeight(newHeight); | ||
}, []); | ||
|
||
React.useEffect(() => { | ||
const element = ref.current; | ||
if (element === null || (inHeight !== undefined && inWidth !== undefined)) { | ||
// Ensure the error detection occurs after the first rendering. | ||
displayError.current = true; | ||
}, []); | ||
|
||
useEnhancedEffect(() => { | ||
if (inWidth !== undefined && inHeight !== undefined) { | ||
return () => {}; | ||
} | ||
computeSize(); | ||
|
||
const resizeObserver = new ResizeObserver((entries) => { | ||
if (Array.isArray(entries) && entries.length) { | ||
const entry = entries[0]; | ||
if (inWidth === undefined) { | ||
setWidth(entry.contentRect.width); | ||
} | ||
if (inHeight === undefined) { | ||
setHeight(entry.contentRect.height); | ||
} | ||
} | ||
const elementToObserve = rootRef.current; | ||
if (typeof ResizeObserver === 'undefined') { | ||
return () => {}; | ||
} | ||
|
||
let animationFrame: number; | ||
const observer = new ResizeObserver(() => { | ||
// See https://github.com/mui/mui-x/issues/8733 | ||
animationFrame = window.requestAnimationFrame(() => { | ||
computeSize(); | ||
}); | ||
}); | ||
resizeObserver.observe(element); | ||
|
||
return () => resizeObserver.disconnect(); | ||
}, [height, inHeight, inWidth, width]); | ||
if (elementToObserve) { | ||
observer.observe(elementToObserve); | ||
} | ||
|
||
return () => { | ||
if (animationFrame) { | ||
window.cancelAnimationFrame(animationFrame); | ||
} | ||
|
||
if (elementToObserve) { | ||
observer.unobserve(elementToObserve); | ||
} | ||
}; | ||
}, [computeSize, inHeight, inWidth]); | ||
|
||
return [ref, inWidth ?? width, inHeight ?? height]; | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (displayError.current && inWidth === undefined && width === 0) { | ||
console.error( | ||
`MUI: Charts does not have \`width\` prop, and its container has no \`width\` defined.`, | ||
); | ||
displayError.current = false; | ||
} | ||
if (displayError.current && inHeight === undefined && height === 0) { | ||
console.error( | ||
`MUI: Charts does not have \`height\` prop, and its container has no \`height\` defined.`, | ||
); | ||
displayError.current = false; | ||
} | ||
} | ||
|
||
return [rootRef, inWidth ?? width, inHeight ?? height]; | ||
}; | ||
|
||
export type ResponsiveChartContainerProps = MakeOptional<ChartContainerProps, 'width' | 'height'>; | ||
|
||
const ResizableContainer = styled('div', { | ||
name: 'MuiResponsiveChart', | ||
slot: 'Container', | ||
})<{ ownerState: Pick<ResponsiveChartContainerProps, 'width' | 'height'> }>(({ ownerState }) => ({ | ||
width: ownerState.width ?? '100%', | ||
height: ownerState.height ?? '100%', | ||
display: 'flex', | ||
position: 'relative', | ||
flexGrow: 1, | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
overflow: 'hidden', | ||
})); | ||
|
||
export function ResponsiveChartContainer(props: ResponsiveChartContainerProps) { | ||
const [containerRef, width, height] = useChartDimensions(props.width, props.height); | ||
|
||
return ( | ||
<div | ||
<ResizableContainer | ||
ref={containerRef} | ||
style={{ width: props.width ?? '100%', height: props.height ?? '100%', padding: 0 }} | ||
ownerState={{ width: props.width, height: props.height }} | ||
> | ||
<ChartContainer {...props} width={width} height={height} /> | ||
</div> | ||
</ResizableContainer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters