Files
supabase/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultChart.tsx
T
Danny White d067e81a69 fix(ui): align primary colours across text, buttons, and controls (#50697)
## Problem

Primary colour serves readable text and selected controls, but those
uses need different shades. Light mode needs darker text, while dark
mode needs a deeper button fill. Fixed brand green on interactive chrome
also prevents a custom primary hue from carrying through the interface.
Some slider tracks and selected text are hard to read.

## Solution

- Keep `--primary` for accessible text and small selected indicators.
Use `--primary-solid` for button fills, which need a deeper shade in
dark mode.
- Add `--primary-bright` for focus rings, selected control chrome, chart
accents, and other interactive highlights. It follows `--primary-hue`;
`brand-*` stays fixed for Supabase identity.
- Make slider troughs clearer and text selection translucent with theme
foreground text.
- Document the split in the design-system colour guide.

| Before | After |
| --- | --- |
| <img width="980" height="244" alt="Before: light mode primary
controls"
src="https://github.com/user-attachments/assets/dfae325d-0dfe-4231-8bcd-3f89c4b9d793"
/> | <img width="982" height="204" alt="After: light mode primary
controls"
src="https://github.com/user-attachments/assets/5fdcb531-a6e3-4549-8a13-9d9a5ebe6e20"
/> |
| <img width="610" height="120" alt="Before: slider track"
src="https://github.com/user-attachments/assets/04f768e0-51e8-4d06-9b97-c52f4a34f122"
/> | <img width="622" height="126" alt="After: slider track"
src="https://github.com/user-attachments/assets/95127f4e-13dc-4f0f-b63c-cf5d70a28b42"
/> |
| <img width="652" height="512" alt="Before: dark mode controls"
src="https://github.com/user-attachments/assets/3f88de66-90cc-40ee-8cf1-b5f4eb87b09a"
/> | <img width="658" height="498" alt="After: dark mode controls"
src="https://github.com/user-attachments/assets/906bec30-6ca1-4614-9fb3-6cf5e5feec22"
/> |

## Review instructions

1. Compare light and dark mode in the [colour usage
guide](https://design-system-git-dnywh-fix-bright-brand-chrome-supabase.vercel.app/design-system/docs/color-usage#primary-and-brand-colors).
Check primary ink, primary-solid, primary-bright, and fixed brand
swatches.
2. In Studio, open the ‘new table’ sheet in [Table
Editor](https://studio-staging-git-dnywh-fix-bright-brand-chrome-supabase.vercel.app/dashboard/project/_/editor).
Tab through the new table sheet's fields and toggles. Check the focus
rings, selected controls, and the sheet's edges in both themes. You do
not need to save a table.
3. Select text in Studio in both themes, including a link or
primary-coloured label. The selection and text should remain legible.
4. Check the
[Field](https://design-system-git-dnywh-fix-bright-brand-chrome-supabase.vercel.app/design-system/docs/components/field)
Price Range slider: the unused track should remain visible in both
themes. The selected field card border should follow primary-bright.
5. Check the
[Button](https://design-system-git-dnywh-fix-bright-brand-chrome-supabase.vercel.app/design-system/docs/components/button)
and [Radio
Group](https://design-system-git-dnywh-fix-bright-brand-chrome-supabase.vercel.app/design-system/docs/components/radio-group)
previews. In dark mode, `primary` button fill should be deeper than
primary
[text](https://design-system-git-dnywh-fix-bright-brand-chrome-supabase.vercel.app/design-system/docs/color-usage#text);
selected radios should remain readable.
2026-09-24 09:56:31 +10:00

164 lines
5.6 KiB
TypeScript

import { useMemo } from 'react'
import { cn, type ChartConfig as ChartSeriesConfig } from 'ui'
import { Chart, ChartBar, ChartCard, ChartContent, ChartLine } from 'ui-patterns/Chart'
import { type QueryResult } from '../types'
import NoDataPlaceholder from '@/components/ui/Charts/NoDataPlaceholder'
import {
computeYAxisWidth,
formatLogTick,
formatYAxisTick,
getCumulativeResults,
} from '@/components/ui/QueryBlock/QueryBlock.utils'
import { type ChartConfig } from '@/data/content/notebooks/notebook-schema'
interface QueryResultChartProps {
chart?: ChartConfig
result?: QueryResult
}
const Y_SERIES_COLORS = ['var(--primary-bright)', 'var(--chart-2)', 'var(--chart-3)']
const toChartValue = (value: unknown): string | number => {
if (typeof value === 'number' || typeof value === 'string') return value
if (value === null || value === undefined) return ''
return String(value)
}
/**
* Y series are keyed by position rather than by the column name they came from.
*
* `ChartContainer` writes every config key into a `<style>` element as
* `--color-<key>`, and rrweb records `<style>` text verbatim: its text-node serializer
* skips masking whenever the parent is a `STYLE` element, so neither `maskTextFn` nor
* `maskAttributeFn` sees it. Keying by column name would therefore put the customer's own
* column names into a recording. The name still reaches the chart as `label`, which
* renders as text and is masked.
*
* The X column keeps its own name. Only config keys reach the `<style>`, and the X column
* is never a config key, so renaming it buys nothing. It also costs something: ChartBar
* and ChartLine branch on `xKey === 'timestamp'` to format tooltip dates and render the
* date-range footer.
*/
const seriesKeyFor = (index: number) => `series_${index}`
/**
* Guards the one collision the positional keys introduce: an X column literally named
* `series_0`. Appends underscores until the X key is distinct from every series key.
*/
export function xKeyFor(xColumn: string, seriesKeys: string[]): string {
let key = xColumn
while (seriesKeys.includes(key)) key = `${key}_`
return key
}
export const QueryResultChart = ({ chart, result }: QueryResultChartProps) => {
const { type, x_column, y_series = [], cumulative, show_labels, scale } = chart ?? {}
const hasConfig = !!x_column && y_series.length > 0
// Logarithmic scale only makes sense for a single series — DisplaySettingsButton
// resets `scale` to linear once a second Y column is added
const effectiveScale = y_series.length > 1 ? 'linear' : scale
const seriesKeys = useMemo(() => y_series.map((_, index) => seriesKeyFor(index)), [y_series])
const chartConfig: ChartSeriesConfig = useMemo(
() =>
y_series.reduce((acc, column, index) => {
acc[seriesKeyFor(index)] = { label: column, color: Y_SERIES_COLORS[index] }
return acc
}, {} as ChartSeriesConfig),
[y_series]
)
const xKey = useMemo(() => xKeyFor(x_column ?? '', seriesKeys), [x_column, seriesKeys])
const chartRows = useMemo(() => {
const sourceColumn = x_column ?? ''
return (result?.rows ?? []).map((row) => {
const chartRow: Record<string, string | number> = { [xKey]: toChartValue(row[sourceColumn]) }
y_series.forEach((column, index) => {
chartRow[seriesKeyFor(index)] = toChartValue(row[column])
})
return chartRow
})
}, [result, x_column, xKey, y_series])
const cumulativeResults = useMemo(
() => getCumulativeResults({ rows: chartRows }, { yKey: seriesKeys }),
[chartRows, seriesKeys]
)
const resultToRender = cumulative ? cumulativeResults : chartRows
const yAxisWidth = Math.max(
36,
...seriesKeys.map((key) =>
computeYAxisWidth(resultToRender, key, { isLogScale: effectiveScale === 'log' })
)
)
const yAxisProps = {
...(show_labels ? { width: yAxisWidth } : {}),
scale: effectiveScale === 'log' ? 'log' : 'auto',
domain: effectiveScale === 'log' ? ([1, 'auto'] as const) : undefined,
tickFormatter: effectiveScale === 'log' ? formatLogTick : formatYAxisTick,
}
if (!result || (result?.rows && result.rows.length === 0)) {
return (
<NoDataPlaceholder
className="border-0 min-h-0! py-8"
size="normal"
message="No results"
description="Your query returned no rows"
/>
)
}
if (!hasConfig) {
return (
<NoDataPlaceholder
className="border-0 min-h-0! py-8"
size="normal"
message="Configure your chart"
description="Select your X and Y axis in the display settings"
/>
)
}
return (
<Chart className="flex flex-grow min-h-0">
<ChartCard className="flex flex-grow rounded-none border-0 min-h-0">
<ChartContent className={cn('min-h-0 h-full w-full', show_labels && 'pl-2 pb-2')}>
{type === 'bar' && (
<ChartBar
isFullHeight
xKey={xKey}
dataKey={seriesKeys[0]}
dataKeys={seriesKeys}
config={chartConfig}
showXAxis={show_labels}
showYAxis={show_labels}
data={resultToRender}
YAxisProps={yAxisProps}
/>
)}
{type === 'line' && (
<ChartLine
isFullHeight
xKey={xKey}
dataKey={seriesKeys[0]}
dataKeys={seriesKeys}
config={chartConfig}
showXAxis={show_labels}
showYAxis={show_labels}
data={resultToRender}
YAxisProps={yAxisProps}
/>
)}
</ChartContent>
</ChartCard>
</Chart>
)
}