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(charts): snappier MetricCard hover, honor reduced motion (#86831)

posthog[bot]2c849d34f44b
diff --git a/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx
index 939a79f335fb..6c6e060cdef6 100644
--- a/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx
+++ b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx
@@ -107,8 +107,8 @@ function MetricCardInner({
subtitle,
restingSubtitle,
hoverChangeFromPreviousPoint = false,
- animationMs = 350,
- hoverIntentMs = 140,
+ animationMs = 120,
+ hoverIntentMs = 60,
className,
dataAttr,
}: Omit<MetricCardProps, 'onError'>): React.ReactElement | null {
diff --git a/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.test.tsx b/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.test.tsx
index bc69eb3ec011..6d66ba7fd057 100644
--- a/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.test.tsx
+++ b/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.test.tsx
@@ -95,6 +95,21 @@ describe('useAnimatedNumber', () => {
expect(raf.pendingCount()).toBe(0)
})
+ it('snaps immediately when the OS asks for reduced motion', () => {
+ const originalMatchMedia = window.matchMedia
+ window.matchMedia = jest.fn().mockReturnValue({ matches: true }) as unknown as typeof window.matchMedia
+ try {
+ const { result, rerender } = renderHook(({ target }) => useAnimatedNumber(target, 350), {
+ initialProps: { target: 0 },
+ })
+ rerender({ target: 100 })
+ expect(result.current).toBe(100)
+ expect(raf.pendingCount()).toBe(0)
+ } finally {
+ window.matchMedia = originalMatchMedia
+ }
+ })
+
it('does not schedule animation when target is unchanged', () => {
const { rerender } = renderHook(({ target }) => useAnimatedNumber(target, 350), {
initialProps: { target: 10 },
diff --git a/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.ts b/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.ts
index 591a3fe69d1b..d7279fce80a4 100644
--- a/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.ts
+++ b/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.ts
@@ -1,16 +1,17 @@
import { useEffect, useRef, useState } from 'react'
+import { prefersReducedMotion } from '../../core/motion'
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 {
+export function useAnimatedNumber(target: number, duration = 120): number {
const [value, setValue] = useState(target)
// Written during render so the animation reads the latest value from the same render pass.
const valueRef = useRef(value)
valueRef.current = value
useEffect(() => {
- if (duration <= 0 || !Number.isFinite(target)) {
+ if (duration <= 0 || !Number.isFinite(target) || prefersReducedMotion()) {
setValue(target)
return
}
diff --git a/packages/quill/packages/charts/src/core/motion.ts b/packages/quill/packages/charts/src/core/motion.ts
new file mode 100644
index 000000000000..71c05a8255de
--- /dev/null
+++ b/packages/quill/packages/charts/src/core/motion.ts
@@ -0,0 +1,13 @@
+/** True when the OS asks for reduced motion, so animations can snap to their end state instead of easing.
+ *
+ * Read through `typeof` guards because `window`/`matchMedia` are absent in the jsdom test workers and
+ * other non-browser hosts the library runs under — a bare access would throw mid-render and trip the
+ * chart's error boundary. Missing support reads as "no preference", so animations run as before.
+ */
+export function prefersReducedMotion(): boolean {
+ return (
+ typeof window !== 'undefined' &&
+ typeof window.matchMedia === 'function' &&
+ window.matchMedia('(prefers-reduced-motion: reduce)').matches
+ )
+}
diff --git a/packages/quill/packages/components/src/metric.tsx b/packages/quill/packages/components/src/metric.tsx
index ae41c3d13949..c81580e31a71 100644
--- a/packages/quill/packages/components/src/metric.tsx
+++ b/packages/quill/packages/components/src/metric.tsx
@@ -188,8 +188,8 @@ function MetricInner({
subtitle,
restingSubtitle,
hoverChangeFromPreviousPoint = false,
- animationMs = 350,
- hoverIntentMs = 140,
+ animationMs = 120,
+ hoverIntentMs = 60,
className,
dataAttr,
children,