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
@@ -21,10 +21,8 @@ import com.margelo.nitro.core.*
21
21
  @Keep
22
22
  @Suppress("RedundantSuppression", "KotlinJniMissingFunction", "PropertyName", "RedundantUnitReturnType", "unused")
23
23
  abstract class HybridNativePlatformSpec: HybridObject() {
24
- protected val TAG = "HybridNativePlatformSpec"
25
-
26
24
  @DoNotStrip
27
- val mHybridData: HybridData = initHybrid()
25
+ private var mHybridData: HybridData = initHybrid()
28
26
 
29
27
  init {
30
28
  // Pass this `HybridData` through to it's base class,
@@ -32,6 +30,13 @@ abstract class HybridNativePlatformSpec: HybridObject() {
32
30
  super.updateNative(mHybridData)
33
31
  }
34
32
 
33
+ /**
34
+ * Call from a child class to initialize HybridData with a child.
35
+ */
36
+ override fun updateNative(hybridData: HybridData) {
37
+ mHybridData = hybridData
38
+ }
39
+
35
40
  // Properties
36
41
 
37
42
 
@@ -55,7 +55,7 @@ namespace margelo::nitro::unistyles {
55
55
  * the future, HybridNativePlatformSpecCxx can directly inherit from the C++ class HybridNativePlatformSpec
56
56
  * to simplify the whole structure and memory management.
57
57
  */
58
- class HybridNativePlatformSpecSwift final: public HybridNativePlatformSpec {
58
+ class HybridNativePlatformSpecSwift: public virtual HybridNativePlatformSpec {
59
59
  public:
60
60
  // Constructor from a Swift instance
61
61
  explicit HybridNativePlatformSpecSwift(const Unistyles::HybridNativePlatformSpecCxx& swiftPart):
@@ -27,7 +27,7 @@ import NitroModules
27
27
  * }
28
28
  * ```
29
29
  */
30
- public protocol HybridNativePlatformSpec: HybridObjectSpec {
30
+ public protocol HybridNativePlatformSpec: AnyObject, HybridObjectSpec {
31
31
  // Properties
32
32
 
33
33
 
@@ -17,7 +17,7 @@ import NitroModules
17
17
  * - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper
18
18
  * - Throwing methods need to be wrapped with a Result<T, Error> type, as exceptions cannot be propagated to C++
19
19
  */
20
- public final class HybridNativePlatformSpecCxx {
20
+ public class HybridNativePlatformSpecCxx {
21
21
  /**
22
22
  * The Swift <> C++ bridge's namespace (`margelo::nitro::unistyles::bridge::swift`)
23
23
  * from `Unistyles-Swift-Cxx-Bridge.hpp`.
@@ -28,7 +28,15 @@ public final class HybridNativePlatformSpecCxx {
28
28
  /**
29
29
  * Holds an instance of the `HybridNativePlatformSpec` Swift protocol.
30
30
  */
31
- private(set) var implementation: HybridNativePlatformSpec
31
+ private var implementation: HybridNativePlatformSpec
32
+
33
+ /**
34
+ * Get the actual `HybridNativePlatformSpec` instance this class wraps.
35
+ */
36
+ @inline(__always)
37
+ public func getHybridNativePlatformSpec() -> HybridNativePlatformSpec {
38
+ return implementation
39
+ }
32
40
 
33
41
  /**
34
42
  * Create a new `HybridNativePlatformSpecCxx` that wraps the given `HybridNativePlatformSpec`.
@@ -36,15 +44,18 @@ public final class HybridNativePlatformSpecCxx {
36
44
  */
37
45
  public init(_ implementation: HybridNativePlatformSpec) {
38
46
  self.implementation = implementation
47
+ /* no base class */
39
48
  }
40
49
 
41
50
  /**
42
51
  * Contains a (weak) reference to the C++ HybridObject to cache it.
43
52
  */
44
53
  public var hybridContext: margelo.nitro.HybridContext {
54
+ @inline(__always)
45
55
  get {
46
56
  return self.implementation.hybridContext
47
57
  }
58
+ @inline(__always)
48
59
  set {
49
60
  self.implementation.hybridContext = newValue
50
61
  }
@@ -54,6 +65,7 @@ public final class HybridNativePlatformSpecCxx {
54
65
  * Get the memory size of the Swift class (plus size of any other allocations)
55
66
  * so the JS VM can properly track it and garbage-collect the JS object if needed.
56
67
  */
68
+ @inline(__always)
57
69
  public var memorySize: Int {
58
70
  return self.implementation.memorySize
59
71
  }
@@ -43,9 +43,12 @@ namespace margelo::nitro::unistyles {
43
43
  /**
44
44
  * An abstract base class for `NativePlatform`
45
45
  * Inherit this class to create instances of `HybridNativePlatformSpec` in C++.
46
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
46
47
  * @example
47
48
  * ```cpp
48
49
  * class HybridNativePlatform: public HybridNativePlatformSpec {
50
+ * public:
51
+ * HybridNativePlatform(...): HybridObject(TAG) { ... }
49
52
  * // ...
50
53
  * };
51
54
  * ```
@@ -24,9 +24,12 @@ namespace margelo::nitro::unistyles {
24
24
  /**
25
25
  * An abstract base class for `UnistylesNavigationBar`
26
26
  * Inherit this class to create instances of `HybridUnistylesNavigationBarSpec` in C++.
27
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
27
28
  * @example
28
29
  * ```cpp
29
30
  * class HybridUnistylesNavigationBar: public HybridUnistylesNavigationBarSpec {
31
+ * public:
32
+ * HybridUnistylesNavigationBar(...): HybridObject(TAG) { ... }
30
33
  * // ...
31
34
  * };
32
35
  * ```
@@ -39,9 +39,12 @@ namespace margelo::nitro::unistyles {
39
39
  /**
40
40
  * An abstract base class for `UnistylesRuntime`
41
41
  * Inherit this class to create instances of `HybridUnistylesRuntimeSpec` in C++.
42
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
42
43
  * @example
43
44
  * ```cpp
44
45
  * class HybridUnistylesRuntime: public HybridUnistylesRuntimeSpec {
46
+ * public:
47
+ * HybridUnistylesRuntime(...): HybridObject(TAG) { ... }
45
48
  * // ...
46
49
  * };
47
50
  * ```
@@ -24,9 +24,12 @@ namespace margelo::nitro::unistyles {
24
24
  /**
25
25
  * An abstract base class for `UnistylesShadowRegistry`
26
26
  * Inherit this class to create instances of `HybridUnistylesShadowRegistrySpec` in C++.
27
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
27
28
  * @example
28
29
  * ```cpp
29
30
  * class HybridUnistylesShadowRegistry: public HybridUnistylesShadowRegistrySpec {
31
+ * public:
32
+ * HybridUnistylesShadowRegistry(...): HybridObject(TAG) { ... }
30
33
  * // ...
31
34
  * };
32
35
  * ```
@@ -24,9 +24,12 @@ namespace margelo::nitro::unistyles {
24
24
  /**
25
25
  * An abstract base class for `UnistylesStatusBar`
26
26
  * Inherit this class to create instances of `HybridUnistylesStatusBarSpec` in C++.
27
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
27
28
  * @example
28
29
  * ```cpp
29
30
  * class HybridUnistylesStatusBar: public HybridUnistylesStatusBarSpec {
31
+ * public:
32
+ * HybridUnistylesStatusBar(...): HybridObject(TAG) { ... }
30
33
  * // ...
31
34
  * };
32
35
  * ```
@@ -15,7 +15,7 @@ namespace margelo::nitro::unistyles {
15
15
  // load custom methods/properties
16
16
  registerHybrids(this, [](Prototype& prototype) {
17
17
  prototype.registerHybridGetter("hairlineWidth", &HybridUnistylesStyleSheetSpec::getHairlineWidth);
18
- prototype.registerHybridGetter("___unid", &HybridUnistylesStyleSheetSpec::get___unid);
18
+ prototype.registerHybridGetter("__unid", &HybridUnistylesStyleSheetSpec::get__unid);
19
19
  });
20
20
  }
21
21
 
@@ -24,9 +24,12 @@ namespace margelo::nitro::unistyles {
24
24
  /**
25
25
  * An abstract base class for `UnistylesStyleSheet`
26
26
  * Inherit this class to create instances of `HybridUnistylesStyleSheetSpec` in C++.
27
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
27
28
  * @example
28
29
  * ```cpp
29
30
  * class HybridUnistylesStyleSheet: public HybridUnistylesStyleSheetSpec {
31
+ * public:
32
+ * HybridUnistylesStyleSheet(...): HybridObject(TAG) { ... }
30
33
  * // ...
31
34
  * };
32
35
  * ```
@@ -42,7 +45,7 @@ namespace margelo::nitro::unistyles {
42
45
  public:
43
46
  // Properties
44
47
  virtual double getHairlineWidth() = 0;
45
- virtual double get___unid() = 0;
48
+ virtual double get__unid() = 0;
46
49
 
47
50
  public:
48
51
  // Methods
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "3.0.0-alpha.13",
3
+ "version": "3.0.0-alpha.15",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -74,11 +74,11 @@
74
74
  "husky": "9.1.5",
75
75
  "jest": "29.7.0",
76
76
  "metro-react-native-babel-preset": "0.77.0",
77
- "nitro-codegen": "0.10.0",
77
+ "nitro-codegen": "0.11.0",
78
78
  "react": "18.3.1",
79
79
  "react-native": "0.75.2",
80
80
  "react-native-builder-bob": "0.30.0",
81
- "react-native-nitro-modules": "0.10.0",
81
+ "react-native-nitro-modules": "0.11.0",
82
82
  "react-native-web": "0.19.12",
83
83
  "react-test-renderer": "18.3.1",
84
84
  "release-it": "17.6.0",
package/plugin/index.js CHANGED
@@ -42,6 +42,37 @@ module.exports = function ({ types: t }) {
42
42
  }
43
43
  }
44
44
  },
45
+ FunctionDeclaration(path, state) {
46
+ const componentName = path.node.id
47
+ ? path.node.id.name
48
+ : null
49
+
50
+ if (componentName) {
51
+ state.file.hasVariants = false
52
+ }
53
+ },
54
+ ClassDeclaration(path, state) {
55
+ const componentName = path.node.id
56
+ ? path.node.id.name
57
+ : null
58
+
59
+ if (componentName) {
60
+ state.file.hasVariants = false
61
+ }
62
+ },
63
+ VariableDeclaration(path, state) {
64
+ path.node.declarations.forEach((declaration) => {
65
+ if (t.isArrowFunctionExpression(declaration.init) || t.isFunctionExpression(declaration.init)) {
66
+ const componentName = declaration.id
67
+ ? declaration.id.name
68
+ : null
69
+
70
+ if (componentName) {
71
+ state.file.hasVariants = false
72
+ }
73
+ }
74
+ })
75
+ },
45
76
  ImportDeclaration(path, state) {
46
77
  const importSource = path.node.source.value
47
78
 
package/src/index.ts CHANGED
@@ -3,6 +3,9 @@ import React from 'react'
3
3
  export * from './specs'
4
4
  export { mq } from './mq'
5
5
  export type { UnistylesThemes, UnistylesBreakpoints } from './global'
6
+ // TODO: export Native createUnistylesComponent
7
+ // can't import it this way, it will break native build
8
+ // export { createUnistylesComponent } from './web/createUnistylesComponent'
6
9
 
7
10
  // todo verify true min version
8
11
  const minReactVersionRequiredByUnistyles = '18.3.1'
@@ -27,7 +27,13 @@ const findShadowNodeForHandle = (handle: ViewHandle) => {
27
27
  }
28
28
 
29
29
  HybridShadowRegistry.add = (handle, style, variants, args) => {
30
- if (!handle || typeof style !== 'object') {
30
+ // virtualized nodes can be null
31
+ if (!handle || !style) {
32
+ return
33
+ }
34
+
35
+ // at this point unistyle can be only object or dynamic function
36
+ if (typeof style !== 'object' && typeof style !== 'function') {
31
37
  return
32
38
  }
33
39
 
@@ -21,7 +21,7 @@ export const convertToTypeStyle = (value: UnistylesValues) => {
21
21
  ...value._web
22
22
  }).flatMap(([unistylesKey, unistylesValue]) => {
23
23
  // Keys to omit
24
- if (['_css', '_web', 'variants', 'compoundVariants', 'uni__dependencies'].includes(unistylesKey)) {
24
+ if (['_css', '_web', 'variants', 'compoundVariants', 'uni__dependencies', '__unistyles-secrets__'].includes(unistylesKey) || unistylesKey.startsWith('variant-')) {
25
25
  return []
26
26
  }
27
27
 
package/src/web/create.ts CHANGED
@@ -1,105 +1,50 @@
1
- import type { TypeStyle } from 'typestyle'
2
1
  import type { ReactNativeStyleSheet } from '../types'
3
2
  import type { StyleSheetWithSuperPowers, StyleSheet } from '../types/stylesheet'
4
- import { UnistylesRegistry } from './registry'
5
- import { keyInObject, reduceObject, toReactNativeClassName } from './utils'
3
+ import { assignSecrets, reduceObject, getStyles } from './utils'
6
4
  import { UnistylesRuntime } from './runtime'
7
5
  import { createUseVariants, getVariants } from './variants'
8
- import { UnistylesListener } from './listener'
9
- import type { UnistyleDependency } from '../specs/NativePlatform'
10
-
11
- type ListenToDependenciesProps = {
12
- value: StyleSheet[keyof StyleSheet],
13
- key: PropertyKey,
14
- unistyles: TypeStyle,
15
- className: string,
16
- args?: Array<any>
17
- }
18
-
19
- type WebUnistyle = ReturnType<typeof UnistylesRegistry.createStyles>
20
6
 
21
7
  export const create = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>) => {
22
8
  const computedStylesheet = typeof stylesheet === 'function'
23
9
  ? stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
24
10
  : stylesheet
25
- let lastlySelectedVariants: Record<string, any> = {}
26
-
27
- const listenToDependencies = ({ key, className, unistyles, value, args = [] } : ListenToDependenciesProps) => {
28
- const dependencies = ('uni__dependencies' in value ? value['uni__dependencies'] : []) as Array<UnistyleDependency>
29
-
30
- if (dependencies.length === 0) {
31
- return
32
- }
33
-
34
- return UnistylesListener.addListeners(dependencies, () => {
35
- const newComputedStylesheet = typeof stylesheet === 'function'
36
- ? stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
37
- : stylesheet
38
-
39
- if (!keyInObject(newComputedStylesheet, key)) {
40
- return
41
- }
42
-
43
- const value = newComputedStylesheet[key]!
44
- const result = typeof value === 'function'
45
- ? value(...args)
46
- : value
11
+ const selectedVariants = new Map<string, any>()
12
+
13
+ const copyVariants = () => Object.fromEntries(selectedVariants.entries())
14
+ const addSecrets = (value: any, key: string, args?: Array<any>) => assignSecrets(value, {
15
+ __uni__key: key,
16
+ __uni__refs: new Set(),
17
+ __uni__stylesheet: stylesheet,
18
+ __uni__args: args,
19
+ __uni__variants: copyVariants()
20
+ })
47
21
 
48
- UnistylesRegistry.updateStyles(unistyles, result, className)
49
- })
50
- }
22
+ const styles = reduceObject(computedStylesheet, (value, _key) => {
23
+ const key = String(_key)
51
24
 
52
- const styles = reduceObject(computedStylesheet, (value, key) => {
53
25
  if (typeof value === 'function') {
54
- const webUnistyleByRef = new Map<HTMLElement, WebUnistyle>()
55
- const disposeByRef = new Map<HTMLElement, VoidFunction | undefined>()
56
-
57
- return (...args: Array<any>) => {
58
- const [ref] = args.slice(-1)
26
+ const dynamicStyle = (...args: Array<any>) => {
59
27
  const result = value(...args)
60
- const variants = Object.fromEntries(getVariants({ [key]: result } as ReactNativeStyleSheet<StyleSheet>, lastlySelectedVariants))
28
+ const variants = Object.fromEntries(getVariants({ [key]: result } as ReactNativeStyleSheet<StyleSheet>, copyVariants()))
61
29
  const resultWithVariants = {
62
30
  ...result,
63
31
  ...variants[key]
64
32
  }
65
33
 
66
- if (ref instanceof HTMLElement) {
67
- const storedWebUnistyle = webUnistyleByRef.get(ref)
68
- const webUnistyle = storedWebUnistyle ?? UnistylesRegistry.createStyles(resultWithVariants, key)
69
-
70
- webUnistyleByRef.set(ref, webUnistyle)
71
- disposeByRef.get(ref)?.()
72
- disposeByRef.set(ref, listenToDependencies({
73
- key,
74
- value,
75
- unistyles: webUnistyle.unistyles,
76
- className: webUnistyle.className,
77
- args
78
- }))
79
- ref.classList.add(webUnistyle.className)
80
-
81
- if (storedWebUnistyle) {
82
- UnistylesRegistry.updateStyles(webUnistyle.unistyles, resultWithVariants, webUnistyle.className)
83
- }
84
-
85
-
86
- return
87
- }
88
-
89
- return toReactNativeClassName(null, resultWithVariants)
34
+ // Add secrets to result of dynamic styles function
35
+ return addSecrets(getStyles(resultWithVariants), key, args)
90
36
  }
91
- }
92
-
93
- const { className, unistyles } = UnistylesRegistry.createStyles(value, key)
94
37
 
95
- listenToDependencies({ key, value, unistyles, className })
38
+ // Add secrets to dynamic styles function
39
+ return addSecrets(dynamicStyle, key)
40
+ }
96
41
 
97
- return toReactNativeClassName(className, value)
42
+ // Add secrets to static styles
43
+ return addSecrets(getStyles(value), key)
98
44
  }) as ReactNativeStyleSheet<StyleSheet>
99
45
 
100
- createUseVariants(styles, newVariants => {
101
- lastlySelectedVariants = newVariants
102
- })
46
+ // Inject useVariants hook to styles
47
+ createUseVariants(styles, newVariants => Object.entries(newVariants).forEach(([key, value]) => selectedVariants.set(key, value)))
103
48
 
104
49
  return styles
105
50
  }
@@ -0,0 +1,56 @@
1
+ import React, { useEffect, useState } from 'react'
2
+ import { extractHiddenProperties, extractSecrets } from './utils'
3
+ import { UnistylesRuntime } from './runtime'
4
+ import { getVariants } from './variants'
5
+ import { UnistylesListener } from './listener'
6
+
7
+ const getStyles = (value: Record<string, any>) => {
8
+ const secrets = extractSecrets(value)
9
+
10
+ if (secrets.length === 0) {
11
+ return undefined
12
+ }
13
+
14
+ return secrets.reduce((acc, { __uni__stylesheet, __uni__key, __uni__args = [], __uni__variants }) => {
15
+ const newComputedStylesheet = typeof __uni__stylesheet === 'function'
16
+ ? __uni__stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
17
+ : __uni__stylesheet
18
+ const style = newComputedStylesheet[__uni__key]
19
+ const resultHidden = typeof style === 'function'
20
+ ? style(...__uni__args)
21
+ : style
22
+ const result = extractHiddenProperties(resultHidden)
23
+ const { variants } = Object.fromEntries(getVariants({ variants: result }, __uni__variants ))
24
+ const resultWithVariants = {
25
+ ...result,
26
+ ...variants
27
+ }
28
+
29
+ return {
30
+ ...acc,
31
+ ...resultWithVariants
32
+ }
33
+ }, {} as Record<string, any>)
34
+ }
35
+
36
+ export const createUnistylesComponent = <TProps extends object>(Component: React.ComponentType<TProps>) => (props: TProps) => {
37
+ const passedStyles = (props as TProps & { style: Record<string, any> }).style ?? {}
38
+ const [style, setStyle] = useState(getStyles(passedStyles))
39
+
40
+ useEffect(() => {
41
+ const newStyles = getStyles(passedStyles)
42
+ const dependencies = newStyles?.['uni__dependencies'] ?? []
43
+ const dispose = UnistylesListener.addListeners(dependencies, () => setStyle(getStyles(passedStyles)))
44
+
45
+ setStyle(newStyles)
46
+
47
+ return dispose
48
+ }, [passedStyles])
49
+
50
+ return (
51
+ <Component
52
+ {...props}
53
+ style={style}
54
+ />
55
+ )
56
+ }
package/src/web/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { create } from './create'
2
- import { UnistylesRuntime } from './runtime'
3
2
  import { UnistylesState } from './state'
4
3
  import { deepMergeObjects } from './utils'
5
4
 
@@ -19,6 +18,8 @@ export const StyleSheet = {
19
18
  }
20
19
 
21
20
  export { UnistylesRuntime } from './runtime'
21
+ export { UnistylesShadowRegistry } from './shadowRegistry'
22
22
  export * from './mock'
23
23
 
24
- export const getSSRUnistyles = () => UnistylesRuntime.getSSRUnistyles()
24
+ // TODO: Work on SSR
25
+ export const getSSRUnistyles = () => []
@@ -1 +1,2 @@
1
1
  export { UnistylesListener } from './listener'
2
+ export { listenToDependencies } from './listenToDependencies'
@@ -0,0 +1,50 @@
1
+ import type { TypeStyle } from 'typestyle'
2
+ import type { StyleSheet, StyleSheetWithSuperPowers } from '../../types/stylesheet'
3
+ import type { UnistyleDependency } from '../../specs/NativePlatform'
4
+ import { UnistylesListener } from './listener'
5
+ import { UnistylesRuntime } from '../runtime'
6
+ import { keyInObject } from '../utils'
7
+ import { UnistylesRegistry } from '../registry'
8
+
9
+ type ListenToDependenciesProps = {
10
+ key: PropertyKey,
11
+ unistyles: TypeStyle,
12
+ className: string,
13
+ stylesheet: StyleSheetWithSuperPowers<StyleSheet>
14
+ args?: Array<any>
15
+ }
16
+
17
+ export const listenToDependencies = ({ key, className, unistyles, args = [], stylesheet }: ListenToDependenciesProps) => {
18
+ const newComputedStylesheet = typeof stylesheet === 'function'
19
+ ? stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
20
+ : stylesheet
21
+ const _value = keyInObject(newComputedStylesheet, key) ? newComputedStylesheet[key] : undefined
22
+
23
+ if (!_value) {
24
+ return
25
+ }
26
+
27
+ const value = typeof _value === 'function' ? _value(...args) : _value
28
+ const dependencies = ('uni__dependencies' in value ? value['uni__dependencies'] : []) as Array<UnistyleDependency>
29
+
30
+ if (dependencies.length === 0) {
31
+ return
32
+ }
33
+
34
+ return UnistylesListener.addListeners(dependencies, () => {
35
+ const newComputedStylesheet = typeof stylesheet === 'function'
36
+ ? stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
37
+ : stylesheet
38
+
39
+ if (!keyInObject(newComputedStylesheet, key)) {
40
+ return
41
+ }
42
+
43
+ const value = newComputedStylesheet[key]!
44
+ const result = typeof value === 'function'
45
+ ? value(...args)
46
+ : value
47
+
48
+ UnistylesRegistry.updateStyles(unistyles, result, className)
49
+ })
50
+ }
@@ -1,7 +1,7 @@
1
1
  import { UnistyleDependency } from '../../specs/NativePlatform'
2
2
  import { UnistylesRuntime } from '../runtime'
3
3
 
4
- export class UnistylesListenerBuilder {
4
+ class UnistylesListenerBuilder {
5
5
  private isInitialized = false
6
6
  private listeners = Array.from({ length: Object.keys(UnistyleDependency).length / 2 }, () => new Set<VoidFunction>())
7
7
 
package/src/web/mock.ts CHANGED
@@ -23,13 +23,3 @@ export const NavigationBar: NavigationBarSpec = {
23
23
  __type: 'web',
24
24
  name: 'NavigationBar'
25
25
  }
26
-
27
- export const UnistylesShadowRegistry = {
28
- name: 'UnistylesShadowRegistry',
29
- __type: 'web',
30
- equals: () => true,
31
- toString: () => 'UnistylesShadowRegistry',
32
- dispose: () => {},
33
- add: () => {},
34
- remove: () => {},
35
- }