41
41
// });
42
42
43
43
import React , { type ComponentType , type ReactNode } from 'react' ;
44
- import { ImageStyle , TextStyle , ViewStyle , StyleProp } from 'react-native' ;
44
+ import { ImageStyle , TextStyle , ViewStyle , StyleProp , StyleSheet } from 'react-native' ;
45
45
46
46
// Define a type that removes token strings from style properties
47
47
type ResolvedStyle = ViewStyle & TextStyle & ImageStyle ;
@@ -104,7 +104,7 @@ function styles<V extends VariantOptions<V>>(config: VariantStyleConfig<V>) {
104
104
105
105
return ( props ?: Props ) : StyleProp < ResolvedStyle > => {
106
106
// Start with base styles
107
- let styles : StyleObject = {
107
+ let stylesObj : StyleObject = {
108
108
...( config . base || { } ) ,
109
109
} ;
110
110
@@ -125,17 +125,17 @@ function styles<V extends VariantOptions<V>>(config: VariantStyleConfig<V>) {
125
125
if ( typeof value === 'boolean' ) {
126
126
const booleanValue : BooleanVariantKey = value ? 'true' : 'false' ;
127
127
if ( variantGroup [ booleanValue as keyof typeof variantGroup ] ) {
128
- styles = {
129
- ...styles ,
128
+ stylesObj = {
129
+ ...stylesObj ,
130
130
...variantGroup [ booleanValue as keyof typeof variantGroup ] ,
131
131
} ;
132
132
}
133
133
} else {
134
134
// Handle string/enum variants
135
135
const variantValue = value || config . defaultVariants ?. [ propName ] ;
136
136
if ( variantValue && variantGroup [ variantValue as keyof typeof variantGroup ] ) {
137
- styles = {
138
- ...styles ,
137
+ stylesObj = {
138
+ ...stylesObj ,
139
139
...variantGroup [ variantValue as keyof typeof variantGroup ] ,
140
140
} ;
141
141
}
@@ -155,15 +155,18 @@ function styles<V extends VariantOptions<V>>(config: VariantStyleConfig<V>) {
155
155
return mergedProps [ propName as keyof V ] === value ;
156
156
} )
157
157
) {
158
- styles = {
159
- ...styles ,
158
+ stylesObj = {
159
+ ...stylesObj ,
160
160
...compound . style ,
161
161
} ;
162
162
}
163
163
}
164
164
}
165
165
166
- return styles as StyleProp < ResolvedStyle > ;
166
+ // Create a StyleSheet for better performance
167
+ return StyleSheet . create ( {
168
+ style : stylesObj as ResolvedStyle ,
169
+ } ) . style ;
167
170
} ;
168
171
}
169
172
@@ -284,6 +287,11 @@ export function defineTokens<T extends TokenConfig>(tokenConfig: T): CreateToken
284
287
// Resolve tokens in the style object
285
288
const resolvedStyle = resolveTokens ( styleObject , tokens ) ;
286
289
290
+ // Create StyleSheet using StyleSheet.create for better performance
291
+ const styles = StyleSheet . create ( {
292
+ style : resolvedStyle as ResolvedStyle ,
293
+ } ) ;
294
+
287
295
// Create and return a new component that applies the resolved styles
288
296
const StyledComponent : ComponentType <
289
297
P &
@@ -300,12 +308,12 @@ export function defineTokens<T extends TokenConfig>(tokenConfig: T): CreateToken
300
308
style ?: StyleProp < ViewStyle | TextStyle | ImageStyle > ;
301
309
} & Record < string , unknown > ;
302
310
303
- // Merge the resolved style with any style passed in props
311
+ // Merge the StyleSheet style with any style passed in props
304
312
const mergedStyle = propStyle
305
313
? Array . isArray ( propStyle )
306
- ? [ resolvedStyle , ...propStyle ]
307
- : [ resolvedStyle , propStyle ]
308
- : resolvedStyle ;
314
+ ? [ styles . style , ...propStyle ]
315
+ : [ styles . style , propStyle ]
316
+ : styles . style ;
309
317
310
318
// We need to cast here to handle the component props correctly
311
319
const componentProps = {
0 commit comments