Skip to content

fix(useSizeMonitor): separate size monitoring for height and width in Charts #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/charts/src/components/BarChart/demo.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default {
};

function Demo() {
const [full, setFull] = useState(true);
const [full, setFull] = useState(false);

return (
<div
Expand Down
45 changes: 26 additions & 19 deletions packages/charts/src/hooks/useSizeMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { useCallback, useEffect, useRef, useState } from 'react';

export const useSizeMonitor = (props, container) => {
const { height: heightProp, width: widthProp, minHeight, minWidth } = props;
const [height, setHeight] = useState(null);
const [width, setWidth] = useState(null);
const [sizeState, setSizeState] = useState({
height: null,
width: null
});
const observer = useRef(null);

const enableSizeMonitor = typeof heightProp === 'string' || typeof widthProp === 'string';
const dynamicHeightProp = typeof heightProp === 'string';
const dynamicWidthProp = typeof heightProp === 'string';
const enableSizeMonitor = dynamicHeightProp || dynamicWidthProp;

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

// console.log(props);

setHeight(Math.max(minHeight, clientRectHeight));
setWidth(Math.max(minWidth, clientRectWidth));
if (dynamicHeightProp || dynamicWidthProp) {
setSizeState((state) => ({
...state,
...(dynamicHeightProp && { height: Math.max(minHeight, clientRectHeight) }),
...(dynamicWidthProp && { width: Math.max(minWidth, clientRectWidth) })
}));
}
},
[container.current, setHeight, setWidth]
[setSizeState, minWidth, minHeight, dynamicHeightProp, dynamicWidthProp]
);

const observer = useRef(new ResizeObserver(recalculateSize));

// @ts-ignore
useEffect(() => {
if (enableSizeMonitor && container.current) {
// @ts-ignore
observer.current = new ResizeObserver(recalculateSize);
observer.current.observe(container.current);

recalculateSize();
return () => {
observer.current.disconnect();
};
}
}, []);
return () => {
if (observer.current) {
observer.current.disconnect();
}
};
}, [recalculateSize]);

return {
height: enableSizeMonitor ? height : heightProp,
width: enableSizeMonitor ? width : widthProp
height: dynamicHeightProp ? sizeState.height : heightProp,
width: dynamicWidthProp ? sizeState.width : widthProp
};
};
10 changes: 9 additions & 1 deletion packages/charts/src/internal/withChartContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ export const withChartContainer = (Component: ComponentType<any>) => {
const chartHeight = useMemo(() => (noLegend ? height : height - 60), [noLegend, height]);

const chartWrapperStyles: CSSProperties = useMemo(
() => ({ position: 'relative', height: `${chartHeight}px`, width: `${width}px` }),
() => {
let innerChartWrapperStyles : CSSProperties = {
position: 'relative',
height: chartHeight >= 0 ? `${chartHeight}px` : 'auto',
width: width ? `${width}px` : 'auto'
};

return innerChartWrapperStyles
},
[chartHeight, width]
);

Expand Down