From 5a86931d43a1b791976545f70d9304eb3e3ff57e Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Fri, 30 Jul 2021 20:54:56 +1000 Subject: [PATCH 1/2] Fix fileScope error if `composeStyles` is called at runtime --- .changeset/rich-walls-cry.md | 5 +++++ packages/css/src/composeStyles.ts | 12 +++++++++++- packages/css/src/fileScope.ts | 4 ++++ tests/sprinkles/sprinkles.test.ts | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .changeset/rich-walls-cry.md diff --git a/.changeset/rich-walls-cry.md b/.changeset/rich-walls-cry.md new file mode 100644 index 000000000..41a4ff7dc --- /dev/null +++ b/.changeset/rich-walls-cry.md @@ -0,0 +1,5 @@ +--- +'@vanilla-extract/css': patch +--- + +Fix fileScope error if `composeStyles` is called at runtime diff --git a/packages/css/src/composeStyles.ts b/packages/css/src/composeStyles.ts index fcbca00a8..0023e8875 100644 --- a/packages/css/src/composeStyles.ts +++ b/packages/css/src/composeStyles.ts @@ -1,5 +1,6 @@ import { generateIdentifier } from './identifier'; import { registerComposition, registerClassName } from './adapter'; +import { hasFileScope } from './fileScope'; type ClassNames = string | Array; @@ -43,5 +44,14 @@ export function dudupeAndJoinClassList(classNames: Array) { } export function composeStyles(...classNames: Array) { - 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); } diff --git a/packages/css/src/fileScope.ts b/packages/css/src/fileScope.ts index 9dafa52b0..ed9618207 100644 --- a/packages/css/src/fileScope.ts +++ b/packages/css/src/fileScope.ts @@ -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( diff --git a/tests/sprinkles/sprinkles.test.ts b/tests/sprinkles/sprinkles.test.ts index 39c2ac466..0bf907103 100644 --- a/tests/sprinkles/sprinkles.test.ts +++ b/tests/sprinkles/sprinkles.test.ts @@ -2,7 +2,7 @@ import { createMapValueFn, createNormalizeValueFn, } from '@vanilla-extract/sprinkles'; -import { createAtomsFn } from '@vanilla-extract/sprinkles/createRuntimeAtomsFn'; +import { createAtomsFn } from '@vanilla-extract/sprinkles'; import { atomicStyles, From b613e83035794a4fbc4658a1674e4a95d0309c97 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Fri, 30 Jul 2021 21:04:47 +1000 Subject: [PATCH 2/2] Merge imports --- tests/sprinkles/sprinkles.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/sprinkles/sprinkles.test.ts b/tests/sprinkles/sprinkles.test.ts index 0bf907103..e9cfb26ea 100644 --- a/tests/sprinkles/sprinkles.test.ts +++ b/tests/sprinkles/sprinkles.test.ts @@ -1,8 +1,8 @@ import { createMapValueFn, createNormalizeValueFn, + createAtomsFn, } from '@vanilla-extract/sprinkles'; -import { createAtomsFn } from '@vanilla-extract/sprinkles'; import { atomicStyles,