Skip to content

: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.

fix(storybook): wait for quill chart canvases to finish painting (#113112)

Julian Bez0c2d2eab914b
diff --git a/common/storybook/.storybook/test-runner.ts b/common/storybook/.storybook/test-runner.ts
index 5f5001d694ca..a3c484f87763 100644
--- a/common/storybook/.storybook/test-runner.ts
+++ b/common/storybook/.storybook/test-runner.ts
@@ -526,6 +526,30 @@ async function waitForPossiblyFramedSelector(page: Page, selector: string, timeo
await page.frameLocator(frameSelector.trim()).locator(innerSelector.trim()).first().waitFor({ timeout })
}
+/** A resize wipes a quill chart canvas before its repaint, and the body-size check cannot see a chart resizing inside a fixed-height card. */
+async function waitForChartCanvasesPainted(page: Page): Promise<void> {
+ await page
+ .waitForFunction(
+ () =>
+ new Promise<boolean>((resolve) => {
+ let settledFrames = 0
+ const check = (): void => {
+ const pending = document.querySelector('canvas[data-hog-charts-paint="pending"]')
+ settledFrames = pending ? 0 : settledFrames + 1
+ if (settledFrames >= 5) {
+ resolve(true)
+ return
+ }
+ requestAnimationFrame(check)
+ }
+ requestAnimationFrame(check)
+ }),
+ undefined,
+ { timeout: 3000 }
+ )
+ .catch(() => {})
+}
+
async function takeSnapshotWithTheme(
page: Page,
context: TestContext,
@@ -669,6 +693,8 @@ async function takeSnapshotWithTheme(
// final wait for any remaining renders
await page.waitForTimeout(1000)
+ await waitForChartCanvasesPainted(page)
+
// Do take the snapshot
await doTakeSnapshotWithTheme(page, context, browser, theme, storyContext)
}
@@ -762,6 +788,8 @@ async function expectStoryToMatchComponentSnapshot(
}
})
})
+ // Widening the root for a popover can resize a chart again.
+ await waitForChartCanvasesPainted(page)
await expectLocatorToMatchStorySnapshot(page.locator(targetSelector), context, browser, theme, {
omitBackground: true,
diff --git a/docs/published/handbook/engineering/conventions/frontend-coding.md b/docs/published/handbook/engineering/conventions/frontend-coding.md
index 164108902d09..f2d0b43e87c5 100644
--- a/docs/published/handbook/engineering/conventions/frontend-coding.md
+++ b/docs/published/handbook/engineering/conventions/frontend-coding.md
@@ -85,7 +85,7 @@ Every pattern below caused a real quarantine. Each one has a fix that removes th
##### What the runner already does
-`common/storybook/.storybook/test-runner.ts` turns off animations and transitions, makes lazy images eager and waits for them to decode, preloads fonts, waits for a known loader to disappear, waits for network idle, and sets the theme on `body[theme]` before each snapshot.
+`common/storybook/.storybook/test-runner.ts` turns off animations and transitions, makes lazy images eager and waits for them to decode, preloads fonts, waits for a known loader to disappear, waits for network idle, waits for every quill chart canvas to finish painting, and sets the theme on `body[theme]` before each snapshot.
Do not add your own waits for these.
The loader wait checks only the first element that matches a loader selector.
When a story shows more than one loader, set `waitForSelector` to content that renders after the last one.
diff --git a/packages/quill/packages/charts/AGENTS.md b/packages/quill/packages/charts/AGENTS.md
index 5bec405d2746..78ab064fdb48 100644
--- a/packages/quill/packages/charts/AGENTS.md
+++ b/packages/quill/packages/charts/AGENTS.md
@@ -38,6 +38,7 @@ const theme = useChartTheme() // reads CSS vars, tracks light/dark switches
- Omit `color` on a series to get palette assignment by index (preferred). An explicit `color` must be a concrete color (hex, rgb): line, area, and bar series hand it to the canvas unresolved, so resolve `var(--...)` in the host first. Only `Heatmap` and `ScatterChart` resolve `var()` themselves.
- The theme helpers carry the default chrome (faint dashed grid, stronger axis line, dashed crosshair), and `DEFAULT_CHART_CONFIG` carries the matching switches. Consumers opt out field by field (`showGrid: false`). Details: [docs/axes.md](./src/docs/axes.md).
- `theme.skipDraw` mounts the canvas without painting, for deterministic visual snapshots.
+- The static canvas carries `data-hog-charts-paint`: `pending` from a wipe or an input change until the next paint, then `done`. The Storybook runner waits for no `pending` canvas before a snapshot.
## Series shape
diff --git a/packages/quill/packages/charts/src/core/hooks/useChartCanvas.ts b/packages/quill/packages/charts/src/core/hooks/useChartCanvas.ts
index 5701020303e6..78e3456f0489 100644
--- a/packages/quill/packages/charts/src/core/hooks/useChartCanvas.ts
+++ b/packages/quill/packages/charts/src/core/hooks/useChartCanvas.ts
@@ -1,6 +1,7 @@
import { useEffect, useRef, useState } from 'react'
import { buildDimensions, sameDimensions, syncCanvasSize, type SizeRect } from '../canvas-size'
+import { markPaintPending } from '../paint-state'
import type { ChartDimensions, ChartMargins } from '../types'
import { useLatest } from './useLatest'
@@ -70,6 +71,9 @@ export function useChartCanvas(options: UseChartCanvasOptions): UseChartCanvasRe
const staticWiped = syncCanvasSize(canvas, rect, dpr)
const overlayWiped = syncCanvasSize(overlayCanvas, rect, dpr)
+ if (staticWiped || forceRepaint) {
+ markPaintPending(canvas)
+ }
// The draw loops key on `dimensions` *identity*, so publishing a fresh object is what
// schedules a repaint. That matters whenever a bitmap was discarded without any value
diff --git a/packages/quill/packages/charts/src/core/hooks/useChartDraw.ts b/packages/quill/packages/charts/src/core/hooks/useChartDraw.ts
index aef44e9df5a6..b8e453552cf9 100644
--- a/packages/quill/packages/charts/src/core/hooks/useChartDraw.ts
+++ b/packages/quill/packages/charts/src/core/hooks/useChartDraw.ts
@@ -1,5 +1,6 @@
import { useEffect, useRef } from 'react'
+import { markPaintDone, markPaintPending } from '../paint-state'
import type {
ChartDimensions,
ChartDrawArgs,
@@ -56,9 +57,15 @@ export function useChartDraw({
cancelAnimationFrame(staticRafRef.current)
staticRafRef.current = null
}
- if (!ctx || !dimensions || !scales || theme.skipDraw) {
+ if (!ctx) {
return
}
+ // Nothing to paint is a settled state too, or a snapshot runner would wait on it.
+ if (!dimensions || !scales || theme.skipDraw) {
+ markPaintDone(ctx.canvas)
+ return
+ }
+ markPaintPending(ctx.canvas)
staticRafRef.current = requestAnimationFrame(() => {
staticRafRef.current = null
clearAndPrepare(ctx, dimensions)
@@ -77,6 +84,7 @@ export function useChartDraw({
})
} finally {
ctx.restore()
+ markPaintDone(ctx.canvas)
}
})
return () => {
diff --git a/packages/quill/packages/charts/src/core/paint-state.ts b/packages/quill/packages/charts/src/core/paint-state.ts
new file mode 100644
index 000000000000..18d240ce89c0
--- /dev/null
+++ b/packages/quill/packages/charts/src/core/paint-state.ts
@@ -0,0 +1,10 @@
+/** Snapshot runners wait for no `pending` canvas: a screenshot between a wipe and its repaint shows a stale plot. */
+const PAINT_STATE_ATTRIBUTE = 'data-hog-charts-paint'
+
+export function markPaintPending(canvas: HTMLCanvasElement): void {
+ canvas.setAttribute(PAINT_STATE_ATTRIBUTE, 'pending')
+}
+
+export function markPaintDone(canvas: HTMLCanvasElement): void {
+ canvas.setAttribute(PAINT_STATE_ATTRIBUTE, 'done')
+}