An open source design system that's fully customizable and agent ready
TypeScript
refactor(charts): consume public data variables (#6833)
Cindy Zhangd6da8e43ff2a
diff --git a/packages/build/__fixtures__/layer-split/App.tsx b/packages/build/__fixtures__/layer-split/App.tsxindex 27300c0d17..6746cd102d 100644--- a/packages/build/__fixtures__/layer-split/App.tsx+++ b/packages/build/__fixtures__/layer-split/App.tsx@@ -9,13 +9,13 @@* overrides on the same element, so the built stylesheet answers which layer* wins rather than only which rules were emitted.*- * The `data-*` swatches read `--color-data-*` custom properties, whose defaults- * are declared once at `:root`. Each one is a different cascade question: does- * the default reach an element, does a theme's override beat it, and does a- * nested theme inherit that override instead of shadowing it.+ * The `data-*` swatches use the public StyleX data variables. Importing that+ * group emits the canonical defaults in `astryx-base`; the fixture then asks+ * whether a theme override wins and whether a nested theme inherits it.*/import * as stylex from '@stylexjs/stylex';+import {dataVars} from '@astryxdesign/core/theme/dataTokens.stylex';import {Button} from '@astryxdesign/core/Button';import {Theme} from '@astryxdesign/core/theme';import {nestedTheme, nestedDarkTheme} from './theme';@@ -55,35 +55,35 @@ export function App() {/><spanid="data-default"- style={{color: 'var(--color-data-categorical-orange)'}}>+ style={{color: dataVars['--color-data-categorical-orange']}}>default</span><spanid="data-override"- style={{color: 'var(--color-data-categorical-blue)'}}>+ style={{color: dataVars['--color-data-categorical-blue']}}>overridden</span><Theme theme={nestedTheme}><spanid="data-nested"- style={{color: 'var(--color-data-categorical-blue)'}}>+ style={{color: dataVars['--color-data-categorical-blue']}}>nested, inherited</span><spanid="data-nested-default"- style={{color: 'var(--color-data-categorical-orange)'}}>+ style={{color: dataVars['--color-data-categorical-orange']}}>nested, default</span></Theme><Theme theme={nestedDarkTheme} mode="dark"><spanid="data-dark"- style={{color: 'var(--color-data-categorical-blue)'}}>+ style={{color: dataVars['--color-data-categorical-blue']}}>dark, inherited</span><spanid="data-dark-default"- style={{color: 'var(--color-data-neutral)'}}>+ style={{color: dataVars['--color-data-neutral']}}>dark, default</span></Theme>diff --git a/packages/lab/src/Chart/ChartCandlestick.test.tsx b/packages/lab/src/Chart/ChartCandlestick.test.tsxindex 2f414d5fa8..8c206b746b 100644--- a/packages/lab/src/Chart/ChartCandlestick.test.tsx+++ b/packages/lab/src/Chart/ChartCandlestick.test.tsx@@ -3,10 +3,12 @@/*** @file ChartCandlestick.test.tsx* @input Renders ChartCandlestick inside Chart with a stubbed ResizeObserver- * @output Render-smoke coverage for candlesticks: default and bar variants, up/down colors, band guard (#4295)+ * @output Render-smoke coverage for candlesticks: default public data variables,+ * explicit up/down colors, variants, and the band guard (#4295)* @position Colocated test for ChartCandlestick.tsx*/+import {dataVars} from '@astryxdesign/core/theme/dataTokens.stylex';import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';import {render, act} from '@testing-library/react';import {Chart} from './Chart';@@ -67,6 +69,27 @@ describe('ChartCandlestick', () => {expect(container.querySelectorAll(PLOT_RECTS)).toHaveLength(3);});+ it('uses public data variables for default up and down colors', () => {+ const {container} = render(+ <Chart data={ohlc} xKey="day" yKeys={['high', 'low']}>+ <ChartCandlestick high="high" low="low" open="open" close="close" />+ </Chart>,+ );++ reportWidth(464);++ const bodies = Array.from(container.querySelectorAll(PLOT_RECTS));+ expect(bodies.map(body => body.getAttribute('fill'))).toEqual([+ dataVars['--color-data-categorical-green'],+ dataVars['--color-data-categorical-red'],+ dataVars['--color-data-categorical-green'],+ ]);+ expect(container.querySelector('line')).toHaveAttribute(+ 'stroke',+ dataVars['--color-data-categorical-green'],+ );+ });+it('renders a range line and open/close ticks per datum in the bar variant', () => {const {container} = render(<Chart data={ohlc} xKey="day" yKeys={['high', 'low']}>diff --git a/packages/lab/src/Chart/ChartCandlestick.tsx b/packages/lab/src/Chart/ChartCandlestick.tsxindex ec0e09baf8..50ff4e9b01 100644--- a/packages/lab/src/Chart/ChartCandlestick.tsx+++ b/packages/lab/src/Chart/ChartCandlestick.tsx@@ -2,6 +2,7 @@/*** @file ChartCandlestick.tsx+ * @input Chart context plus public StyleX data variables and optional colors* @output Renders candlestick/box-whisker marks for OHLC or statistical data* @position Child of Chart; reads scales from context*@@ -10,6 +11,7 @@* - `'bar'` — OHLC bar — vertical line with open/close ticks*/+import {dataVars} from '@astryxdesign/core/theme/dataTokens.stylex';import {useChart} from './ChartContext';import {isBandScale} from './utils';@@ -96,8 +98,8 @@ export function ChartCandlestick({const fill =color ??(isUp- ? (upColor ?? 'var(--color-data-categorical-green)')- : (downColor ?? 'var(--color-data-categorical-red)'));+ ? (upColor ?? dataVars['--color-data-categorical-green'])+ : (downColor ?? dataVars['--color-data-categorical-red']));const centerX = xVal + bw / 2;const yHigh = yScale(hVal);diff --git a/scripts/data-token-consumers.test.mjs b/scripts/data-token-consumers.test.mjsnew file mode 100644index 0000000000..d6133207fd--- /dev/null+++ b/scripts/data-token-consumers.test.mjs@@ -0,0 +1,54 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file data-token-consumers.test.mjs+ * @input Tracked TypeScript source in every workspace package and app+ * @output Fails when source constructs a fallback-free `var(--color-data-*)`+ * @position Repository guard for the public dataVars consumer contract+ *+ * A literal data-variable string never reaches StyleX, so nothing retains the+ * canonical group and the color is undefined wherever no other consumer+ * imports `dataVars`. CSS-capable code passes `dataVars[...]`; raw CSS and+ * templates that cannot import it carry an explicit fallback instead.+ */++import {execFileSync} from 'node:child_process';+import fs from 'node:fs';+import path from 'node:path';+import {fileURLToPath} from 'node:url';+import {describe, expect, it} from 'vitest';++const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');+const FALLBACK_FREE_DATA_VAR = /var\(--color-data-[a-z0-9-]+\)/g;++function trackedSource() {+ return execFileSync(+ 'git',+ [+ 'ls-files',+ '--',+ 'packages/*.ts',+ 'packages/*.tsx',+ 'apps/*.ts',+ 'apps/*.tsx',+ ],+ {cwd: ROOT, encoding: 'utf8'},+ )+ .split('\n')+ .filter(Boolean)+ .filter(file => !/\.test\.tsx?$|\.d\.ts$/.test(file));+}++describe('data-token consumers', () => {+ it('pass dataVars instead of constructing a fallback-free var() string', () => {+ const offenders = [];+ for (const file of trackedSource()) {+ const source = fs.readFileSync(path.join(ROOT, file), 'utf8');+ for (const match of source.matchAll(FALLBACK_FREE_DATA_VAR)) {+ const line = source.slice(0, match.index).split('\n').length;+ offenders.push(`${file}:${line} ${match[0]}`);+ }+ }+ expect(offenders).toEqual([]);+ });+});