Theme: wire up theme.chart and decide how the chart background color works
Problem
RechartsTheme.chart (a CSSProperties) has been on the theme type for a while, but nothing reads it. #7879 starts using chart.backgroundColor: Treemap outlines its tiles in that color, and lightTheme / darkTheme now set it to #fff / #18181b.
The built-in themes are each designed for one background color, but that color is only stated implicitly:
- Recharts never paints a chart background. It depends entirely on the host page.
- Before #7879 the only place the background showed up was
tooltip.contentStyle.backgroundColorandlegend.wrapperStyle.backgroundColor. Those happen to match the chart background, but only by coincidence, not by intent. test/theme/themeAccessibility.spec.tshardcodes the assumed background per theme (CHART_BACKGROUND) and checks it against those surfaces.
So a chart using darkTheme on a white page renders with light text and no background of its own. Several colors are chosen relative to the background: axis, grid, the active fill of shapes, Treemap tile separators, and possibly the future label color picking (see #7880). Those tokens have nothing reliable to refer to.
Goals
- Have one intended, documented place that says what background a theme is designed for.
- Let components that need it (Treemap separators, active shapes that "cut out", legible label colors) read it from there.
- Keep existing charts looking the same where possible.
Alternatives
1. Paint theme.chart on the chart wrapper, and set an opaque background in the built-in themes
Merge theme.chart styles into the RechartsWrapper style, below the user's explicit style prop, so user styles win. The built-in themes set backgroundColor: '#fff' / '#18181b'.
- The theme looks as designed on any page, with contrast guaranteed.
- Components can use the color for comparisons (label contrast) and for drawing (separators, cut-out active shapes).
- Visible change: every themed chart gets a solid background. That changes many VR screenshots, and it may surprise users who put a themed chart on a card with a different color.
RechartsWrapperalso renders Treemap and Sankey, so they get it for free. Check thatResponsiveContainerand the surface border radius look fine.
2. Paint theme.chart, but set backgroundColor: 'transparent' in the built-in themes
The wiring from (1), but the built-in themes default to transparent, so nothing visibly changes. Users can opt in to a solid background.
- Minimal visible change, and the connection exists: set a color once and it's both painted and used by components.
- Treemap separators stroked in
transparentwould show the real page background through the gaps, which is exactly right. - Anything that needs to know the color, such as label contrast picking or the accessibility tests, has nothing to work with.
transparentcan't be compared. Those need a separate source for "the other text color", or they have to accept worse contrast. - The accessibility spec loses its link between theme and background, and would go back to a hardcoded assumption.
3. Separate "painted background" from "designed-for background"
Keep chart as styles that are painted (for example transparent by default), and add a token that describes the background the palette was designed for, without painting it. For example surface: '#18181b', or colorScheme: { background }.
- The explicit, intentional version of what the Tooltip background was doing by accident.
- Components can compute contrast and draw matching separators without changing how charts look.
- New theme API. Also, if the real page background differs from the designed-for one, separators and cut-outs look wrong. With (2), transparent separators never look wrong.
4. Use the painted background only where it is safe, and fall back elsewhere
Keep (2), and when the background is transparent, give components individual fallbacks: separators use transparent, label contrast falls back to the typography color, active cut-outs use none.
- No new API and no visual change.
- Silently degrades accessibility in exactly the default setup.
5. Detect the actual background at runtime
Walk up the DOM and read getComputedStyle(...).backgroundColor until something is opaque.
- Works with any page.
- Brittle (images, gradients,
backdrop-filter, portals, SSR), costly, and breaks the declarative model. It's not recommended beyond an experiment.
Open questions
- Should the built-in themes paint a background by default, (1), or stay transparent, (2)?
- If transparent, where do contrast-dependent features get a dark and light reference color from: (3), or accept (4)?
- Should Tooltip and Legend default their
backgroundColortochart.backgroundColor, so there is one source of truth? - Does
chartstay a free-formCSSProperties, or get a narrower documented type?
Acceptance
theme.chartis either wired to the chart wrapper or clearly documented as not painted.themeAccessibility.spec.tsreads the background from the theme instead of hardcodingCHART_BACKGROUND, or explains why it can't.- The decision is reflected in the theming guide.
Source: recharts/recharts