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.
Files changed (137) hide show
  1. package/cxx/core/UnistyleWrapper.h +1 -1
  2. package/cxx/core/UnistylesCommitHook.cpp +9 -0
  3. package/cxx/core/UnistylesCommitHook.h +1 -0
  4. package/cxx/core/UnistylesMountHook.cpp +15 -1
  5. package/cxx/core/UnistylesRegistry.h +3 -1
  6. package/cxx/hybridObjects/HybridStyleSheet.cpp +1 -1
  7. package/cxx/hybridObjects/HybridStyleSheet.h +1 -1
  8. package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +1 -1
  9. package/cxx/shadowTree/ShadowTrafficController.h +33 -0
  10. package/cxx/shadowTree/ShadowTreeManager.cpp +7 -0
  11. package/cxx/shadowTree/ShadowTreeManager.h +1 -0
  12. package/lib/commonjs/index.js +4 -0
  13. package/lib/commonjs/index.js.map +1 -1
  14. package/lib/commonjs/specs/ShadowRegistry/index.js +7 -1
  15. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  16. package/lib/commonjs/web/convert/index.js +1 -1
  17. package/lib/commonjs/web/convert/index.js.map +1 -1
  18. package/lib/commonjs/web/create.js +25 -63
  19. package/lib/commonjs/web/create.js.map +1 -1
  20. package/lib/commonjs/web/createUnistylesComponent.js +61 -0
  21. package/lib/commonjs/web/createUnistylesComponent.js.map +1 -0
  22. package/lib/commonjs/web/index.js +12 -3
  23. package/lib/commonjs/web/index.js.map +1 -1
  24. package/lib/commonjs/web/listener/index.js +7 -0
  25. package/lib/commonjs/web/listener/index.js.map +1 -1
  26. package/lib/commonjs/web/listener/listenToDependencies.js +39 -0
  27. package/lib/commonjs/web/listener/listenToDependencies.js.map +1 -0
  28. package/lib/commonjs/web/listener/listener.js +1 -2
  29. package/lib/commonjs/web/listener/listener.js.map +1 -1
  30. package/lib/commonjs/web/mock.js +1 -10
  31. package/lib/commonjs/web/mock.js.map +1 -1
  32. package/lib/commonjs/web/pseudo.js +2 -3
  33. package/lib/commonjs/web/pseudo.js.map +1 -1
  34. package/lib/commonjs/web/registry.js +2 -4
  35. package/lib/commonjs/web/registry.js.map +1 -1
  36. package/lib/commonjs/web/runtime.js +0 -1
  37. package/lib/commonjs/web/runtime.js.map +1 -1
  38. package/lib/commonjs/web/shadowRegistry.js +120 -0
  39. package/lib/commonjs/web/shadowRegistry.js.map +1 -0
  40. package/lib/commonjs/web/state.js +0 -40
  41. package/lib/commonjs/web/state.js.map +1 -1
  42. package/lib/commonjs/web/utils.js +66 -14
  43. package/lib/commonjs/web/utils.js.map +1 -1
  44. package/lib/commonjs/web/variants/useVariants.js +1 -34
  45. package/lib/commonjs/web/variants/useVariants.js.map +1 -1
  46. package/lib/module/index.js +4 -0
  47. package/lib/module/index.js.map +1 -1
  48. package/lib/module/specs/ShadowRegistry/index.js +7 -1
  49. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  50. package/lib/module/web/convert/index.js +1 -1
  51. package/lib/module/web/convert/index.js.map +1 -1
  52. package/lib/module/web/create.js +26 -64
  53. package/lib/module/web/create.js.map +1 -1
  54. package/lib/module/web/createUnistylesComponent.js +54 -0
  55. package/lib/module/web/createUnistylesComponent.js.map +1 -0
  56. package/lib/module/web/index.js +4 -2
  57. package/lib/module/web/index.js.map +1 -1
  58. package/lib/module/web/listener/index.js +1 -0
  59. package/lib/module/web/listener/index.js.map +1 -1
  60. package/lib/module/web/listener/listenToDependencies.js +34 -0
  61. package/lib/module/web/listener/listenToDependencies.js.map +1 -0
  62. package/lib/module/web/listener/listener.js +1 -1
  63. package/lib/module/web/listener/listener.js.map +1 -1
  64. package/lib/module/web/mock.js +0 -9
  65. package/lib/module/web/mock.js.map +1 -1
  66. package/lib/module/web/pseudo.js +2 -3
  67. package/lib/module/web/pseudo.js.map +1 -1
  68. package/lib/module/web/registry.js +2 -4
  69. package/lib/module/web/registry.js.map +1 -1
  70. package/lib/module/web/runtime.js +0 -1
  71. package/lib/module/web/runtime.js.map +1 -1
  72. package/lib/module/web/shadowRegistry.js +116 -0
  73. package/lib/module/web/shadowRegistry.js.map +1 -0
  74. package/lib/module/web/state.js +0 -40
  75. package/lib/module/web/state.js.map +1 -1
  76. package/lib/module/web/utils.js +59 -12
  77. package/lib/module/web/utils.js.map +1 -1
  78. package/lib/module/web/variants/useVariants.js +1 -34
  79. package/lib/module/web/variants/useVariants.js.map +1 -1
  80. package/lib/typescript/example/App.d.ts.map +1 -1
  81. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  82. package/lib/typescript/src/web/create.d.ts.map +1 -1
  83. package/lib/typescript/src/web/createUnistylesComponent.d.ts +3 -0
  84. package/lib/typescript/src/web/createUnistylesComponent.d.ts.map +1 -0
  85. package/lib/typescript/src/web/index.d.ts +2 -1
  86. package/lib/typescript/src/web/index.d.ts.map +1 -1
  87. package/lib/typescript/src/web/listener/index.d.ts +1 -0
  88. package/lib/typescript/src/web/listener/index.d.ts.map +1 -1
  89. package/lib/typescript/src/web/listener/listenToDependencies.d.ts +12 -0
  90. package/lib/typescript/src/web/listener/listenToDependencies.d.ts.map +1 -0
  91. package/lib/typescript/src/web/listener/listener.d.ts +2 -1
  92. package/lib/typescript/src/web/listener/listener.d.ts.map +1 -1
  93. package/lib/typescript/src/web/mock.d.ts +0 -9
  94. package/lib/typescript/src/web/mock.d.ts.map +1 -1
  95. package/lib/typescript/src/web/pseudo.d.ts +4 -2
  96. package/lib/typescript/src/web/pseudo.d.ts.map +1 -1
  97. package/lib/typescript/src/web/registry.d.ts +1 -1
  98. package/lib/typescript/src/web/registry.d.ts.map +1 -1
  99. package/lib/typescript/src/web/runtime.d.ts +0 -1
  100. package/lib/typescript/src/web/runtime.d.ts.map +1 -1
  101. package/lib/typescript/src/web/shadowRegistry.d.ts +17 -0
  102. package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -0
  103. package/lib/typescript/src/web/state.d.ts +0 -5
  104. package/lib/typescript/src/web/state.d.ts.map +1 -1
  105. package/lib/typescript/src/web/utils.d.ts +22 -7
  106. package/lib/typescript/src/web/utils.d.ts.map +1 -1
  107. package/lib/typescript/src/web/variants/useVariants.d.ts.map +1 -1
  108. package/nitrogen/generated/android/c++/JHybridNativePlatformSpec.hpp +6 -3
  109. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/HybridNativePlatformSpec.kt +8 -3
  110. package/nitrogen/generated/ios/c++/HybridNativePlatformSpecSwift.hpp +1 -1
  111. package/nitrogen/generated/ios/swift/HybridNativePlatformSpec.swift +1 -1
  112. package/nitrogen/generated/ios/swift/HybridNativePlatformSpecCxx.swift +14 -2
  113. package/nitrogen/generated/shared/c++/HybridNativePlatformSpec.hpp +3 -0
  114. package/nitrogen/generated/shared/c++/HybridUnistylesNavigationBarSpec.hpp +3 -0
  115. package/nitrogen/generated/shared/c++/HybridUnistylesRuntimeSpec.hpp +3 -0
  116. package/nitrogen/generated/shared/c++/HybridUnistylesShadowRegistrySpec.hpp +3 -0
  117. package/nitrogen/generated/shared/c++/HybridUnistylesStatusBarSpec.hpp +3 -0
  118. package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.cpp +1 -1
  119. package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.hpp +4 -1
  120. package/package.json +3 -3
  121. package/src/index.ts +3 -0
  122. package/src/specs/ShadowRegistry/index.ts +7 -1
  123. package/src/web/convert/index.ts +1 -1
  124. package/src/web/create.ts +24 -79
  125. package/src/web/createUnistylesComponent.tsx +56 -0
  126. package/src/web/index.ts +3 -2
  127. package/src/web/listener/index.ts +1 -0
  128. package/src/web/listener/listenToDependencies.ts +50 -0
  129. package/src/web/listener/listener.ts +1 -1
  130. package/src/web/mock.ts +0 -10
  131. package/src/web/pseudo.ts +131 -5
  132. package/src/web/registry.ts +3 -5
  133. package/src/web/runtime.ts +0 -2
  134. package/src/web/shadowRegistry.ts +121 -0
  135. package/src/web/state.ts +0 -44
  136. package/src/web/utils.ts +90 -16
  137. package/src/web/variants/useVariants.ts +1 -37
@@ -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.12",
3
+ "version": "3.0.0-alpha.14",
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/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) {
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
- }
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) {