Skip to content

Add support for @position-try rules types #1589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .changeset/minions-chips-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/css': minor
---

Add support `@position-try` rules
11 changes: 11 additions & 0 deletions fixtures/features/src/features.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,14 @@ export const styleVariantsCompositionInSelector = styleVariants({
globalStyle(`body ${styleVariantsCompositionInSelector.variant}`, {
fontSize: '24px',
});

// Style with position-try
export const styleWithPositionTry = style({
backgroundColor: 'black',
'@position-try': {
'--custom-left': {
width: '100px',
margin: '0 10px 0 0',
},
},
});
3 changes: 2 additions & 1 deletion fixtures/features/src/html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as styles from './features.css';
import testNodes from '../test-nodes.json';
import * as styles from './features.css';

export default `
<div id="${testNodes.mergedStyle}" class="${styles.mergedStyle}">Merged style</div>
Expand All @@ -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.styleWithPositionTry}" class="${styles.styleVariantsCompositionInSelector.variant}">Style with @position-try</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",
"styleWithPositionTry": "styleWithPositionTry"
}
7 changes: 5 additions & 2 deletions fixtures/next-app-router/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"name": "next"
}
],
"strictNullChecks": true
"strictNullChecks": true,
"module": "esnext"
},
"include": [
"next-env.d.ts",
Expand All @@ -17,5 +18,7 @@
".next/types/**/*.ts",
"dist/types/**/*.ts"
],
"exclude": ["node_modules"]
"exclude": [
"node_modules"
]
}
14 changes: 11 additions & 3 deletions fixtures/next-pages-router/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
"compilerOptions": {
"forceConsistentCasingInFileNames": true,
"jsx": "preserve",
"strictNullChecks": true
"strictNullChecks": true,
"module": "esnext",
"moduleResolution": "node"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
285 changes: 282 additions & 3 deletions packages/css/src/transformCss.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setFileScope, endFileScope } from './fileScope';
import { createVar } from './vars';
import { transformCss } from './transformCss';
import { endFileScope, setFileScope } from './fileScope';
import { style } from './style';
import { transformCss } from './transformCss';
import { createVar } from './vars';

setFileScope('test');

Expand Down Expand Up @@ -2500,6 +2500,285 @@ describe('transformCss', () => {
}
`);
});

it('should handle @position-try declaration', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
display: 'flex',
'@position-try': {
'--custom-left': {
width: '100px',
margin: '0 10px 0 0',
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
.testClass {
display: flex;
}
@position-try --custom-left {
.testClass {
width: 100px;
margin: 0 10px 0 0;
}
}
`);
});

it('should multiple custom position inside @position-try declaration', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
display: 'flex',
'@position-try': {
'--custom-left': {
width: '100px',
margin: '0 10px 0 0',
},
'--custom-right': {
width: '100px',
margin: '0 10px 0 0',
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
.testClass {
display: flex;
}
@position-try --custom-left {
.testClass {
width: 100px;
margin: 0 10px 0 0;
}
}
@position-try --custom-right {
.testClass {
width: 100px;
margin: 0 10px 0 0;
}
}
`);
});

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

it('should handle @position-try inside container queries', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
display: 'flex',
'@container': {
'sidebar (min-width: 700px)': {
'@position-try': {
'--custom-left': {
width: '100px',
margin: '0 10px 0 0',
},
},
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
.testClass {
display: flex;
}
@container sidebar (min-width: 700px) {
@position-try --custom-left {
.testClass {
width: 100px;
margin: 0 10px 0 0;
}
}
}
`);
});

it('should handle @position-try inside a layer', () => {
expect(
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
'@layer': {
'mock-layer': {
'@position-try': {
'--custom-left': {
width: '100px',
margin: '0 10px 0 0',
},
},
},
},
},
},
],
}).join('\n'),
).toMatchInlineSnapshot(`
@layer mock-layer;
@layer mock-layer {
@position-try --custom-left {
.testClass {
width: 100px;
margin: 0 10px 0 0;
}
}
}
`);
});

it('should throw an error when not using a <dashed-ident> type in @position-try scope', () => {
expect(() =>
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
display: 'flex',
'@position-try': {
// @ts-expect-error This test is to cover the error for non-allowed properties inside @position-try scope
invalidName: {
backgroundColor: 'blue',
},
},
},
},
],
}),
).toThrowErrorMatchingInlineSnapshot(
'Invalid @position-try name: "invalidName". Position names must follow the <dashed-ident> type (--custom-name).',
);
});

it('should throw an error when using a non allowed property inside @position-try scope', () => {
expect(() =>
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
display: 'flex',
'@position-try': {
'--custom-left': {
// @ts-expect-error This test is to cover the error for non-allowed properties inside @position-try scope
backgroundColor: 'blue',
},
},
},
},
],
}),
).toThrowErrorMatchingInlineSnapshot(
'Invalid properties in @position-try --custom-left rule: backgroundColor. Only inset, margin, sizing, self-alignment, position-anchor, and position-area properties are allowed.',
);
});

it('should throw an error when a at-rule is use inside @position-try scope', () => {
expect(() =>
transformCss({
composedClassLists: [],
localClassNames: ['testClass'],
cssObjs: [
{
type: 'local',
selector: 'testClass',
rule: {
display: 'flex',
'@position-try': {
'--custom-left': {
width: '100px',
margin: '0 10px 0 0',
// @ts-expect-error This test is to cover the error when a at-rule is used inside @position-try scope
'@media': {
'screen and (min-width: 700px)': {
display: 'grid',
},
},
},
},
},
},
],
}),
).toThrowErrorMatchingInlineSnapshot(
'Nested at-rules (e.g. "@media") are not allowed inside @position-try block.',
);
});
});

endFileScope();
Loading