UNPKG

@mui/x-charts

Version:

The community edition of the Charts components (MUI X).

66 lines (65 loc) 2.41 kB
import * as React from 'react'; import useEnhancedEffect from '@mui/utils/useEnhancedEffect'; import ownerWindow from '@mui/utils/ownerWindow'; export const useChartContainerDimensions = (inWidth, inHeight) => { const rootRef = React.useRef(null); const displayError = React.useRef(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 = Math.floor(parseFloat(computedStyle.height)) || 0; const newWidth = Math.floor(parseFloat(computedStyle.width)) || 0; setWidth(newWidth); setHeight(newHeight); }, []); React.useEffect(() => { // Ensure the error detection occurs after the first rendering. displayError.current = true; }, []); useEnhancedEffect(() => { if (inWidth !== undefined && inHeight !== undefined) { return () => {}; } computeSize(); const elementToObserve = rootRef.current; if (typeof ResizeObserver === 'undefined') { return () => {}; } let animationFrame; const observer = new ResizeObserver(() => { // See https://github.com/mui/mui-x/issues/8733 animationFrame = requestAnimationFrame(() => { computeSize(); }); }); if (elementToObserve) { observer.observe(elementToObserve); } return () => { if (animationFrame) { window.cancelAnimationFrame(animationFrame); } if (elementToObserve) { observer.unobserve(elementToObserve); } }; }, [computeSize, inHeight, inWidth]); if (process.env.NODE_ENV !== 'production') { if (displayError.current && inWidth === undefined && width === 0) { console.error(`MUI X Charts: ChartContainer 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 X Charts: ChartContainer does not have \`height\` prop, and its container has no \`height\` defined.`); displayError.current = false; } } return [rootRef, inWidth ?? width, inHeight ?? height]; };