Files
supabase/apps/studio/lib/theme-overrides.ts
Danny White 0cb8bd95dd feat(studio): add spot colour control to Appearance (#50782)
## Problem

The theme's primary hue can change in CSS, but Appearance had no way to
try other spot colours. That makes it hard to find controls whose colour
still depends on the fixed Supabase brand palette.

## Solution

Add a **Spot color** control under Appearance → Theme colors
(employee-only via ConfigCat `appearanceSpotColor`, targeted to Supabase
Team Email).

### Spot color UX
- Rainbow spectrum track with a thin outline so pale tracks stay visible
- Live trifecta swatches for `--primary-solid`, `--primary`, and
`--primary-bright` (darkest → lightest) next to the degree readout
- Drag updates are rAF-batched so React paint and CSS preview stay to
one frame

### Canvas tint coupling
- `--surface-hue` is derived in CSS as `calc(var(--primary-hue) +
var(--surface-hue-offset))`
- Dark: offset `0` (same hue as spot)
- Light: offset `180` (complementary canvas tint; brand green ≈157.5° →
rose ≈337.5°)
- No JS override of `--surface-hue`. Changing Spot color moves primary
controls and the low-chroma canvas tint together

### Other theme sliders
- Renamed **Color intensity** → **Surface tint** (it only drives the
neutral ramp via `--chroma`, not spot chroma)
- Meaning-shaped tracks for every knob (spectrum, grey→tint, soft→hard,
dark→light, flat→lift)
- Same outline treatment on those tracks

| Before | After |
| --- | --- |
| <img width="1476" height="2174" alt="CleanShot 2026-10-05 at 15 02
21@2x"
src="https://github.com/user-attachments/assets/d08b0fa8-32af-450e-adce-861f59c9d6ca"
/> | <img width="1474" height="2354" alt="CleanShot 2026-10-05 at 14 56
35@2x"
src="https://github.com/user-attachments/assets/9a1e6183-a4ba-4c8e-a458-bb0eea946db8"
/> |
| _Anyone else_ | _With staff flag, custom settings_ |

## Review instructions

1. Confirm ConfigCat flag `appearanceSpotColor` is on for your staff
account (or flip it in the Dev Toolbar).
2. Open `/account/me` → **Appearance → Theme colors**.
3. Without the flag: Spot color is hidden; other theme sliders still
work.
4. With the flag: drag Spot color in light and dark. Primary controls
and canvas tint should move together; Supabase brand assets should stay
fixed.
5. Raise Surface tint and confirm the canvas hue follows the
complementary (light) or same-hue (dark) offset.
6. Refresh, switch modes, and use **Reset** to check persistence and
defaults.
2026-10-06 10:11:26 +11:00

229 lines
7.0 KiB
TypeScript

import * as z from 'zod'
export type ThemeOverrideKey = 'primaryHue' | 'chroma' | 'contrast' | 'surface' | 'elevationStep'
export type ThemeOverrideMode = 'dark' | 'light'
/** ConfigCat flag: Spot color control (employee-only via Supabase Team Email segment). */
export const APPEARANCE_SPOT_COLOR_FLAG = 'appearanceSpotColor'
type ThemeOverrideRange = { min: number; max: number }
export interface ThemeOverrideKnob {
key: ThemeOverrideKey
cssVar: string
label: string
description: string
ranges: Record<ThemeOverrideMode, ThemeOverrideRange>
}
export const THEME_OVERRIDE_KNOBS: readonly ThemeOverrideKnob[] = [
{
key: 'primaryHue',
cssVar: '--primary-hue',
label: 'Spot color',
description: 'Changes the hue of primary controls and the canvas.',
ranges: { dark: { min: 0, max: 360 }, light: { min: 0, max: 360 } },
},
{
key: 'chroma',
cssVar: '--chroma',
label: 'Surface tint',
description: 'Controls how much color tints backgrounds, text, and borders.',
ranges: { dark: { min: 0, max: 0.04 }, light: { min: 0, max: 0.03 } },
},
{
key: 'contrast',
cssVar: '--contrast',
label: 'Contrast',
description: 'Controls the difference between text, borders, and backgrounds.',
ranges: { dark: { min: 0.4, max: 0.8 }, light: { min: 0.45, max: 0.8 } },
},
{
key: 'surface',
cssVar: '--surface',
label: 'Surface brightness',
description: 'Controls how light or dark background surfaces appear.',
ranges: { dark: { min: 0.12, max: 0.32 }, light: { min: 0.82, max: 0.995 } },
},
{
key: 'elevationStep',
cssVar: '--elevation-step',
label: 'Layer contrast',
description: 'Controls the difference between stacked surfaces.',
ranges: { dark: { min: 0, max: 0.24 }, light: { min: 0, max: 0.024 } },
},
]
export const THEME_OVERRIDE_DEFAULTS: Record<
ThemeOverrideMode,
Record<ThemeOverrideKey, number>
> = {
dark: { primaryHue: 157.5, chroma: 0.005, contrast: 0.5, surface: 0.19, elevationStep: 0.025 },
light: { primaryHue: 157.5, chroma: 0, contrast: 0.53, surface: 0.995, elevationStep: 0.024 },
}
export type ThemeOverrides = Partial<Record<ThemeOverrideKey, number>>
export type ThemeOverridesByMode = Partial<Record<ThemeOverrideMode, ThemeOverrides>>
const themeOverridesSchema = z
.object({
primaryHue: z.number().finite().optional(),
chroma: z.number().finite().optional(),
contrast: z.number().finite().optional(),
surface: z.number().finite().optional(),
elevationStep: z.number().finite().optional(),
})
.strip()
const themeOverridesByModeSchema = z
.object({ dark: themeOverridesSchema.optional(), light: themeOverridesSchema.optional() })
.strip()
export function resolveThemeOverrideMode(resolvedTheme: string | undefined): ThemeOverrideMode {
return resolvedTheme?.includes('dark') ? 'dark' : 'light'
}
export function getThemeOverrideRange(
knob: ThemeOverrideKnob,
mode: ThemeOverrideMode
): ThemeOverrideRange {
return knob.ranges[mode]
}
export function clampThemeOverride(
knob: ThemeOverrideKnob,
mode: ThemeOverrideMode,
value: number
): number {
const { min, max } = getThemeOverrideRange(knob, mode)
return Math.min(max, Math.max(min, value))
}
export function themeOverrideToSliderValue(
knob: ThemeOverrideKnob,
mode: ThemeOverrideMode,
value: number
): number {
const { min, max } = getThemeOverrideRange(knob, mode)
const clamped = clampThemeOverride(knob, mode, value)
return Math.round(((clamped - min) / (max - min)) * 100)
}
export function sliderValueToThemeOverride(
knob: ThemeOverrideKnob,
mode: ThemeOverrideMode,
value: number
): number {
const { min, max } = getThemeOverrideRange(knob, mode)
const clamped = Math.min(100, Math.max(0, value))
return Number((min + (max - min) * (clamped / 100)).toFixed(6))
}
export function getThemeOverrideValue(
knob: ThemeOverrideKnob,
mode: ThemeOverrideMode,
overrides: ThemeOverrides
): number {
const value = overrides[knob.key] ?? THEME_OVERRIDE_DEFAULTS[mode][knob.key]
return clampThemeOverride(knob, mode, value)
}
function isDefaultValue(knob: ThemeOverrideKnob, mode: ThemeOverrideMode, value: number): boolean {
const defaultValue = THEME_OVERRIDE_DEFAULTS[mode][knob.key]
return (
themeOverrideToSliderValue(knob, mode, value) ===
themeOverrideToSliderValue(knob, mode, defaultValue)
)
}
export function mergeThemeOverride(
current: ThemeOverridesByMode,
mode: ThemeOverrideMode,
key: ThemeOverrideKey,
value: number
): ThemeOverridesByMode {
const knob = THEME_OVERRIDE_KNOBS.find((candidate) => candidate.key === key)
if (knob === undefined) return current
const nextMode = { ...current[mode] }
const clamped = clampThemeOverride(knob, mode, value)
if (isDefaultValue(knob, mode, clamped)) delete nextMode[key]
else nextMode[key] = clamped
if (!hasThemeOverrides(nextMode)) return clearThemeOverridesForMode(current, mode)
return { ...current, [mode]: nextMode }
}
export function clearThemeOverridesForMode(
current: ThemeOverridesByMode,
mode: ThemeOverrideMode
): ThemeOverridesByMode {
const { [mode]: _removed, ...rest } = current
return rest
}
export function hasThemeOverrides(overrides: ThemeOverrides): boolean {
return THEME_OVERRIDE_KNOBS.some((knob) => overrides[knob.key] !== undefined)
}
/** Drops the Spot color override when the employee-only flag is off. */
export function resolveThemeOverridesForFlags(
overrides: ThemeOverrides,
{ isSpotColorEnabled }: { isSpotColorEnabled: boolean }
): ThemeOverrides {
if (isSpotColorEnabled || overrides.primaryHue === undefined) return overrides
const { primaryHue: _removed, ...rest } = overrides
return rest
}
function sanitizeThemeOverrides(
mode: ThemeOverrideMode,
overrides: ThemeOverrides
): ThemeOverrides {
return THEME_OVERRIDE_KNOBS.reduce<ThemeOverrides>((result, knob) => {
const value = overrides[knob.key]
if (value === undefined) return result
const clamped = clampThemeOverride(knob, mode, value)
if (!isDefaultValue(knob, mode, clamped)) result[knob.key] = clamped
return result
}, {})
}
export function parseThemeOverridesByMode(value: unknown): ThemeOverridesByMode {
const parsed = themeOverridesByModeSchema.safeParse(value)
if (!parsed.success) return {}
const dark = sanitizeThemeOverrides('dark', parsed.data.dark ?? {})
const light = sanitizeThemeOverrides('light', parsed.data.light ?? {})
return {
...(hasThemeOverrides(dark) ? { dark } : {}),
...(hasThemeOverrides(light) ? { light } : {}),
}
}
export function applyThemeOverrides(
root: HTMLElement,
mode: ThemeOverrideMode,
overrides: ThemeOverrides
) {
THEME_OVERRIDE_KNOBS.forEach((knob) => {
const value = overrides[knob.key]
if (value === undefined) root.style.removeProperty(knob.cssVar)
else root.style.setProperty(knob.cssVar, String(clampThemeOverride(knob, mode, value)))
})
}
export function previewThemeOverride(
knob: ThemeOverrideKnob,
mode: ThemeOverrideMode,
value: number
) {
document.documentElement.style.setProperty(
knob.cssVar,
String(clampThemeOverride(knob, mode, value))
)
}