: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
feat(quill-charts): make the chart theme optional and read it from a provider (#125455)
Sam Pennington1574ea7ff47d
diff --git a/packages/quill/packages/charts/AGENTS.md b/packages/quill/packages/charts/AGENTS.mdindex 7b39fdd7ead2..23c1ebd270a5 100644--- a/packages/quill/packages/charts/AGENTS.md+++ b/packages/quill/packages/charts/AGENTS.md@@ -41,6 +41,7 @@ const theme = useChartTheme() // reads CSS vars, tracks light/dark switches<LineChart series={series} labels={labels} theme={theme} />```+- `theme` is optional. A chart without it uses the nearest `ChartThemeProvider` (`<ChartThemeProvider value={theme}>`), then the CSS vars. Wrap the app root once instead of passing `theme` to every chart.- Series colors come from `--data-color-1..15`; chrome from `--color-graph-axis-label` / `--color-graph-axis-line` / `--color-graph-crosshair`. `themeFromCssVars()` is the one-shot version; `DEFAULT_CHART_COLORS` is the no-DOM fallback.- 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`, `ScatterChart`, and `SankeyChart` (node and link colors) 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).diff --git a/packages/quill/packages/charts/src/README.md b/packages/quill/packages/charts/src/README.mdindex 843415e38505..0d72940432ab 100644--- a/packages/quill/packages/charts/src/README.md+++ b/packages/quill/packages/charts/src/README.md@@ -43,6 +43,17 @@ function MyChart() {}```+`theme` is optional. A chart without it uses the nearest `ChartThemeProvider`, and+without a provider it reads the vars itself. Set the theme once near the app root:++```tsx+import { ChartThemeProvider, useChartTheme } from '@posthog/quill-charts'++function App() {+ return <ChartThemeProvider value={useChartTheme()}>{/* … */}</ChartThemeProvider>+}+```+- `useChartTheme(opts?)` — React hook; re-reads the vars when the `class` / `theme`attribute flips on `<html>` or `<body>`.- `themeFromCssVars(opts?)` — one-shot, non-React read.diff --git a/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx b/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsxindex 1a8a40040225..264f26d7d343 100644--- a/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx+++ b/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx@@ -3,8 +3,8 @@ import React, { useCallback, useMemo, useRef } from 'react'import { ChartLegend } from '../../components/Legend/ChartLegend'import { useChartLegend } from '../../components/Legend/useChartLegend'import { bandCenter, type BarChartPrivate, groupedBarCenter } from '../../core/bar-layout'-import { applyChartDefaults } from '../../core/chart-config'import { Chart } from '../../core/Chart'+import { applyChartDefaults } from '../../core/chart-config'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'import { useLatest } from '../../core/hooks/useLatest'import {@@ -20,6 +20,7 @@ import {toYAxisScales,yTickCountForHeight,} from '../../core/scales'+import { useResolvedChartTheme } from '../../core/theme'import type {BarChartConfig,ChartDimensions,@@ -46,7 +47,8 @@ export interface BarChartProps<Meta = unknown> {series: Series<Meta>[]labels: string[]config?: BarChartConfig- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemetooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonPointClick?: (data: PointClickData<Meta>) => void/** Right-click counterpart of `onPointClick`. See `ChartProps.onPointContextMenu`. */@@ -75,7 +77,7 @@ function BarChartInner<Meta = unknown>({series,labels,config: rawConfig,- theme,+ theme: themeProp,tooltip,onPointClick,onPointContextMenu,@@ -85,6 +87,7 @@ function BarChartInner<Meta = unknown>({dataAttr,children,}: Omit<BarChartProps<Meta>, 'onError'>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const config = useMemo(() => applyChartDefaults(rawConfig), [rawConfig])const {yScaleType = 'linear',diff --git a/packages/quill/packages/charts/src/charts/BarList/BarList.tsx b/packages/quill/packages/charts/src/charts/BarList/BarList.tsxindex 4c6c46fae0f5..898bbf0d9666 100644--- a/packages/quill/packages/charts/src/charts/BarList/BarList.tsx+++ b/packages/quill/packages/charts/src/charts/BarList/BarList.tsx@@ -1,6 +1,7 @@import React, { useMemo } from 'react'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'+import { useResolvedChartTheme } from '../../core/theme'import type { ChartTheme, Series, TooltipContext } from '../../core/types'import { partValue } from '../ProportionBar/proportion-bar-data'import { BarListLabel } from './BarListLabel'@@ -25,7 +26,8 @@ export interface BarListProps<Meta = unknown> {/** One series per row, valued like a `PieChart` slice: the sum of `data`, floored at 0. Rows* keep the order of `series`, so sort them before you pass them in. */series: Series<Meta>[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: BarListConfig/** The whole that each share is a fraction of. Defaults to the sum of the rows. Set it when the* rows are the top of a larger whole, so the shares stay true to that whole. */@@ -58,7 +60,7 @@ function defaultValueFormatter(value: number): string {function BarListInner<Meta = unknown>({series,- theme,+ theme: themeProp,config,total,valueFormatter = defaultValueFormatter,@@ -67,6 +69,7 @@ function BarListInner<Meta = unknown>({dataAttr,onError,}: BarListProps<Meta>): React.ReactElement | null {+ const theme = useResolvedChartTheme(themeProp)const { scale = 'max', valueDisplay = 'value', labelPosition = 'start' } = config ?? {}const rows = useMemo(diff --git a/packages/quill/packages/charts/src/charts/BarList/StackedBarList.tsx b/packages/quill/packages/charts/src/charts/BarList/StackedBarList.tsxindex ef0a67e5bb51..b727ac0edae9 100644--- a/packages/quill/packages/charts/src/charts/BarList/StackedBarList.tsx+++ b/packages/quill/packages/charts/src/charts/BarList/StackedBarList.tsx@@ -2,6 +2,7 @@import React, { useMemo } from 'react'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'+import { useResolvedChartTheme } from '../../core/theme'import type {BarChartConfig,ChartTheme,@@ -32,7 +33,8 @@ export interface StackedBarListProps<Meta = unknown> {labels: string[]/** Stacked segments: `data[i]` is the value in row `i`, and `trackData[i]` caps that row's track. */series: Series<Meta>[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: StackedBarListConfig/** Wrap long text in a `truncate` element. */renderLabel: (rowIndex: number) => React.ReactNode@@ -64,7 +66,7 @@ export function StackedBarList<Meta = unknown>({ onError, ...rest }: StackedBarLfunction StackedBarListInner<Meta = unknown>({labels,series,- theme,+ theme: themeProp,config,renderLabel,renderValue,@@ -74,6 +76,7 @@ function StackedBarListInner<Meta = unknown>({onError,children,}: StackedBarListProps<Meta>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const labelPosition = config?.labelPosition ?? 'start'const {rowHeight = DEFAULT_ROW_HEIGHT[labelPosition],diff --git a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsxindex 44948df53b66..fbb82c032a70 100644--- a/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx+++ b/packages/quill/packages/charts/src/charts/BoxPlot/BoxPlot.tsx@@ -14,6 +14,7 @@ import { Chart } from '../../core/Chart'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'import { dimColor } from '../../core/color-utils'import { createBarScales, type BarScaleSet, yTickCountForHeight } from '../../core/scales'+import { useResolvedChartTheme } from '../../core/theme'import { resolveAxisLines } from '../../core/types'import type {ChartConfig,@@ -92,7 +93,8 @@ export interface BoxPlotClickData<Meta = unknown> {export interface BoxPlotProps<Meta = unknown> {series: BoxPlotSeries<Meta>[]labels: string[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: BoxPlotConfig/** Optional custom tooltip. Receives the adapter `TooltipContext`; consumers can read* the original datum back out via `entry.series.meta?.datums?.[ctx.dataIndex]`. */@@ -117,7 +119,7 @@ export function BoxPlot<Meta = unknown>({ onError, ...rest }: BoxPlotProps<Meta>function BoxPlotInner<Meta = unknown>({series,labels,- theme,+ theme: themeProp,config,tooltip,onBoxClick,@@ -125,6 +127,7 @@ function BoxPlotInner<Meta = unknown>({dataAttr,children,}: Omit<BoxPlotProps<Meta>, 'onError'>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {yScaleType = 'linear',showGrid = false,diff --git a/packages/quill/packages/charts/src/charts/ComboChart/ComboChart.tsx b/packages/quill/packages/charts/src/charts/ComboChart/ComboChart.tsxindex 1eceb7a98ac9..1b0429924e46 100644--- a/packages/quill/packages/charts/src/charts/ComboChart/ComboChart.tsx+++ b/packages/quill/packages/charts/src/charts/ComboChart/ComboChart.tsx@@ -22,8 +22,8 @@ import {type BarRect,type DrawContext,} from '../../core/canvas-renderer'-import { applyChartDefaults } from '../../core/chart-config'import { Chart } from '../../core/Chart'+import { applyChartDefaults } from '../../core/chart-config'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'import { barColorAt } from '../../core/color-utils'import { type ComboChartPrivate, createComboScales, partitionByType, resolveSeriesType } from '../../core/combo-scales'@@ -40,6 +40,7 @@ import {toYAxisScales,yTickCountForHeight,} from '../../core/scales'+import { useResolvedChartTheme } from '../../core/theme'import type {ChartDimensions,ChartDrawArgs,@@ -62,7 +63,8 @@ export interface ComboChartProps<Meta = unknown> {series: Series<Meta>[]labels: string[]config?: ComboChartConfig- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemetooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonPointClick?: (data: PointClickData<Meta>) => voidclassName?: string@@ -84,13 +86,14 @@ function ComboChartInner<Meta = unknown>({series,labels,config: rawConfig,- theme,+ theme: themeProp,tooltip,onPointClick,className,dataAttr,children,}: Omit<ComboChartProps<Meta>, 'onError'>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const config = useMemo(() => applyChartDefaults(rawConfig), [rawConfig])const {yScaleType = 'linear',@@ -127,9 +130,7 @@ function ComboChartInner<Meta = unknown>({if (barLayout === 'percent') {return computePercentStackData(barSeries, labels)}- return divergingStack- ? computeDivergingStackData(barSeries, labels)- : computeStackData(barSeries, labels)+ return divergingStack ? computeDivergingStackData(barSeries, labels) : computeStackData(barSeries, labels)}, [barLayout, divergingStack, series, labels, seriesTypeOf])// Per-axis topmost bar — only bar layers below the cap forgo corner rounding. Non-bar series arediff --git a/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.tsx b/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.tsxindex cf431a9360ff..ec49118fd19f 100644--- a/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.tsx+++ b/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.tsx@@ -1,6 +1,7 @@import React, { useCallback, useEffect, useMemo, useState } from 'react'import { useChartLayout } from '../../core/chart-context'+import { useResolvedChartTheme } from '../../core/theme'import type {BarChartConfig,BarsConfig,@@ -80,7 +81,8 @@ export interface FunnelChartProps<Meta = unknown> {* conversion from the first step as a percent (0–100). The hatched track drawn behind each* bar covers the remainder up to 100%. See `funnelFromCounts` for the raw-counts case. */series: Series<Meta>[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: FunnelChartConfig/** Custom tooltip content. Omit for the built-in tooltip with percent formatting. */tooltip?: (ctx: TooltipContext<Meta>) => React.ReactNode@@ -193,7 +195,7 @@ function StepFooterRow({export function FunnelChart<Meta = unknown>({steps,series,- theme,+ theme: themeProp,config,tooltip,onStepClick,@@ -203,6 +205,7 @@ export function FunnelChart<Meta = unknown>({children,onError,}: FunnelChartProps<Meta>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {tooltip: tooltipConfig,legend,diff --git a/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx b/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsxindex 6daffe4ad854..eb0fc6916078 100644--- a/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx+++ b/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx@@ -3,6 +3,7 @@ import React, { useCallback, useMemo } from 'react'import { Chart } from '../../core/Chart'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'import { resolveCssColor } from '../../core/color-utils'+import { useResolvedChartTheme } from '../../core/theme'import type {AreaSelectData,ChartConfig,@@ -150,7 +151,8 @@ export interface HeatmapProps {/** Dense grid of counts: `cells[rowIndex][colIndex]`, aligned with `yLabels`/`xLabels`.* 0 (or a missing entry) means an empty cell — nothing is drawn there. */cells: number[][]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: HeatmapConfig/** Custom tooltip. Each `ctx.seriesData` entry is one row at the hovered column; narrow to* the hovered cell via `findClosestSeriesKey(ctx.seriesData, ctx.hoverPosition.y)`. */@@ -179,7 +181,7 @@ function HeatmapInner({xLabels,yLabels,cells,- theme,+ theme: themeProp,config,tooltip,onCellClick,@@ -188,6 +190,7 @@ function HeatmapInner({dataAttr,children,}: Omit<HeatmapProps, 'onError'>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const { colorScale: requestedColorScale = 'auto' } = config ?? {}// Resolve a `var(--…)` accent to a concrete color — canvas `fillStyle` (and `dimColor`'s// d3 parse) can't handle CSS variables, so an unresolved var would blank out every cell.diff --git a/packages/quill/packages/charts/src/charts/LineChart/LineChart.tsx b/packages/quill/packages/charts/src/charts/LineChart/LineChart.tsxindex ab6a8f4999ef..227f954acbf9 100644--- a/packages/quill/packages/charts/src/charts/LineChart/LineChart.tsx+++ b/packages/quill/packages/charts/src/charts/LineChart/LineChart.tsx@@ -11,8 +11,8 @@ import {resolveAxisLineColor,} from '../../core/canvas-renderer'import type { DrawContext } from '../../core/canvas-renderer'-import { applyChartDefaults } from '../../core/chart-config'import { Chart } from '../../core/Chart'+import { applyChartDefaults } from '../../core/chart-config'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'import {buildSegmentResolveValue,@@ -25,6 +25,7 @@ import {yTickCountForHeight,} from '../../core/scales'import type { ScaleSet, StackedBand } from '../../core/scales'+import { useResolvedChartTheme } from '../../core/theme'import { DEFAULT_Y_AXIS_ID, resolveAxisLines } from '../../core/types'import type {ChartDimensions,@@ -51,7 +52,8 @@ export interface LineChartProps<Meta = unknown> {series: Series<Meta>[]labels: string[]config?: LineChartConfig- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemetooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonPointClick?: (data: PointClickData<Meta>) => void/** Right-click counterpart of `onPointClick`. See `ChartProps.onPointContextMenu`. */@@ -76,7 +78,7 @@ function LineChartInner<Meta = unknown>({series,labels,config: rawConfig,- theme,+ theme: themeProp,tooltip,onPointClick,onPointContextMenu,@@ -85,6 +87,7 @@ function LineChartInner<Meta = unknown>({dataAttr,children,}: LineChartProps<Meta>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const config = useMemo(() => applyChartDefaults(rawConfig), [rawConfig])const {yScaleType = 'linear',diff --git a/packages/quill/packages/charts/src/charts/PieChart/PieChart.tsx b/packages/quill/packages/charts/src/charts/PieChart/PieChart.tsxindex 6ed37d4f11f6..414046fa7379 100644--- a/packages/quill/packages/charts/src/charts/PieChart/PieChart.tsx+++ b/packages/quill/packages/charts/src/charts/PieChart/PieChart.tsx@@ -8,6 +8,7 @@ import type { RadialSlicePayload } from '../../core/hooks/useRadialInteraction'import { useRadialLayout } from '../../core/radial-context'import { RadialChart } from '../../core/RadialChart'import type { RadialLayoutBuilder } from '../../core/RadialChart'+import { useResolvedChartTheme } from '../../core/theme'import type {ChartDrawArgs,ChartLegendConfig,@@ -67,7 +68,8 @@ export interface PieChartConfig<Meta = unknown> {export interface PieChartProps<Meta = unknown> {series: Series<Meta>[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: PieChartConfig<Meta>tooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonSliceClick?: (payload: RadialSlicePayload<Meta>) => void@@ -136,7 +138,7 @@ function defaultValueFormatter(value: number): string {function PieChartInner<Meta = unknown>({series,- theme,+ theme: themeProp,config,tooltip,onSliceClick,@@ -146,6 +148,7 @@ function PieChartInner<Meta = unknown>({centerLabel,children,}: Omit<PieChartProps<Meta>, 'onError'>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {innerRadiusRatio = 0,showValueOnSlice = !(innerRadiusRatio > 0),diff --git a/packages/quill/packages/charts/src/charts/ProportionBar/ProportionBar.tsx b/packages/quill/packages/charts/src/charts/ProportionBar/ProportionBar.tsxindex dc28133db4f3..fb25e9a2e4c9 100644--- a/packages/quill/packages/charts/src/charts/ProportionBar/ProportionBar.tsx+++ b/packages/quill/packages/charts/src/charts/ProportionBar/ProportionBar.tsx@@ -4,6 +4,7 @@ import { ChartLegend } from '../../components/Legend/ChartLegend'import { useChartLegend } from '../../components/Legend/useChartLegend'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'import type { RadialSlicePayload } from '../../core/hooks/useRadialInteraction'+import { useResolvedChartTheme } from '../../core/theme'import type {BarChartConfig,ChartLegendConfig,@@ -37,7 +38,8 @@ export interface ProportionBarConfig {export interface ProportionBarProps<Meta = unknown> {/** One series per part, valued like a `PieChart` slice: the sum of `data`, floored at 0. */series: Series<Meta>[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: ProportionBarConfig/** Formats a part's raw value in the legend and the default tooltip. */valueFormatter?: (value: number) => string@@ -69,7 +71,7 @@ function defaultValueFormatter(value: number): string {function ProportionBarInner<Meta = unknown>({series,- theme,+ theme: themeProp,config,valueFormatter = defaultValueFormatter,tooltip,@@ -78,6 +80,7 @@ function ProportionBarInner<Meta = unknown>({dataAttr,onError,}: ProportionBarProps<Meta>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {barHeight = DEFAULT_BAR_HEIGHT,barCornerRadius = DEFAULT_CORNER_RADIUS,diff --git a/packages/quill/packages/charts/src/charts/SankeyChart/SankeyChart.tsx b/packages/quill/packages/charts/src/charts/SankeyChart/SankeyChart.tsxindex d3df8e020a70..245ee7019988 100644--- a/packages/quill/packages/charts/src/charts/SankeyChart/SankeyChart.tsx+++ b/packages/quill/packages/charts/src/charts/SankeyChart/SankeyChart.tsx@@ -8,6 +8,7 @@ import { resolveCssColor } from '../../core/color-utils'import { useChartCanvas } from '../../core/hooks/useChartCanvas'import { useChartDraw } from '../../core/hooks/useChartDraw'import { applyMarginOverride } from '../../core/hooks/useChartMargins'+import { useResolvedChartTheme } from '../../core/theme'import type { ChartDrawArgs, ChartMargins, ChartScales } from '../../core/types'import { defaultResolveValue } from '../../core/types'import { Tooltip } from '../../overlays/Tooltip'@@ -69,7 +70,7 @@ export function SankeyChart<NodeMeta = unknown, LinkMeta = NodeMeta>({function SankeyChartInner<NodeMeta = unknown, LinkMeta = NodeMeta>({nodes,links,- theme,+ theme: themeProp,config,tooltip,onNodeClick,@@ -80,6 +81,7 @@ function SankeyChartInner<NodeMeta = unknown, LinkMeta = NodeMeta>({dataAttr,children,}: Omit<SankeyChartProps<NodeMeta, LinkMeta>, 'onError'>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {nodeWidth = DEFAULT_NODE_WIDTH,nodePadding = DEFAULT_NODE_PADDING,diff --git a/packages/quill/packages/charts/src/charts/SankeyChart/types.ts b/packages/quill/packages/charts/src/charts/SankeyChart/types.tsindex 0de68c3fc491..07bee8a14c7d 100644--- a/packages/quill/packages/charts/src/charts/SankeyChart/types.ts+++ b/packages/quill/packages/charts/src/charts/SankeyChart/types.ts@@ -62,7 +62,8 @@ export interface SankeyChartConfig {export interface SankeyChartProps<NodeMeta = unknown, LinkMeta = NodeMeta> {nodes: SankeyNodeInput<NodeMeta>[]links: SankeyLinkInput<LinkMeta>[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: SankeyChartConfigtooltip?: (ctx: SankeyTooltipContext<NodeMeta, LinkMeta>) => React.ReactNodeonNodeClick?: (node: SankeyNodeDatum<NodeMeta>) => voiddiff --git a/packages/quill/packages/charts/src/charts/ScatterChart/ScatterChart.tsx b/packages/quill/packages/charts/src/charts/ScatterChart/ScatterChart.tsxindex df0ec3c3ec1a..9dfa965ba416 100644--- a/packages/quill/packages/charts/src/charts/ScatterChart/ScatterChart.tsx+++ b/packages/quill/packages/charts/src/charts/ScatterChart/ScatterChart.tsx@@ -8,6 +8,7 @@ import { Chart } from '../../core/Chart'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'import { applyMarginOverride, DEFAULT_MARGINS } from '../../core/hooks/useChartMargins'import { sanitizeFixedDomain } from '../../core/scales'+import { useResolvedChartTheme } from '../../core/theme'import type {AreaSelectData,ChartConfig,@@ -52,7 +53,8 @@ const NO_CATEGORY_X_LABELS = (): null => nullexport interface ScatterChartProps<Meta = unknown> {series: ScatterSeries<Meta>[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: ScatterChartConfig<Meta>tooltip?: (ctx: ScatterTooltipContext<Meta>) => React.ReactNodeonPointClick?: (point: ScatterPointDatum<Meta>) => void@@ -75,7 +77,7 @@ export function ScatterChart<Meta = unknown>({ onError, ...rest }: ScatterChartPfunction ScatterChartInner<Meta = unknown>({series,- theme,+ theme: themeProp,config,tooltip,onPointClick,@@ -84,6 +86,7 @@ function ScatterChartInner<Meta = unknown>({dataAttr,children,}: Omit<ScatterChartProps<Meta>, 'onError'>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {xAxis = EMPTY_AXIS_CONFIG,yAxis = EMPTY_AXIS_CONFIG,diff --git a/packages/quill/packages/charts/src/charts/SlopeChart/SlopeChart.tsx b/packages/quill/packages/charts/src/charts/SlopeChart/SlopeChart.tsxindex 157487319c45..939eb2901a1d 100644--- a/packages/quill/packages/charts/src/charts/SlopeChart/SlopeChart.tsx+++ b/packages/quill/packages/charts/src/charts/SlopeChart/SlopeChart.tsx@@ -4,6 +4,7 @@ import { ChartLegend } from '../../components/Legend/ChartLegend'import { useChartLegend } from '../../components/Legend/useChartLegend'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'import { applyMarginOverride } from '../../core/hooks/useChartMargins'+import { useResolvedChartTheme } from '../../core/theme'import type {ChartConfig,ChartLegendConfig,@@ -66,7 +67,8 @@ export interface SlopeChartProps<Meta = SlopeSeriesMeta> {series: Series<Meta>[]labels: string[]config?: SlopeChartConfig- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemetooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonPointClick?: (data: PointClickData<Meta>) => voidclassName?: string@@ -92,7 +94,7 @@ function SlopeChartInner<Meta = SlopeSeriesMeta>({series,labels,config,- theme,+ theme: themeProp,tooltip,onPointClick,className,@@ -100,6 +102,7 @@ function SlopeChartInner<Meta = SlopeSeriesMeta>({children,onError,}: SlopeChartProps<Meta>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {showSeriesLabels = true,showStartLabels = true,diff --git a/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.tsx b/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.tsxindex 3684c1e1c852..3bb8c94b2ad3 100644--- a/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.tsx+++ b/packages/quill/packages/charts/src/charts/Sparkline/Sparkline.tsx@@ -3,6 +3,7 @@ import React, { useEffect, useMemo } from 'react'import { useChartHover } from '../../core/chart-context'import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'import { useLatest } from '../../core/hooks/useLatest'+import { useResolvedChartTheme } from '../../core/theme'import type { BarChartConfig, ChartTheme, LineChartConfig, Series, TooltipContext, ValueDomain } from '../../core/types'import { BarChart } from '../BarChart/BarChart'import { LineChart } from '../LineChart/LineChart'@@ -17,7 +18,8 @@ export interface SparklineProps {/** Optional x-axis labels — when omitted, indices stand in. Consumers can look up* the hovered index against their own labels. */labels?: string[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemecolor?: string/** `line` (default) draws a gradient-filled trend line; `bar` draws stacked bars. */type?: 'line' | 'bar'@@ -67,7 +69,7 @@ function SparklineInner({data,series,labels,- theme,+ theme: themeProp,color,type = 'line',height = 120,@@ -81,6 +83,7 @@ function SparklineInner({dataAttr,onError,}: SparklineProps): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const resolvedColor = color ?? theme.colors[0]const chartSeries = useMemo<Series[]>(() => {if (series) {diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsxindex f2c7f545a679..49f2ec71f6b0 100644--- a/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsx+++ b/packages/quill/packages/charts/src/charts/TimeSeriesBarChart/TimeSeriesBarChart.tsx@@ -1,6 +1,7 @@import React, { useMemo } from 'react'import { ChartLegend } from '../../components/Legend/ChartLegend'+import { useResolvedChartTheme } from '../../core/theme'import type {AxisLinesConfig,BarChartConfig,@@ -77,7 +78,8 @@ export interface TimeSeriesBarChartConfig {export interface TimeSeriesBarChartProps<Meta = unknown> {series: Series<Meta>[]labels: string[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: TimeSeriesBarChartConfigtooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonPointClick?: (data: PointClickData<Meta>) => void@@ -96,7 +98,7 @@ export interface TimeSeriesBarChartProps<Meta = unknown> {export function TimeSeriesBarChart<Meta = unknown>({series,labels,- theme,+ theme: themeProp,config,tooltip,onPointClick,@@ -108,6 +110,7 @@ export function TimeSeriesBarChart<Meta = unknown>({children,onError,}: TimeSeriesBarChartProps<Meta>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {xAxis,yAxis,diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.tsxindex 0e1c55bcb2e5..074ae761a525 100644--- a/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.tsx+++ b/packages/quill/packages/charts/src/charts/TimeSeriesComboChart/TimeSeriesComboChart.tsx@@ -1,6 +1,7 @@import React, { useMemo } from 'react'import { ChartLegend } from '../../components/Legend/ChartLegend'+import { useResolvedChartTheme } from '../../core/theme'import type {AxisLinesConfig,ChartLegendConfig,@@ -66,7 +67,8 @@ export interface TimeSeriesComboChartConfig {export interface TimeSeriesComboChartProps<Meta = unknown> {series: Series<Meta>[]labels: string[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: TimeSeriesComboChartConfigtooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonPointClick?: (data: PointClickData<Meta>) => void@@ -82,7 +84,7 @@ export interface TimeSeriesComboChartProps<Meta = unknown> {export function TimeSeriesComboChart<Meta = unknown>({series,labels,- theme,+ theme: themeProp,config,tooltip,onPointClick,@@ -91,6 +93,7 @@ export function TimeSeriesComboChart<Meta = unknown>({children,onError,}: TimeSeriesComboChartProps<Meta>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {xAxis,yAxis,diff --git a/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx b/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsxindex bb8b90a12e88..7abdc61002a0 100644--- a/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx+++ b/packages/quill/packages/charts/src/charts/TimeSeriesLineChart/TimeSeriesLineChart.tsx@@ -1,6 +1,7 @@import React, { useMemo } from 'react'import { ChartLegend } from '../../components/Legend/ChartLegend'+import { useResolvedChartTheme } from '../../core/theme'import type {AxisLinesConfig,ChartLegendConfig,@@ -71,7 +72,8 @@ export interface TimeSeriesLineChartProps<Meta = unknown> {* collapses onto the first occurrence's position and draws the series backwards. Display* formatting belongs in `xAxis.timezone`/`interval` (auto tick formatter) or `tickFormatter`. */labels: string[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemeconfig?: TimeSeriesLineChartConfigtooltip?: (ctx: TooltipContext<Meta>) => React.ReactNodeonPointClick?: (data: PointClickData<Meta>) => void@@ -87,7 +89,7 @@ export interface TimeSeriesLineChartProps<Meta = unknown> {export function TimeSeriesLineChart<Meta = unknown>({series,labels,- theme,+ theme: themeProp,config,tooltip,onPointClick,@@ -98,6 +100,7 @@ export function TimeSeriesLineChart<Meta = unknown>({children,onError,}: TimeSeriesLineChartProps<Meta>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {xAxis,yAxis,diff --git a/packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsx b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsxindex 7a4e97794914..836ab136f517 100644--- a/packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsx+++ b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.test.tsx@@ -24,13 +24,16 @@ describe('MetricCard', () => {})describe('with sparkline (data + theme)', () => {- it('shows the last data point and label by default', () => {+ it.each([+ { name: 'a theme prop', theme: THEME },+ { name: 'no theme prop', theme: undefined },+ ])('shows the sparkline, last data point and label by default with $name', ({ theme }) => {const { container } = renderChart(<MetricCardtitle="Total"data={[100, 200, 300, 400]}labels={LABELS}- theme={THEME}+ theme={theme}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.tsxindex 36d308ff7878..6bb4e87990f4 100644--- a/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx+++ b/packages/quill/packages/charts/src/components/MetricCard/MetricCard.tsx@@ -32,7 +32,7 @@ export interface MetricCardProps {/** Formats a label for the default subtitle, e.g. an ISO time as a short date. The sparkline* keeps the raw `labels`, which must stay unique. */formatLabel?: (label: string) => React.ReactNode- /** Required when `data` is present. */+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */theme?: ChartTheme/** Sparkline line + fill color. Falls back to `theme.colors[0]`. */color?: string@@ -121,7 +121,7 @@ function MetricCardInner({dataAttr,onError,}: MetricCardProps): React.ReactElement | null {- const sparklineData = data != null && data.length > 0 && theme != null ? data : null+ const sparklineData = data != null && data.length > 0 ? data : nullconst lastIndex = sparklineData ? sparklineData.length - 1 : -1const [hoverIndex, setHoverIndex] = useState(-1)@@ -215,7 +215,7 @@ function MetricCardInner({</div>)}- {sparklineData && theme && (+ {sparklineData && (<Sparklinedata={sparklineData}labels={labels}diff --git a/packages/quill/packages/charts/src/core/Chart.tsx b/packages/quill/packages/charts/src/core/Chart.tsxindex 89067753059f..4d7c2ba4e910 100644--- a/packages/quill/packages/charts/src/core/Chart.tsx+++ b/packages/quill/packages/charts/src/core/Chart.tsx@@ -28,6 +28,7 @@ import { useLatest } from './hooks/useLatest'import { useResolvedYFormatter } from './hooks/useResolvedYFormatters'import { useStableResolveValue } from './hooks/useStableResolveValue'import { useYAxisMaps } from './hooks/useYAxisMaps'+import { useResolvedChartTheme } from './theme'import type {AreaSelectData,ChartConfig,@@ -63,7 +64,8 @@ export interface ChartProps<Meta = unknown> {series: Series<Meta>[]labels: string[]config?: ChartConfig- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemecreateScales: CreateScalesFn/** Static layer — grid, lines, areas, points. Redrawn only when chart inputs change. */drawStatic: (args: ChartDrawArgs) => void@@ -128,7 +130,7 @@ export function Chart<Meta = unknown>({series,labels,config,- theme,+ theme: themeProp,createScales: createScalesFn,drawStatic,drawHover,@@ -149,6 +151,7 @@ export function Chart<Meta = unknown>({wrapClickData,resolveHoverIndex,}: ChartProps<Meta>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const {xTickFormatter,xTickLabelRotation = 0,diff --git a/packages/quill/packages/charts/src/core/RadialChart.tsx b/packages/quill/packages/charts/src/core/RadialChart.tsxindex 735cd162b6b4..c1a3acf2d884 100644--- a/packages/quill/packages/charts/src/core/RadialChart.tsx+++ b/packages/quill/packages/charts/src/core/RadialChart.tsx@@ -12,6 +12,7 @@ import type { RadialSlicePayload } from './hooks/useRadialInteraction'import { RadialLayoutContext } from './radial-context'import type { RadialLayoutContextValue } from './radial-context'import type { PieLayout } from './radial-layout'+import { useResolvedChartTheme } from './theme'import type {ChartDrawArgs,ChartMargins,@@ -38,7 +39,8 @@ export type RadialLayoutBuilder<Meta = unknown> = (export interface RadialChartProps<Meta = unknown> {series: Series<Meta>[]- theme: ChartTheme+ /** Defaults to the nearest `ChartThemeProvider`, then the quill CSS vars. */+ theme?: ChartThemebuildLayout: RadialLayoutBuilder<Meta>drawStatic: (args: ChartDrawArgs) => voiddrawHover: (args: ChartDrawArgs) => DrawHoverResult@@ -56,7 +58,7 @@ export interface RadialChartProps<Meta = unknown> {export function RadialChart<Meta = unknown>({series,- theme,+ theme: themeProp,buildLayout,drawStatic,drawHover,@@ -69,6 +71,7 @@ export function RadialChart<Meta = unknown>({dataAttr,children,}: RadialChartProps<Meta>): React.ReactElement {+ const theme = useResolvedChartTheme(themeProp)const { canvasRef, overlayCanvasRef, wrapperRef, dimensions, ctx, overlayCtx } = useChartCanvas({margins: RADIAL_MARGINS,})diff --git a/packages/quill/packages/charts/src/core/theme.test.ts b/packages/quill/packages/charts/src/core/theme.test.tsindex 84c245bb679c..208b0d00c468 100644--- a/packages/quill/packages/charts/src/core/theme.test.ts+++ b/packages/quill/packages/charts/src/core/theme.test.ts@@ -1,6 +1,10 @@+import { renderHook } from '@testing-library/react'+import { createElement, type ReactNode } from 'react'+import { dataColorPalette } from '@posthog/quill-tokens'-import { DEFAULT_CHART_COLORS, themeFromCssVars } from './theme'+import { ChartThemeProvider, DEFAULT_CHART_COLORS, themeFromCssVars, useResolvedChartTheme } from './theme'+import type { ChartTheme } from './types'describe('chart theme', () => {afterEach(() => {@@ -71,4 +75,20 @@ describe('chart theme', () => {expect(colors).toHaveLength(colorCount)expect(colors[DEFAULT_CHART_COLORS.length]).toBe(DEFAULT_CHART_COLORS[0])})++ const PROP_THEME: ChartTheme = { colors: ['#000001'] }+ const PROVIDED_THEME: ChartTheme = { colors: ['#000002'] }++ it.each([+ { name: 'the prop over the provider', prop: PROP_THEME, provided: PROVIDED_THEME, expected: '#000001' },+ { name: 'the provider without a prop', prop: undefined, provided: PROVIDED_THEME, expected: '#000002' },+ { name: 'the CSS vars without either', prop: undefined, provided: null, expected: '#000003' },+ ])('resolves a chart theme from $name', ({ prop, provided, expected }) => {+ document.body.style.setProperty('--data-color-1', '#000003')+ const wrapper = ({ children }: { children: ReactNode }): ReactNode =>+ provided ? createElement(ChartThemeProvider, { value: provided }, children) : children+ const { result } = renderHook(() => useResolvedChartTheme(prop), { wrapper })+ expect(result.current.colors[0]).toBe(expected)+ document.body.style.removeProperty('--data-color-1')+ })})diff --git a/packages/quill/packages/charts/src/core/theme.ts b/packages/quill/packages/charts/src/core/theme.tsindex 5db549512bb5..42f8cf73d7b8 100644--- a/packages/quill/packages/charts/src/core/theme.ts+++ b/packages/quill/packages/charts/src/core/theme.ts@@ -1,4 +1,4 @@-import { useEffect, useState } from 'react'+import { createContext, useContext, useEffect, useState } from 'react'import type { ChartTheme } from './types'@@ -124,11 +124,15 @@ export function themeFromCssVars(options: ThemeFromCssOptions = {}): ChartTheme* one or the other) and re-reads the vars whenever they flip.*/export function useChartTheme(options: ThemeFromCssOptions = {}): ChartTheme {+ return useCssVarTheme(options, true)+}++function useCssVarTheme(options: ThemeFromCssOptions, enabled: boolean): ChartTheme {const { root, colorCount } = options- const [theme, setTheme] = useState<ChartTheme>(() => themeFromCssVars(options))+ const [theme, setTheme] = useState<ChartTheme>(() => (enabled ? themeFromCssVars(options) : FALLBACK_THEME))useEffect(() => {- if (typeof document === 'undefined' || typeof MutationObserver !== 'function') {+ if (!enabled || typeof document === 'undefined' || typeof MutationObserver !== 'function') {return}const reread = (): void => setTheme(themeFromCssVars({ root, colorCount }))@@ -139,7 +143,24 @@ export function useChartTheme(options: ThemeFromCssOptions = {}): ChartTheme {observer.observe(document.body, opts)return () => observer.disconnect()// root/colorCount are the only inputs; options identity is intentionally ignored.- }, [root, colorCount])+ }, [root, colorCount, enabled])return theme}++const FALLBACK_THEME: ChartTheme = { colors: [...DEFAULT_CHART_COLORS] }++const ChartThemeContext = createContext<ChartTheme | null>(null)++/** Gives every chart below it a theme, so a chart can omit its `theme` prop. Pass the result of+ * {@link useChartTheme} (or the host's own theme hook) as `value`. A new `value` object repaints+ * every chart below, so keep it stable. */+export const ChartThemeProvider = ChartThemeContext.Provider++/** The theme a chart draws with: its own `theme` prop, else the nearest {@link ChartThemeProvider},+ * else the quill CSS vars. Only the last case reads and watches the CSS vars. */+export function useResolvedChartTheme(theme?: ChartTheme): ChartTheme {+ const provided = useContext(ChartThemeContext)+ const fallback = useCssVarTheme({}, !theme && !provided)+ return theme ?? provided ?? fallback+}diff --git a/packages/quill/packages/charts/src/docs/CONTRIBUTING.md b/packages/quill/packages/charts/src/docs/CONTRIBUTING.mdindex 74c193bdecb7..da84621b3bbe 100644--- a/packages/quill/packages/charts/src/docs/CONTRIBUTING.md+++ b/packages/quill/packages/charts/src/docs/CONTRIBUTING.md@@ -20,7 +20,7 @@ Where new code goes:## Conventions-- **No kea, no PostHog imports.** Theme, colors, and data are passed in as props. The library has no app dependencies — it's used by trends but shouldn't know about them.+- **No kea, no PostHog imports.** Colors and data are passed in as props. The theme comes from a prop or a `ChartThemeProvider`, resolved with `useResolvedChartTheme`. The library has no app dependencies — it's used by trends but shouldn't know about them.- **`ChartScales` must not expose d3 types.** Public interface is `x(label) => px`, `y(value) => px`, `yTicks() => number[]`. d3 stays inside the chart-type's `createScales`.- **Canvas functions are stateless.** Pure functions in `canvas-renderer.ts`, no React or side effects. State lives in React; drawing reads it.- **Overlays use the granular hooks.** `useChartLayout()` doesn't re-render on hover; `useChartHover()` does. Use `useChart()` only when an overlay genuinely needs both — it re-renders on every mousemove.diff --git a/packages/quill/packages/charts/src/index.ts b/packages/quill/packages/charts/src/index.tsindex 201bab33e795..3e5eefc901d1 100644--- a/packages/quill/packages/charts/src/index.ts+++ b/packages/quill/packages/charts/src/index.ts@@ -200,7 +200,14 @@ export type {export { DEFAULT_Y_AXIS_ID, resolveAxisLines } from './core/types'// Theme: read a ChartTheme from quill data-viz CSS vars (with a built-in fallback palette)-export { themeDefaultsFromCssVars, themeFromCssVars, useChartTheme, DEFAULT_CHART_COLORS } from './core/theme'+export {+ ChartThemeProvider,+ themeDefaultsFromCssVars,+ themeFromCssVars,+ useChartTheme,+ useResolvedChartTheme,+ DEFAULT_CHART_COLORS,+} from './core/theme'export { applyChartDefaults, DEFAULT_CHART_CONFIG } from './core/chart-config'export type { ThemeFromCssOptions } from './core/theme'