Skip to content

Commit

Permalink
Fix fileScope error if composeStyles is called at runtime (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcompiles authored Jul 30, 2021
1 parent b8bb7ad commit 385155f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-walls-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/css': patch
---

Fix fileScope error if `composeStyles` is called at runtime
12 changes: 11 additions & 1 deletion packages/css/src/composeStyles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { generateIdentifier } from './identifier';
import { registerComposition, registerClassName } from './adapter';
import { hasFileScope } from './fileScope';

type ClassNames = string | Array<ClassNames>;

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

export function composeStyles(...classNames: Array<ClassNames>) {
return createComposition(dudupeAndJoinClassList(classNames));
const classList = dudupeAndJoinClassList(classNames);

// When using Sprinkles with the runtime (e.g. within a jest test)
// `composeStyles` can be called outside of a fileScope. Checking
// the fileScope is bit of a hack but will solve the issue for now
if (!hasFileScope()) {
return classList;
}

return createComposition(classList);
}
4 changes: 4 additions & 0 deletions packages/css/src/fileScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export function endFileScope() {
fileScopes.splice(0, 1);
}

export function hasFileScope() {
return fileScopes.length > 0;
}

export function getFileScope(): FileScope {
if (fileScopes.length === 0) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion tests/sprinkles/sprinkles.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
createMapValueFn,
createNormalizeValueFn,
createAtomsFn,
} from '@vanilla-extract/sprinkles';
import { createAtomsFn } from '@vanilla-extract/sprinkles/createRuntimeAtomsFn';

import {
atomicStyles,
Expand Down

0 comments on commit 385155f

Please sign in to comment.