- {!hasValidDimensions(chartDimensions) ? null : (
+ {containerBounds == null || !hasValidBounds(containerBounds) ? null : (
{cloneElement<{
- dimensions: Dimensions;
+ containerBounds: BoundingRect;
}>(children, {
- dimensions: chartDimensions!,
+ containerBounds,
})}
@@ -174,10 +173,6 @@ export function ChartDimensions({
);
}
-function hasValidDimensions(chartDimensions: Dimensions | null) {
- if (chartDimensions == null) {
- return false;
- }
-
- return chartDimensions.width > 0 && chartDimensions.height > 0;
+function hasValidBounds(containerBounds: BoundingRect) {
+ return containerBounds.width > 0 && containerBounds.height > 0;
}
diff --git a/packages/polaris-viz/src/components/ChartContainer/components/ContainerBounds/index.ts b/packages/polaris-viz/src/components/ChartContainer/components/ContainerBounds/index.ts
new file mode 100644
index 000000000..9acf08087
--- /dev/null
+++ b/packages/polaris-viz/src/components/ChartContainer/components/ContainerBounds/index.ts
@@ -0,0 +1 @@
+export {ContainerBounds} from './ContainerBounds';
diff --git a/packages/polaris-viz/src/components/ChartContainer/components/index.ts b/packages/polaris-viz/src/components/ChartContainer/components/index.ts
index 9632f62c3..5ab6a5096 100644
--- a/packages/polaris-viz/src/components/ChartContainer/components/index.ts
+++ b/packages/polaris-viz/src/components/ChartContainer/components/index.ts
@@ -1 +1 @@
-export {ChartDimensions} from './ChartDimensions/';
+export {ContainerBounds} from './ContainerBounds/';
diff --git a/packages/polaris-viz/src/components/ChartErrorBoundary/ChartErrorBoundary.tsx b/packages/polaris-viz/src/components/ChartErrorBoundary/ChartErrorBoundary.tsx
index d46edba07..be9c7512b 100644
--- a/packages/polaris-viz/src/components/ChartErrorBoundary/ChartErrorBoundary.tsx
+++ b/packages/polaris-viz/src/components/ChartErrorBoundary/ChartErrorBoundary.tsx
@@ -1,7 +1,7 @@
import type {
+ BoundingRect,
DataGroup,
DataSeries,
- Dimensions,
ErrorBoundaryResponse,
} from '@shopify/polaris-viz-core';
import {ChartState} from '@shopify/polaris-viz-core';
@@ -15,9 +15,9 @@ import {checkForMismatchedData} from './utilities/checkForMismatchedData';
interface ErrorBoundaryProps {
data: DataSeries[] | DataGroup[];
- dimensions: Dimensions;
type: SkeletonType;
children?: ReactNode;
+ containerBounds?: BoundingRect;
onError?: ErrorBoundaryResponse;
}
@@ -60,7 +60,7 @@ export class ChartErrorBoundary extends Component<
);
}
diff --git a/packages/polaris-viz/src/components/ChartErrorBoundary/tests/ChartErrorBoundary.test.tsx b/packages/polaris-viz/src/components/ChartErrorBoundary/tests/ChartErrorBoundary.test.tsx
index 6e24c8e07..a3a34c808 100644
--- a/packages/polaris-viz/src/components/ChartErrorBoundary/tests/ChartErrorBoundary.test.tsx
+++ b/packages/polaris-viz/src/components/ChartErrorBoundary/tests/ChartErrorBoundary.test.tsx
@@ -8,7 +8,7 @@ import {BarChart} from '../../BarChart';
const MOCK_PROPS = {
data: [],
- dimensions: {
+ containerDimensions: {
width: 900,
height: 400,
},
diff --git a/packages/polaris-viz/src/components/ChartSkeleton/ChartSkeleton.tsx b/packages/polaris-viz/src/components/ChartSkeleton/ChartSkeleton.tsx
index 10770e29b..a1248449f 100644
--- a/packages/polaris-viz/src/components/ChartSkeleton/ChartSkeleton.tsx
+++ b/packages/polaris-viz/src/components/ChartSkeleton/ChartSkeleton.tsx
@@ -1,4 +1,4 @@
-import type {Dimensions} from '@shopify/polaris-viz-core';
+import type {BoundingRect} from '@shopify/polaris-viz-core';
import {ChartState, useTheme} from '@shopify/polaris-viz-core';
import type {Size} from '../SimpleNormalizedChart';
@@ -23,7 +23,9 @@ export type SkeletonType =
| 'SimpleNormalized';
interface ChartSkeletonProps {
- dimensions?: Dimensions;
+ // containerBounds is optional because it's passed down
+ // from ContainerBounds with cloneElement.
+ containerBounds?: BoundingRect;
errorText?: string;
state?: ChartState;
theme?: string;
@@ -55,7 +57,7 @@ export interface SimpleNormalizedSkeletonProps extends ChartSkeletonProps {
size?: Size;
}
-type Props =
+export type Props =
| DefaultSkeletonProps
| DonutSkeletonProps
| FunnelSkeletonProps
@@ -65,7 +67,7 @@ type Props =
export function ChartSkeleton(props: Props) {
const {
- dimensions,
+ containerBounds,
errorText = 'Could not load the chart',
state = ChartState.Loading,
theme,
@@ -76,17 +78,17 @@ export function ChartSkeleton(props: Props) {
chartContainer: {backgroundColor},
} = useTheme(theme);
- const {width, height} = dimensions || {width: 0, height: 0};
+ const containerDimensions = {
+ height: containerBounds?.height ?? 0,
+ width: containerBounds?.width ?? 0,
+ };
const SkeletonMarkup = () => {
switch (type) {
case 'Donut':
return (
@@ -94,10 +96,7 @@ export function ChartSkeleton(props: Props) {
case 'Funnel':
return (
@@ -105,10 +104,7 @@ export function ChartSkeleton(props: Props) {
case 'SimpleBar':
return (
@@ -117,10 +113,7 @@ export function ChartSkeleton(props: Props) {
const {showLegend = true, size = 'small'} = props;
return (
@@ -143,10 +133,7 @@ export function ChartSkeleton(props: Props) {
default:
return (
@@ -154,7 +141,7 @@ export function ChartSkeleton(props: Props) {
}
};
- if (width === 0) return null;
+ if (containerDimensions.width === 0) return null;
return (
@@ -162,8 +149,8 @@ export function ChartSkeleton(props: Props) {
{state === ChartState.Loading && (
)}
diff --git a/packages/polaris-viz/src/components/ChartSkeleton/components/DonutSkeleton/DonutSkeleton.tsx b/packages/polaris-viz/src/components/ChartSkeleton/components/DonutSkeleton/DonutSkeleton.tsx
index f1d14d497..10afce077 100644
--- a/packages/polaris-viz/src/components/ChartSkeleton/components/DonutSkeleton/DonutSkeleton.tsx
+++ b/packages/polaris-viz/src/components/ChartSkeleton/components/DonutSkeleton/DonutSkeleton.tsx
@@ -16,13 +16,13 @@ const RADIUS_PADDING = 20;
const SECONDARY_DELAY = 200;
interface Props {
- dimensions: Dimensions;
+ containerDimensions: Dimensions;
state: ChartState;
errorText: string;
}
export function DonutSkeleton({
- dimensions: {height, width},
+ containerDimensions: {height, width},
state,
errorText,
}: Props) {
diff --git a/packages/polaris-viz/src/components/ChartSkeleton/components/FunnelSkeleton/FunnelSkeleton.tsx b/packages/polaris-viz/src/components/ChartSkeleton/components/FunnelSkeleton/FunnelSkeleton.tsx
index f3f6520bc..9dd521030 100644
--- a/packages/polaris-viz/src/components/ChartSkeleton/components/FunnelSkeleton/FunnelSkeleton.tsx
+++ b/packages/polaris-viz/src/components/ChartSkeleton/components/FunnelSkeleton/FunnelSkeleton.tsx
@@ -11,13 +11,13 @@ import {Bar} from '../../../shared';
import {ErrorText} from '../ErrorText';
interface Props {
- dimensions: Dimensions;
+ containerDimensions: Dimensions;
state: ChartState;
errorText: string;
}
-export function FunnelSkeleton({dimensions, state, errorText}: Props) {
- const {width, height} = dimensions;
+export function FunnelSkeleton({containerDimensions, state, errorText}: Props) {
+ const {width, height} = containerDimensions;
const {
grid: {color: gridColor},
diff --git a/packages/polaris-viz/src/components/ChartSkeleton/components/GridSkeleton/GridSkeleton.tsx b/packages/polaris-viz/src/components/ChartSkeleton/components/GridSkeleton/GridSkeleton.tsx
index 49580c41c..d08db5b7d 100644
--- a/packages/polaris-viz/src/components/ChartSkeleton/components/GridSkeleton/GridSkeleton.tsx
+++ b/packages/polaris-viz/src/components/ChartSkeleton/components/GridSkeleton/GridSkeleton.tsx
@@ -16,13 +16,13 @@ const INITIAL_DELAY = 200;
const NUMBER_OF_BRICKS = 5;
interface Props {
- dimensions: Dimensions;
+ containerDimensions: Dimensions;
state: ChartState;
errorText: string;
}
-export function GridSkeleton({dimensions, state, errorText}: Props) {
- const {width, height} = dimensions;
+export function GridSkeleton({containerDimensions, state, errorText}: Props) {
+ const {width, height} = containerDimensions;
const {
chartContainer: {padding},
diff --git a/packages/polaris-viz/src/components/ChartSkeleton/components/SimpleBarSkeleton/SimpleBarSkeleton.tsx b/packages/polaris-viz/src/components/ChartSkeleton/components/SimpleBarSkeleton/SimpleBarSkeleton.tsx
index 7fd0f8439..092ab2e7b 100644
--- a/packages/polaris-viz/src/components/ChartSkeleton/components/SimpleBarSkeleton/SimpleBarSkeleton.tsx
+++ b/packages/polaris-viz/src/components/ChartSkeleton/components/SimpleBarSkeleton/SimpleBarSkeleton.tsx
@@ -7,13 +7,17 @@ import {ErrorText} from '../ErrorText';
import styles from './SimpleBarSkeleton.scss';
interface Props {
- dimensions: Dimensions;
+ containerDimensions: Dimensions;
state: ChartState;
errorText: string;
}
-export function SimpleBarSkeleton({dimensions, state, errorText}: Props) {
- const {width, height} = dimensions;
+export function SimpleBarSkeleton({
+ containerDimensions,
+ state,
+ errorText,
+}: Props) {
+ const {width, height} = containerDimensions;
const {
grid: {color: gridColor},
diff --git a/packages/polaris-viz/src/components/ChartSkeleton/components/SimpleNormalizedSkeleton/SimpleNormalizedSkeleton.tsx b/packages/polaris-viz/src/components/ChartSkeleton/components/SimpleNormalizedSkeleton/SimpleNormalizedSkeleton.tsx
index 8ad6f4df4..4549d6dfa 100644
--- a/packages/polaris-viz/src/components/ChartSkeleton/components/SimpleNormalizedSkeleton/SimpleNormalizedSkeleton.tsx
+++ b/packages/polaris-viz/src/components/ChartSkeleton/components/SimpleNormalizedSkeleton/SimpleNormalizedSkeleton.tsx
@@ -14,7 +14,7 @@ const SIZE_TO_PX = {
};
interface Props {
- dimensions: Dimensions;
+ containerDimensions: Dimensions;
errorText: string;
showLegend: boolean;
size: Size;
@@ -22,13 +22,13 @@ interface Props {
}
export function SimpleNormalizedSkeleton({
- dimensions,
+ containerDimensions,
errorText,
showLegend,
size,
state,
}: Props) {
- const {width, height} = dimensions;
+ const {width, height} = containerDimensions;
const {
grid: {color: gridColor},
diff --git a/packages/polaris-viz/src/components/ChartSkeleton/components/SparkSkeleton/SparkSkeleton.tsx b/packages/polaris-viz/src/components/ChartSkeleton/components/SparkSkeleton/SparkSkeleton.tsx
index 6d0389659..ff6a4759f 100644
--- a/packages/polaris-viz/src/components/ChartSkeleton/components/SparkSkeleton/SparkSkeleton.tsx
+++ b/packages/polaris-viz/src/components/ChartSkeleton/components/SparkSkeleton/SparkSkeleton.tsx
@@ -8,13 +8,13 @@ import {
import {ErrorText} from '../ErrorText';
interface Props {
- dimensions: Dimensions;
+ containerDimensions: Dimensions;
state: ChartState;
errorText: string;
}
-export function SparkSkeleton({dimensions, state, errorText}: Props) {
- const {width, height} = dimensions;
+export function SparkSkeleton({containerDimensions, state, errorText}: Props) {
+ const {width, height} = containerDimensions;
const {
grid: {color: gridColor},
diff --git a/packages/polaris-viz/src/components/ChartSkeleton/tests/ChartSkeleton.test.tsx b/packages/polaris-viz/src/components/ChartSkeleton/tests/ChartSkeleton.test.tsx
index 809102059..9c0dea278 100644
--- a/packages/polaris-viz/src/components/ChartSkeleton/tests/ChartSkeleton.test.tsx
+++ b/packages/polaris-viz/src/components/ChartSkeleton/tests/ChartSkeleton.test.tsx
@@ -1,5 +1,6 @@
import {ChartState} from '@shopify/polaris-viz-core';
+import type {Props} from '../ChartSkeleton';
import {ChartSkeleton} from '../ChartSkeleton';
import {
mountWithProvider,
@@ -7,10 +8,12 @@ import {
} from '../../../test-utilities/mountWithProvider';
import {Shimmer} from '../components';
-const MOCK_PROPS = {
- dimensions: {
+const MOCK_PROPS: Props = {
+ containerBounds: {
width: 900,
height: 400,
+ x: 0,
+ y: 0,
},
};
diff --git a/packages/polaris-viz/src/components/ComboChart/Chart.tsx b/packages/polaris-viz/src/components/ComboChart/Chart.tsx
index 47694ff28..7627c8462 100644
--- a/packages/polaris-viz/src/components/ComboChart/Chart.tsx
+++ b/packages/polaris-viz/src/components/ComboChart/Chart.tsx
@@ -9,7 +9,6 @@ import {
LINE_HEIGHT,
} from '@shopify/polaris-viz-core';
import type {
- Dimensions,
DataGroup,
BoundingRect,
XAxisOptions,
@@ -61,14 +60,14 @@ export interface ChartProps {
showLegend: boolean;
theme: string;
xAxisOptions: Required
;
- dimensions?: Dimensions;
+ containerBounds?: BoundingRect;
renderLegendContent?: RenderLegendContent;
}
export function Chart({
annotationsLookupTable,
data,
- dimensions,
+ containerBounds,
renderTooltipContent,
showLegend,
theme,
@@ -87,10 +86,15 @@ export function Chart({
const [activeIndex, setActiveIndex] = useState(null);
const [annotationsHeight, setAnnotationsHeight] = useState(0);
+ const containerDimensions = {
+ height: containerBounds?.height ?? 0,
+ width: containerBounds?.width ?? 0,
+ };
+
const {legend, setLegendDimensions, height, width} = useLegend({
colors,
data,
- dimensions,
+ containerDimensions,
showLegend,
seriesNameFormatter,
});
@@ -335,6 +339,7 @@ export function Chart({
{showLegend && (
null,
showLegend: false,
theme: 'Light',
diff --git a/packages/polaris-viz/src/components/Docs/stories/components/SampleComponents.tsx b/packages/polaris-viz/src/components/Docs/stories/components/SampleComponents.tsx
index 110088ea6..45074aaa7 100644
--- a/packages/polaris-viz/src/components/Docs/stories/components/SampleComponents.tsx
+++ b/packages/polaris-viz/src/components/Docs/stories/components/SampleComponents.tsx
@@ -242,7 +242,7 @@ export const SampleLegendContainer = ({theme} = {theme: 'Light'}) => {
},
],
showLegend: true,
- dimensions: {height: 0, width: 0},
+ containerDimensions: {height: 0, width: 0},
colors,
seriesNameFormatter: (value) => `${value}`,
});
@@ -254,6 +254,7 @@ export const SampleLegendContainer = ({theme} = {theme: 'Light'}) => {
colorVisionType=""
data={legend}
onDimensionChange={() => {}}
+ containerDimensions={{height: 0, width: 0}}
/>
diff --git a/packages/polaris-viz/src/components/DonutChart/Chart.tsx b/packages/polaris-viz/src/components/DonutChart/Chart.tsx
index da01f72de..e626d6784 100644
--- a/packages/polaris-viz/src/components/DonutChart/Chart.tsx
+++ b/packages/polaris-viz/src/components/DonutChart/Chart.tsx
@@ -13,9 +13,9 @@ import {
import type {
DataPoint,
DataSeries,
- Dimensions,
LabelFormatter,
Direction,
+ BoundingRect,
} from '@shopify/polaris-viz-core';
import {getAnimationDelayForItems} from '../../utilities/getAnimationDelayForItems';
@@ -55,7 +55,7 @@ export interface ChartProps {
theme: string;
accessibilityLabel?: string;
comparisonMetric?: ComparisonMetricProps;
- dimensions?: Dimensions;
+ containerBounds?: BoundingRect;
errorText?: string;
legendFullWidth?: boolean;
renderInnerValueContent?: RenderInnerValueContent;
@@ -74,7 +74,7 @@ export function Chart({
theme,
accessibilityLabel = '',
comparisonMetric,
- dimensions = {height: 0, width: 0},
+ containerBounds,
errorText,
legendFullWidth = false,
renderInnerValueContent,
@@ -100,13 +100,18 @@ export function Chart({
? 'vertical'
: 'horizontal';
+ const containerDimensions = {
+ height: containerBounds?.height ?? 0,
+ width: containerBounds?.width ?? 0,
+ };
+
const maxLegendWidth =
- legendDirection === 'vertical' ? dimensions.width / 2 : 0;
+ legendDirection === 'vertical' ? containerDimensions.width / 2 : 0;
const {height, width, legend, setLegendDimensions, isLegendMounted} =
useLegend({
data: [{series: data, shape: 'Bar'}],
- dimensions,
+ containerDimensions,
showLegend,
direction: legendDirection,
colors: seriesColor,
@@ -120,7 +125,7 @@ export function Chart({
useColorVisionEvents({
enabled: shouldUseColorVisionEvents,
- dimensions: {...dimensions, x: 0, y: 0},
+ containerDimensions,
});
useWatchColorVisionEvents({
@@ -188,7 +193,7 @@ export function Chart({