react-native-unistyles 3.0.0-alpha.13 → 3.0.0-alpha.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. package/cxx/core/UnistyleWrapper.h +1 -1
  2. package/cxx/core/UnistylesCommitHook.cpp +13 -10
  3. package/cxx/core/UnistylesMountHook.cpp +3 -19
  4. package/cxx/hybridObjects/HybridStyleSheet.cpp +4 -3
  5. package/cxx/hybridObjects/HybridStyleSheet.h +1 -1
  6. package/cxx/parser/Parser.cpp +13 -9
  7. package/cxx/shadowTree/ShadowTreeManager.cpp +5 -5
  8. package/lib/commonjs/index.js +4 -0
  9. package/lib/commonjs/index.js.map +1 -1
  10. package/lib/commonjs/specs/ShadowRegistry/index.js +7 -1
  11. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  12. package/lib/commonjs/web/convert/index.js +1 -1
  13. package/lib/commonjs/web/convert/index.js.map +1 -1
  14. package/lib/commonjs/web/create.js +25 -63
  15. package/lib/commonjs/web/create.js.map +1 -1
  16. package/lib/commonjs/web/createUnistylesComponent.js +61 -0
  17. package/lib/commonjs/web/createUnistylesComponent.js.map +1 -0
  18. package/lib/commonjs/web/index.js +12 -3
  19. package/lib/commonjs/web/index.js.map +1 -1
  20. package/lib/commonjs/web/listener/index.js +7 -0
  21. package/lib/commonjs/web/listener/index.js.map +1 -1
  22. package/lib/commonjs/web/listener/listenToDependencies.js +39 -0
  23. package/lib/commonjs/web/listener/listenToDependencies.js.map +1 -0
  24. package/lib/commonjs/web/listener/listener.js +1 -2
  25. package/lib/commonjs/web/listener/listener.js.map +1 -1
  26. package/lib/commonjs/web/mock.js +1 -10
  27. package/lib/commonjs/web/mock.js.map +1 -1
  28. package/lib/commonjs/web/pseudo.js +2 -3
  29. package/lib/commonjs/web/pseudo.js.map +1 -1
  30. package/lib/commonjs/web/registry.js +2 -4
  31. package/lib/commonjs/web/registry.js.map +1 -1
  32. package/lib/commonjs/web/runtime.js +0 -1
  33. package/lib/commonjs/web/runtime.js.map +1 -1
  34. package/lib/commonjs/web/shadowRegistry.js +120 -0
  35. package/lib/commonjs/web/shadowRegistry.js.map +1 -0
  36. package/lib/commonjs/web/state.js +0 -40
  37. package/lib/commonjs/web/state.js.map +1 -1
  38. package/lib/commonjs/web/utils.js +66 -14
  39. package/lib/commonjs/web/utils.js.map +1 -1
  40. package/lib/commonjs/web/variants/useVariants.js +15 -31
  41. package/lib/commonjs/web/variants/useVariants.js.map +1 -1
  42. package/lib/module/index.js +4 -0
  43. package/lib/module/index.js.map +1 -1
  44. package/lib/module/specs/ShadowRegistry/index.js +7 -1
  45. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  46. package/lib/module/web/convert/index.js +1 -1
  47. package/lib/module/web/convert/index.js.map +1 -1
  48. package/lib/module/web/create.js +26 -64
  49. package/lib/module/web/create.js.map +1 -1
  50. package/lib/module/web/createUnistylesComponent.js +54 -0
  51. package/lib/module/web/createUnistylesComponent.js.map +1 -0
  52. package/lib/module/web/index.js +4 -2
  53. package/lib/module/web/index.js.map +1 -1
  54. package/lib/module/web/listener/index.js +1 -0
  55. package/lib/module/web/listener/index.js.map +1 -1
  56. package/lib/module/web/listener/listenToDependencies.js +34 -0
  57. package/lib/module/web/listener/listenToDependencies.js.map +1 -0
  58. package/lib/module/web/listener/listener.js +1 -1
  59. package/lib/module/web/listener/listener.js.map +1 -1
  60. package/lib/module/web/mock.js +0 -9
  61. package/lib/module/web/mock.js.map +1 -1
  62. package/lib/module/web/pseudo.js +2 -3
  63. package/lib/module/web/pseudo.js.map +1 -1
  64. package/lib/module/web/registry.js +2 -4
  65. package/lib/module/web/registry.js.map +1 -1
  66. package/lib/module/web/runtime.js +0 -1
  67. package/lib/module/web/runtime.js.map +1 -1
  68. package/lib/module/web/shadowRegistry.js +116 -0
  69. package/lib/module/web/shadowRegistry.js.map +1 -0
  70. package/lib/module/web/state.js +0 -40
  71. package/lib/module/web/state.js.map +1 -1
  72. package/lib/module/web/utils.js +59 -12
  73. package/lib/module/web/utils.js.map +1 -1
  74. package/lib/module/web/variants/useVariants.js +16 -32
  75. package/lib/module/web/variants/useVariants.js.map +1 -1
  76. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  77. package/lib/typescript/src/web/create.d.ts.map +1 -1
  78. package/lib/typescript/src/web/createUnistylesComponent.d.ts +3 -0
  79. package/lib/typescript/src/web/createUnistylesComponent.d.ts.map +1 -0
  80. package/lib/typescript/src/web/index.d.ts +2 -1
  81. package/lib/typescript/src/web/index.d.ts.map +1 -1
  82. package/lib/typescript/src/web/listener/index.d.ts +1 -0
  83. package/lib/typescript/src/web/listener/index.d.ts.map +1 -1
  84. package/lib/typescript/src/web/listener/listenToDependencies.d.ts +12 -0
  85. package/lib/typescript/src/web/listener/listenToDependencies.d.ts.map +1 -0
  86. package/lib/typescript/src/web/listener/listener.d.ts +2 -1
  87. package/lib/typescript/src/web/listener/listener.d.ts.map +1 -1
  88. package/lib/typescript/src/web/mock.d.ts +0 -9
  89. package/lib/typescript/src/web/mock.d.ts.map +1 -1
  90. package/lib/typescript/src/web/pseudo.d.ts +4 -2
  91. package/lib/typescript/src/web/pseudo.d.ts.map +1 -1
  92. package/lib/typescript/src/web/registry.d.ts +1 -1
  93. package/lib/typescript/src/web/registry.d.ts.map +1 -1
  94. package/lib/typescript/src/web/runtime.d.ts +0 -1
  95. package/lib/typescript/src/web/runtime.d.ts.map +1 -1
  96. package/lib/typescript/src/web/shadowRegistry.d.ts +17 -0
  97. package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -0
  98. package/lib/typescript/src/web/state.d.ts +0 -5
  99. package/lib/typescript/src/web/state.d.ts.map +1 -1
  100. package/lib/typescript/src/web/utils.d.ts +22 -7
  101. package/lib/typescript/src/web/utils.d.ts.map +1 -1
  102. package/lib/typescript/src/web/variants/useVariants.d.ts.map +1 -1
  103. package/nitrogen/generated/android/c++/JHybridNativePlatformSpec.hpp +6 -3
  104. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/HybridNativePlatformSpec.kt +8 -3
  105. package/nitrogen/generated/ios/c++/HybridNativePlatformSpecSwift.hpp +1 -1
  106. package/nitrogen/generated/ios/swift/HybridNativePlatformSpec.swift +1 -1
  107. package/nitrogen/generated/ios/swift/HybridNativePlatformSpecCxx.swift +14 -2
  108. package/nitrogen/generated/shared/c++/HybridNativePlatformSpec.hpp +3 -0
  109. package/nitrogen/generated/shared/c++/HybridUnistylesNavigationBarSpec.hpp +3 -0
  110. package/nitrogen/generated/shared/c++/HybridUnistylesRuntimeSpec.hpp +3 -0
  111. package/nitrogen/generated/shared/c++/HybridUnistylesShadowRegistrySpec.hpp +3 -0
  112. package/nitrogen/generated/shared/c++/HybridUnistylesStatusBarSpec.hpp +3 -0
  113. package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.cpp +1 -1
  114. package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.hpp +4 -1
  115. package/package.json +3 -3
  116. package/plugin/index.js +31 -0
  117. package/src/index.ts +3 -0
  118. package/src/specs/ShadowRegistry/index.ts +7 -1
  119. package/src/web/convert/index.ts +1 -1
  120. package/src/web/create.ts +24 -79
  121. package/src/web/createUnistylesComponent.tsx +56 -0
  122. package/src/web/index.ts +3 -2
  123. package/src/web/listener/index.ts +1 -0
  124. package/src/web/listener/listenToDependencies.ts +50 -0
  125. package/src/web/listener/listener.ts +1 -1
  126. package/src/web/mock.ts +0 -10
  127. package/src/web/pseudo.ts +131 -5
  128. package/src/web/registry.ts +3 -5
  129. package/src/web/runtime.ts +0 -2
  130. package/src/web/shadowRegistry.ts +121 -0
  131. package/src/web/state.ts +0 -44
  132. package/src/web/utils.ts +90 -16
  133. package/src/web/variants/useVariants.ts +18 -32
package/src/web/pseudo.ts CHANGED
@@ -1,11 +1,137 @@
1
- // TODO: Add complete list of pseudo selectors
2
1
  export const pseudos = [
2
+ '_-moz-broken',
3
+ '_-moz-drag-over',
4
+ '_-moz-first-node',
5
+ '_-moz-handler-blocked',
6
+ '_-moz-handler-crashed',
7
+ '_-moz-handler-disabled',
8
+ '_-moz-last-node',
9
+ '_-moz-loading',
10
+ '_-moz-locale-dir(ltr)',
11
+ '_-moz-locale-dir(rtl)',
12
+ '_-moz-only-whitespace',
13
+ '_-moz-submit-invalid',
14
+ '_-moz-suppressed',
15
+ '_-moz-user-disabled',
16
+ '_-moz-window-inactive',
17
+ '_active',
18
+ '_any-link',
19
+ '_autofill',
20
+ '_blank',
21
+ '_buffering',
22
+ '_checked',
23
+ '_current',
24
+ '_default',
25
+ '_defined',
26
+ '_dir(',
27
+ '_disabled',
28
+ '_empty',
29
+ '_enabled',
30
+ '_first',
31
+ '_first-child',
32
+ '_first-of-type',
33
+ '_focus',
34
+ '_focus-visible',
35
+ '_focus-within',
36
+ '_fullscreen',
37
+ '_future',
38
+ '_has(',
39
+ '_host',
40
+ '_host-context(',
41
+ '_host(',
3
42
  '_hover',
4
- '_css',
43
+ '_in-range',
44
+ '_indeterminate',
45
+ '_invalid',
46
+ '_is(',
47
+ '_lang(',
48
+ '_last-child',
49
+ '_last-of-type',
50
+ '_left',
51
+ '_link',
52
+ '_local-link',
53
+ '_modal',
54
+ '_muted',
55
+ '_not(',
56
+ '_nth-child(',
57
+ '_nth-last-child(',
58
+ '_nth-last-of-type(',
59
+ '_nth-of-type(',
60
+ '_only-child',
61
+ '_only-of-type',
62
+ '_optional',
63
+ '_out-of-range',
64
+ '_past',
65
+ '_paused',
66
+ '_picture-in-picture',
67
+ '_placeholder-shown',
68
+ '_playing',
69
+ '_popover-open',
70
+ '_read-only',
71
+ '_read-write',
72
+ '_required',
73
+ '_right',
74
+ '_scope',
75
+ '_seeking',
76
+ '_stalled',
77
+ '_state(',
78
+ '_target',
79
+ '_target-within',
80
+ '_user-invalid',
81
+ '_user-valid',
82
+ '_valid',
83
+ '_visited',
84
+ '_volume-locked',
85
+ '_where(',
86
+ '_-moz-color-swatch',
87
+ '_-moz-focus-inner',
88
+ '_-moz-list-bullet',
89
+ '_-moz-list-number',
90
+ '_-moz-meter-bar',
91
+ '_-moz-progress-bar',
92
+ '_-moz-range-progress',
93
+ '_-moz-range-thumb',
94
+ '_-moz-range-track',
95
+ '_-webkit-inner-spin-button',
96
+ '_-webkit-meter-barDeprecated',
97
+ '_-webkit-meter-even-less-good-value',
98
+ '_-webkit-meter-inner-element',
99
+ '_-webkit-meter-optimum-value',
100
+ '_-webkit-meter-suboptimum-value',
101
+ '_-webkit-progress-bar',
102
+ '_-webkit-progress-inner-element',
103
+ '_-webkit-progress-value',
104
+ '_-webkit-scrollbar',
105
+ '_-webkit-search-cancel-button',
106
+ '_-webkit-search-results-button',
107
+ '_-webkit-slider-runnable-track',
108
+ '_-webkit-slider-thumb',
109
+ '_after',
110
+ '_backdrop',
5
111
  '_before',
6
- '_after'
112
+ '_cue',
113
+ '_file-selector-button',
114
+ '_first-letter',
115
+ '_first-line',
116
+ '_grammar-error',
117
+ '_highlight(',
118
+ '_marker',
119
+ '_part(',
120
+ '_placeholder',
121
+ '_selection',
122
+ '_slotted(',
123
+ '_spelling-error',
124
+ '_target-text',
125
+ '_view-transition',
126
+ '_view-transition-group',
127
+ '_view-transition-image-pair',
128
+ '_view-transition-new',
129
+ '_view-transition-old',
7
130
  ] as const
8
131
 
9
- export const isPseudo = (selector: string) => pseudos.includes(selector as Pseudo)
132
+ export const isPseudo = (selector: string) => pseudos.some(pseudo => selector.startsWith(pseudo))
133
+
134
+ type AddDynamicSelector<T extends string> = T extends `${infer U}(` ? `${U}(${string})` : T
135
+
136
+ export type Pseudo = AddDynamicSelector<typeof pseudos[number]>
10
137
 
11
- export type Pseudo = typeof pseudos[number]
@@ -1,16 +1,14 @@
1
1
  import { createTypeStyle, TypeStyle } from 'typestyle'
2
2
  import type { UnistylesValues } from '../types'
3
3
  import { convertToTypeStyle } from './convert'
4
- import { UnistylesState } from './state'
5
4
 
6
5
  class UnistylesRegistryBuilder {
7
- createStyles = (stylesheet: UnistylesValues, key: string | number) => {
8
- const stylesTag = UnistylesState.createTag()
9
- const unistyles = createTypeStyle(stylesTag)
6
+ createStyles = (stylesheet: UnistylesValues, key: string) => {
7
+ const unistyles = createTypeStyle()
10
8
  const typestyleStylesheet = convertToTypeStyle(stylesheet)
11
9
 
12
10
  const className = unistyles.style({
13
- $debugName: String(key),
11
+ $debugName: `${key}-${Math.random().toString(16).slice(10)}`,
14
12
  }, typestyleStylesheet)
15
13
 
16
14
  if (stylesheet._web?._css) {
@@ -191,8 +191,6 @@ class UnistylesRuntimeBuilder {
191
191
 
192
192
  UnistylesState.themes.set(themeName, updater(oldTheme))
193
193
  }
194
-
195
- getSSRUnistyles = () => UnistylesState.tags
196
194
  }
197
195
 
198
196
  export const UnistylesRuntime = new UnistylesRuntimeBuilder()
@@ -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
- import { equal, reduceObject } from '../utils'
5
- import { UnistylesRegistry } from '../registry'
3
+ import { equal, keyInObject, reduceObject } from '../utils'
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,28 +25,14 @@ export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, set
29
25
  return result
30
26
  }, [lastSelectedVariantsRef.current])
31
27
 
32
- if (unistylesMap.size === 0 && combinedVariantStyles.length > 0) {
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
- const selectedVariantStyles = selectedVariantStylesMap.get(key)
46
30
 
47
- if (!unistyles || !className) {
31
+ if (!styleEntry) {
48
32
  return
49
33
  }
50
34
 
51
- if (variantsChanged) {
52
- UnistylesRegistry.updateStyles(unistyles, value, className)
53
- }
35
+ const selectedVariantStyles = selectedVariantStylesMap.get(key)
54
36
 
55
37
  Object.defineProperties(styleEntry, reduceObject(selectedVariantStyles ?? {}, value => ({
56
38
  value,
@@ -58,18 +40,22 @@ export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, set
58
40
  configurable: true
59
41
  })))
60
42
 
61
- if (styleEntry) {
62
- Object.keys(styleEntry ?? {}).forEach(key => {
63
- if (!key.startsWith('variant-')) {
64
- return
65
- }
43
+ // Add __uni__variants to static styles
44
+ Object.keys(styleEntry).forEach(key => {
45
+ if (!key.startsWith('__uni__secrets__')) {
46
+ return
47
+ }
48
+
49
+ const secret = keyInObject(styleEntry, key) ? styleEntry[key] : undefined
50
+
51
+ if (!secret) {
52
+ return
53
+ }
66
54
 
67
- // @ts-expect-error - remove old variants
68
- delete styleEntry[key]
55
+ Object.defineProperty(secret, '__uni__variants', {
56
+ value: selectedVariants
69
57
  })
70
- // @ts-expect-error - apply variant className
71
- styleEntry[className] = className
72
- }
58
+ })
73
59
  })
74
60
  }
75
61