Skip to content

Commit 0f64c39

Browse files
feat: add support for @starting-style at-rule (when nested) (#1786)
* feat: add support for starting-style at-rule * refactor: update starting-style example * test: add tests for @starting-style at-rule * refactor: remove starting-style from nested postcss plugin * docs: add changeset * refactor: add starting-style to postcss-nested bubble list * test: add starting style example snapshot * test: update snapshot to use hamburger unicode character instead of emoji * test: remove page layout prototype storybook * test: update starting-style test to nest the at-rule
1 parent c8bab93 commit 0f64c39

File tree

8 files changed

+59
-6
lines changed

8 files changed

+59
-6
lines changed

.changeset/ten-pens-refuse.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@compiled/babel-plugin': minor
3+
'@compiled/css': minor
4+
---
5+
6+
Added support for the @starting-style at-rule.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"@typescript-eslint/parser": "^6.21.0",
8989
"@typescript-eslint/utils": "^6.21.0",
9090
"babel-loader": "^9.1.2",
91+
"csstype": "^3.1.3",
9192
"eslint": "^8.57.1",
9293
"eslint-plugin-import": "^2.31.0",
9394
"eslint-plugin-json-files": "^2.2.0",

packages/babel-plugin/src/css-map/__tests__/at-rules-and-selectors.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,35 @@ describe('css map advanced functionality (at rules, selectors object)', () => {
277277
}
278278
});
279279

280+
// TODO: add a unit test for the `@starting-style` at-rule when it is NOT nested. This is currently not working as
281+
// Compiled only supports processing at-rules that have two "halves", e.g. `@media screen`
282+
// When nested, the at-rule is not processed like an at-rule - it is processed like a CSS selector.
283+
it('should parse the @starting-style at-rule when nested', () => {
284+
const actual = transform(`
285+
import { cssMap } from '@compiled/react';
286+
287+
const styles = cssMap({
288+
success: {
289+
color: 'red',
290+
'@media (prefers-reduced-motion: no-preference)': {
291+
'@starting-style': {
292+
color: 'blue'
293+
},
294+
},
295+
},
296+
});
297+
298+
${EXAMPLE_USAGE}
299+
`);
300+
301+
expect(actual).toIncludeMultiple([
302+
'._syaz5scu{color:red}',
303+
'@media (prefers-reduced-motion:no-preference){@starting-style{._ff1013q2{color:blue}}}',
304+
305+
'const styles={success:"_syaz5scu _ff1013q2"}',
306+
]);
307+
});
308+
280309
it('should error if more than one selectors key passed', () => {
281310
expect(() => {
282311
transform(`

packages/babel-plugin/src/utils/css-map.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const atRules: Record<AtRules, boolean> = {
2020
'@namespace': true,
2121
'@page': true,
2222
'@property': true,
23+
'@scope': true,
2324
'@scroll-timeline': true,
25+
'@starting-style': true,
2426
'@supports': true,
2527
'@viewport': true,
2628
};

packages/css/src/plugins/__tests__/atomicify-rules.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,14 @@ describe('atomicify rules', () => {
384384
@supports selector(h2 > p) {
385385
color: pink;
386386
}
387+
388+
@starting-style {
389+
color: green;
390+
}
387391
`;
388392

389393
expect(actual).toMatchInlineSnapshot(
390-
`"@container (width > 300px){._eq985scu h2{color:red}}@when font-tech(color-COLRv1) and font-tech(variations){@font-face{font-family:test;src:url(test.woff2)}}@else font-tech(color-SVG){@font-face{font-family:test;src:url(test2.woff2)}}@else{@font-face{font-family:test;src:url(test3.woff2)}}@-moz-document url-prefix(){._qral13q2{color:blue}}@layer state{._8tgm6x50{background-color:brown}}@media (min-width: 30rem){._hi7c1ule{display:block}._1l5zgktf{font-size:20px}}@supports selector(h2 > p){._1ll732ev{color:pink}}"`
394+
`"@container (width > 300px){._eq985scu h2{color:red}}@when font-tech(color-COLRv1) and font-tech(variations){@font-face{font-family:test;src:url(test.woff2)}}@else font-tech(color-SVG){@font-face{font-family:test;src:url(test2.woff2)}}@else{@font-face{font-family:test;src:url(test3.woff2)}}@-moz-document url-prefix(){._qral13q2{color:blue}}@layer state{._8tgm6x50{background-color:brown}}@media (min-width: 30rem){._hi7c1ule{display:block}._1l5zgktf{font-size:20px}}@supports selector(h2 > p){._1ll732ev{color:pink}}@starting-style{._p77hbf54{color:green}}"`
391395
);
392396
});
393397

packages/css/src/plugins/atomicify-rules.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ const canAtomicifyAtRule = (node: AtRule): boolean => {
188188
'else',
189189
'layer',
190190
'media',
191+
'starting-style',
191192
'supports',
192193
'when',
193194
];

packages/css/src/transform.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ export const transformCss = (
4242
discardEmptyRules(),
4343
parentOrphanedPseudos(),
4444
nested({
45-
bubble: ['container', '-moz-document', 'layer', 'else', 'when'],
45+
bubble: [
46+
'container',
47+
'-moz-document',
48+
'layer',
49+
'else',
50+
'when',
51+
// postcss-nested bubbles `starting-style` by default in versions from 6.0.2 onwards:
52+
// https://github.com/postcss/postcss-nested?tab=readme-ov-file#bubble
53+
// When we upgrade to a version that includes this change, we can remove this from the list.
54+
'starting-style',
55+
],
4656
unwrap: ['color-profile', 'counter-style', 'font-palette-values', 'page', 'property'],
4757
}),
4858
...normalizeCSS(opts),

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7600,10 +7600,10 @@ cssstyle@^2.3.0:
76007600
dependencies:
76017601
cssom "~0.3.6"
76027602

7603-
csstype@^3.0.2, csstype@^3.1.2:
7604-
version "3.1.2"
7605-
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.2.tgz#1d4bf9d572f11c14031f0436e1c10bc1f571f50b"
7606-
integrity sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==
7603+
csstype@^3.0.2, csstype@^3.1.2, csstype@^3.1.3:
7604+
version "3.1.3"
7605+
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.3.tgz#d80ff294d114fb0e6ac500fbf85b60137d7eff81"
7606+
integrity sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==
76077607

76087608
csv-generate@^3.4.3:
76097609
version "3.4.3"

0 commit comments

Comments
 (0)