Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .changeset/tender-beans-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@vanilla-extract/css': minor
---

`style`: Add support for `@starting-style` rules

**EXAMPLE USAGE**:

```ts
import { style } from '@vanilla-extact/css';
export const styleWithStartingStyle = style({
backgroundColor: 'black',
'@starting-style': {
backgroundColor: 'white',
},
});
``
7 changes: 7 additions & 0 deletions fixtures/features/src/features.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ export const styleVariantsCompositionInSelector = styleVariants({
globalStyle(`body ${styleVariantsCompositionInSelector.variant}`, {
fontSize: '24px',
});

export const styleWithStartingStyle = style({
backgroundColor: 'black',
'@starting-style': {
backgroundColor: 'white',
},
});
1 change: 1 addition & 0 deletions fixtures/features/src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default `
<div id="${testNodes.compositionOnly}" class="${styles.compositionOnly}">Composition only</div>
<div id="${testNodes.styleCompositionInSelector}" class="${styles.styleCompositionInSelector}">Style composition in selector</div>
<div id="${testNodes.styleVariantsCompositionInSelector}" class="${styles.styleVariantsCompositionInSelector.variant}">Style variants composition in selector</div>
<div id="${testNodes.styleWithStartingStyle}" class="${styles.styleWithStartingStyle}">Style with @starting-style rule</div>
`;

// @ts-expect-error Vite env not defined
Expand Down
3 changes: 2 additions & 1 deletion fixtures/features/test-nodes.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"styleVariantsWithMappedComposition": "styleVariantsWithMappedComposition",
"compositionOnly": "compositionOnly",
"styleCompositionInSelector": "styleCompositionInSelector",
"styleVariantsCompositionInSelector": "styleVariantsCompositionInSelector"
"styleVariantsCompositionInSelector": "styleVariantsCompositionInSelector",
"styleWithStartingStyle": "styleWithStartingStyle"
}
334 changes: 334 additions & 0 deletions packages/css/src/transformCss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2500,6 +2500,340 @@ describe('transformCss', () => {
}
`);
});

it('should handle @starting-style declaration', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
opacity: 1,
top: '100%',
'@starting-style': {
opacity: 0,
top: '50%',
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
.testClass {
opacity: 1;
top: 100%;
@starting-style {
opacity: 0;
top: 50%;
}
}
`);
});

it('should handle @starting-style inside media queries', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
display: 'flex',
'@media': {
'screen and (min-width: 700px)': {
top: '0',
'@starting-style': {
top: '100%',
},
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
.testClass {
display: flex;
}
@media screen and (min-width: 700px) {
.testClass {
top: 0;
@starting-style {
top: 100%;
}
}
}
`);
});

it('should handle @starting-style inside container queries', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
'@container': {
'sidebar (min-width: 700px)': {
top: '0',
'@starting-style': {
top: '100%',
},
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
@container sidebar (min-width: 700px) {
.testClass {
top: 0;
@starting-style {
top: 100%;
}
}
}
`);
});

it('should handle @starting-style inside a layer', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
'@layer': {
'mock-layer': {
top: '0',
'@starting-style': {
top: '100%',
},
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
@layer mock-layer;
@layer mock-layer {
.testClass {
top: 0;
@starting-style {
top: 100%;
}
}
}
`);
});

it('should handle @starting-style inside selectors', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
selectors: {
'&:hover': {
top: '0',
'@starting-style': {
top: '100%',
},
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
.testClass:hover {
top: 0;
@starting-style {
top: 100%;
}
}
`);
});

it('should handle @starting-style inside @supports', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
'@supports': {
'(display: grid)': {
top: '0',
'@starting-style': {
top: '100%',
},
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
@supports (display: grid) {
.testClass {
top: 0;
@starting-style {
top: 100%;
}
}
}
`);
});

it('should process both simple pseudos and selectors inside @starting-style', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
'@starting-style': {
color: 'green',
':hover': {
color: 'red',
},
':focus': {
backgroundColor: 'blue',
},
selectors: {
'&.active': {
color: 'purple',
},
'& + &': {
marginLeft: '10px',
},
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
.testClass {
@starting-style {
color: green;
:hover {
color: red;
}
:focus {
background-color: blue;
}
selectors {
&.active {
color: purple;
}
& + & {
margin-left: 10px;
}
}
}
}
`);
});

it('should not process simple pseudos and selectors inside @starting-style for non-local root types', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: [],
cssObjs: [
{
type: 'global',
selector: '.globalClass',
rule: {
'@starting-style': {
color: 'green',
':hover': {
color: 'red',
},
':focus': {
backgroundColor: 'blue',
},
selectors: {
'&.active': {
color: 'purple',
},
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
.globalClass {
@starting-style {
color: green;
:hover {
color: red;
}
:focus {
background-color: blue;
}
selectors {
&.active {
color: purple;
}
}
}
}
`);
});

it('should throw an error when a at-rule is use inside @starting-style scope', () => {
expect(() =>
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
'@starting-style': {
// @ts-expect-error - Using a media query inside @starting-style for testing purposes
'@media': {
'screen and (min-width: 700px)': {
display: 'grid',
},
},
},
},
},
],
}),
).toThrowErrorMatchingInlineSnapshot(
'Nested at-rules (e.g. "@media") are not allowed inside @starting-style.',
);
});
});

endFileScope();
Loading