Skip to content

Commit 9f2cdc0

Browse files
authored
fix(css): every package stylesheet and reset.css register the Astryx layer order (#7585)
* fix(css): every package stylesheet and reset.css register the Astryx layer order (AST-080 FR3) * fix(css): the GitHub Pages landing ships the updated reset.css
1 parent 3649b43 commit 9f2cdc0

6 files changed

Lines changed: 148 additions & 9 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@astryxdesign/core': patch
3+
'@astryxdesign/richtext': patch
4+
---
5+
6+
[fix] `astryx.css`, `reset.css`, and every other package stylesheet (`richtext.css`, `lab.css`, `charts.css`) now open with `@layer reset, astryx-base, astryx-theme;`, the same line theme stylesheets carry. The Astryx layer order holds whichever Astryx stylesheet loads first: loading `reset.css` after `astryx.css` no longer lets the reset override component styles, and theme overrides keep winning. Setups that already declare the order on their own first line are unchanged.
7+
8+
@cixzhang

‎.github/pages/assets/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"files": {
88
"reset.css": {
9-
"sha256": "8423107ecd6d1ea308d277ebc83fb5d6115ccddece700ce0f489ba44d42d8461"
9+
"sha256": "67ea29d9956ef672bef552cfafb3a54dc936a1285296b03207bc0c46a735b41f"
1010
},
1111
"astryx.css": {
1212
"sha256": "bc66e60ffeb0eb3b2f2b303d89ba6bae1e7e922b10bb380f7b25624d73c765ba"

‎.github/pages/assets/reset.css‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
* import '@astryxdesign/core/astryx.css'; // @layer astryx-base — component styles
2626
* import '@astryxdesign/theme-neutral/theme.css'; // @layer astryx-theme — theme overrides
2727
*
28-
* Layer order: reset → astryx-base → astryx-theme
28+
* Layer order: reset → astryx-base → astryx-theme. This file registers that
29+
* order itself, like every Astryx stylesheet, so it holds in any load order.
2930
* Consumer styles (unlayered) always override all Astryx layers.
3031
* The reset has the lowest priority and can be overridden by any other layer.
3132
*
@@ -36,6 +37,8 @@
3637
* - modern-normalize
3738
*/
3839

40+
@layer reset, astryx-base, astryx-theme;
41+
3942
@layer reset {
4043

4144
/* ===========================================================================

‎packages/core/src/reset.css‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
* import '@astryxdesign/core/astryx.css'; // @layer astryx-base — component styles
2626
* import '@astryxdesign/theme-neutral/theme.css'; // @layer astryx-theme — theme overrides
2727
*
28-
* Layer order: reset → astryx-base → astryx-theme
28+
* Layer order: reset → astryx-base → astryx-theme. This file registers that
29+
* order itself, like every Astryx stylesheet, so it holds in any load order.
2930
* Consumer styles (unlayered) always override all Astryx layers.
3031
* The reset has the lowest priority and can be overridden by any other layer.
3132
*
@@ -36,6 +37,8 @@
3637
* - modern-normalize
3738
*/
3839

40+
@layer reset, astryx-base, astryx-theme;
41+
3942
@layer reset {
4043

4144
/* ===========================================================================

‎scripts/build-css.layers.test.mjs‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file build-css.layers.test.mjs
5+
* spec:AST-080 FR3: every package stylesheet registers the Astryx layer order
6+
* and keeps every rule inside its one layer, `astryx-base`.
7+
*/
8+
9+
import fs from 'node:fs/promises';
10+
import path from 'node:path';
11+
import {fileURLToPath} from 'node:url';
12+
import {beforeAll, describe, expect, it} from 'vitest';
13+
import {ensureCoreBuilt} from '../packages/cli/clients/cli/commands/ensure-core-built.mjs';
14+
import {ASTRYX_LAYER_ORDER, composePackageStylesheet} from './build-css.mjs';
15+
16+
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
17+
18+
/**
19+
* Split a stylesheet into its top-level statements, comments removed: each is
20+
* either an at-rule statement ending in `;` or a block (`prelude { … }`).
21+
*/
22+
function topLevelStatements(css) {
23+
const text = css.replace(/\/\*[\s\S]*?\*\//g, '');
24+
const statements = [];
25+
let depth = 0;
26+
let start = 0;
27+
for (let i = 0; i < text.length; i++) {
28+
const ch = text[i];
29+
if (ch === '{') depth++;
30+
else if (ch === '}') {
31+
depth--;
32+
if (depth === 0) {
33+
statements.push(text.slice(start, i + 1).trim());
34+
start = i + 1;
35+
}
36+
} else if (ch === ';' && depth === 0) {
37+
statements.push(text.slice(start, i + 1).trim());
38+
start = i + 1;
39+
}
40+
}
41+
expect(depth).toBe(0);
42+
expect(text.slice(start).trim()).toBe('');
43+
return statements.filter(Boolean);
44+
}
45+
46+
/** Assert FR3's shape: the order line first, then exactly one block in `layer`. */
47+
function expectOneAstryxLayer(css, layer) {
48+
const statements = topLevelStatements(css);
49+
expect(statements[0]).toBe(ASTRYX_LAYER_ORDER);
50+
expect(statements).toHaveLength(2);
51+
expect(statements[1].startsWith(`@layer ${layer} {`)).toBe(true);
52+
}
53+
54+
describe('package stylesheets register the Astryx layer order (spec:AST-080 FR3)', () => {
55+
it('names the same order theme output registers', () => {
56+
expect(ASTRYX_LAYER_ORDER).toBe('@layer reset, astryx-base, astryx-theme;');
57+
});
58+
59+
it('wraps compiled rules after the order line, inside astryx-base only', () => {
60+
const css = composePackageStylesheet(
61+
'Probe',
62+
'.x1{color:red}\n@media (hover: hover){.x2:hover{color:blue}}',
63+
);
64+
expectOneAstryxLayer(css, 'astryx-base');
65+
expect(css).toContain('.x1{color:red}');
66+
expect(css).toContain('@media (hover: hover){.x2:hover{color:blue}}');
67+
});
68+
69+
it('fails the shape check for an unlayered rule', () => {
70+
const css = `${composePackageStylesheet('Probe', '.x1{color:red}')}.x9{color:red}\n`;
71+
expect(() => expectOneAstryxLayer(css, 'astryx-base')).toThrow();
72+
});
73+
74+
it('fails the shape check without the order line', () => {
75+
const css = composePackageStylesheet('Probe', '.x1{color:red}').replace(
76+
`${ASTRYX_LAYER_ORDER}\n`,
77+
'',
78+
);
79+
expect(() => expectOneAstryxLayer(css, 'astryx-base')).toThrow();
80+
});
81+
82+
describe('the built Core stylesheet', () => {
83+
let astryxCss;
84+
beforeAll(async () => {
85+
ensureCoreBuilt();
86+
astryxCss = await fs.readFile(
87+
path.join(ROOT, 'packages/core/dist/astryx.css'),
88+
'utf8',
89+
);
90+
}, 180_000);
91+
92+
it('astryx.css is the order line plus one astryx-base block', () => {
93+
expectOneAstryxLayer(astryxCss, 'astryx-base');
94+
});
95+
});
96+
97+
it('reset.css is the order line plus one reset block', async () => {
98+
const resetCss = await fs.readFile(
99+
path.join(ROOT, 'packages/core/src/reset.css'),
100+
'utf8',
101+
);
102+
expectOneAstryxLayer(resetCss, 'reset');
103+
});
104+
});

‎scripts/build-css.mjs‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
* This script:
1313
* 1. Runs Babel with the StyleX plugin over the target package's source files
1414
* 2. Collects all StyleX rules
15-
* 3. Outputs a combined stylesheet with all rules in @layer astryx-base
15+
* 3. Outputs a combined stylesheet that registers the Astryx layer order and
16+
* places all rules in @layer astryx-base
1617
*
1718
* Dist consumers import the full stylesheet, e.g.:
1819
* import '@astryxdesign/core/astryx.css';
@@ -302,6 +303,26 @@ export async function collectStyleXCSS(target) {
302303
return allRules;
303304
}
304305

306+
/**
307+
* The cascade-layer order every Astryx stylesheet registers before its own
308+
* rules (spec:AST-080 FR3), the same line theme output carries
309+
* (spec:AST-066 FR7). Registering it in each file keeps
310+
* `reset < astryx-base < astryx-theme` whichever Astryx stylesheet loads first.
311+
*/
312+
export const ASTRYX_LAYER_ORDER = '@layer reset, astryx-base, astryx-theme;';
313+
314+
/**
315+
* Wrap a package's compiled StyleX rules as its prebuilt stylesheet: a banner,
316+
* the layer-order registration, then every rule inside `@layer astryx-base`.
317+
*/
318+
export function composePackageStylesheet(banner, css) {
319+
const body = css
320+
.split('\n')
321+
.map(line => ' ' + line)
322+
.join('\n');
323+
return `/* ${banner} */\n/* Auto-generated. Do not edit manually. */\n\n${ASTRYX_LAYER_ORDER}\n\n@layer astryx-base {\n${body}\n}\n`;
324+
}
325+
305326
async function main() {
306327
const target = parseTarget();
307328
const allRules = await collectStyleXCSS(target);
@@ -316,11 +337,11 @@ async function main() {
316337
const combinedCSS = stylexBabelPlugin.processStylexRules(allRules, false);
317338

318339
const outPath = path.resolve(target.dist, target.outFile);
319-
const combinedFileContents = `/* ${target.banner} */\n/* Auto-generated. Do not edit manually. */\n\n@layer astryx-base {\n${combinedCSS
320-
.split('\n')
321-
.map(line => ' ' + line)
322-
.join('\n')}\n}\n`;
323-
await fs.writeFile(outPath, combinedFileContents, 'utf8');
340+
await fs.writeFile(
341+
outPath,
342+
composePackageStylesheet(target.banner, combinedCSS),
343+
'utf8',
344+
);
324345
console.log(
325346
`${target.outFile}: ${(combinedCSS.length / 1024).toFixed(1)} KB`,
326347
);

0 commit comments

Comments
 (0)