1
- import { useMemo , useCallback } from 'react' ;
1
+ import { useMemo } from 'react' ;
2
2
import { titleCase } from '_lib/string' ;
3
3
4
4
import type { PressableStateCallbackType } from 'react-native' ;
@@ -18,19 +18,17 @@ export function useVariants<S>(
18
18
) : {
19
19
vstyles : VStyleSheet < S > ,
20
20
} {
21
- const isVState = useCallback ( ( v : string ) : boolean =>
22
- v . toLowerCase ( ) === 'state'
23
- , [ ] ) ;
21
+ const isVState = ( v : string ) : boolean => v . toLowerCase ( ) === 'state' ;
24
22
25
- const buildStyles = useCallback ( ( slug : VStyleKey < S > , styles : S ) => {
23
+ const buildStyles = ( slug : VStyleKey < S > , styles : S ) => {
26
24
const vstyles : [ VStyleCond , VStyleMod < S > ] [ ] = [ [ null , styles [ slug ] ] ] ;
27
25
const vnames = Object . entries ( variants ) . sort ( ( a , b ) => a [ 0 ] . localeCompare ( b [ 0 ] ) ) ;
28
26
// Sort and and loop through all vars
29
- for ( const [ v1 , primary ] of vnames ) {
27
+ vnames . forEach ( ( [ v1 , primary ] ) => {
30
28
// Single variant for this component
31
29
if ( vnames . length === 1 ) {
32
30
// Loop through all values for the variant
33
- for ( const v1v of primary ) {
31
+ primary . forEach ( v1v => {
34
32
// Build id for this variant combination
35
33
const vkey = `${ slug } ${ titleCase ( v1 ) } ${ v1v } ` as VStyleKey < S > ;
36
34
// Look up the id in the stylesheet
@@ -46,18 +44,18 @@ export function useVariants<S>(
46
44
) ;
47
45
// Add the variant combo style to styles
48
46
vstyles . push ( [ vcond , vstyle ] ) ;
49
- }
47
+ } ) ;
50
48
// Multiple variants, create compound styles
51
49
} else {
52
50
// Prevent state from being used as primary variant (it's a secondary)
53
51
if ( isVState ( v1 ) ) return ;
54
52
// Loop through all values for the variant
55
- for ( const v1v of primary ) {
53
+ primary . forEach ( v1v => {
56
54
// For this value, loop all values of the other variants
57
- for ( const [ v2 , secondary ] of vnames ) {
55
+ vnames . forEach ( ( [ v2 , secondary ] ) => {
58
56
if ( v1 === v2 ) return ;
59
57
// Loop through all the values in the other variant
60
- for ( const v2v of secondary ) {
58
+ secondary . forEach ( v2v => {
61
59
// Build id for this variant combination
62
60
const vkey = `${ slug } ${ titleCase ( v1 ) } ${ v1v } ${ titleCase ( v2 ) } ${ v2v } ` as VStyleKey < S > ;
63
61
// Look up the id in the stylesheet
@@ -75,15 +73,15 @@ export function useVariants<S>(
75
73
) ;
76
74
// Add the variant combo style to styles
77
75
vstyles . push ( [ vcond , vstyle ] ) ;
78
- }
79
- }
80
- }
76
+ } ) ;
77
+ } ) ;
78
+ } ) ;
81
79
}
82
- }
80
+ } ) ;
83
81
return vstyles ;
84
- } , [ variants , states , isVState ] ) ;
82
+ }
85
83
86
- const proxyStyles = useCallback ( ( o : S ) : VStyleSheet < S > => {
84
+ const proxyStyles = ( o : S ) : VStyleSheet < S > => {
87
85
// Cache the styles for each variant combo as they are accessed
88
86
const cache = new Map < string , ReturnType < typeof buildStyles > > ( ) ;
89
87
// Create empty object to proxy the styles, inherit types from stylesheet
@@ -98,18 +96,16 @@ export function useVariants<S>(
98
96
styles = buildStyles ( k , o ) ;
99
97
cache . set ( k , styles ) ;
100
98
}
101
- // Still no styles found, return empty array
102
- if ( ! styles ) return [ ] ;
103
99
// Return styles that match the current variant values and/or state
104
100
return styles
105
101
. filter ( ( [ c ] ) => c === null || c ?.( e ) )
106
102
. map ( ( [ , s ] ) => s ) ;
107
103
} ;
108
104
}
109
105
return proxy ;
110
- } , [ buildStyles ] ) ;
106
+ } ;
111
107
112
108
return {
113
- vstyles : useMemo ( ( ) => proxyStyles ( styles ) , [ styles , proxyStyles ] ) ,
109
+ vstyles : useMemo ( ( ) => proxyStyles ( styles ) , [ styles ] ) ,
114
110
} ;
115
111
}
0 commit comments