react-native-unistyles 3.0.0-alpha.12 → 3.0.0-alpha.14
Sign up to get free protection for your applications and to get access to all the features.
- package/cxx/core/UnistyleWrapper.h +1 -1
- package/cxx/core/UnistylesCommitHook.cpp +9 -0
- package/cxx/core/UnistylesCommitHook.h +1 -0
- package/cxx/core/UnistylesMountHook.cpp +15 -1
- package/cxx/core/UnistylesRegistry.h +3 -1
- package/cxx/hybridObjects/HybridStyleSheet.cpp +1 -1
- package/cxx/hybridObjects/HybridStyleSheet.h +1 -1
- package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +1 -1
- package/cxx/shadowTree/ShadowTrafficController.h +33 -0
- package/cxx/shadowTree/ShadowTreeManager.cpp +7 -0
- package/cxx/shadowTree/ShadowTreeManager.h +1 -0
- package/lib/commonjs/index.js +4 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js +7 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/commonjs/web/convert/index.js +1 -1
- package/lib/commonjs/web/convert/index.js.map +1 -1
- package/lib/commonjs/web/create.js +25 -63
- package/lib/commonjs/web/create.js.map +1 -1
- package/lib/commonjs/web/createUnistylesComponent.js +61 -0
- package/lib/commonjs/web/createUnistylesComponent.js.map +1 -0
- package/lib/commonjs/web/index.js +12 -3
- package/lib/commonjs/web/index.js.map +1 -1
- package/lib/commonjs/web/listener/index.js +7 -0
- package/lib/commonjs/web/listener/index.js.map +1 -1
- package/lib/commonjs/web/listener/listenToDependencies.js +39 -0
- package/lib/commonjs/web/listener/listenToDependencies.js.map +1 -0
- package/lib/commonjs/web/listener/listener.js +1 -2
- package/lib/commonjs/web/listener/listener.js.map +1 -1
- package/lib/commonjs/web/mock.js +1 -10
- package/lib/commonjs/web/mock.js.map +1 -1
- package/lib/commonjs/web/pseudo.js +2 -3
- package/lib/commonjs/web/pseudo.js.map +1 -1
- package/lib/commonjs/web/registry.js +2 -4
- package/lib/commonjs/web/registry.js.map +1 -1
- package/lib/commonjs/web/runtime.js +0 -1
- package/lib/commonjs/web/runtime.js.map +1 -1
- package/lib/commonjs/web/shadowRegistry.js +120 -0
- package/lib/commonjs/web/shadowRegistry.js.map +1 -0
- package/lib/commonjs/web/state.js +0 -40
- package/lib/commonjs/web/state.js.map +1 -1
- package/lib/commonjs/web/utils.js +66 -14
- package/lib/commonjs/web/utils.js.map +1 -1
- package/lib/commonjs/web/variants/useVariants.js +1 -34
- package/lib/commonjs/web/variants/useVariants.js.map +1 -1
- package/lib/module/index.js +4 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/specs/ShadowRegistry/index.js +7 -1
- package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/module/web/convert/index.js +1 -1
- package/lib/module/web/convert/index.js.map +1 -1
- package/lib/module/web/create.js +26 -64
- package/lib/module/web/create.js.map +1 -1
- package/lib/module/web/createUnistylesComponent.js +54 -0
- package/lib/module/web/createUnistylesComponent.js.map +1 -0
- package/lib/module/web/index.js +4 -2
- package/lib/module/web/index.js.map +1 -1
- package/lib/module/web/listener/index.js +1 -0
- package/lib/module/web/listener/index.js.map +1 -1
- package/lib/module/web/listener/listenToDependencies.js +34 -0
- package/lib/module/web/listener/listenToDependencies.js.map +1 -0
- package/lib/module/web/listener/listener.js +1 -1
- package/lib/module/web/listener/listener.js.map +1 -1
- package/lib/module/web/mock.js +0 -9
- package/lib/module/web/mock.js.map +1 -1
- package/lib/module/web/pseudo.js +2 -3
- package/lib/module/web/pseudo.js.map +1 -1
- package/lib/module/web/registry.js +2 -4
- package/lib/module/web/registry.js.map +1 -1
- package/lib/module/web/runtime.js +0 -1
- package/lib/module/web/runtime.js.map +1 -1
- package/lib/module/web/shadowRegistry.js +116 -0
- package/lib/module/web/shadowRegistry.js.map +1 -0
- package/lib/module/web/state.js +0 -40
- package/lib/module/web/state.js.map +1 -1
- package/lib/module/web/utils.js +59 -12
- package/lib/module/web/utils.js.map +1 -1
- package/lib/module/web/variants/useVariants.js +1 -34
- package/lib/module/web/variants/useVariants.js.map +1 -1
- package/lib/typescript/example/App.d.ts.map +1 -1
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
- package/lib/typescript/src/web/create.d.ts.map +1 -1
- package/lib/typescript/src/web/createUnistylesComponent.d.ts +3 -0
- package/lib/typescript/src/web/createUnistylesComponent.d.ts.map +1 -0
- package/lib/typescript/src/web/index.d.ts +2 -1
- package/lib/typescript/src/web/index.d.ts.map +1 -1
- package/lib/typescript/src/web/listener/index.d.ts +1 -0
- package/lib/typescript/src/web/listener/index.d.ts.map +1 -1
- package/lib/typescript/src/web/listener/listenToDependencies.d.ts +12 -0
- package/lib/typescript/src/web/listener/listenToDependencies.d.ts.map +1 -0
- package/lib/typescript/src/web/listener/listener.d.ts +2 -1
- package/lib/typescript/src/web/listener/listener.d.ts.map +1 -1
- package/lib/typescript/src/web/mock.d.ts +0 -9
- package/lib/typescript/src/web/mock.d.ts.map +1 -1
- package/lib/typescript/src/web/pseudo.d.ts +4 -2
- package/lib/typescript/src/web/pseudo.d.ts.map +1 -1
- package/lib/typescript/src/web/registry.d.ts +1 -1
- package/lib/typescript/src/web/registry.d.ts.map +1 -1
- package/lib/typescript/src/web/runtime.d.ts +0 -1
- package/lib/typescript/src/web/runtime.d.ts.map +1 -1
- package/lib/typescript/src/web/shadowRegistry.d.ts +17 -0
- package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -0
- package/lib/typescript/src/web/state.d.ts +0 -5
- package/lib/typescript/src/web/state.d.ts.map +1 -1
- package/lib/typescript/src/web/utils.d.ts +22 -7
- package/lib/typescript/src/web/utils.d.ts.map +1 -1
- package/lib/typescript/src/web/variants/useVariants.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JHybridNativePlatformSpec.hpp +6 -3
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/HybridNativePlatformSpec.kt +8 -3
- package/nitrogen/generated/ios/c++/HybridNativePlatformSpecSwift.hpp +1 -1
- package/nitrogen/generated/ios/swift/HybridNativePlatformSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridNativePlatformSpecCxx.swift +14 -2
- package/nitrogen/generated/shared/c++/HybridNativePlatformSpec.hpp +3 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesNavigationBarSpec.hpp +3 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesRuntimeSpec.hpp +3 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesShadowRegistrySpec.hpp +3 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesStatusBarSpec.hpp +3 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.hpp +4 -1
- package/package.json +3 -3
- package/src/index.ts +3 -0
- package/src/specs/ShadowRegistry/index.ts +7 -1
- package/src/web/convert/index.ts +1 -1
- package/src/web/create.ts +24 -79
- package/src/web/createUnistylesComponent.tsx +56 -0
- package/src/web/index.ts +3 -2
- package/src/web/listener/index.ts +1 -0
- package/src/web/listener/listenToDependencies.ts +50 -0
- package/src/web/listener/listener.ts +1 -1
- package/src/web/mock.ts +0 -10
- package/src/web/pseudo.ts +131 -5
- package/src/web/registry.ts +3 -5
- package/src/web/runtime.ts +0 -2
- package/src/web/shadowRegistry.ts +121 -0
- package/src/web/state.ts +0 -44
- package/src/web/utils.ts +90 -16
- package/src/web/variants/useVariants.ts +1 -37
package/src/web/runtime.ts
CHANGED
@@ -0,0 +1,121 @@
|
|
1
|
+
import type { UnistylesValues } from '../types'
|
2
|
+
import { listenToDependencies } from './listener'
|
3
|
+
import { UnistylesRegistry } from './registry'
|
4
|
+
import { UnistylesRuntime } from './runtime'
|
5
|
+
import { createDoubleMap, extractHiddenProperties, extractSecrets, isInDocument } from './utils'
|
6
|
+
import { getVariants } from './variants'
|
7
|
+
|
8
|
+
type WebUnistyle = ReturnType<typeof UnistylesRegistry.createStyles>
|
9
|
+
|
10
|
+
type Style = UnistylesValues | ((...args: Array<any>) => UnistylesValues)
|
11
|
+
|
12
|
+
class UnistylesShadowRegistryBuilder {
|
13
|
+
// MOCKS
|
14
|
+
name = 'UnistylesShadowRegistry'
|
15
|
+
__type = 'web'
|
16
|
+
equals = () => true
|
17
|
+
toString = () => 'UnistylesShadowRegistry'
|
18
|
+
dispose = () => {}
|
19
|
+
// END MOCKS
|
20
|
+
|
21
|
+
private readonly webUnistylesMap = createDoubleMap<HTMLElement, string, WebUnistyle>()
|
22
|
+
private readonly disposeMap = createDoubleMap<HTMLElement, string, VoidFunction | undefined>()
|
23
|
+
private readonly stylesMap = createDoubleMap<HTMLElement, string, HTMLStyleElement>()
|
24
|
+
|
25
|
+
add = (ref: any, _style?: Style | Array<Style>, _variants?: Record<string, any>, _args?: Array<any>) => {
|
26
|
+
// Style is not provided
|
27
|
+
if (!_style) {
|
28
|
+
return
|
29
|
+
}
|
30
|
+
|
31
|
+
// Array of styles
|
32
|
+
if (Array.isArray(_style)) {
|
33
|
+
_style.forEach(style => this.add(ref, style, _variants, _args))
|
34
|
+
|
35
|
+
return
|
36
|
+
}
|
37
|
+
|
38
|
+
// Not a unistyle
|
39
|
+
if (!Object.keys(_style).some(key => key.startsWith('__uni__'))) {
|
40
|
+
return
|
41
|
+
}
|
42
|
+
|
43
|
+
// Ref is unmounted, remove style tags from the document
|
44
|
+
if (ref === null) {
|
45
|
+
const secrets = extractSecrets(_style)
|
46
|
+
|
47
|
+
secrets.forEach(({ __uni__refs }) => {
|
48
|
+
__uni__refs.forEach(ref => {
|
49
|
+
if (isInDocument(ref)) {
|
50
|
+
return
|
51
|
+
}
|
52
|
+
|
53
|
+
this.remove(ref, _style)
|
54
|
+
})
|
55
|
+
})
|
56
|
+
|
57
|
+
return
|
58
|
+
}
|
59
|
+
|
60
|
+
// Ref is not an HTMLElement
|
61
|
+
if (!(ref instanceof HTMLElement)) {
|
62
|
+
return
|
63
|
+
}
|
64
|
+
|
65
|
+
extractSecrets(_style).forEach(secret => {
|
66
|
+
const { __uni__key, __uni__stylesheet, __uni__refs, __uni__variants, __uni__args = [] } = secret
|
67
|
+
const newComputedStylesheet = typeof __uni__stylesheet === 'function'
|
68
|
+
? __uni__stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
|
69
|
+
: __uni__stylesheet
|
70
|
+
const style = newComputedStylesheet[__uni__key]
|
71
|
+
const args = _args ?? __uni__args
|
72
|
+
const resultHidden = typeof style === 'function'
|
73
|
+
? style(...args)
|
74
|
+
: style
|
75
|
+
const result = extractHiddenProperties(resultHidden)
|
76
|
+
const { variants } = Object.fromEntries(getVariants({ variants: result }, _variants && Object.keys(_variants).length > 0 ? _variants : __uni__variants))
|
77
|
+
const resultWithVariants = {
|
78
|
+
...result,
|
79
|
+
...variants
|
80
|
+
}
|
81
|
+
const storedWebUnistyle = this.webUnistylesMap.get(ref, __uni__key)
|
82
|
+
const webUnistyle = storedWebUnistyle ?? UnistylesRegistry.createStyles(resultWithVariants, __uni__key)
|
83
|
+
|
84
|
+
this.webUnistylesMap.set(ref, __uni__key, webUnistyle)
|
85
|
+
this.disposeMap.get(ref, __uni__key)?.()
|
86
|
+
this.disposeMap.set(ref, __uni__key, listenToDependencies({
|
87
|
+
key: __uni__key,
|
88
|
+
stylesheet: __uni__stylesheet,
|
89
|
+
args,
|
90
|
+
className: webUnistyle.className,
|
91
|
+
unistyles: webUnistyle.unistyles,
|
92
|
+
}))
|
93
|
+
|
94
|
+
if (!storedWebUnistyle) {
|
95
|
+
const styleTag = document.createElement('style')
|
96
|
+
|
97
|
+
ref.classList.add(webUnistyle.className)
|
98
|
+
webUnistyle.unistyles.setStylesTarget(styleTag)
|
99
|
+
document.head.appendChild(styleTag)
|
100
|
+
__uni__refs.add(ref)
|
101
|
+
this.stylesMap.set(ref, __uni__key, styleTag)
|
102
|
+
}
|
103
|
+
|
104
|
+
if (storedWebUnistyle) {
|
105
|
+
UnistylesRegistry.updateStyles(webUnistyle.unistyles, resultWithVariants, webUnistyle.className)
|
106
|
+
}
|
107
|
+
})
|
108
|
+
}
|
109
|
+
|
110
|
+
remove = (ref: HTMLElement, style: Style) => {
|
111
|
+
extractSecrets(style).forEach(({ __uni__key }) => {
|
112
|
+
this.webUnistylesMap.delete(ref, __uni__key)
|
113
|
+
this.disposeMap.get(ref, __uni__key)?.()
|
114
|
+
this.disposeMap.delete(ref, __uni__key)
|
115
|
+
this.stylesMap.get(ref, __uni__key)?.remove()
|
116
|
+
this.stylesMap.delete(ref, __uni__key)
|
117
|
+
})
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
export const UnistylesShadowRegistry = new UnistylesShadowRegistryBuilder()
|
package/src/web/state.ts
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
import { type ReactElement, createElement, createRef } from 'react'
|
2
1
|
import type { UnistylesTheme } from '../types'
|
3
2
|
import type { UnistylesConfig } from '../specs/StyleSheet'
|
4
3
|
import type { AppBreakpoint, AppThemeName } from '../specs/types'
|
@@ -10,7 +9,6 @@ import { UnistyleDependency } from '../specs/NativePlatform'
|
|
10
9
|
|
11
10
|
class UnistylesStateBuilder {
|
12
11
|
private readonly isSSR = isServer()
|
13
|
-
readonly tags = [] as Array<ReactElement>
|
14
12
|
|
15
13
|
themes = new Map<string, UnistylesTheme>()
|
16
14
|
themeName?: AppThemeName
|
@@ -101,48 +99,6 @@ class UnistylesStateBuilder {
|
|
101
99
|
})
|
102
100
|
})
|
103
101
|
}
|
104
|
-
|
105
|
-
createTag() {
|
106
|
-
if (!this.isSSR) {
|
107
|
-
const tag = document.createElement('style')
|
108
|
-
|
109
|
-
document.head.appendChild(tag)
|
110
|
-
|
111
|
-
return tag
|
112
|
-
}
|
113
|
-
|
114
|
-
const tagRef = new Proxy(createRef<HTMLStyleElement>(), {
|
115
|
-
set: (target, prop, value) => {
|
116
|
-
// When ref is assigned
|
117
|
-
if ('textContent' in value) {
|
118
|
-
value.textContent = tag.textContent
|
119
|
-
}
|
120
|
-
|
121
|
-
return Reflect.set(target, prop, value)
|
122
|
-
}
|
123
|
-
})
|
124
|
-
const tagElement = createElement('style', { ref: tagRef, key: this.tags.length }, '')
|
125
|
-
const tag = new Proxy({ textContent: '' }, {
|
126
|
-
set: (target, prop, value) => {
|
127
|
-
if (prop !== 'textContent') {
|
128
|
-
return false
|
129
|
-
}
|
130
|
-
|
131
|
-
// When css is updated
|
132
|
-
target.textContent = value
|
133
|
-
|
134
|
-
if (tagRef.current) {
|
135
|
-
tagRef.current.textContent = value
|
136
|
-
}
|
137
|
-
|
138
|
-
return true
|
139
|
-
}
|
140
|
-
})
|
141
|
-
|
142
|
-
this.tags.push(tagElement)
|
143
|
-
|
144
|
-
return tag
|
145
|
-
}
|
146
102
|
}
|
147
103
|
|
148
104
|
export const UnistylesState = new UnistylesStateBuilder()
|
package/src/web/utils.ts
CHANGED
@@ -1,26 +1,11 @@
|
|
1
1
|
import { ColorScheme, type AppThemeName} from '../specs/types'
|
2
|
-
import type { UnistylesValues } from '../types'
|
2
|
+
import type { StyleSheet, StyleSheetWithSuperPowers, UnistylesValues } from '../types/stylesheet'
|
3
3
|
|
4
4
|
export const reduceObject = <TObj extends Record<string, any>, TReducer>(
|
5
5
|
obj: TObj,
|
6
6
|
reducer: (value: TObj[keyof TObj], key: keyof TObj) => TReducer,
|
7
7
|
) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, reducer(value as TObj[keyof TObj], key)])) as { [K in keyof TObj]: TReducer }
|
8
8
|
|
9
|
-
export const toReactNativeClassName = (className: string | null, values: UnistylesValues) => {
|
10
|
-
const returnValue = className ? {
|
11
|
-
$$css: true,
|
12
|
-
[className]: className
|
13
|
-
} : {}
|
14
|
-
|
15
|
-
Object.defineProperties(returnValue, reduceObject(values, value => ({
|
16
|
-
value,
|
17
|
-
enumerable: false,
|
18
|
-
configurable: true
|
19
|
-
})))
|
20
|
-
|
21
|
-
return returnValue
|
22
|
-
}
|
23
|
-
|
24
9
|
export const keyInObject = <T extends Record<string, any>>(obj: T, key: PropertyKey): key is keyof T => key in obj
|
25
10
|
|
26
11
|
export const schemeToTheme = (scheme: ColorScheme): AppThemeName => {
|
@@ -93,3 +78,92 @@ export const equal = <T>(a: T, b: T) => {
|
|
93
78
|
|
94
79
|
return keysA.every(key => Object.is(a[key], b[key]) && Object.prototype.hasOwnProperty.call(b, key))
|
95
80
|
}
|
81
|
+
|
82
|
+
type UnistyleSecrets = {
|
83
|
+
__uni__stylesheet: StyleSheetWithSuperPowers<StyleSheet>,
|
84
|
+
__uni__key: string,
|
85
|
+
__uni__refs: Set<HTMLElement>
|
86
|
+
__uni__variants: Record<string, any>
|
87
|
+
__uni__args?: Array<any>
|
88
|
+
}
|
89
|
+
|
90
|
+
export const assignSecrets = <T>(object: T, secrets: UnistyleSecrets) => {
|
91
|
+
// @ts-expect-error - assign secrets to object
|
92
|
+
object[`__uni__secrets__${Math.random().toString(16).slice(2)}`] = secrets
|
93
|
+
|
94
|
+
return object
|
95
|
+
}
|
96
|
+
|
97
|
+
export const extractSecrets = (object: any) => {
|
98
|
+
const secrets = Object.entries(object).reduce((acc, [key, value]) => {
|
99
|
+
if (key.startsWith('__uni__secrets__')) {
|
100
|
+
acc.push(value as UnistyleSecrets)
|
101
|
+
}
|
102
|
+
|
103
|
+
return acc
|
104
|
+
}, [] as Array<UnistyleSecrets>)
|
105
|
+
|
106
|
+
return secrets
|
107
|
+
}
|
108
|
+
|
109
|
+
export const getStyles = (values: UnistylesValues) => {
|
110
|
+
const returnValue = {}
|
111
|
+
|
112
|
+
Object.defineProperties(returnValue, reduceObject(values, value => ({
|
113
|
+
value,
|
114
|
+
enumerable: false,
|
115
|
+
configurable: true
|
116
|
+
})))
|
117
|
+
|
118
|
+
return returnValue
|
119
|
+
}
|
120
|
+
|
121
|
+
export const createDoubleMap = <TKey, TSecondKey, TValue>() => {
|
122
|
+
const map = new Map<TKey, Map<TSecondKey, TValue>>()
|
123
|
+
|
124
|
+
return {
|
125
|
+
get: (key: TKey, secondKey: TSecondKey) => {
|
126
|
+
const mapForKey = map.get(key)
|
127
|
+
|
128
|
+
if (!mapForKey) {
|
129
|
+
return undefined
|
130
|
+
}
|
131
|
+
|
132
|
+
return mapForKey.get(secondKey)
|
133
|
+
},
|
134
|
+
set: (key: TKey, secondKey: TSecondKey, value: TValue) => {
|
135
|
+
const mapForKey = map.get(key) ?? new Map<TSecondKey, TValue>()
|
136
|
+
|
137
|
+
map.set(key, mapForKey)
|
138
|
+
mapForKey.set(secondKey, value)
|
139
|
+
},
|
140
|
+
delete: (key: TKey, secondKey: TSecondKey) => {
|
141
|
+
const mapForKey = map.get(key)
|
142
|
+
|
143
|
+
if (!mapForKey) {
|
144
|
+
return
|
145
|
+
}
|
146
|
+
|
147
|
+
mapForKey.delete(secondKey)
|
148
|
+
},
|
149
|
+
forEach: (callback: (key: TKey, secondKey: TSecondKey, value: TValue) => void) => {
|
150
|
+
map.forEach((mapForKey, key) => {
|
151
|
+
mapForKey.forEach((value, secondKey) => {
|
152
|
+
callback(key, secondKey, value)
|
153
|
+
})
|
154
|
+
})
|
155
|
+
}
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
export const extractHiddenProperties = (object: any) => {
|
160
|
+
const hiddenProperties = Object.getOwnPropertyNames(object)
|
161
|
+
|
162
|
+
return Object.fromEntries(
|
163
|
+
hiddenProperties
|
164
|
+
.filter(key => !key.startsWith('__uni__'))
|
165
|
+
.map(key => [key, object[key]])
|
166
|
+
)
|
167
|
+
}
|
168
|
+
|
169
|
+
export const isInDocument = (element: HTMLElement) => document.body.contains(element)
|
@@ -1,14 +1,10 @@
|
|
1
1
|
import { useMemo, useRef, useState } from 'react'
|
2
|
-
import type { TypeStyle } from 'typestyle'
|
3
2
|
import type { ReactNativeStyleSheet, StyleSheet } from '../../types'
|
4
3
|
import { equal, reduceObject } from '../utils'
|
5
|
-
import { UnistylesRegistry } from '../registry'
|
6
4
|
import { getVariants } from './getVariants'
|
7
5
|
|
8
6
|
export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, setSelectedVariants: (variants: Record<string, any>) => void) => {
|
9
7
|
const useVariants = (selectedVariants: Record<string, any>) => {
|
10
|
-
const [unistylesMap] = useState(() => new Map<string, TypeStyle>())
|
11
|
-
const [classNameMap] = useState(() => new Map<string, string>())
|
12
8
|
const [selectedVariantStylesMap] = useState(() => new Map<string, Record<string, any>>())
|
13
9
|
const lastSelectedVariantsRef = useRef<Record<string, any>>()
|
14
10
|
// Variable that determines if variants have changed and we need to recalculate styles
|
@@ -29,47 +25,15 @@ export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, set
|
|
29
25
|
return result
|
30
26
|
}, [lastSelectedVariantsRef.current])
|
31
27
|
|
32
|
-
|
33
|
-
combinedVariantStyles.forEach(([key, value]) => {
|
34
|
-
const { className, unistyles } = UnistylesRegistry.createStyles(value, `variant-${key}`)
|
35
|
-
|
36
|
-
unistylesMap.set(key, unistyles)
|
37
|
-
classNameMap.set(key, className)
|
38
|
-
})
|
39
|
-
}
|
40
|
-
|
41
|
-
combinedVariantStyles.forEach(([key, value]) => {
|
28
|
+
combinedVariantStyles.forEach(([key]) => {
|
42
29
|
const styleEntry = styles[key]
|
43
|
-
const unistyles = unistylesMap.get(key)
|
44
|
-
const className = classNameMap.get(key)
|
45
30
|
const selectedVariantStyles = selectedVariantStylesMap.get(key)
|
46
31
|
|
47
|
-
if (!unistyles || !className) {
|
48
|
-
return
|
49
|
-
}
|
50
|
-
|
51
|
-
if (variantsChanged) {
|
52
|
-
UnistylesRegistry.updateStyles(unistyles, value, className)
|
53
|
-
}
|
54
|
-
|
55
32
|
Object.defineProperties(styleEntry, reduceObject(selectedVariantStyles ?? {}, value => ({
|
56
33
|
value,
|
57
34
|
enumerable: false,
|
58
35
|
configurable: true
|
59
36
|
})))
|
60
|
-
|
61
|
-
if (styleEntry) {
|
62
|
-
Object.keys(styleEntry ?? {}).forEach(key => {
|
63
|
-
if (!key.startsWith('variant-')) {
|
64
|
-
return
|
65
|
-
}
|
66
|
-
|
67
|
-
// @ts-expect-error - remove old variants
|
68
|
-
delete styleEntry[key]
|
69
|
-
})
|
70
|
-
// @ts-expect-error - apply variant className
|
71
|
-
styleEntry[className] = className
|
72
|
-
}
|
73
37
|
})
|
74
38
|
}
|
75
39
|
|