From df2e894e9a80a0e575669845e940d9107b4bc374 Mon Sep 17 00:00:00 2001 From: alexandre Date: Thu, 6 Jun 2024 12:14:52 +0200 Subject: [PATCH 1/2] [charts] Add watermark on the pro ResponsiveChartContainer --- .../ResponsiveChartContainer.tsx | 284 ++++++++++++++++++ .../ResposiveChartContainer.test.tsx | 21 ++ .../src/ResponsiveChartContainer/index.ts | 1 + .../useChartContainerDimensions.ts | 88 ++++++ packages/x-charts-pro/src/index.ts | 28 +- .../src/internals/utils/releaseInfo.ts | 15 + packages/x-charts/.mocharc.js | 2 +- .../x-charts/src/hooks/useReducedMotion.ts | 7 + 8 files changed, 444 insertions(+), 2 deletions(-) create mode 100644 packages/x-charts-pro/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx create mode 100644 packages/x-charts-pro/src/ResponsiveChartContainer/ResposiveChartContainer.test.tsx create mode 100644 packages/x-charts-pro/src/ResponsiveChartContainer/index.ts create mode 100644 packages/x-charts-pro/src/ResponsiveChartContainer/useChartContainerDimensions.ts diff --git a/packages/x-charts-pro/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx b/packages/x-charts-pro/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx new file mode 100644 index 000000000000..5789814225c0 --- /dev/null +++ b/packages/x-charts-pro/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx @@ -0,0 +1,284 @@ +import * as React from 'react'; +import PropTypes from 'prop-types'; +import { styled } from '@mui/material/styles'; +import { useLicenseVerifier, Watermark } from '@mui/x-license'; +import { ChartContainer, ChartContainerProps } from '@mui/x-charts/ChartContainer'; +import { useChartContainerDimensions } from './useChartContainerDimensions'; +import { getReleaseInfo } from '../internals/utils/releaseInfo'; + +export interface ResponsiveChartContainerProps + extends Omit { + /** + * The width of the chart in px. If not defined, it takes the width of the parent element. + */ + width?: number; + /** + * The height of the chart in px. If not defined, it takes the height of the parent element. + */ + height?: number; +} + +const ResizableContainer = styled('div', { + name: 'MuiResponsiveChart', + slot: 'Container', +})<{ ownerState: Pick }>(({ ownerState }) => ({ + width: ownerState.width ?? '100%', + height: ownerState.height ?? '100%', + display: 'flex', + position: 'relative', + flexGrow: 1, + flexDirection: 'column', + alignItems: 'center', + justifyContent: 'center', + overflow: 'hidden', + '&>svg': { + width: '100%', + height: '100%', + }, +})); + +const releaseInfo = getReleaseInfo(); + +const ResponsiveChartContainer = React.forwardRef(function ResponsiveChartContainer( + props: ResponsiveChartContainerProps, + ref, +) { + const { width: inWidth, height: inHeight, ...other } = props; + const [containerRef, width, height] = useChartContainerDimensions(inWidth, inHeight); + + useLicenseVerifier('x-charts-pro', releaseInfo); + + return ( + + {width && height ? ( + + ) : null} + + + ); +}); + +ResponsiveChartContainer.propTypes = { + // ----------------------------- Warning -------------------------------- + // | These PropTypes are generated from the TypeScript type definitions | + // | To update them edit the TypeScript types and run "pnpm proptypes" | + // ---------------------------------------------------------------------- + children: PropTypes.node, + className: PropTypes.string, + /** + * Color palette used to colorize multiple series. + * @default blueberryTwilightPalette + */ + colors: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string), PropTypes.func]), + /** + * An array of objects that can be used to populate series and axes data using their `dataKey` property. + */ + dataset: PropTypes.arrayOf(PropTypes.object), + desc: PropTypes.string, + /** + * If `true`, the charts will not listen to the mouse move event. + * It might break interactive features, but will improve performance. + * @default false + */ + disableAxisListener: PropTypes.bool, + /** + * The height of the chart in px. If not defined, it takes the height of the parent element. + */ + height: PropTypes.number, + /** + * The item currently highlighted. Turns highlighting into a controlled prop. + */ + highlightedItem: PropTypes.shape({ + dataIndex: PropTypes.number, + seriesId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), + }), + /** + * The margin between the SVG and the drawing area. + * It's used for leaving some space for extra information such as the x- and y-axis or legend. + * Accepts an object with the optional properties: `top`, `bottom`, `left`, and `right`. + * @default object Depends on the charts type. + */ + margin: PropTypes.shape({ + bottom: PropTypes.number, + left: PropTypes.number, + right: PropTypes.number, + top: PropTypes.number, + }), + /** + * The callback fired when the highlighted item changes. + * + * @param {HighlightItemData | null} highlightedItem The newly highlighted item. + */ + onHighlightChange: PropTypes.func, + /** + * An array of plugins defining how to preprocess data. + * If not provided, the container supports line, bar, scatter and pie charts. + */ + plugins: PropTypes.arrayOf(PropTypes.object), + /** + * The array of series to display. + * Each type of series has its own specificity. + * Please refer to the appropriate docs page to learn more about it. + */ + series: PropTypes.arrayOf(PropTypes.object).isRequired, + sx: PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), + PropTypes.func, + PropTypes.object, + ]), + title: PropTypes.string, + viewBox: PropTypes.shape({ + height: PropTypes.number, + width: PropTypes.number, + x: PropTypes.number, + y: PropTypes.number, + }), + /** + * The width of the chart in px. If not defined, it takes the width of the parent element. + */ + width: PropTypes.number, + /** + * The configuration of the x-axes. + * If not provided, a default axis config is used. + * An array of [[AxisConfig]] objects. + */ + xAxis: PropTypes.arrayOf( + PropTypes.shape({ + axisId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), + classes: PropTypes.object, + colorMap: PropTypes.oneOfType([ + PropTypes.shape({ + colors: PropTypes.arrayOf(PropTypes.string).isRequired, + type: PropTypes.oneOf(['ordinal']).isRequired, + unknownColor: PropTypes.string, + values: PropTypes.arrayOf( + PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]) + .isRequired, + ), + }), + PropTypes.shape({ + color: PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.string.isRequired), + PropTypes.func, + ]).isRequired, + max: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]), + min: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]), + type: PropTypes.oneOf(['continuous']).isRequired, + }), + PropTypes.shape({ + colors: PropTypes.arrayOf(PropTypes.string).isRequired, + thresholds: PropTypes.arrayOf( + PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired, + ).isRequired, + type: PropTypes.oneOf(['piecewise']).isRequired, + }), + ]), + data: PropTypes.array, + dataKey: PropTypes.string, + disableLine: PropTypes.bool, + disableTicks: PropTypes.bool, + fill: PropTypes.string, + hideTooltip: PropTypes.bool, + id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), + label: PropTypes.string, + labelFontSize: PropTypes.number, + labelStyle: PropTypes.object, + max: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]), + min: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]), + position: PropTypes.oneOf(['bottom', 'top']), + reverse: PropTypes.bool, + scaleType: PropTypes.oneOf(['band', 'linear', 'log', 'point', 'pow', 'sqrt', 'time', 'utc']), + slotProps: PropTypes.object, + slots: PropTypes.object, + stroke: PropTypes.string, + tickFontSize: PropTypes.number, + tickInterval: PropTypes.oneOfType([ + PropTypes.oneOf(['auto']), + PropTypes.array, + PropTypes.func, + ]), + tickLabelInterval: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.func]), + tickLabelPlacement: PropTypes.oneOf(['middle', 'tick']), + tickLabelStyle: PropTypes.object, + tickMaxStep: PropTypes.number, + tickMinStep: PropTypes.number, + tickNumber: PropTypes.number, + tickPlacement: PropTypes.oneOf(['end', 'extremities', 'middle', 'start']), + tickSize: PropTypes.number, + valueFormatter: PropTypes.func, + }), + ), + /** + * The configuration of the y-axes. + * If not provided, a default axis config is used. + * An array of [[AxisConfig]] objects. + */ + yAxis: PropTypes.arrayOf( + PropTypes.shape({ + axisId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), + classes: PropTypes.object, + colorMap: PropTypes.oneOfType([ + PropTypes.shape({ + colors: PropTypes.arrayOf(PropTypes.string).isRequired, + type: PropTypes.oneOf(['ordinal']).isRequired, + unknownColor: PropTypes.string, + values: PropTypes.arrayOf( + PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]) + .isRequired, + ), + }), + PropTypes.shape({ + color: PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.string.isRequired), + PropTypes.func, + ]).isRequired, + max: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]), + min: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]), + type: PropTypes.oneOf(['continuous']).isRequired, + }), + PropTypes.shape({ + colors: PropTypes.arrayOf(PropTypes.string).isRequired, + thresholds: PropTypes.arrayOf( + PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]).isRequired, + ).isRequired, + type: PropTypes.oneOf(['piecewise']).isRequired, + }), + ]), + data: PropTypes.array, + dataKey: PropTypes.string, + disableLine: PropTypes.bool, + disableTicks: PropTypes.bool, + fill: PropTypes.string, + hideTooltip: PropTypes.bool, + id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), + label: PropTypes.string, + labelFontSize: PropTypes.number, + labelStyle: PropTypes.object, + max: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]), + min: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number]), + position: PropTypes.oneOf(['left', 'right']), + reverse: PropTypes.bool, + scaleType: PropTypes.oneOf(['band', 'linear', 'log', 'point', 'pow', 'sqrt', 'time', 'utc']), + slotProps: PropTypes.object, + slots: PropTypes.object, + stroke: PropTypes.string, + tickFontSize: PropTypes.number, + tickInterval: PropTypes.oneOfType([ + PropTypes.oneOf(['auto']), + PropTypes.array, + PropTypes.func, + ]), + tickLabelInterval: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.func]), + tickLabelPlacement: PropTypes.oneOf(['middle', 'tick']), + tickLabelStyle: PropTypes.object, + tickMaxStep: PropTypes.number, + tickMinStep: PropTypes.number, + tickNumber: PropTypes.number, + tickPlacement: PropTypes.oneOf(['end', 'extremities', 'middle', 'start']), + tickSize: PropTypes.number, + valueFormatter: PropTypes.func, + }), + ), +} as any; + +export { ResponsiveChartContainer }; diff --git a/packages/x-charts-pro/src/ResponsiveChartContainer/ResposiveChartContainer.test.tsx b/packages/x-charts-pro/src/ResponsiveChartContainer/ResposiveChartContainer.test.tsx new file mode 100644 index 000000000000..c72848612223 --- /dev/null +++ b/packages/x-charts-pro/src/ResponsiveChartContainer/ResposiveChartContainer.test.tsx @@ -0,0 +1,21 @@ +import * as React from 'react'; +import { expect } from 'chai'; +import { createRenderer, screen, waitFor } from '@mui/internal-test-utils'; +import { LicenseInfo } from '@mui/x-license'; +import { ResponsiveChartContainer } from './ResponsiveChartContainer'; + +describe(' - License', () => { + const { render } = createRenderer(); + + it('should render watermark when the license is missing', async () => { + LicenseInfo.setLicenseKey(''); + + expect(() => + render(), + ).toErrorDev(['MUI X: Missing license key.']); + + await waitFor(() => { + expect(screen.getByText('MUI X Missing license key')).to.not.equal(null); + }); + }); +}); diff --git a/packages/x-charts-pro/src/ResponsiveChartContainer/index.ts b/packages/x-charts-pro/src/ResponsiveChartContainer/index.ts new file mode 100644 index 000000000000..0d6ff4b41a20 --- /dev/null +++ b/packages/x-charts-pro/src/ResponsiveChartContainer/index.ts @@ -0,0 +1 @@ +export * from './ResponsiveChartContainer'; diff --git a/packages/x-charts-pro/src/ResponsiveChartContainer/useChartContainerDimensions.ts b/packages/x-charts-pro/src/ResponsiveChartContainer/useChartContainerDimensions.ts new file mode 100644 index 000000000000..4e82a43889af --- /dev/null +++ b/packages/x-charts-pro/src/ResponsiveChartContainer/useChartContainerDimensions.ts @@ -0,0 +1,88 @@ +import * as React from 'react'; +import useEnhancedEffect from '@mui/utils/useEnhancedEffect'; +import ownerWindow from '@mui/utils/ownerWindow'; + +export const useChartContainerDimensions = ( + inWidth?: number, + inHeight?: number, +): [React.RefObject, number, number] => { + 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: number; + 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]; +}; diff --git a/packages/x-charts-pro/src/index.ts b/packages/x-charts-pro/src/index.ts index 4fec074aff7a..a5579c0307c6 100644 --- a/packages/x-charts-pro/src/index.ts +++ b/packages/x-charts-pro/src/index.ts @@ -1 +1,27 @@ -export * from '@mui/x-charts'; +export * from '@mui/x-charts/constants'; +export * from '@mui/x-charts/context'; +export * from '@mui/x-charts/hooks'; +export * from '@mui/x-charts/colorPalettes'; +export * from '@mui/x-charts/models'; +export * from '@mui/x-charts/ChartsClipPath'; +export * from '@mui/x-charts/ChartsReferenceLine'; +export * from '@mui/x-charts/ChartsAxis'; +export * from '@mui/x-charts/ChartsXAxis'; +export * from '@mui/x-charts/ChartsYAxis'; +export * from '@mui/x-charts/ChartsGrid'; +export * from '@mui/x-charts/ChartsText'; +export * from '@mui/x-charts/ChartsTooltip'; +export * from '@mui/x-charts/ChartsLegend'; +export * from '@mui/x-charts/ChartsAxisHighlight'; +export * from '@mui/x-charts/ChartsVoronoiHandler'; +export * from '@mui/x-charts/ChartsOnAxisClickHandler'; +export * from '@mui/x-charts/BarChart'; +export * from '@mui/x-charts/LineChart'; +export * from '@mui/x-charts/PieChart'; +export * from '@mui/x-charts/ScatterChart'; +export * from '@mui/x-charts/SparkLineChart'; +export * from '@mui/x-charts/Gauge'; +export * from '@mui/x-charts/ChartsSurface'; +export * from '@mui/x-charts/ChartContainer'; + +export * from './ResponsiveChartContainer'; diff --git a/packages/x-charts-pro/src/internals/utils/releaseInfo.ts b/packages/x-charts-pro/src/internals/utils/releaseInfo.ts index e69de29bb2d1..2830a4fd656a 100644 --- a/packages/x-charts-pro/src/internals/utils/releaseInfo.ts +++ b/packages/x-charts-pro/src/internals/utils/releaseInfo.ts @@ -0,0 +1,15 @@ +import { ponyfillGlobal } from '@mui/utils'; + +export const getReleaseInfo = () => { + const releaseInfo = '__RELEASE_INFO__'; + if (process.env.NODE_ENV !== 'production') { + // A simple hack to set the value in the test environment (has no build step). + // eslint-disable-next-line no-useless-concat + if (releaseInfo === '__RELEASE' + '_INFO__') { + // eslint-disable-next-line no-underscore-dangle + return ponyfillGlobal.__MUI_RELEASE_INFO__; + } + } + + return releaseInfo; +}; diff --git a/packages/x-charts/.mocharc.js b/packages/x-charts/.mocharc.js index 8fad90b9b6f0..8c6c28b43a26 100644 --- a/packages/x-charts/.mocharc.js +++ b/packages/x-charts/.mocharc.js @@ -30,5 +30,5 @@ module.exports = { '**/build/**', 'docs/.next/**', ], - spec: ['packages/x-charts/**/*.test.{js,ts,tsx}'], + spec: ['packages/x-charts{,-pro,-premium}/**/*.test.{js,ts,tsx}'], }; diff --git a/packages/x-charts/src/hooks/useReducedMotion.ts b/packages/x-charts/src/hooks/useReducedMotion.ts index d9df669f241b..80661a06abc3 100644 --- a/packages/x-charts/src/hooks/useReducedMotion.ts +++ b/packages/x-charts/src/hooks/useReducedMotion.ts @@ -11,6 +11,13 @@ export const useReducedMotion = () => { // Taken from: https://github.com/pmndrs/react-spring/blob/02ec877bbfab0df46da0e4a47d5f68d3e731206a/packages/shared/src/hooks/useReducedMotion.ts#L13 useIsomorphicLayoutEffect(() => { + if (!window.matchMedia) { + // skip animation in environments where `window.matchMedia` would not be available (i.e. test/jsdom) + Globals.assign({ + skipAnimation: true, + }); + return () => {}; + } const mql = window.matchMedia('(prefers-reduced-motion)'); const handleMediaChange = (e: MediaQueryListEvent | MediaQueryList) => { From 58549270594cada877c98b59952222b13b978452 Mon Sep 17 00:00:00 2001 From: alexandre Date: Thu, 6 Jun 2024 14:41:05 +0200 Subject: [PATCH 2/2] Rename ResponsiveChartContainerPro --- .../src/ResponsiveChartContainer/index.ts | 1 - .../useChartContainerDimensions.ts | 88 ------------------- .../ResponsiveChartContainerPro.test.tsx} | 6 +- .../ResponsiveChartContainerPro.tsx} | 45 ++-------- .../src/ResponsiveChartContainerPro/index.ts | 1 + packages/x-charts-pro/src/index.ts | 3 +- .../ResizableContainer.tsx | 26 ++++++ .../ResponsiveChartContainer.tsx | 21 +---- packages/x-charts/src/internals/index.ts | 3 + 9 files changed, 43 insertions(+), 151 deletions(-) delete mode 100644 packages/x-charts-pro/src/ResponsiveChartContainer/index.ts delete mode 100644 packages/x-charts-pro/src/ResponsiveChartContainer/useChartContainerDimensions.ts rename packages/x-charts-pro/src/{ResponsiveChartContainer/ResposiveChartContainer.test.tsx => ResponsiveChartContainerPro/ResponsiveChartContainerPro.test.tsx} (70%) rename packages/x-charts-pro/src/{ResponsiveChartContainer/ResponsiveChartContainer.tsx => ResponsiveChartContainerPro/ResponsiveChartContainerPro.tsx} (88%) create mode 100644 packages/x-charts-pro/src/ResponsiveChartContainerPro/index.ts create mode 100644 packages/x-charts/src/ResponsiveChartContainer/ResizableContainer.tsx diff --git a/packages/x-charts-pro/src/ResponsiveChartContainer/index.ts b/packages/x-charts-pro/src/ResponsiveChartContainer/index.ts deleted file mode 100644 index 0d6ff4b41a20..000000000000 --- a/packages/x-charts-pro/src/ResponsiveChartContainer/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './ResponsiveChartContainer'; diff --git a/packages/x-charts-pro/src/ResponsiveChartContainer/useChartContainerDimensions.ts b/packages/x-charts-pro/src/ResponsiveChartContainer/useChartContainerDimensions.ts deleted file mode 100644 index 4e82a43889af..000000000000 --- a/packages/x-charts-pro/src/ResponsiveChartContainer/useChartContainerDimensions.ts +++ /dev/null @@ -1,88 +0,0 @@ -import * as React from 'react'; -import useEnhancedEffect from '@mui/utils/useEnhancedEffect'; -import ownerWindow from '@mui/utils/ownerWindow'; - -export const useChartContainerDimensions = ( - inWidth?: number, - inHeight?: number, -): [React.RefObject, number, number] => { - 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: number; - 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]; -}; diff --git a/packages/x-charts-pro/src/ResponsiveChartContainer/ResposiveChartContainer.test.tsx b/packages/x-charts-pro/src/ResponsiveChartContainerPro/ResponsiveChartContainerPro.test.tsx similarity index 70% rename from packages/x-charts-pro/src/ResponsiveChartContainer/ResposiveChartContainer.test.tsx rename to packages/x-charts-pro/src/ResponsiveChartContainerPro/ResponsiveChartContainerPro.test.tsx index c72848612223..e8fcf52be087 100644 --- a/packages/x-charts-pro/src/ResponsiveChartContainer/ResposiveChartContainer.test.tsx +++ b/packages/x-charts-pro/src/ResponsiveChartContainerPro/ResponsiveChartContainerPro.test.tsx @@ -2,16 +2,16 @@ import * as React from 'react'; import { expect } from 'chai'; import { createRenderer, screen, waitFor } from '@mui/internal-test-utils'; import { LicenseInfo } from '@mui/x-license'; -import { ResponsiveChartContainer } from './ResponsiveChartContainer'; +import { ResponsiveChartContainerPro } from './ResponsiveChartContainerPro'; -describe(' - License', () => { +describe(' - License', () => { const { render } = createRenderer(); it('should render watermark when the license is missing', async () => { LicenseInfo.setLicenseKey(''); expect(() => - render(), + render(), ).toErrorDev(['MUI X: Missing license key.']); await waitFor(() => { diff --git a/packages/x-charts-pro/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx b/packages/x-charts-pro/src/ResponsiveChartContainerPro/ResponsiveChartContainerPro.tsx similarity index 88% rename from packages/x-charts-pro/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx rename to packages/x-charts-pro/src/ResponsiveChartContainerPro/ResponsiveChartContainerPro.tsx index 5789814225c0..c508e7209514 100644 --- a/packages/x-charts-pro/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx +++ b/packages/x-charts-pro/src/ResponsiveChartContainerPro/ResponsiveChartContainerPro.tsx @@ -1,46 +1,17 @@ import * as React from 'react'; import PropTypes from 'prop-types'; -import { styled } from '@mui/material/styles'; import { useLicenseVerifier, Watermark } from '@mui/x-license'; -import { ChartContainer, ChartContainerProps } from '@mui/x-charts/ChartContainer'; -import { useChartContainerDimensions } from './useChartContainerDimensions'; +import { ChartContainer } from '@mui/x-charts/ChartContainer'; +import { ResponsiveChartContainerProps } from '@mui/x-charts/ResponsiveChartContainer'; +import { ResizableContainer, useChartContainerDimensions } from '@mui/x-charts/internals'; import { getReleaseInfo } from '../internals/utils/releaseInfo'; -export interface ResponsiveChartContainerProps - extends Omit { - /** - * The width of the chart in px. If not defined, it takes the width of the parent element. - */ - width?: number; - /** - * The height of the chart in px. If not defined, it takes the height of the parent element. - */ - height?: number; -} - -const ResizableContainer = styled('div', { - name: 'MuiResponsiveChart', - slot: 'Container', -})<{ ownerState: Pick }>(({ ownerState }) => ({ - width: ownerState.width ?? '100%', - height: ownerState.height ?? '100%', - display: 'flex', - position: 'relative', - flexGrow: 1, - flexDirection: 'column', - alignItems: 'center', - justifyContent: 'center', - overflow: 'hidden', - '&>svg': { - width: '100%', - height: '100%', - }, -})); +export interface ResponsiveChartContainerProProps extends ResponsiveChartContainerProps {} const releaseInfo = getReleaseInfo(); -const ResponsiveChartContainer = React.forwardRef(function ResponsiveChartContainer( - props: ResponsiveChartContainerProps, +const ResponsiveChartContainerPro = React.forwardRef(function ResponsiveChartContainerPro( + props: ResponsiveChartContainerProProps, ref, ) { const { width: inWidth, height: inHeight, ...other } = props; @@ -58,7 +29,7 @@ const ResponsiveChartContainer = React.forwardRef(function ResponsiveChartContai ); }); -ResponsiveChartContainer.propTypes = { +ResponsiveChartContainerPro.propTypes = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit the TypeScript types and run "pnpm proptypes" | @@ -281,4 +252,4 @@ ResponsiveChartContainer.propTypes = { ), } as any; -export { ResponsiveChartContainer }; +export { ResponsiveChartContainerPro }; diff --git a/packages/x-charts-pro/src/ResponsiveChartContainerPro/index.ts b/packages/x-charts-pro/src/ResponsiveChartContainerPro/index.ts new file mode 100644 index 000000000000..4f60ae3dd54b --- /dev/null +++ b/packages/x-charts-pro/src/ResponsiveChartContainerPro/index.ts @@ -0,0 +1 @@ +export * from './ResponsiveChartContainerPro'; diff --git a/packages/x-charts-pro/src/index.ts b/packages/x-charts-pro/src/index.ts index a5579c0307c6..6aa3762c874e 100644 --- a/packages/x-charts-pro/src/index.ts +++ b/packages/x-charts-pro/src/index.ts @@ -22,6 +22,5 @@ export * from '@mui/x-charts/ScatterChart'; export * from '@mui/x-charts/SparkLineChart'; export * from '@mui/x-charts/Gauge'; export * from '@mui/x-charts/ChartsSurface'; -export * from '@mui/x-charts/ChartContainer'; -export * from './ResponsiveChartContainer'; +export * from './ResponsiveChartContainerPro'; diff --git a/packages/x-charts/src/ResponsiveChartContainer/ResizableContainer.tsx b/packages/x-charts/src/ResponsiveChartContainer/ResizableContainer.tsx new file mode 100644 index 000000000000..2ea1f0f81c9a --- /dev/null +++ b/packages/x-charts/src/ResponsiveChartContainer/ResizableContainer.tsx @@ -0,0 +1,26 @@ +import { styled } from '@mui/material/styles'; +import type { ResponsiveChartContainerProps } from './ResponsiveChartContainer'; + +/** + * Wrapping div that take the shape of its parent. + * + * @ignore - do not document. + */ +export const ResizableContainer = styled('div', { + name: 'MuiResponsiveChart', + slot: 'Container', +})<{ ownerState: Pick }>(({ ownerState }) => ({ + width: ownerState.width ?? '100%', + height: ownerState.height ?? '100%', + display: 'flex', + position: 'relative', + flexGrow: 1, + flexDirection: 'column', + alignItems: 'center', + justifyContent: 'center', + overflow: 'hidden', + '&>svg': { + width: '100%', + height: '100%', + }, +})); diff --git a/packages/x-charts/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx b/packages/x-charts/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx index d58ef1b33fad..52616f191ce2 100644 --- a/packages/x-charts/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx +++ b/packages/x-charts/src/ResponsiveChartContainer/ResponsiveChartContainer.tsx @@ -1,8 +1,8 @@ import * as React from 'react'; import PropTypes from 'prop-types'; -import { styled } from '@mui/material/styles'; import { ChartContainer, ChartContainerProps } from '../ChartContainer'; import { useChartContainerDimensions } from './useChartContainerDimensions'; +import { ResizableContainer } from './ResizableContainer'; export interface ResponsiveChartContainerProps extends Omit { @@ -16,25 +16,6 @@ export interface ResponsiveChartContainerProps height?: number; } -const ResizableContainer = styled('div', { - name: 'MuiResponsiveChart', - slot: 'Container', -})<{ ownerState: Pick }>(({ ownerState }) => ({ - width: ownerState.width ?? '100%', - height: ownerState.height ?? '100%', - display: 'flex', - position: 'relative', - flexGrow: 1, - flexDirection: 'column', - alignItems: 'center', - justifyContent: 'center', - overflow: 'hidden', - '&>svg': { - width: '100%', - height: '100%', - }, -})); - const ResponsiveChartContainer = React.forwardRef(function ResponsiveChartContainer( props: ResponsiveChartContainerProps, ref, diff --git a/packages/x-charts/src/internals/index.ts b/packages/x-charts/src/internals/index.ts index b146ce17ee53..c7f28ec8588c 100644 --- a/packages/x-charts/src/internals/index.ts +++ b/packages/x-charts/src/internals/index.ts @@ -1 +1,4 @@ export * from './configInit'; + +export * from '../ResponsiveChartContainer/useChartContainerDimensions'; +export * from '../ResponsiveChartContainer/ResizableContainer';