Skip to content

Commit a771fd3

Browse files
authored
fix(Charts): separate size monitoring for height and width (#417)
1 parent 31f8699 commit a771fd3

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

packages/charts/src/components/BarChart/demo.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default {
4040
};
4141

4242
function Demo() {
43-
const [full, setFull] = useState(true);
43+
const [full, setFull] = useState(false);
4444

4545
return (
4646
<div

packages/charts/src/hooks/useSizeMonitor.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { useCallback, useEffect, useRef, useState } from 'react';
22

33
export const useSizeMonitor = (props, container) => {
44
const { height: heightProp, width: widthProp, minHeight, minWidth } = props;
5-
const [height, setHeight] = useState(null);
6-
const [width, setWidth] = useState(null);
5+
const [sizeState, setSizeState] = useState({
6+
height: null,
7+
width: null
8+
});
9+
const observer = useRef(null);
710

8-
const enableSizeMonitor = typeof heightProp === 'string' || typeof widthProp === 'string';
11+
const dynamicHeightProp = typeof heightProp === 'string';
12+
const dynamicWidthProp = typeof heightProp === 'string';
13+
const enableSizeMonitor = dynamicHeightProp || dynamicWidthProp;
914

1015
const recalculateSize = useCallback(
1116
(e?) => {
@@ -20,30 +25,32 @@ export const useSizeMonitor = (props, container) => {
2025
clientRectWidth = e[0].contentRect.width;
2126
}
2227

23-
// console.log(props);
24-
25-
setHeight(Math.max(minHeight, clientRectHeight));
26-
setWidth(Math.max(minWidth, clientRectWidth));
28+
if (dynamicHeightProp || dynamicWidthProp) {
29+
setSizeState((state) => ({
30+
...state,
31+
...(dynamicHeightProp && { height: Math.max(minHeight, clientRectHeight) }),
32+
...(dynamicWidthProp && { width: Math.max(minWidth, clientRectWidth) })
33+
}));
34+
}
2735
},
28-
[container.current, setHeight, setWidth]
36+
[setSizeState, minWidth, minHeight, dynamicHeightProp, dynamicWidthProp]
2937
);
3038

31-
const observer = useRef(new ResizeObserver(recalculateSize));
32-
33-
// @ts-ignore
3439
useEffect(() => {
3540
if (enableSizeMonitor && container.current) {
41+
// @ts-ignore
42+
observer.current = new ResizeObserver(recalculateSize);
3643
observer.current.observe(container.current);
37-
38-
recalculateSize();
39-
return () => {
40-
observer.current.disconnect();
41-
};
4244
}
43-
}, []);
45+
return () => {
46+
if (observer.current) {
47+
observer.current.disconnect();
48+
}
49+
};
50+
}, [recalculateSize]);
4451

4552
return {
46-
height: enableSizeMonitor ? height : heightProp,
47-
width: enableSizeMonitor ? width : widthProp
53+
height: dynamicHeightProp ? sizeState.height : heightProp,
54+
width: dynamicWidthProp ? sizeState.width : widthProp
4855
};
4956
};

packages/charts/src/internal/withChartContainer.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ export const withChartContainer = (Component: ComponentType<any>) => {
6464
const chartHeight = useMemo(() => (noLegend ? height : height - 60), [noLegend, height]);
6565

6666
const chartWrapperStyles: CSSProperties = useMemo(
67-
() => ({ position: 'relative', height: `${chartHeight}px`, width: `${width}px` }),
67+
() => {
68+
let innerChartWrapperStyles : CSSProperties = {
69+
position: 'relative',
70+
height: chartHeight >= 0 ? `${chartHeight}px` : 'auto',
71+
width: width ? `${width}px` : 'auto'
72+
};
73+
74+
return innerChartWrapperStyles
75+
},
6876
[chartHeight, width]
6977
);
7078

0 commit comments

Comments
 (0)