11import type { Contract , MapLeafNodes } from '@vanilla-extract/private' ;
2- import type { ThemeVars , Tokens } from './types' ;
2+ import type { GlobalStyleRule , Resolve , ThemeVars , Tokens } from './types' ;
33import { appendCss , registerClassName } from './adapter' ;
44import { getFileScope } from './fileScope' ;
55import { generateIdentifier } from './identifier' ;
66import { createThemeContract , assignVars } from './vars' ;
77
8+ type WithOptionalLayer < T extends Tokens > = T & {
9+ '@layer' ?: string ;
10+ } ;
11+
12+ type WithoutLayer < T > = Omit < T , '@layer' > ;
13+
814export function createGlobalTheme < ThemeTokens extends Tokens > (
915 selector : string ,
10- tokens : ThemeTokens ,
11- ) : ThemeVars < ThemeTokens > ;
16+ tokens : WithOptionalLayer < ThemeTokens > ,
17+ ) : Resolve < WithoutLayer < ThemeVars < ThemeTokens > > > ;
1218export function createGlobalTheme < ThemeContract extends Contract > (
1319 selector : string ,
1420 themeContract : ThemeContract ,
15- tokens : MapLeafNodes < ThemeContract , string > ,
21+ tokens : WithOptionalLayer < MapLeafNodes < ThemeContract , string > > ,
1622) : void ;
1723export function createGlobalTheme (
1824 selector : string ,
1925 arg2 : any ,
2026 arg3 ?: any ,
2127) : any {
22- const shouldCreateVars = Boolean ( ! arg3 ) ;
28+ const themeContractProvided = Boolean ( arg3 ) ;
29+
30+ const tokenArg = (
31+ themeContractProvided ? arg3 : arg2
32+ ) as WithOptionalLayer < Tokens > ;
2333
24- const themeVars = shouldCreateVars
25- ? createThemeContract ( arg2 )
26- : ( arg2 as ThemeVars < any > ) ;
34+ const { layerName, tokens } = extractLayerFromTokens ( tokenArg ) ;
2735
28- const tokens = shouldCreateVars ? arg2 : arg3 ;
36+ const themeContract = themeContractProvided
37+ ? ( arg2 as ThemeVars < any > )
38+ : createThemeContract ( tokens ) ;
39+
40+ let rule : GlobalStyleRule = {
41+ vars : assignVars ( themeContract , tokens ) ,
42+ } ;
43+
44+ if ( layerName ) {
45+ rule = {
46+ '@layer' : {
47+ [ layerName ] : rule ,
48+ } ,
49+ } ;
50+ }
2951
3052 appendCss (
3153 {
3254 type : 'global' ,
3355 selector : selector ,
34- rule : { vars : assignVars ( themeVars , tokens ) } ,
56+ rule,
3557 } ,
3658 getFileScope ( ) ,
3759 ) ;
3860
39- if ( shouldCreateVars ) {
40- return themeVars ;
61+ if ( ! themeContractProvided ) {
62+ return themeContract ;
4163 }
4264}
4365
4466export function createTheme < ThemeContract extends Contract > (
4567 themeContract : ThemeContract ,
46- tokens : MapLeafNodes < ThemeContract , string > ,
68+ tokens : WithOptionalLayer < MapLeafNodes < ThemeContract , string > > ,
4769 debugId ?: string ,
4870) : string ;
4971export function createTheme < ThemeTokens extends Tokens > (
50- tokens : ThemeTokens ,
72+ tokens : WithOptionalLayer < ThemeTokens > ,
5173 debugId ?: string ,
52- ) : [ className : string , vars : ThemeVars < ThemeTokens > ] ;
74+ ) : [ className : string , vars : Resolve < WithoutLayer < ThemeVars < ThemeTokens > > > ] ;
5375export function createTheme ( arg1 : any , arg2 ?: any , arg3 ?: string ) : any {
5476 const themeClassName = generateIdentifier (
5577 typeof arg2 === 'object' ? arg3 : arg2 ,
@@ -64,3 +86,18 @@ export function createTheme(arg1: any, arg2?: any, arg3?: string): any {
6486
6587 return vars ? [ themeClassName , vars ] : themeClassName ;
6688}
89+
90+ function extractLayerFromTokens (
91+ tokens : WithOptionalLayer < MapLeafNodes < any , string > > ,
92+ ) : {
93+ layerName ?: string ;
94+ tokens : MapLeafNodes < any , string > ;
95+ } {
96+ if ( '@layer' in tokens ) {
97+ const { '@layer' : layerName , ...rest } = tokens ;
98+
99+ return { layerName, tokens : rest } ;
100+ }
101+
102+ return { tokens } ;
103+ }
0 commit comments