Skip to content

Commit 00012e4

Browse files
aspirisengraup
andauthored
Add Circular Selectors section to docs (#1096)
* Add tests for getter selector * Add Circular Selectors case to docs * Fix typo Co-authored-by: Paul Grau <[email protected]> * Fix lint --------- Co-authored-by: Paul Grau <[email protected]>
1 parent 50064fe commit 00012e4

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

packages/css/src/transformCss.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,50 @@ describe('transformCss', () => {
971971
`);
972972
});
973973

974+
it('should handle getter selectors', () => {
975+
expect(
976+
transformCss({
977+
composedClassLists: [],
978+
localClassNames: ['testClass', 'parentClass'],
979+
cssObjs: [
980+
{
981+
type: 'local',
982+
selector: 'testClass',
983+
rule: {
984+
color: 'red',
985+
get selectors() {
986+
return {
987+
'&:nth-child(3)': {
988+
color: 'blue',
989+
},
990+
'parentClass > div > span ~ &.someGlobalClass:hover': {
991+
background: 'green',
992+
},
993+
'parentClass&': {
994+
background: 'black',
995+
},
996+
};
997+
},
998+
},
999+
},
1000+
],
1001+
}).join('\n'),
1002+
).toMatchInlineSnapshot(`
1003+
.testClass {
1004+
color: red;
1005+
}
1006+
.testClass:nth-child(3) {
1007+
color: blue;
1008+
}
1009+
.parentClass > div > span ~ .testClass.someGlobalClass:hover {
1010+
background: green;
1011+
}
1012+
.parentClass.testClass {
1013+
background: black;
1014+
}
1015+
`);
1016+
});
1017+
9741018
it('should handle complex selectors within media queries', () => {
9751019
expect(
9761020
transformCss({

site/docs/overview/styling.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,35 @@ globalStyle(`${parent} a[href]`, {
237237
});
238238
```
239239

240+
### Circular Selectors
241+
242+
If your selectors are dependent on each other you can use [getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) to define them:
243+
244+
```ts compiled
245+
// styles.css.ts
246+
import { style } from '@vanilla-extract/css';
247+
248+
export const child = style({
249+
background: 'blue',
250+
get selectors() {
251+
return {
252+
[`${parent} &`]: {
253+
color: 'red'
254+
}
255+
};
256+
}
257+
});
258+
259+
export const parent = style({
260+
background: 'yellow',
261+
selectors: {
262+
[`&:has(${child})`]: {
263+
padding: 10
264+
}
265+
}
266+
});
267+
```
268+
240269
## Container Queries
241270

242271
Container queries work the same as [media queries] and are nested inside the `@container` key.

tests/compiler/compiler.vitest.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,35 @@ describe('compiler', () => {
271271
`);
272272
});
273273

274+
test('getter selector', async () => {
275+
const compiler = compilers.default;
276+
277+
const cssPath = path.join(__dirname, 'fixtures/selectors/getter.css.ts');
278+
const output = await compiler.processVanillaFile(cssPath);
279+
const { css } = await compiler.getCssForFile(cssPath);
280+
281+
expect(output.source).toMatchInlineSnapshot(`
282+
"import 'fixtures/selectors/getter.css.ts.vanilla.css';
283+
export var child = 'getter_child__ux95kn0';
284+
export var parent = 'getter_parent__ux95kn1';"
285+
`);
286+
287+
expect(css).toMatchInlineSnapshot(`
288+
".getter_child__ux95kn0 {
289+
background: blue;
290+
}
291+
.getter_parent__ux95kn1 .getter_child__ux95kn0 {
292+
color: red;
293+
}
294+
.getter_parent__ux95kn1 {
295+
background: yellow;
296+
}
297+
.getter_parent__ux95kn1:has(.getter_child__ux95kn0) {
298+
padding: 10px;
299+
}"
300+
`);
301+
});
302+
274303
afterAll(async () => {
275304
await Promise.allSettled(
276305
Object.values(compilers).map((compiler) => compiler.close()),
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { style } from '@vanilla-extract/css';
2+
3+
export const child = style({
4+
background: 'blue',
5+
get selectors() {
6+
return {
7+
[`${parent} &`]: {
8+
color: 'red',
9+
},
10+
};
11+
},
12+
});
13+
14+
export const parent = style({
15+
background: 'yellow',
16+
selectors: {
17+
[`&:has(${child})`]: {
18+
padding: 10,
19+
},
20+
},
21+
});

0 commit comments

Comments
 (0)