:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(charts): keep charts painted when their container collapses (#113847)
Anders343019018549
diff --git a/packages/quill/packages/charts/src/core/hooks/useChartCanvas.test.tsx b/packages/quill/packages/charts/src/core/hooks/useChartCanvas.test.tsxindex 0419accc0f41..9a0a06c3ba61 100644--- a/packages/quill/packages/charts/src/core/hooks/useChartCanvas.test.tsx+++ b/packages/quill/packages/charts/src/core/hooks/useChartCanvas.test.tsx@@ -30,7 +30,54 @@ function renderChart(drawStatic: (args: ChartDrawArgs) => void): HTMLCanvasElemereturn chart.canvas}+let resizeObserverCallback: ResizeObserverCallback = () => {}++function reportWrapperSize(width: number, height: number): void {+ const entry = { borderBoxSize: [{ inlineSize: width, blockSize: height }] } as unknown as ResizeObserverEntry+ act(() => resizeObserverCallback([entry], {} as ResizeObserver))+}+describe('useChartCanvas', () => {+ let resizeObserverSpy: jest.SpyInstance++ beforeEach(() => {+ resizeObserverSpy = jest.spyOn(globalThis, 'ResizeObserver').mockImplementation((callback) => {+ resizeObserverCallback = callback+ return { observe: () => {}, unobserve: () => {}, disconnect: () => {} }+ })+ })++ afterEach(() => {+ resizeObserverSpy.mockRestore()+ })++ it('keeps the painted canvas when the wrapper collapses to zero size and comes back', () => {+ const drawStatic = jest.fn()+ const canvas = renderChart(drawStatic)+ drawStatic.mockClear()++ reportWrapperSize(0, 0)+ expect(canvas.width).toBe(mockRect.width)++ reportWrapperSize(mockRect.width, mockRect.height)+ expect(drawStatic).not.toHaveBeenCalled()+ })++ it('repaints a context restored while the wrapper had zero size once the size comes back', () => {+ const drawStatic = jest.fn()+ const canvas = renderChart(drawStatic)+ drawStatic.mockClear()++ jest.mocked(Element.prototype.getBoundingClientRect).mockReturnValueOnce({ ...mockRect, width: 0, height: 0 })+ act(() => {+ canvas.dispatchEvent(new Event('contextrestored'))+ })+ expect(drawStatic).not.toHaveBeenCalled()++ reportWrapperSize(mockRect.width, mockRect.height)+ expect(drawStatic).toHaveBeenCalled()+ })+it('repaints against a restored backing store after the 2D context is lost', () => {const drawStatic = jest.fn()const canvas = renderChart(drawStatic)diff --git a/packages/quill/packages/charts/src/core/hooks/useChartCanvas.ts b/packages/quill/packages/charts/src/core/hooks/useChartCanvas.tsindex 78e3456f0489..981692e31682 100644--- a/packages/quill/packages/charts/src/core/hooks/useChartCanvas.ts+++ b/packages/quill/packages/charts/src/core/hooks/useChartCanvas.ts@@ -44,6 +44,7 @@ export function useChartCanvas(options: UseChartCanvasOptions): UseChartCanvasReif (!wrapper) {return}+ let deferredRepaint = false// `forceRepaint` publishes fresh state even when nothing moved — for a restored context,// whose bitmap came back blank while every value stayed identical.@@ -66,12 +67,21 @@ export function useChartCanvas(options: UseChartCanvasOptions): UseChartCanvasRe}const rect = layoutRect ?? wrapper.getBoundingClientRect()+ // A sub-pixel wrapper (a `display: none` ancestor, Chromium shrinking the viewport for a tall+ // screenshot) has nothing to draw, and resizing to it would wipe the bitmap. Keep the last frame,+ // and replay a requested repaint (a restored context) once a real size arrives.+ if (rect.width < 1 || rect.height < 1) {+ deferredRepaint ||= forceRepaint+ return+ }+ const repaint = forceRepaint || deferredRepaint+ deferredRepaint = falserectRef.current = rectconst dpr = window.devicePixelRatio || 1const staticWiped = syncCanvasSize(canvas, rect, dpr)const overlayWiped = syncCanvasSize(overlayCanvas, rect, dpr)- if (staticWiped || forceRepaint) {+ if (staticWiped || repaint) {markPaintPending(canvas)}@@ -83,7 +93,7 @@ export function useChartCanvas(options: UseChartCanvasOptions): UseChartCanvasReconst next = buildDimensions(rect, marginsRef.current)setCanvasState((prev) =>prev &&- !forceRepaint &&+ !repaint &&!staticWiped &&!overlayWiped &&prev.ctx === context &&diff --git a/packages/quill/packages/charts/src/docs/axes.md b/packages/quill/packages/charts/src/docs/axes.mdindex 812282bfcddd..5e9e1c24413a 100644--- a/packages/quill/packages/charts/src/docs/axes.md+++ b/packages/quill/packages/charts/src/docs/axes.md@@ -62,6 +62,7 @@ A single object stays single-axis.## Margins and sizing- Charts fill their container and need a parent with real dimensions. A zero-height flex child renders nothing. Give the wrapper an explicit height (`h-64`, or `flex-1` in a sized column). `Sparkline` alone takes `height` and `width` props.+- A wrapper that measures under one pixel wide or high does not resize the chart. `useChartCanvas` keeps the last painted frame and waits for the next real size, so a chart whose container collapses for a moment (an ancestor set to `display: none`, Chromium capturing a screenshot taller than the viewport) does not go blank or lay out again.- `ChartConfig.margins` overrides only the sides it sets. A side left `undefined` keeps the computed margin, so an object built conditionally (`{ top: reserveOrUndefined }`) is safe. `SlopeChart` merges consumer margins over its computed label gutters with the same rule (`applyMarginOverride`). Pass a module-level constant.## When the plot is blank