22import { Dimensions , Platform } from 'react-native'
33import { Orientation , StyleDependency } from '../../types'
44import { ComponentState , GenerateStyleSheetsCallback , RNStyle , Style , StyleSheets } from '../types'
5+ import { cloneWithAccessors } from './native-utils'
56import { parseBoxShadow , parseFontVariant , parseTransformsMutation , resolveGradient } from './parsers'
67import { UniwindRuntime } from './runtime'
78
@@ -11,8 +12,10 @@ type StylesResult = {
1112}
1213
1314class UniwindStoreBuilder {
14- stylesheets = { } as StyleSheets
15- listeners = {
15+ runtime = UniwindRuntime
16+ private stylesheet = { } as StyleSheets
17+ private vars = { } as Record < string , unknown >
18+ private listeners = {
1619 [ StyleDependency . ColorScheme ] : new Set < ( ) => void > ( ) ,
1720 [ StyleDependency . Theme ] : new Set < ( ) => void > ( ) ,
1821 [ StyleDependency . Dimensions ] : new Set < ( ) => void > ( ) ,
@@ -21,8 +24,8 @@ class UniwindStoreBuilder {
2124 [ StyleDependency . FontScale ] : new Set < ( ) => void > ( ) ,
2225 [ StyleDependency . Rtl ] : new Set < ( ) => void > ( ) ,
2326 }
24- runtime = UniwindRuntime
25- cache = new Map < string , StylesResult > ( )
27+ private cache = new Map < string , StylesResult > ( )
28+ private generateStyleSheetCallbackResult : ReturnType < GenerateStyleSheetsCallback > | null = null
2629
2730 subscribe ( onStoreChange : ( ) => void , dependencies : Array < StyleDependency > ) {
2831 dependencies . forEach ( dep => {
@@ -65,19 +68,28 @@ class UniwindStoreBuilder {
6568 }
6669
6770 reinit = ( generateStyleSheetCallback ?: GenerateStyleSheetsCallback ) => {
68- const styleSheet = generateStyleSheetCallback ?.( this . runtime ) ?? this . stylesheets
69- const themeVars = styleSheet [ `__uniwind-theme-${ this . runtime . currentThemeName } ` ]
70- const platformVars = styleSheet [ `__uniwind-platform-${ Platform . OS } ` ]
71+ const config = generateStyleSheetCallback ?.( this . runtime ) ?? this . generateStyleSheetCallbackResult
72+
73+ if ( ! config ) {
74+ return
75+ }
76+
77+ const { scopedVars, stylesheet, vars } = config
78+
79+ this . generateStyleSheetCallbackResult = config
80+ this . stylesheet = stylesheet
81+ this . vars = vars
82+
83+ const themeVars = scopedVars [ `__uniwind-theme-${ this . runtime . currentThemeName } ` ]
84+ const platformVars = scopedVars [ `__uniwind-platform-${ Platform . OS } ` ]
7185
7286 if ( themeVars ) {
73- Object . assign ( styleSheet , themeVars )
87+ Object . defineProperties ( this . vars , Object . getOwnPropertyDescriptors ( themeVars ) )
7488 }
7589
7690 if ( platformVars ) {
77- Object . assign ( styleSheet , platformVars )
91+ Object . defineProperties ( this . vars , Object . getOwnPropertyDescriptors ( platformVars ) )
7892 }
79-
80- this . stylesheets = styleSheet
8193 }
8294
8395 notifyListeners = ( dependencies : Array < StyleDependency > ) => {
@@ -86,15 +98,16 @@ class UniwindStoreBuilder {
8698
8799 private resolveStyles ( classNames : string , state ?: ComponentState ) {
88100 const result = { } as Record < string , any >
101+ let vars = this . vars
89102 const dependencies = [ ] as Array < StyleDependency >
90103 const bestBreakpoints = new Map < string , Style > ( )
91104
92105 for ( const className of classNames . split ( ' ' ) ) {
93- if ( ! ( className in this . stylesheets ) ) {
106+ if ( ! ( className in this . stylesheet ) ) {
94107 continue
95108 }
96109
97- for ( const style of this . stylesheets [ className ] as Array < Style > ) {
110+ for ( const style of this . stylesheet [ className ] as Array < Style > ) {
98111 dependencies . push ( ...style . dependencies )
99112
100113 if (
@@ -110,16 +123,6 @@ class UniwindStoreBuilder {
110123 continue
111124 }
112125
113- style . usedVars . forEach ( varName => {
114- if ( varName in this . stylesheets && ! ( varName in result ) ) {
115- Object . defineProperty ( result , varName , {
116- configurable : true ,
117- enumerable : false ,
118- get : this . stylesheets [ varName ] as ( ) => unknown ,
119- } )
120- }
121- } )
122-
123126 for ( const [ property , valueGetter ] of style . entries ) {
124127 const previousBest = bestBreakpoints . get ( property )
125128
@@ -134,11 +137,25 @@ class UniwindStoreBuilder {
134137 continue
135138 }
136139
137- Object . defineProperty ( result , property , {
138- configurable : true ,
139- get : valueGetter ,
140- enumerable : property [ 0 ] !== '-' ,
141- } )
140+ if ( property [ 0 ] === '-' ) {
141+ // Clone vars object if we are adding inline variables
142+ if ( vars === this . vars ) {
143+ vars = cloneWithAccessors ( this . vars )
144+ }
145+
146+ Object . defineProperty ( vars , property , {
147+ configurable : true ,
148+ enumerable : true ,
149+ get : valueGetter ,
150+ } )
151+ } else {
152+ Object . defineProperty ( result , property , {
153+ configurable : true ,
154+ enumerable : true ,
155+ get : ( ) => valueGetter . call ( vars ) ,
156+ } )
157+ }
158+
142159 bestBreakpoints . set ( property , style )
143160 }
144161 }
0 commit comments