4141// });
4242
4343import 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' ;
4545
4646// Define a type that removes token strings from style properties
4747type ResolvedStyle = ViewStyle & TextStyle & ImageStyle ;
@@ -104,7 +104,7 @@ function styles<V extends VariantOptions<V>>(config: VariantStyleConfig<V>) {
104104
105105 return ( props ?: Props ) : StyleProp < ResolvedStyle > => {
106106 // Start with base styles
107- let styles : StyleObject = {
107+ let stylesObj : StyleObject = {
108108 ...( config . base || { } ) ,
109109 } ;
110110
@@ -125,17 +125,17 @@ function styles<V extends VariantOptions<V>>(config: VariantStyleConfig<V>) {
125125 if ( typeof value === 'boolean' ) {
126126 const booleanValue : BooleanVariantKey = value ? 'true' : 'false' ;
127127 if ( variantGroup [ booleanValue as keyof typeof variantGroup ] ) {
128- styles = {
129- ...styles ,
128+ stylesObj = {
129+ ...stylesObj ,
130130 ...variantGroup [ booleanValue as keyof typeof variantGroup ] ,
131131 } ;
132132 }
133133 } else {
134134 // Handle string/enum variants
135135 const variantValue = value || config . defaultVariants ?. [ propName ] ;
136136 if ( variantValue && variantGroup [ variantValue as keyof typeof variantGroup ] ) {
137- styles = {
138- ...styles ,
137+ stylesObj = {
138+ ...stylesObj ,
139139 ...variantGroup [ variantValue as keyof typeof variantGroup ] ,
140140 } ;
141141 }
@@ -155,15 +155,18 @@ function styles<V extends VariantOptions<V>>(config: VariantStyleConfig<V>) {
155155 return mergedProps [ propName as keyof V ] === value ;
156156 } )
157157 ) {
158- styles = {
159- ...styles ,
158+ stylesObj = {
159+ ...stylesObj ,
160160 ...compound . style ,
161161 } ;
162162 }
163163 }
164164 }
165165
166- return styles as StyleProp < ResolvedStyle > ;
166+ // Create a StyleSheet for better performance
167+ return StyleSheet . create ( {
168+ style : stylesObj as ResolvedStyle ,
169+ } ) . style ;
167170 } ;
168171}
169172
@@ -284,6 +287,11 @@ export function defineTokens<T extends TokenConfig>(tokenConfig: T): CreateToken
284287 // Resolve tokens in the style object
285288 const resolvedStyle = resolveTokens ( styleObject , tokens ) ;
286289
290+ // Create StyleSheet using StyleSheet.create for better performance
291+ const styles = StyleSheet . create ( {
292+ style : resolvedStyle as ResolvedStyle ,
293+ } ) ;
294+
287295 // Create and return a new component that applies the resolved styles
288296 const StyledComponent : ComponentType <
289297 P &
@@ -300,12 +308,12 @@ export function defineTokens<T extends TokenConfig>(tokenConfig: T): CreateToken
300308 style ?: StyleProp < ViewStyle | TextStyle | ImageStyle > ;
301309 } & Record < string , unknown > ;
302310
303- // Merge the resolved style with any style passed in props
311+ // Merge the StyleSheet style with any style passed in props
304312 const mergedStyle = propStyle
305313 ? Array . isArray ( propStyle )
306- ? [ resolvedStyle , ...propStyle ]
307- : [ resolvedStyle , propStyle ]
308- : resolvedStyle ;
314+ ? [ styles . style , ...propStyle ]
315+ : [ styles . style , propStyle ]
316+ : styles . style ;
309317
310318 // We need to cast here to handle the component props correctly
311319 const componentProps = {
0 commit comments