From 326a0086b7ee96da943e25dd102dbd6c5729b3a3 Mon Sep 17 00:00:00 2001 From: Evan Yang Date: Sat, 7 Mar 2026 17:46:37 +0800 Subject: [PATCH] Fix chart container sizing warnings --- ui/components/ui/chart.tsx | 70 +++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/ui/components/ui/chart.tsx b/ui/components/ui/chart.tsx index 16f3ded3..329e1cb6 100644 --- a/ui/components/ui/chart.tsx +++ b/ui/components/ui/chart.tsx @@ -48,22 +48,84 @@ function ChartContainer({ }) { const uniqueId = React.useId(); const chartId = `chart-${id || uniqueId.replace(/:/g, '')}`; + const containerRef = React.useRef(null); + const [containerSize, setContainerSize] = React.useState({ + width: 0, + height: 0, + }); + + React.useLayoutEffect(() => { + const element = containerRef.current; + if (!element) { + return; + } + + const updateSize = (width: number, height: number) => { + setContainerSize((current) => { + if (current.width === width && current.height === height) { + return current; + } + return { width, height }; + }); + }; + + const measure = () => { + const rect = element.getBoundingClientRect(); + updateSize(rect.width, rect.height); + }; + + measure(); + + if (typeof ResizeObserver === 'undefined') { + return; + } + + const observer = new ResizeObserver((entries) => { + const entry = entries[0]; + if (!entry) { + return; + } + + updateSize(entry.contentRect.width, entry.contentRect.height); + }); + + observer.observe(element); + return () => observer.disconnect(); + }, []); + + const hasUsableSize = containerSize.width > 0 && containerSize.height > 0; + const initialDimension = React.useMemo( + () => ({ + width: Math.max(1, Math.round(containerSize.width)), + height: Math.max(1, Math.round(containerSize.height)), + }), + [containerSize.height, containerSize.width] + ); return (
- - {children} - + {hasUsableSize ? ( + + {children} + + ) : null}
);