keep charts responsive when the viewport resizes

The chart canvas in src/components/charts/Chart.tsx was rendered
directly inside the Box wrapper. Chart.js writes inline pixel sizes
onto the canvas, and while the canvas lives in the normal flow that
pixel width propagates up as min and max content through every flex
parent and into the surrounding CSS Grid track on the Tabs panel.
The track therefore stayed at whatever width the canvas had when the
page first loaded, and the chart could only grow on resize, never
shrink, until the user reloaded.

Wrap the canvas in a position-relative div and position the canvas
absolutely. Out-of-flow elements do not contribute to ancestor
intrinsic sizing, so the wrapper now takes its size purely from the
parent layout. Chart.js' ResizeObserver picks up the wrapper size and
resizes the canvas to fit, in both directions, without a reload.

Verified in playwright with resize 1280 to 800 (canvas 925 to 699)
and 800 to 1400 (canvas 699 to 1117), both without reload, and that
the click-to-toggle legend, the focusLabel hover behaviour, and the
website overview / revenue charts that share this component all
still render and update normally.
This commit is contained in:
Yan committed 2026-05-08 10:16:58 -04:00
1 parent a9508e7aae
commit cdc29a812f
1 file changed
+14 -1
+14 -1
View File
@@ -122,7 +122,20 @@ export function Chart({
return (
<Column gap="6">
<Box {...props}>
<canvas ref={canvas} />
{/*
Position the canvas absolutely inside a relative-positioned
wrapper. Chart.js writes inline pixel sizes onto the canvas, and
while it lives in the normal flow that pixel width propagates up
as min/max-content through every flex parent into the surrounding
CSS Grid track, pinning the chart's column at its widest measured
size and only resetting on a full page reload. Taking the canvas
out of flow with position: absolute breaks that propagation; the
wrapper sizes purely from its parent (width: 100%, height: 100%)
and Chart.js' ResizeObserver picks up viewport changes.
*/}
<div style={{ position: 'relative', width: '100%', height: '100%' }}>
<canvas ref={canvas} style={{ position: 'absolute', top: 0, left: 0 }} />
</div>
</Box>
<Legend items={legendItems} onClick={handleLegendClick} />
</Column>