fix(react-ui): stop false "unknown key" warnings for valid *ChartPalette theme keys#721
Open
ankit-thesys wants to merge 1 commit into
Open
fix(react-ui): stop false "unknown key" warnings for valid *ChartPalette theme keys#721ankit-thesys wants to merge 1 commit into
ankit-thesys wants to merge 1 commit into
Conversation
…eme keys The Theme type declares eight *ChartPalette keys, but both theme validators derived their allow-list from Object.keys(defaultLightTheme), which defines none of them - so the typed chart-theming API warned "unknown key ... It will be ignored" on every dev boot (twice per key), even though the palettes flow through the merge and charts apply them. Both validators now share one allow-list (default theme keys + a CHART_PALETTE_KEYS constant guarded by `satisfies Record<keyof ChartColorPalette, true>`), so type/runtime drift is a compile error. Regression tests cover the allow-list, warning behavior, and palette flow. Fixes #714 Co-authored-by: Cursor <cursoragent@cursor.com>
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #714
Background
Theming charts the typed way —
createTheme({ defaultChartPalette: [...] })passed toThemeProvider'slightTheme/darkTheme— compiles cleanly but printed dev-console warnings on every boot, twice per palette key:The
Themetype declares eight*ChartPalettekeys (default/bar/line/area/pie/radar/radial/horizontalBar), but both validators derived their allow-list fromObject.keys(defaultLightTheme)(ThemeProvider.tsx:112,utils.ts:70), and the default themes define none of those keys — so every valid palette key tripped "unknown key". A runtime probe through the realThemeProvider → useTheme() → useChartPalettechain showed the warning text was wrong: the theme merge spreads user overrides wholesale, so the palettes were never ignored — charts applied them all along, including the fallback todefaultChartPalettefor unset chart types. Dev-only noise, but convincing enough that a downstream consumer abandoned theme-level palettes for per-chartcustomPaletteworkarounds.Goal
Make the typed chart-palette API usable without false warnings, keep typo detection for genuinely unknown keys, and ensure the type-vs-runtime drift that caused this cannot silently recur.
What this PR does
KNOWN_THEME_KEYS=Object.keys(defaultLightTheme)+CHART_PALETTE_KEYS, consumed by bothcreateTheme()and the ThemeProvider prop validator (previously two independent copies).CHART_PALETTE_KEYSwithsatisfies Record<keyof ChartColorPalette, true>, so adding or removing a palette key on the type without updating the allow-list is a compile error.defaultTheme.ts: a default palette would shadow the per-chartthemeprop fallback inuseChartPalette, andthemeToCssVarsfilters to string values, so array palettes never belonged in CSS vars anyway.ThemeProvider/__tests__/ThemeProvider.test.tsx) plustsconfig.test.json(required by the ESLint override for__tests__files, mirroring react-headless), and updates the ThemeProviderAGENTS.mdwith the palette-key exception to the "adding a new token" recipe.Outcome
Re-ran the exact pre-fix reproduction probe against this branch:
theme.barChartPalettein contextdefaultChartPalete)defaultChartPaletteVerification:
pnpm --filter @openuidev/react-ui run test— 13/13 pass (7 new: allow-list covers every declared palette key + every default-theme key;createThemesilent for all 8 palette keys; ThemeProvider props silent for palette keys; genuine unknown keys still warn with suggestion; palettes flow theme →useTheme()→useChartPaletteincl.defaultChartPalettefallback)pnpm --filter @openuidev/react-ui run typecheck— cleanpnpm --filter @openuidev/react-ui run lint:check— 0 errors (pre-existing warnings only)pnpm --filter @openuidev/react-ui run format:check— clean