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): debounce metric card hover so it doesn't chase the cursor (#62238)

Paul D'Ambra7f301226f172
diff --git a/packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsx b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsx
index 35d27bb090f7..eb2fe7544c35 100644
--- a/packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsx
+++ b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsx
@@ -1,4 +1,4 @@
-import { render } from '@testing-library/react'
+import { act, render } from '@testing-library/react'
import type { ChartTheme } from '../../core/types'
import { renderHogChart, setupJsdom, setupSyncRaf } from '../../testing'
@@ -96,6 +96,37 @@ describe('MetricCard', () => {
expect(container.textContent).toContain('+200.0%')
})
+ it('ignores a quick pass-through and only follows the cursor once it settles past the dwell', () => {
+ jest.useFakeTimers()
+ try {
+ const { container, chart } = renderHogChart(
+ <MetricCard
+ title="Total"
+ data={[100, 200, 300, 400]}
+ labels={LABELS}
+ theme={THEME}
+ animationMs={0}
+ formatValue={(v) => `$${Math.round(v)}`}
+ />
+ )
+
+ // Cursor crosses a point but hasn't dwelled — headline stays at rest.
+ act(() => chart.hoverAtIndex(1))
+ expect(container.textContent).toContain('$400')
+ expect(container.textContent).toContain('Apr')
+ expect(container.textContent).not.toContain('$200')
+
+ // Pointer settles past the dwell — now the headline follows it.
+ act(() => {
+ jest.advanceTimersByTime(140)
+ })
+ expect(container.textContent).toContain('$200')
+ expect(container.textContent).toContain('Feb')
+ } finally {
+ jest.useRealTimers()
+ }
+ })
+
it('updates the headline value and label when hovering a different point', () => {
const { container, chart } = renderHogChart(
<MetricCard
@@ -104,6 +135,7 @@ describe('MetricCard', () => {
labels={LABELS}
theme={THEME}
animationMs={0}
+ hoverIntentMs={0}
formatValue={(v) => `$${Math.round(v)}`}
/>
)
@@ -120,6 +152,7 @@ describe('MetricCard', () => {
labels={LABELS}
theme={THEME}
animationMs={0}
+ hoverIntentMs={0}
value={9999}
formatValue={(v) => `$${Math.round(v)}`}
/>
@@ -138,6 +171,7 @@ describe('MetricCard', () => {
labels={LABELS}
theme={THEME}
animationMs={0}
+ hoverIntentMs={0}
change={{ value: 12.5, label: '+12.5% vs. last week' }}
formatValue={(v) => `$${Math.round(v)}`}
/>
diff --git a/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx
index 7fdc4affb448..a5f3bb10823d 100644
--- a/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx
+++ b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx
@@ -6,6 +6,7 @@ import { percentage } from '../../utils/format'
import { Sparkline } from '../../charts/Sparkline/Sparkline'
import { type MetricChange, resolveDelta } from './resolveDelta'
import { useAnimatedNumber } from './useAnimatedNumber'
+import { useHoverIntent } from './useHoverIntent'
export type { MetricChange }
@@ -42,6 +43,9 @@ export interface MetricCardProps {
/** Caption under the headline. Defaults to `labels[activeIndex]` when a sparkline is present. */
subtitle?: React.ReactNode
animationMs?: number
+ /** Dwell (ms) a pointer must settle on the sparkline before the headline follows it.
+ * Keeps a quick pass-through from grabbing attention. `0` disables the gating. */
+ hoverIntentMs?: number
className?: string
dataAttr?: string
onError?: (error: Error, info: React.ErrorInfo) => void
@@ -84,6 +88,7 @@ function MetricCardInner({
negativeColor = DEFAULT_NEGATIVE_COLOR,
subtitle,
animationMs = 350,
+ hoverIntentMs = 140,
className,
dataAttr,
}: Omit<MetricCardProps, 'onError'>): React.ReactElement | null {
@@ -91,10 +96,11 @@ function MetricCardInner({
const lastIndex = sparklineData ? sparklineData.length - 1 : -1
const [hoverIndex, setHoverIndex] = useState(-1)
- const activeIndex = hoverIndex >= 0 ? hoverIndex : lastIndex
+ const intentIndex = useHoverIntent(hoverIndex, hoverIntentMs)
+ const activeIndex = intentIndex >= 0 ? intentIndex : lastIndex
const restingValue = value ?? (sparklineData ? sparklineData[lastIndex] : undefined)
- const animationTarget = sparklineData && hoverIndex >= 0 ? (sparklineData[hoverIndex] ?? 0) : (restingValue ?? 0)
+ const animationTarget = sparklineData && intentIndex >= 0 ? (sparklineData[intentIndex] ?? 0) : (restingValue ?? 0)
const animatedValue = useAnimatedNumber(animationTarget, animationMs)
const baselineValue = useMemo(() => sparklineData?.find((v) => v !== 0 && Number.isFinite(v)), [sparklineData])
diff --git a/packages/quill/packages/charts/src/components/MetricCard/useHoverIntent.ts b/packages/quill/packages/charts/src/components/MetricCard/useHoverIntent.ts
new file mode 100644
index 000000000000..89edbe64687c
--- /dev/null
+++ b/packages/quill/packages/charts/src/components/MetricCard/useHoverIntent.ts
@@ -0,0 +1,24 @@
+import { useEffect, useState } from 'react'
+
+/**
+ * Debounces a sparkline hover index so a cursor merely passing over the card on its way
+ * elsewhere doesn't swap the headline — the pointer has to settle on a point for `delayMs`
+ * before it takes effect. Leaving the sparkline (a negative index) applies immediately, so
+ * the headline drops straight back to rest. `delayMs <= 0` opts out.
+ *
+ * Mirrors the app's `useDebouncedValue`, which this standalone package can't import.
+ */
+export function useHoverIntent(rawIndex: number, delayMs: number): number {
+ const [settledIndex, setSettledIndex] = useState(rawIndex)
+
+ useEffect(() => {
+ if (delayMs <= 0 || rawIndex < 0) {
+ setSettledIndex(rawIndex)
+ return
+ }
+ const timer = setTimeout(() => setSettledIndex(rawIndex), delayMs)
+ return () => clearTimeout(timer)
+ }, [rawIndex, delayMs])
+
+ return delayMs <= 0 || rawIndex < 0 ? rawIndex : settledIndex
+}