Skip to content

Commit 385155f

Browse files
authored
Fix fileScope error if composeStyles is called at runtime (#265)
1 parent b8bb7ad commit 385155f

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

.changeset/rich-walls-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/css': patch
3+
---
4+
5+
Fix fileScope error if `composeStyles` is called at runtime

packages/css/src/composeStyles.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { generateIdentifier } from './identifier';
22
import { registerComposition, registerClassName } from './adapter';
3+
import { hasFileScope } from './fileScope';
34

45
type ClassNames = string | Array<ClassNames>;
56

@@ -43,5 +44,14 @@ export function dudupeAndJoinClassList(classNames: Array<ClassNames>) {
4344
}
4445

4546
export function composeStyles(...classNames: Array<ClassNames>) {
46-
return createComposition(dudupeAndJoinClassList(classNames));
47+
const classList = dudupeAndJoinClassList(classNames);
48+
49+
// When using Sprinkles with the runtime (e.g. within a jest test)
50+
// `composeStyles` can be called outside of a fileScope. Checking
51+
// the fileScope is bit of a hack but will solve the issue for now
52+
if (!hasFileScope()) {
53+
return classList;
54+
}
55+
56+
return createComposition(classList);
4757
}

packages/css/src/fileScope.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export function endFileScope() {
2020
fileScopes.splice(0, 1);
2121
}
2222

23+
export function hasFileScope() {
24+
return fileScopes.length > 0;
25+
}
26+
2327
export function getFileScope(): FileScope {
2428
if (fileScopes.length === 0) {
2529
throw new Error(

tests/sprinkles/sprinkles.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
createMapValueFn,
33
createNormalizeValueFn,
4+
createAtomsFn,
45
} from '@vanilla-extract/sprinkles';
5-
import { createAtomsFn } from '@vanilla-extract/sprinkles/createRuntimeAtomsFn';
66

77
import {
88
atomicStyles,

0 commit comments

Comments
 (0)