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(quill-charts): fall back to a palette color for an unresolved css variable (#129017)

Sam Pennington9e4fb5453821
diff --git a/packages/quill/packages/charts/src/charts/Gauge/gauge-colors.ts b/packages/quill/packages/charts/src/charts/Gauge/gauge-colors.ts
index 7b2d7a2804b5..9c73c40f221b 100644
--- a/packages/quill/packages/charts/src/charts/Gauge/gauge-colors.ts
+++ b/packages/quill/packages/charts/src/charts/Gauge/gauge-colors.ts
@@ -27,15 +27,11 @@ const TRACK_ALPHA = 0.1
const isVar = (color: string): boolean => color.trim().startsWith('var(')
-/** A `var(--…)` that does not resolve would reach the canvas as an invalid color, which the canvas
- * ignores, so it paints with whatever color the previous stroke left behind. */
function resolveColor(color: string, fallback: string, root: HTMLElement): string {
- let resolved = resolveCssColor(color, root).trim()
- // jsdom and other non-browser hosts do not inherit custom properties down to the wrapper.
- if (isVar(resolved)) {
- resolved = resolveCssColor(color).trim()
- }
- return resolved && !isVar(resolved) ? resolved : fallback
+ // jsdom and other non-browser hosts do not inherit custom properties down to the wrapper, so
+ // a miss on the wrapper retries against the body.
+ const resolved = resolveCssColor(color, { root, fallback: '' }) || resolveCssColor(color, { fallback })
+ return resolved.trim() || fallback
}
function inkTint(ink: string, theme: ChartTheme): string {
diff --git a/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx b/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx
index bcc9167ee134..b75c988d3d8b 100644
--- a/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx
+++ b/packages/quill/packages/charts/src/charts/Heatmap/Heatmap.tsx
@@ -3,7 +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 { DEFAULT_CHART_COLORS, useResolvedChartTheme } from '../../core/theme'
import type {
AreaSelectData,
ChartConfig,
@@ -197,7 +197,7 @@ function HeatmapInner({
// theme.colors are already resolved, so the memo re-runs (and re-resolves the accent against
// the current theme) on a light/dark flip too.
const accent = useMemo(
- () => resolveCssColor(config?.color || theme.colors[0] || '#1d4aff'),
+ () => resolveCssColor(config?.color || theme.colors[0] || DEFAULT_CHART_COLORS[0]),
[config?.color, theme.colors]
)
diff --git a/packages/quill/packages/charts/src/charts/SankeyChart/SankeyChart.tsx b/packages/quill/packages/charts/src/charts/SankeyChart/SankeyChart.tsx
index 4f41c3d0ea3b..f496dffc50e3 100644
--- a/packages/quill/packages/charts/src/charts/SankeyChart/SankeyChart.tsx
+++ b/packages/quill/packages/charts/src/charts/SankeyChart/SankeyChart.tsx
@@ -8,7 +8,7 @@ import { resolveCssColor } from '../../core/color-utils'
import { useChartCanvas } from '../../core/hooks/useChartCanvas'
import { useChartDraw } from '../../core/hooks/useChartDraw'
import { applyMarginOverride, useStableMargins } from '../../core/hooks/useChartMargins'
-import { useResolvedChartTheme } from '../../core/theme'
+import { DEFAULT_CHART_COLORS, useResolvedChartTheme } from '../../core/theme'
import type { ChartDrawArgs, ChartMargins, ChartScales } from '../../core/types'
import { defaultResolveValue } from '../../core/types'
import { Tooltip } from '../../overlays/Tooltip'
@@ -53,9 +53,6 @@ function graphKey(
])
}
-/** Used when `theme.colors` is empty, as `Heatmap` does for its accent. */
-const FALLBACK_NODE_COLOR = '#1d4aff'
-
export function SankeyChart<NodeMeta = unknown, LinkMeta = NodeMeta>({
onError,
...rest
@@ -150,10 +147,10 @@ function SankeyChartInner<NodeMeta = unknown, LinkMeta = NodeMeta>({
}
const label = node.label ?? node.id
if (!slots.has(label)) {
- slots.set(label, palette[slots.size % palette.length] || FALLBACK_NODE_COLOR)
+ slots.set(label, palette[slots.size % palette.length] || DEFAULT_CHART_COLORS[0])
}
}
- return (label: string): string => slots.get(label) ?? (palette[0] || FALLBACK_NODE_COLOR)
+ return (label: string): string => slots.get(label) ?? (palette[0] || DEFAULT_CHART_COLORS[0])
}, [nodes, paletteKey])
const layout = useMemo<SankeyChartLayout<NodeMeta, LinkMeta>>(
diff --git a/packages/quill/packages/charts/src/core/color-utils.test.ts b/packages/quill/packages/charts/src/core/color-utils.test.ts
index 61f4973caa07..2e788e8e4609 100644
--- a/packages/quill/packages/charts/src/core/color-utils.test.ts
+++ b/packages/quill/packages/charts/src/core/color-utils.test.ts
@@ -1,4 +1,5 @@
import { mixColors, prefersLightText, resolveCssColor } from './color-utils'
+import { DEFAULT_CHART_COLORS } from './theme'
describe('mixColors', () => {
it.each([
@@ -58,17 +59,20 @@ describe('resolveCssColor', () => {
it('resolves a CSS variable to its computed value', () => {
const root = rootWithVars({ '--data-color-1': '#abcdef' })
- expect(resolveCssColor('var(--data-color-1)', root)).toBe('#abcdef')
+ expect(resolveCssColor('var(--data-color-1)', { root })).toBe('#abcdef')
})
it('uses the fallback when the variable is undefined', () => {
const root = rootWithVars({})
- expect(resolveCssColor('var(--missing, #123456)', root)).toBe('#123456')
+ expect(resolveCssColor('var(--missing, #123456)', { root })).toBe('#123456')
})
- it('returns the raw string when the variable is undefined and has no fallback', () => {
+ it.each([
+ ['the given fallback', { fallback: '#654321' }, '#654321'],
+ ['the first palette color', {}, DEFAULT_CHART_COLORS[0]],
+ ])('returns %s, never the raw var(), when the variable is undefined', (_, options, expected) => {
const root = rootWithVars({})
- expect(resolveCssColor('var(--missing)', root)).toBe('var(--missing)')
+ expect(resolveCssColor('var(--missing)', { root, ...options })).toBe(expected)
})
})
diff --git a/packages/quill/packages/charts/src/core/color-utils.ts b/packages/quill/packages/charts/src/core/color-utils.ts
index fffa5fb6f71b..80b70722d9c8 100644
--- a/packages/quill/packages/charts/src/core/color-utils.ts
+++ b/packages/quill/packages/charts/src/core/color-utils.ts
@@ -1,5 +1,6 @@
import { color as d3Color, rgb as d3Rgb } from 'd3-color'
+import { DEFAULT_CHART_COLORS } from './theme'
import type { ResolvedSeries, Series } from './types'
/** Translucent variant of a series colour (any CSS color form d3 can parse — hex 3/6 digit,
@@ -8,30 +9,36 @@ export function dimColor(color: string, alpha: number): string {
return d3Color(color)?.copy({ opacity: alpha }).toString() ?? color
}
-/** Resolve a CSS custom-property reference (`var(--x)`, with an optional fallback) to its
+interface ResolveCssColorOptions {
+ /** Element to read the custom property from. Defaults to `document.body`, to match {@link themeFromCssVars}. */
+ root?: HTMLElement
+ fallback?: string
+}
+
+/** Resolve a CSS custom-property reference (`var(--x)`, with an optional CSS fallback) to its
* computed color value. A canvas 2D context can't resolve `var()`, so an accent handed to
* `fillStyle`/`strokeStyle` (or through `dimColor`, which relies on `d3.color` parsing it)
- * must be a concrete color first. Non-`var()` inputs, and SSR/no-DOM contexts, return the
- * input unchanged; `root` defaults to `document.body` to match {@link themeFromCssVars}. */
-export function resolveCssColor(color: string, root?: HTMLElement): string {
+ * must be a concrete color first. A reference that does not resolve returns `fallback`, because
+ * the canvas ignores an invalid color and paints with the color of the previous stroke. */
+export function resolveCssColor(
+ color: string,
+ { root, fallback = DEFAULT_CHART_COLORS[0] }: ResolveCssColorOptions = {}
+): string {
const trimmed = color.trim()
if (!trimmed.startsWith('var(')) {
return color
}
- if (typeof document === 'undefined' || typeof getComputedStyle !== 'function') {
- return color
- }
const match = /^var\(\s*(--[^,)]+?)\s*(?:,\s*([\s\S]+))?\)$/.exec(trimmed)
- if (!match) {
- return color
+ if (!match || typeof document === 'undefined' || typeof getComputedStyle !== 'function') {
+ return fallback
}
- const [, name, fallback] = match
+ const [, name, cssFallback] = match
const el = root ?? document.body
const value = getComputedStyle(el).getPropertyValue(name).trim()
if (value) {
return value
}
- return fallback && fallback.trim() ? resolveCssColor(fallback.trim(), el) : color
+ return cssFallback?.trim() ? resolveCssColor(cssFallback.trim(), { root: el, fallback }) : fallback
}
/** Linear RGB interpolation between two colors. `t` is clamped to [0, 1]; `t=0` returns `from`,