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.

chore(quill): move MetricCard into quill components package (#61365)

Sam Penningtonbcfc4d17368d
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 9e3639317559..75a0e9c3a443 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -636,22 +636,22 @@ snapshots:
hash: v1.k794b7964.4c03eb4162445e32a74dc1ff8083feed9788b930aea5a4ce49e15c5da22d1b0b.OgPSbq_YYg9wIYaUs5jvjS-8YAzzBf5RgZIjtgXuZp8
components-hogcharts-linechart--with-zeros-and-negatives--light:
hash: v1.k794b7964.5d0d5d5cf4963ac46b4ed2bfdcedf7b24a63a47b7d84e259882e0606cbf43ce1.UtJNbmKgq3ZFS9AdrrOLJzsEejeh19IDdI9aOwxy9Ps
- components-hogcharts-metric--default--dark:
- hash: v1.k794b7964.121acde0d2ffcbb2ea6657bdb2cd2666ba6d34528f3a0b6d61c517f3bfc0e690.G2RsZqN9I8j6BI-t453mJIL7Lld_JAhtO3FM5WSaV7A
- components-hogcharts-metric--default--light:
- hash: v1.k794b7964.8bd27e40dcb1bbbe2f2b4cb0f01fe04af3cf7b9816c7d735811db71e50c50824.xtUPK0ER9_UfV9lWaT3Y9hTzc97yLgrepJBIFffRQps
- components-hogcharts-metric--falling--dark:
- hash: v1.k794b7964.7a249706a02d3ecafb06f582dd4495c9c3f1a615debf421914f4cd6fd51b60ef.ZNY3dId9hCU0KmcNRCjEbey6sKytBJsRfv2u_30hJOU
- components-hogcharts-metric--falling--light:
- hash: v1.k794b7964.7223f07b305e81987250456c70b176fb0450056f87aa4b2e68aa2712ffbf3795.3aiuOFUzkCEZlYloBWWvbqGWLXasynQyeZ8M8ue-cVI
- components-hogcharts-metric--no-change--dark:
- hash: v1.k794b7964.7f692798e9fdec4e34d22d6237b910cf9d6d2c79bc8bfdd5f430ef4d6d159da9.92INHOJUd37OK1Mhaj-ZXrywofAqC27zHFETF_ZidJ8
- components-hogcharts-metric--no-change--light:
- hash: v1.k794b7964.a1029cc8a495d830890ee00d7ae7b9602e7c486695b5fe9dc49f1778cd3dcf37.wtoEorXIEYDVW-6Urx7d5IynJpP1mYb9ZDinvQr084M
- components-hogcharts-metric--number-only--dark:
- hash: v1.k794b7964.4360546d8053143c263d5315c0dd31ac95aa035467738b96fc24632e0b1fe1b2.p_5tW3kuA973BxYWWjwrQg2iwIyPbTYfbGaybWIwTZs
- components-hogcharts-metric--number-only--light:
- hash: v1.k794b7964.9e1fbd475ba802686d23440d747df99abc015757e7708d037f246464fd46a3f8.K1CGuG2R-VxLBu6HLU-jBXWJKH_49rGnbAUx1YnrTcQ
+ components-hogcharts-metriccard--default--dark:
+ hash: v1.k794b7964.fdae35f43c62ef56c1b5b6a1f20c8276484e1715434a643e69f84fdb0bc13050.palMcesa0sPp_jjblGpdBuLNhAv0-Ay_frIrM4PpDWw
+ components-hogcharts-metriccard--default--light:
+ hash: v1.k794b7964.258352110d21296ef4cd99b01e3c4a247137e0a489e3f28f7843e94e5ac5e001.ThpcqSIeItEkBI8KSjHIV6iKcyk3tiLQwGPeDtdFVN0
+ components-hogcharts-metriccard--falling--dark:
+ hash: v1.k794b7964.b583bc0c2260089501d342928145ebf29c2ecd19a877ef070754f92cf5704a6c.OBsoY8oour3ycVg-1e_MEbuIaoBXHE3T4SNoOkwppOk
+ components-hogcharts-metriccard--falling--light:
+ hash: v1.k794b7964.90a5d170cef7d14bde3db12bf121df5dc7f9199f94b07ece8893c86a2e85107d.sM10Ln2LBoEAXnbpcV-obeQ04UntfoJ5oD_zDieUo_c
+ components-hogcharts-metriccard--no-change--dark:
+ hash: v1.k794b7964.dee982acd6e60ffeb36299d80d748fed4ed8fc6fadf930d62741d70a58fbe7a5.xE0QTdSRilMkACw_Uypmi5JgF4OumpYgJnH83Sle8vE
+ components-hogcharts-metriccard--no-change--light:
+ hash: v1.k794b7964.bf3bd811a81a6d0a618b7b98478390a0510e96feef9b020d41379148c214f2ec.hi_YI79MhU2Z1ITkwnURxMH25lrPAdHv-v9g0WGieJw
+ components-hogcharts-metriccard--number-only--dark:
+ hash: v1.k794b7964.a09b8609071d1aaa038a5ed7981d94669e8640c2a877d11d9b70000482987c3e.JKqQfzNlh19Y1ZHWiGcl5jw6-g0pFjoFk7dClPCCa0w
+ components-hogcharts-metriccard--number-only--light:
+ hash: v1.k794b7964.a6d0fc80e033300bdfeeb68cd9b1c0268164dfe2434ced41f27312a3599984f0.kqH1RsyfSbLcK7vT-1FQW4dPfgo2FS1R7VKRb3fa35Y
components-hogcharts-piechart--donut--dark:
hash: v1.k794b7964.95f52c48e6476dd49327e572e78275692bf3085a46c78b57d7ccb0f2af33229d.UKy0xtoAM3JGpdygHr-mG_fCI8HoINH0cq0e-w5MuYY
components-hogcharts-piechart--donut--light:
diff --git a/frontend/src/lib/hog-charts/blocks/Metric/Metric.stories.tsx b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.stories.tsx
similarity index 90%
rename from frontend/src/lib/hog-charts/blocks/Metric/Metric.stories.tsx
rename to packages/quill/packages/charts/src/components/MetricCard/MetricCard.stories.tsx
index ef85243ad959..30334149256a 100644
--- a/frontend/src/lib/hog-charts/blocks/Metric/Metric.stories.tsx
+++ b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.stories.tsx
@@ -1,14 +1,13 @@
import { Meta, StoryObj } from '@storybook/react'
-import { Stage, useReactiveTheme } from '@posthog/quill-charts/story-helpers'
-
-import { Metric } from './Metric'
+import { Stage, useReactiveTheme } from '../../story-helpers'
+import { MetricCard } from './MetricCard'
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
const REVENUE = [4200, 5100, 4700, 5400, 6000, 5800, 6400, 6900, 7200, 7700, 8100, 8800]
const FALLING = [9800, 9200, 8600, 8400, 7700, 7300, 6900, 6500, 6000, 5400, 4800, 4200]
-const meta: Meta = { title: 'Components/HogCharts/Metric', parameters: { layout: 'centered' } }
+const meta: Meta = { title: 'Components/HogCharts/MetricCard', parameters: { layout: 'centered' } }
export default meta
type Story = StoryObj<{}>
@@ -19,7 +18,7 @@ export const Default: Story = {
return (
<Stage width={360} height={320}>
<div className="rounded-xl border border-primary bg-surface-primary p-5 shadow-sm w-full h-full flex flex-col">
- <Metric
+ <MetricCard
title="Total Revenue"
data={REVENUE}
labels={MONTHS}
@@ -40,7 +39,7 @@ export const Falling: Story = {
return (
<Stage width={360} height={320}>
<div className="rounded-xl border border-primary bg-surface-primary p-5 shadow-sm w-full h-full flex flex-col">
- <Metric
+ <MetricCard
title="Active users"
data={FALLING}
labels={MONTHS}
@@ -61,7 +60,7 @@ export const NoChange: Story = {
return (
<Stage width={360} height={320}>
<div className="rounded-xl border border-primary bg-surface-primary p-5 shadow-sm w-full h-full flex flex-col">
- <Metric
+ <MetricCard
title="Daily signups"
data={REVENUE}
labels={MONTHS}
@@ -81,7 +80,7 @@ export const NumberOnly: Story = {
render: () => (
<Stage width={360} height={200}>
<div className="rounded-xl border border-primary bg-surface-primary p-5 shadow-sm w-full h-full flex flex-col">
- <Metric
+ <MetricCard
title="Lifetime revenue"
value={1_284_320}
change={{ value: 8.4 }}
diff --git a/frontend/src/lib/hog-charts/blocks/Metric/Metric.test.tsx b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsx
similarity index 87%
rename from frontend/src/lib/hog-charts/blocks/Metric/Metric.test.tsx
rename to packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsx
index 582c1ddf928d..35d27bb090f7 100644
--- a/frontend/src/lib/hog-charts/blocks/Metric/Metric.test.tsx
+++ b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsx
@@ -1,16 +1,15 @@
import { render } from '@testing-library/react'
-import type { ChartTheme } from '@posthog/quill-charts'
-import { renderHogChart, setupJsdom, setupSyncRaf } from '@posthog/quill-charts/testing'
-
-import { Metric, type MetricChange } from './Metric'
+import type { ChartTheme } from '../../core/types'
+import { renderHogChart, setupJsdom, setupSyncRaf } from '../../testing'
+import { MetricCard, type MetricChange } from './MetricCard'
const THEME: ChartTheme = { colors: ['#22d3ee'], backgroundColor: '#ffffff' }
const LABELS = ['Jan', 'Feb', 'Mar', 'Apr']
const POSITIVE_COLOR = { background: 'rgb(0 200 0 / 10%)', foreground: '#008800' }
const NEGATIVE_COLOR = { background: 'rgb(200 0 0 / 10%)', foreground: '#aa0000' }
-describe('Metric', () => {
+describe('MetricCard', () => {
let teardownJsdom: () => void
let teardownRaf: () => void
@@ -27,7 +26,7 @@ describe('Metric', () => {
describe('with sparkline (data + theme)', () => {
it('shows the last data point and label by default', () => {
const { container } = renderHogChart(
- <Metric
+ <MetricCard
title="Total"
data={[100, 200, 300, 400]}
labels={LABELS}
@@ -41,7 +40,7 @@ describe('Metric', () => {
it('renders a positive change pill when the series ends above the first non-zero value', () => {
const { container } = renderHogChart(
- <Metric
+ <MetricCard
title="Total"
data={[100, 200, 300, 400]}
labels={LABELS}
@@ -54,7 +53,7 @@ describe('Metric', () => {
it('renders a negative change pill when the series ends below the first value', () => {
const { container } = renderHogChart(
- <Metric
+ <MetricCard
title="Total"
data={[400, 300, 200, 100]}
labels={LABELS}
@@ -67,26 +66,26 @@ describe('Metric', () => {
it('skips the change pill when showChange is false', () => {
const { container } = renderHogChart(
- <Metric title="Total" data={[100, 200]} labels={['Jan', 'Feb']} theme={THEME} showChange={false} />
+ <MetricCard title="Total" data={[100, 200]} labels={['Jan', 'Feb']} theme={THEME} showChange={false} />
)
expect(container.textContent).not.toContain('%')
})
it('omits the change pill when the first non-zero value is undefined', () => {
const { container } = renderHogChart(
- <Metric title="Total" data={[0, 0, 0]} labels={['Jan', 'Feb', 'Mar']} theme={THEME} />
+ <MetricCard title="Total" data={[0, 0, 0]} labels={['Jan', 'Feb', 'Mar']} theme={THEME} />
)
expect(container.textContent).not.toContain('%')
})
it('renders nothing when data is empty and no value is supplied', () => {
- const { container } = render(<Metric title="Total" data={[]} labels={[]} theme={THEME} />)
+ const { container } = render(<MetricCard title="Total" data={[]} labels={[]} theme={THEME} />)
expect(container.textContent).toBe('')
})
it('uses Math.abs in the denominator so a negative baseline still reads as a rise', () => {
const { container } = renderHogChart(
- <Metric
+ <MetricCard
title="Total"
data={[-100, 0, 100]}
labels={['Jan', 'Feb', 'Mar']}
@@ -99,7 +98,7 @@ describe('Metric', () => {
it('updates the headline value and label when hovering a different point', () => {
const { container, chart } = renderHogChart(
- <Metric
+ <MetricCard
title="Total"
data={[100, 200, 300, 400]}
labels={LABELS}
@@ -115,7 +114,7 @@ describe('Metric', () => {
it('headlines the supplied `value` at rest while the chart still draws from `data`', () => {
const { container, chart } = renderHogChart(
- <Metric
+ <MetricCard
title="Revenue"
data={[100, 200, 300, 400]}
labels={LABELS}
@@ -133,7 +132,7 @@ describe('Metric', () => {
it('renders a supplied `change` pill fixed across hover', () => {
const { container, chart } = renderHogChart(
- <Metric
+ <MetricCard
title="Revenue"
data={[100, 200, 300, 400]}
labels={LABELS}
@@ -151,7 +150,7 @@ describe('Metric', () => {
it('formats a supplied `change` via `formatChange` when no label is provided', () => {
const { container } = renderHogChart(
- <Metric
+ <MetricCard
title="Revenue"
data={[100, 200, 300, 400]}
labels={LABELS}
@@ -165,7 +164,7 @@ describe('Metric', () => {
it('suppresses the pill when change is null', () => {
const { container } = renderHogChart(
- <Metric
+ <MetricCard
title="Revenue"
data={[100, 200, 300, 400]}
labels={LABELS}
@@ -179,7 +178,7 @@ describe('Metric', () => {
it('uses the supplied subtitle in place of the hover-driven label', () => {
const { container } = renderHogChart(
- <Metric
+ <MetricCard
title="Revenue"
data={[100, 200, 300, 400]}
labels={LABELS}
@@ -196,7 +195,7 @@ describe('Metric', () => {
describe('number-only (no data)', () => {
it('renders the formatted value without a sparkline', () => {
const { container } = render(
- <Metric title="Revenue" value={8800} formatValue={(v) => `$${v.toLocaleString()}`} />
+ <MetricCard title="Revenue" value={8800} formatValue={(v) => `$${v.toLocaleString()}`} />
)
expect(container.textContent).toContain('Revenue')
expect(container.textContent).toContain('$8,800')
@@ -204,7 +203,7 @@ describe('Metric', () => {
})
it('renders nothing when neither `value` nor `data` is supplied', () => {
- const { container } = render(<Metric title="Revenue" />)
+ const { container } = render(<MetricCard title="Revenue" />)
expect(container.textContent).toBe('')
})
@@ -227,7 +226,7 @@ describe('Metric', () => {
},
])('$name', ({ change, goodDirection, expectedColor }) => {
const { container } = render(
- <Metric
+ <MetricCard
title="Revenue"
value={8800}
change={change}
@@ -243,14 +242,14 @@ describe('Metric', () => {
it('points the change chevron down for a negative change', () => {
const { container } = render(
- <Metric title="Revenue" value={8800} change={{ value: -4.2, label: '-4.2%' }} />
+ <MetricCard title="Revenue" value={8800} change={{ value: -4.2, label: '-4.2%' }} />
)
const chevron = container.querySelector('.rounded-full svg')
expect(chevron?.getAttribute('class')).toContain('rotate-180')
})
it('applies dataAttr to the root', () => {
- const { container } = render(<Metric title="Revenue" value={8800} dataAttr="metric-revenue" />)
+ const { container } = render(<MetricCard title="Revenue" value={8800} dataAttr="metric-revenue" />)
expect(container.querySelector('[data-attr="metric-revenue"]')).not.toBeNull()
})
})
diff --git a/frontend/src/lib/hog-charts/blocks/Metric/Metric.tsx b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx
similarity index 92%
rename from frontend/src/lib/hog-charts/blocks/Metric/Metric.tsx
rename to packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx
index 8e5eef4f4b67..7fdc4affb448 100644
--- a/frontend/src/lib/hog-charts/blocks/Metric/Metric.tsx
+++ b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx
@@ -1,7 +1,9 @@
import React, { useMemo, useState } from 'react'
-import { ChartErrorBoundary, type ChartTheme, percentage, Sparkline } from '@posthog/quill-charts'
-
+import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'
+import type { ChartTheme } from '../../core/types'
+import { percentage } from '../../utils/format'
+import { Sparkline } from '../../charts/Sparkline/Sparkline'
import { type MetricChange, resolveDelta } from './resolveDelta'
import { useAnimatedNumber } from './useAnimatedNumber'
@@ -12,7 +14,7 @@ export interface ChangeColor {
foreground: string
}
-export interface MetricProps {
+export interface MetricCardProps {
title: React.ReactNode
/** Resting headline number. Defaults to `data[data.length - 1]` when `data` is present;
* required when `data` is empty or omitted. */
@@ -54,16 +56,16 @@ const DEFAULT_FORMAT_CHANGE = (p: number): string => {
return p > 0 ? `+${formatted}` : formatted
}
-export function Metric(props: MetricProps): React.ReactElement | null {
+export function MetricCard(props: MetricCardProps): React.ReactElement | null {
const { onError, ...rest } = props
return (
<ChartErrorBoundary onError={onError}>
- <MetricInner {...rest} />
+ <MetricCardInner {...rest} />
</ChartErrorBoundary>
)
}
-function MetricInner({
+function MetricCardInner({
title,
value,
data,
@@ -84,7 +86,7 @@ function MetricInner({
animationMs = 350,
className,
dataAttr,
-}: Omit<MetricProps, 'onError'>): React.ReactElement | null {
+}: Omit<MetricCardProps, 'onError'>): React.ReactElement | null {
const sparklineData = data != null && data.length > 0 && theme != null ? data : null
const lastIndex = sparklineData ? sparklineData.length - 1 : -1
diff --git a/frontend/src/lib/hog-charts/blocks/Metric/resolveDelta.test.ts b/packages/quill/packages/charts/src/components/MetricCard/resolveDelta.test.ts
similarity index 100%
rename from frontend/src/lib/hog-charts/blocks/Metric/resolveDelta.test.ts
rename to packages/quill/packages/charts/src/components/MetricCard/resolveDelta.test.ts
diff --git a/frontend/src/lib/hog-charts/blocks/Metric/resolveDelta.ts b/packages/quill/packages/charts/src/components/MetricCard/resolveDelta.ts
similarity index 100%
rename from frontend/src/lib/hog-charts/blocks/Metric/resolveDelta.ts
rename to packages/quill/packages/charts/src/components/MetricCard/resolveDelta.ts
diff --git a/frontend/src/lib/hog-charts/blocks/Metric/useAnimatedNumber.test.tsx b/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.test.tsx
similarity index 100%
rename from frontend/src/lib/hog-charts/blocks/Metric/useAnimatedNumber.test.tsx
rename to packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.test.tsx
diff --git a/frontend/src/lib/hog-charts/blocks/Metric/useAnimatedNumber.ts b/packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.ts
similarity index 100%
rename from frontend/src/lib/hog-charts/blocks/Metric/useAnimatedNumber.ts
rename to packages/quill/packages/charts/src/components/MetricCard/useAnimatedNumber.ts
diff --git a/packages/quill/packages/charts/src/index.ts b/packages/quill/packages/charts/src/index.ts
index 94d10f3e78d6..b4a81ec16644 100644
--- a/packages/quill/packages/charts/src/index.ts
+++ b/packages/quill/packages/charts/src/index.ts
@@ -16,6 +16,8 @@ export { TimeSeriesBarChart } from './charts/TimeSeriesBarChart/TimeSeriesBarCha
export type { TimeSeriesBarChartConfig, TimeSeriesBarChartProps } from './charts/TimeSeriesBarChart/TimeSeriesBarChart'
export { Sparkline } from './charts/Sparkline/Sparkline'
export type { SparklineProps } from './charts/Sparkline/Sparkline'
+export { MetricCard } from './components/MetricCard/MetricCard'
+export type { MetricCardProps, ChangeColor, MetricChange } from './components/MetricCard/MetricCard'
// Base chart (for building new chart types)
export { Chart } from './core/Chart'
diff --git a/products/mcp_analytics/frontend/MCPAnalyticsDashboardOverview.tsx b/products/mcp_analytics/frontend/MCPAnalyticsDashboardOverview.tsx
index e8eea38caea7..afe01d0e6855 100644
--- a/products/mcp_analytics/frontend/MCPAnalyticsDashboardOverview.tsx
+++ b/products/mcp_analytics/frontend/MCPAnalyticsDashboardOverview.tsx
@@ -3,10 +3,9 @@ import { useMemo } from 'react'
import { IconBolt } from '@posthog/icons'
import { LemonSkeleton, Link } from '@posthog/lemon-ui'
-import { type ChartTheme } from '@posthog/quill-charts'
+import { type ChartTheme, MetricCard } from '@posthog/quill-charts'
import { buildTheme } from 'lib/charts/utils/theme'
-import { Metric } from 'lib/hog-charts/blocks/Metric/Metric'
import { humanFriendlyDuration, humanFriendlyLargeNumber } from 'lib/utils'
import { urls } from 'scenes/urls'
@@ -94,7 +93,7 @@ function KPITile({ tile, theme }: { tile: TileSpec; theme: ChartTheme }): JSX.El
<LemonSkeleton className="h-7 w-20" />
</div>
) : (
- <Metric
+ <MetricCard
className="text-primary"
title={tile.label}
value={metric.value}