: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): guard animations against missing performance global (#66517)
Sam Penningtonf20fbc1d6a8d
diff --git a/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.ts b/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.tsindex b85883c37b21..591a3fe69d1b 100644--- a/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.ts+++ b/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.ts@@ -1,5 +1,7 @@import { useEffect, useRef, useState } from 'react'+import { monotonicNow } from '../../core/time'+/** When `target` changes mid-animation, animation restarts from the currently-displayed value (no snap). */export function useAnimatedNumber(target: number, duration = 350): number {const [value, setValue] = useState(target)@@ -16,10 +18,13 @@ export function useAnimatedNumber(target: number, duration = 350): number {if (from === target) {return}- const start = performance.now()+ const start = monotonicNow()let raf = 0- const tick = (now: number): void => {- const t = Math.min(1, (now - start) / duration)+ // Measure elapsed with monotonicNow() rather than the rAF timestamp, so both sides of the+ // subtraction share one clock — otherwise the Date.now() fallback (when performance is+ // absent) mixes scales with the page-relative rAF timestamp and the eased value blows up.+ const tick = (): void => {+ const t = Math.min(1, (monotonicNow() - start) / duration)const eased = 1 - Math.pow(1 - t, 3)setValue(from + (target - from) * eased)if (t < 1) {diff --git a/packages/quill/packages/charts/src/core/hooks/useHoverAnimation.ts b/packages/quill/packages/charts/src/core/hooks/useHoverAnimation.tsindex b6e6c6c6fb4b..e50a88c59d05 100644--- a/packages/quill/packages/charts/src/core/hooks/useHoverAnimation.ts+++ b/packages/quill/packages/charts/src/core/hooks/useHoverAnimation.ts@@ -1,5 +1,6 @@import { useEffect, useRef } from 'react'+import { monotonicNow } from '../time'import type {ChartDimensions,ChartDrawArgs,@@ -76,19 +77,19 @@ export function useHoverAnimation({// visible frame starts at progress 0, not where the previous bar's fade left off.if (hoverIndex !== hoverAnimRef.current.idx) {hoverAnimRef.current.idx = hoverIndex- hoverAnimRef.current.startTime = performance.now()+ hoverAnimRef.current.startTime = monotonicNow()drewVisibleRef.current = false}const resetHoverFade = (): number => {- hoverAnimRef.current.startTime = performance.now()+ hoverAnimRef.current.startTime = monotonicNow()return 0}const tick = (): void => {// Pin progress to 0 while invisible — see drewVisibleRef comment above.if (!drewVisibleRef.current) {- hoverAnimRef.current.startTime = performance.now()+ hoverAnimRef.current.startTime = monotonicNow()}- const elapsed = performance.now() - hoverAnimRef.current.startTime+ const elapsed = monotonicNow() - hoverAnimRef.current.startTimeconst hoverProgress = hoverAnimationMs > 0 ? Math.min(1, elapsed / hoverAnimationMs) : 1clearAndPrepare(overlayCtx, dimensions)const drewVisible = drawHoverRef.current({@@ -108,7 +109,7 @@ export function useHoverAnimation({drewVisibleRef.current = drewVisible// Recompute after the draw — the chart type may have called resetHoverFade// mid-draw, which would leave the cached hoverProgress stale.- const liveElapsed = performance.now() - hoverAnimRef.current.startTime+ const liveElapsed = monotonicNow() - hoverAnimRef.current.startTimeconst liveProgress = hoverAnimationMs > 0 ? Math.min(1, liveElapsed / hoverAnimationMs) : 1if (drewVisible && liveProgress < 1 && hoverIndex >= 0) {hoverRafRef.current = requestAnimationFrame(tick)diff --git a/packages/quill/packages/charts/src/core/time.test.ts b/packages/quill/packages/charts/src/core/time.test.tsnew file mode 100644index 000000000000..087bbfe2d95a--- /dev/null+++ b/packages/quill/packages/charts/src/core/time.test.ts@@ -0,0 +1,21 @@+import { monotonicNow } from './time'++describe('monotonicNow', () => {+ afterEach(() => {+ jest.restoreAllMocks()+ })++ it('uses performance.now() when it is available', () => {+ // jsdom exposes `performance` via a getter, so spy on the getter rather than replacing it.+ jest.spyOn(globalThis, 'performance', 'get').mockReturnValue({ now: () => 1234.5 } as unknown as Performance)+ expect(monotonicNow()).toBe(1234.5)+ })++ it('falls back to Date.now() without throwing when performance is absent', () => {+ // The jsdom worker that flaked dropped the global `performance`; a bare performance.now()+ // there throws ReferenceError mid-render and trips the chart's error boundary.+ jest.spyOn(globalThis, 'performance', 'get').mockReturnValue(undefined as unknown as Performance)+ const before = Date.now()+ expect(monotonicNow()).toBeGreaterThanOrEqual(before)+ })+})diff --git a/packages/quill/packages/charts/src/core/time.ts b/packages/quill/packages/charts/src/core/time.tsnew file mode 100644index 000000000000..046eabfa1438--- /dev/null+++ b/packages/quill/packages/charts/src/core/time.ts@@ -0,0 +1,14 @@+/** Monotonic millisecond timestamp for the chart animations (hover fade, count-up).+ *+ * Prefers `performance.now()` — sub-millisecond and monotonic — and falls back to `Date.now()`+ * where the global `performance` is absent. It is guaranteed in browsers, but not in every JS host+ * the library runs under: jsdom test workers in particular don't always expose it, and a bare+ * `performance.now()` there throws a `ReferenceError` mid-render that escapes into React's commit+ * phase and trips the chart's error boundary. Reading it through `typeof` keeps the access safe+ * even when the identifier is wholly undeclared.+ */+export function monotonicNow(): number {+ return typeof performance !== 'undefined' && typeof performance.now === 'function'+ ? performance.now()+ : Date.now()+}