Skip to content
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

Fix fileScope error if composeStyles is called at runtime #265

Merged
merged 2 commits into from
Jul 30, 2021
Merged
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/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