react-native-unistyles 3.7.0 → 3.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/README.md +2 -2
  2. package/android/src/main/java/com/unistyles/NativePlatform+android.kt +2 -15
  3. package/android/src/main/java/com/unistyles/NativePlatform+listener.kt +1 -1
  4. package/codemods/use-variants.js +155 -0
  5. package/components/native/TouchableHighlight/package.json +1 -1
  6. package/cxx/converters/TransformOriginConverter.cpp +3 -2
  7. package/cxx/core/DependencyTracker.h +162 -0
  8. package/cxx/core/StyleSheet.h +14 -0
  9. package/cxx/core/Unistyle.h +6 -3
  10. package/cxx/core/UnistylesRegistry.cpp +16 -2
  11. package/cxx/core/UnistylesRegistry.h +2 -1
  12. package/cxx/hybridObjects/HybridShadowRegistry.cpp +16 -5
  13. package/cxx/hybridObjects/HybridShadowRegistry.h +1 -1
  14. package/cxx/hybridObjects/HybridStyleSheet.cpp +8 -1
  15. package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +1 -1
  16. package/cxx/parser/Parser.cpp +83 -31
  17. package/cxx/parser/Parser.h +5 -0
  18. package/cxx/shadowTree/NativeProps.h +23 -0
  19. package/cxx/shadowTree/ShadowTrafficController.h +5 -0
  20. package/cxx/shadowTree/ShadowTreeManager.cpp +73 -1
  21. package/ios/NativePlatform+ios.swift +4 -6
  22. package/ios/Unistyles.h +1 -0
  23. package/lib/commonjs/core/trackDependencies.js +10 -0
  24. package/lib/commonjs/core/trackDependencies.js.map +1 -0
  25. package/lib/commonjs/core/trackDependencies.metro.js +10 -0
  26. package/lib/commonjs/core/trackDependencies.metro.js.map +1 -0
  27. package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js +4 -0
  28. package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
  29. package/lib/commonjs/core/withUnistyles/withUnistyles.native.js +63 -14
  30. package/lib/commonjs/core/withUnistyles/withUnistyles.native.js.map +1 -1
  31. package/lib/commonjs/hooks/useMedia.js +6 -0
  32. package/lib/commonjs/hooks/useMedia.js.map +1 -1
  33. package/lib/commonjs/mocks.js +4 -2
  34. package/lib/commonjs/mocks.js.map +1 -1
  35. package/lib/commonjs/reanimated/variant/useUpdateVariantColor.js +13 -4
  36. package/lib/commonjs/reanimated/variant/useUpdateVariantColor.js.map +1 -1
  37. package/lib/commonjs/specs/ShadowRegistry/index.js +26 -1
  38. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  39. package/lib/commonjs/specs/StyleSheet/index.js +6 -1
  40. package/lib/commonjs/specs/StyleSheet/index.js.map +1 -1
  41. package/lib/commonjs/types/stylesheet.js.map +1 -1
  42. package/lib/commonjs/web/convert/index.js +2 -2
  43. package/lib/commonjs/web/convert/index.js.map +1 -1
  44. package/lib/commonjs/web/convert/style.js +6 -0
  45. package/lib/commonjs/web/convert/style.js.map +1 -1
  46. package/lib/commonjs/web/create.js +35 -6
  47. package/lib/commonjs/web/create.js.map +1 -1
  48. package/lib/commonjs/web/css/core.js +12 -2
  49. package/lib/commonjs/web/css/core.js.map +1 -1
  50. package/lib/commonjs/web/css/sheet.js +223 -0
  51. package/lib/commonjs/web/css/sheet.js.map +1 -0
  52. package/lib/commonjs/web/css/state.js +98 -78
  53. package/lib/commonjs/web/css/state.js.map +1 -1
  54. package/lib/commonjs/web/css/utils.js +1 -31
  55. package/lib/commonjs/web/css/utils.js.map +1 -1
  56. package/lib/commonjs/web/dependencyTracker.js +72 -0
  57. package/lib/commonjs/web/dependencyTracker.js.map +1 -0
  58. package/lib/commonjs/web/registry.js +73 -4
  59. package/lib/commonjs/web/registry.js.map +1 -1
  60. package/lib/commonjs/web/runtime.js +0 -1
  61. package/lib/commonjs/web/runtime.js.map +1 -1
  62. package/lib/commonjs/web/shadowRegistry.js +83 -11
  63. package/lib/commonjs/web/shadowRegistry.js.map +1 -1
  64. package/lib/commonjs/web/types.js +3 -1
  65. package/lib/commonjs/web/types.js.map +1 -1
  66. package/lib/module/core/trackDependencies.js +6 -0
  67. package/lib/module/core/trackDependencies.js.map +1 -0
  68. package/lib/module/core/trackDependencies.metro.js +6 -0
  69. package/lib/module/core/trackDependencies.metro.js.map +1 -0
  70. package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js +4 -0
  71. package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
  72. package/lib/module/core/withUnistyles/withUnistyles.native.js +63 -15
  73. package/lib/module/core/withUnistyles/withUnistyles.native.js.map +1 -1
  74. package/lib/module/hooks/useMedia.js +6 -0
  75. package/lib/module/hooks/useMedia.js.map +1 -1
  76. package/lib/module/mocks.js +4 -2
  77. package/lib/module/mocks.js.map +1 -1
  78. package/lib/module/reanimated/variant/useUpdateVariantColor.js +15 -5
  79. package/lib/module/reanimated/variant/useUpdateVariantColor.js.map +1 -1
  80. package/lib/module/specs/ShadowRegistry/index.js +25 -1
  81. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  82. package/lib/module/specs/StyleSheet/index.js +6 -1
  83. package/lib/module/specs/StyleSheet/index.js.map +1 -1
  84. package/lib/module/types/stylesheet.js.map +1 -1
  85. package/lib/module/web/convert/index.js +2 -2
  86. package/lib/module/web/convert/index.js.map +1 -1
  87. package/lib/module/web/convert/style.js +6 -0
  88. package/lib/module/web/convert/style.js.map +1 -1
  89. package/lib/module/web/create.js +35 -6
  90. package/lib/module/web/create.js.map +1 -1
  91. package/lib/module/web/css/core.js +10 -1
  92. package/lib/module/web/css/core.js.map +1 -1
  93. package/lib/module/web/css/sheet.js +216 -0
  94. package/lib/module/web/css/sheet.js.map +1 -0
  95. package/lib/module/web/css/state.js +98 -78
  96. package/lib/module/web/css/state.js.map +1 -1
  97. package/lib/module/web/css/utils.js +0 -29
  98. package/lib/module/web/css/utils.js.map +1 -1
  99. package/lib/module/web/dependencyTracker.js +67 -0
  100. package/lib/module/web/dependencyTracker.js.map +1 -0
  101. package/lib/module/web/registry.js +73 -4
  102. package/lib/module/web/registry.js.map +1 -1
  103. package/lib/module/web/runtime.js +0 -1
  104. package/lib/module/web/runtime.js.map +1 -1
  105. package/lib/module/web/shadowRegistry.js +83 -11
  106. package/lib/module/web/shadowRegistry.js.map +1 -1
  107. package/lib/module/web/types.js +2 -0
  108. package/lib/module/web/types.js.map +1 -1
  109. package/lib/typescript/src/core/trackDependencies.d.ts +2 -0
  110. package/lib/typescript/src/core/trackDependencies.d.ts.map +1 -0
  111. package/lib/typescript/src/core/trackDependencies.metro.d.ts +2 -0
  112. package/lib/typescript/src/core/trackDependencies.metro.d.ts.map +1 -0
  113. package/lib/typescript/src/core/useProxifiedUnistyles/useProxifiedUnistyles.d.ts.map +1 -1
  114. package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts +2 -1
  115. package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts.map +1 -1
  116. package/lib/typescript/src/hooks/useMedia.d.ts.map +1 -1
  117. package/lib/typescript/src/reanimated/variant/useUpdateVariantColor.d.ts.map +1 -1
  118. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +1 -1
  119. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  120. package/lib/typescript/src/specs/StyleSheet/index.d.ts +3 -1
  121. package/lib/typescript/src/specs/StyleSheet/index.d.ts.map +1 -1
  122. package/lib/typescript/src/types/stylesheet.d.ts +3 -3
  123. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
  124. package/lib/typescript/src/web/convert/style.d.ts.map +1 -1
  125. package/lib/typescript/src/web/create.d.ts.map +1 -1
  126. package/lib/typescript/src/web/css/core.d.ts +4 -1
  127. package/lib/typescript/src/web/css/core.d.ts.map +1 -1
  128. package/lib/typescript/src/web/css/sheet.d.ts +26 -0
  129. package/lib/typescript/src/web/css/sheet.d.ts.map +1 -0
  130. package/lib/typescript/src/web/css/state.d.ts +6 -6
  131. package/lib/typescript/src/web/css/state.d.ts.map +1 -1
  132. package/lib/typescript/src/web/css/utils.d.ts +0 -2
  133. package/lib/typescript/src/web/css/utils.d.ts.map +1 -1
  134. package/lib/typescript/src/web/dependencyTracker.d.ts +9 -0
  135. package/lib/typescript/src/web/dependencyTracker.d.ts.map +1 -0
  136. package/lib/typescript/src/web/registry.d.ts +9 -1
  137. package/lib/typescript/src/web/registry.d.ts.map +1 -1
  138. package/lib/typescript/src/web/runtime.d.ts.map +1 -1
  139. package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -1
  140. package/lib/typescript/src/web/types.d.ts +1 -0
  141. package/lib/typescript/src/web/types.d.ts.map +1 -1
  142. package/metro/index.d.ts +2 -12
  143. package/metro/index.js +57 -33
  144. package/package.json +64 -17
  145. package/plugin/index.d.ts +0 -7
  146. package/plugin/index.js +9 -15
  147. package/src/core/trackDependencies.metro.ts +3 -0
  148. package/src/core/trackDependencies.ts +3 -0
  149. package/src/core/useProxifiedUnistyles/useProxifiedUnistyles.ts +5 -0
  150. package/src/core/withUnistyles/withUnistyles.native.tsx +76 -17
  151. package/src/hooks/useMedia.ts +6 -0
  152. package/src/mocks.ts +5 -2
  153. package/src/reanimated/variant/useUpdateVariantColor.ts +15 -5
  154. package/src/specs/ShadowRegistry/index.ts +29 -2
  155. package/src/specs/StyleSheet/index.ts +4 -2
  156. package/src/types/stylesheet.ts +5 -4
  157. package/src/web/convert/index.ts +2 -2
  158. package/src/web/convert/style.ts +8 -0
  159. package/src/web/create.ts +45 -8
  160. package/src/web/css/core.ts +17 -3
  161. package/src/web/css/sheet.ts +274 -0
  162. package/src/web/css/state.ts +108 -95
  163. package/src/web/css/utils.ts +0 -42
  164. package/src/web/dependencyTracker.ts +71 -0
  165. package/src/web/registry.ts +88 -8
  166. package/src/web/runtime.ts +0 -1
  167. package/src/web/shadowRegistry.ts +101 -16
  168. package/src/web/types.ts +3 -0
  169. package/metro/babel-transformer.js +0 -1102
package/README.md CHANGED
@@ -14,9 +14,9 @@
14
14
 
15
15
  ## Requirements
16
16
 
17
- - React Native 0.81+ with the New Architecture
17
+ - React Native 0.85+ with the New Architecture
18
18
  - React 19+
19
- - Expo SDK 54+ (if you use Expo, development builds only, Expo Go is not supported)
19
+ - Expo SDK 56+ (if you use Expo, development builds only, Expo Go is not supported)
20
20
 
21
21
  Need support for older React Native versions or the Old Architecture? Use [Unistyles 2.0](https://v2.unistyl.es).
22
22
 
@@ -12,7 +12,6 @@ import android.view.Window
12
12
  import android.view.WindowManager
13
13
  import androidx.annotation.Keep
14
14
  import androidx.core.content.OnConfigurationChangedProvider
15
- import androidx.core.text.TextUtilsCompat
16
15
  import androidx.core.util.Consumer
17
16
  import androidx.core.view.ViewCompat
18
17
  import androidx.core.view.WindowCompat
@@ -21,6 +20,7 @@ import androidx.core.view.WindowInsetsControllerCompat
21
20
  import com.facebook.proguard.annotations.DoNotStrip
22
21
  import com.facebook.react.bridge.LifecycleEventListener
23
22
  import com.facebook.react.bridge.ReactApplicationContext
23
+ import com.facebook.react.modules.i18nmanager.I18nUtil
24
24
  import com.margelo.nitro.unistyles.ColorScheme
25
25
  import com.margelo.nitro.unistyles.Dimensions
26
26
  import com.margelo.nitro.unistyles.HybridNativePlatformSpec
@@ -29,7 +29,6 @@ import com.margelo.nitro.unistyles.Orientation
29
29
  import com.margelo.nitro.unistyles.UnistyleDependency
30
30
  import com.margelo.nitro.unistyles.UnistylesNativeMiniRuntime
31
31
  import java.lang.ref.WeakReference
32
- import java.util.Locale
33
32
 
34
33
  @Keep
35
34
  @DoNotStrip
@@ -207,19 +206,7 @@ class NativePlatformAndroid(private val reactContext: ReactApplicationContext):
207
206
  }
208
207
 
209
208
  override fun getPrefersRtlDirection(): Boolean {
210
- // forced by React Native
211
- val sharedPrefs = reactContext.getSharedPreferences(
212
- "com.facebook.react.modules.i18nmanager.I18nUtil",
213
- Context.MODE_PRIVATE
214
- )
215
- val resourcesLocale = configurationResources.configuration.locales[0]
216
- val hasForcedRtl = sharedPrefs.getBoolean("RCTI18nUtil_forceRTL", false)
217
- // user preferences
218
- val isRtl = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL
219
- val isResourcesRtl = TextUtilsCompat.getLayoutDirectionFromLocale(resourcesLocale) == ViewCompat.LAYOUT_DIRECTION_RTL
220
-
221
-
222
- return hasForcedRtl || isRtl || isResourcesRtl
209
+ return I18nUtil.instance.isRTL(reactContext.currentActivity ?: reactContext)
223
210
  }
224
211
 
225
212
  override fun setRootViewBackgroundColor(color: Double) {
@@ -37,7 +37,7 @@ class NativePlatformListener(
37
37
  }
38
38
 
39
39
  private val rtlPreferenceListener = SharedPreferences.OnSharedPreferenceChangeListener { prefs, key ->
40
- if (key == "RCTI18nUtil_forceRTL") {
40
+ if (key == "RCTI18nUtil_forceRTL" || key == "RCTI18nUtil_allowRTL") {
41
41
  notifyConfigChangedWithDelay()
42
42
  }
43
43
  }
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Without the Babel plugin (react-native-unistyles/metro), `useVariants` doesn't change the StyleSheet,
3
+ * it returns the styles with the selected variants:
4
+ *
5
+ * styles.useVariants({ size }) -> const variantStyles = styles.useVariants({ size })
6
+ *
7
+ * Every `styles` read below the call, in the same block (also in callbacks and JSX), reads `variantStyles` instead,
8
+ * up to the next call in that block or in a nested one.
9
+ * like the Babel plugin scoped it. Reads above the call and types (`typeof styles`) keep the StyleSheet.
10
+ *
11
+ * npx jscodeshift --parser=tsx --extensions=tsx,ts,jsx,js -t node_modules/react-native-unistyles/codemods/use-variants.js src
12
+ */
13
+
14
+ const isUseVariantsStatement = (node) =>
15
+ node.type === 'ExpressionStatement' &&
16
+ node.expression.type === 'CallExpression' &&
17
+ node.expression.arguments.length === 1 &&
18
+ node.expression.callee.type === 'MemberExpression' &&
19
+ !node.expression.callee.computed &&
20
+ node.expression.callee.object.type === 'Identifier' &&
21
+ node.expression.callee.property.type === 'Identifier' &&
22
+ node.expression.callee.property.name === 'useVariants'
23
+
24
+ const isAfter = (node, statement) =>
25
+ node.loc.start.line > statement.loc.end.line ||
26
+ (node.loc.start.line === statement.loc.end.line && node.loc.start.column >= statement.loc.end.column)
27
+
28
+ const contains = (block, node) =>
29
+ (block.loc.start.line < node.loc.start.line ||
30
+ (block.loc.start.line === node.loc.start.line && block.loc.start.column <= node.loc.start.column)) &&
31
+ (node.loc.end.line < block.loc.end.line ||
32
+ (node.loc.end.line === block.loc.end.line && node.loc.end.column <= block.loc.end.column))
33
+
34
+ // identifiers that don't read the variable, eg. `obj.styles`, `{ styles: 1 }` or `typeof styles`
35
+ const isReference = (path) => {
36
+ const parent = path.parent.node
37
+ const node = path.node
38
+
39
+ switch (parent.type) {
40
+ case 'MemberExpression':
41
+ case 'OptionalMemberExpression':
42
+ return parent.object === node || parent.computed
43
+ case 'ObjectProperty':
44
+ case 'Property':
45
+ return parent.value === node || (parent.computed && parent.key === node)
46
+ case 'ClassProperty':
47
+ case 'ClassMethod':
48
+ case 'ObjectMethod':
49
+ case 'MethodDefinition':
50
+ return parent.computed && parent.key === node
51
+ case 'TSTypeQuery':
52
+ case 'TSQualifiedName':
53
+ case 'TSPropertySignature':
54
+ case 'LabeledStatement':
55
+ case 'BreakStatement':
56
+ case 'ContinueStatement':
57
+ case 'ImportSpecifier':
58
+ case 'ImportDefaultSpecifier':
59
+ case 'ExportSpecifier':
60
+ return false
61
+ default:
62
+ return true
63
+ }
64
+ }
65
+
66
+ module.exports = function transformer(file, api) {
67
+ const j = api.jscodeshift
68
+ const root = j(file.source)
69
+ const usedNames = new Set(
70
+ root
71
+ .find(j.Identifier)
72
+ .nodes()
73
+ .map((node) => node.name),
74
+ )
75
+ const getName = (styleSheetName) => {
76
+ const base = styleSheetName === 'styles' ? 'variantStyles' : `${styleSheetName}WithVariants`
77
+ let name = base
78
+ let index = 2
79
+
80
+ while (usedNames.has(name)) {
81
+ name = `${base}${index++}`
82
+ }
83
+
84
+ usedNames.add(name)
85
+
86
+ return name
87
+ }
88
+
89
+ // every call starts a scope that ends with its block, a later call (also in a nested block) starts a new one
90
+ const calls = root
91
+ .find(j.ExpressionStatement)
92
+ .filter((path) => isUseVariantsStatement(path.node) && path.parent.node.type === 'BlockStatement')
93
+ .paths()
94
+ .map((statementPath) => {
95
+ const styleSheet = statementPath.node.expression.callee.object
96
+
97
+ return {
98
+ statementPath,
99
+ block: statementPath.parent.node,
100
+ styleSheetName: styleSheet.name,
101
+ scope: statementPath.scope.lookup(styleSheet.name),
102
+ name: getName(styleSheet.name),
103
+ }
104
+ })
105
+ // useVariants is always called on the StyleSheet, like the Babel plugin does
106
+ const callees = new Set(calls.map((call) => call.statementPath.node.expression.callee.object))
107
+
108
+ // resolved before anything is replaced, replaced nodes have no location
109
+ const replacements = root
110
+ .find(j.Identifier)
111
+ .filter((path) => !callees.has(path.node) && path.node.loc && isReference(path))
112
+ .paths()
113
+ .map((path) => {
114
+ // the closest call above the read, of the same StyleSheet, in a block around it
115
+ const call = calls
116
+ .filter(
117
+ (candidate) =>
118
+ candidate.styleSheetName === path.node.name &&
119
+ contains(candidate.block, path.node) &&
120
+ isAfter(path.node, candidate.statementPath.node) &&
121
+ path.scope.lookup(path.node.name) === candidate.scope,
122
+ )
123
+ .at(-1)
124
+
125
+ return { path, call }
126
+ })
127
+ .filter(({ call }) => call !== undefined)
128
+
129
+ replacements.forEach(({ path, call }) => {
130
+ const parent = path.parent.node
131
+
132
+ // { styles } -> { styles: variantStyles }
133
+ if ((parent.type === 'ObjectProperty' || parent.type === 'Property') && parent.shorthand) {
134
+ parent.shorthand = false
135
+ parent.value = j.identifier(call.name)
136
+
137
+ return
138
+ }
139
+
140
+ path.replace(j.identifier(call.name))
141
+ })
142
+
143
+ calls.forEach(({ statementPath, name }) => {
144
+ const declaration = j.variableDeclaration('const', [
145
+ j.variableDeclarator(j.identifier(name), statementPath.node.expression),
146
+ ])
147
+
148
+ declaration.comments = statementPath.node.comments
149
+ statementPath.replace(declaration)
150
+ })
151
+
152
+ return calls.length > 0 ? root.toSource() : file.source
153
+ }
154
+
155
+ module.exports.parser = 'tsx'
@@ -2,5 +2,5 @@
2
2
  "main": "../../../lib/commonjs/components/native/TouchableHighlight.js",
3
3
  "module": "../../../lib/module/components/native/TouchableHighlight.js",
4
4
  "browser": "../../../lib/module/components/native/TouchableHighlight.js",
5
- "react-native": "../../../src/components/native/TouchableHighlight.tsx"
5
+ "react-native": "../../../src/components/native/TouchableHighlight.native.tsx"
6
6
  }
@@ -28,7 +28,8 @@ namespace {
28
28
  // must not reach it. Returns null (default origin) for an invalid value.
29
29
  folly::dynamic processTransformOriginString(const std::string &value) {
30
30
  static const std::regex tokenRegex(
31
- "(top|bottom|left|right|center|\\d+(?:%|px)|0)", std::regex::icase);
31
+ "(top|bottom|left|right|center|-?\\d*\\.?\\d+(?:%|px)|0)",
32
+ std::regex::icase);
32
33
  constexpr size_t INDEX_X = 0;
33
34
  constexpr size_t INDEX_Y = 1;
34
35
  constexpr size_t INDEX_Z = 2;
@@ -99,7 +100,7 @@ folly::dynamic processTransformOriginString(const std::string &value) {
99
100
 
100
101
  result[index] = token;
101
102
  } else {
102
- // 0 or <digits>px
103
+ // 0 or <number>px
103
104
  result[index] = std::strtod(token.c_str(), nullptr);
104
105
  }
105
106
 
@@ -0,0 +1,162 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include <algorithm>
5
+ #include <optional>
6
+ #include <unordered_map>
7
+ #include <vector>
8
+ #include "NativePlatform.h"
9
+
10
+ namespace margelo::nitro::unistyles::core {
11
+
12
+ using namespace facebook;
13
+
14
+ // Without the Babel plugin (react-native-unistyles/metro) StyleSheets don't carry `uni__dependencies`.
15
+ // Instead, the mini runtime records what a StyleSheet or a dynamic function reads while it runs.
16
+ struct DependencyTracker {
17
+ using Dependencies = std::vector<UnistyleDependency>;
18
+
19
+ // every read of the mini runtime lands in the innermost scope, reads outside of any scope are ignored
20
+ struct Scope {
21
+ Scope() {
22
+ scopes().push_back(&this->_dependencies);
23
+ }
24
+
25
+ ~Scope() {
26
+ scopes().pop_back();
27
+ }
28
+
29
+ Scope(const Scope&) = delete;
30
+ Scope(Scope&&) = delete;
31
+
32
+ const Dependencies& getDependencies() const {
33
+ return this->_dependencies;
34
+ }
35
+
36
+ private:
37
+ Dependencies _dependencies{};
38
+ };
39
+
40
+ static void record(UnistyleDependency dependency) {
41
+ auto& activeScopes = scopes();
42
+
43
+ if (activeScopes.empty()) {
44
+ return;
45
+ }
46
+
47
+ auto* dependencies = activeScopes.back();
48
+
49
+ if (std::find(dependencies->begin(), dependencies->end(), dependency) == dependencies->end()) {
50
+ dependencies->push_back(dependency);
51
+ }
52
+ }
53
+
54
+ private:
55
+ // StyleSheets run only on the JS thread
56
+ static std::vector<Dependencies*>& scopes() {
57
+ thread_local std::vector<Dependencies*> activeScopes{};
58
+
59
+ return activeScopes;
60
+ }
61
+ };
62
+
63
+ // mini runtime passed to StyleSheets, records the dependency of every property that is read
64
+ // eg. rt.screen.width -> Dimensions, rt.insets.top -> Insets, rt.insets.ime -> Ime
65
+ class TrackedMiniRuntime: public jsi::HostObject {
66
+ public:
67
+ using DependencyMap = std::unordered_map<std::string, UnistyleDependency>;
68
+
69
+ TrackedMiniRuntime(jsi::Object target, const DependencyMap& dependencies, std::optional<UnistyleDependency> fallbackDependency)
70
+ : _target{std::move(target)}, _dependencies{dependencies}, _fallbackDependency{fallbackDependency} {}
71
+
72
+ static jsi::Value create(jsi::Runtime& rt, jsi::Value&& miniRuntime) {
73
+ static const DependencyMap dependencies = {
74
+ {"themeName", UnistyleDependency::THEMENAME},
75
+ {"breakpoint", UnistyleDependency::BREAKPOINTS},
76
+ {"hasAdaptiveThemes", UnistyleDependency::ADAPTIVETHEMES},
77
+ {"colorScheme", UnistyleDependency::COLORSCHEME},
78
+ {"screen", UnistyleDependency::DIMENSIONS},
79
+ {"contentSizeCategory", UnistyleDependency::CONTENTSIZECATEGORY},
80
+ {"pixelRatio", UnistyleDependency::PIXELRATIO},
81
+ {"fontScale", UnistyleDependency::FONTSCALE},
82
+ {"rtl", UnistyleDependency::RTL},
83
+ {"statusBar", UnistyleDependency::STATUSBAR},
84
+ {"navigationBar", UnistyleDependency::NAVIGATIONBAR},
85
+ {"isPortrait", UnistyleDependency::ORIENTATION},
86
+ {"isLandscape", UnistyleDependency::ORIENTATION}
87
+ // insets are tracked by their own object, ime is a separate dependency
88
+ };
89
+
90
+ return wrap(rt, miniRuntime.asObject(rt), dependencies, std::nullopt);
91
+ }
92
+
93
+ jsi::Value get(jsi::Runtime& rt, const jsi::PropNameID& propNameId) override {
94
+ auto propertyName = propNameId.utf8(rt);
95
+ auto it = this->_dependencies.find(propertyName);
96
+
97
+ if (it != this->_dependencies.end()) {
98
+ DependencyTracker::record(it->second);
99
+ } else if (this->_fallbackDependency.has_value() && this->_target.hasProperty(rt, propNameId)) {
100
+ DependencyTracker::record(this->_fallbackDependency.value());
101
+ }
102
+
103
+ if (propertyName == "insets") {
104
+ return this->getInsets(rt);
105
+ }
106
+
107
+ return this->_target.getProperty(rt, propNameId);
108
+ }
109
+
110
+ void set(jsi::Runtime& rt, const jsi::PropNameID& propNameId, const jsi::Value& value) override {
111
+ this->_target.setProperty(rt, propNameId, value);
112
+ }
113
+
114
+ std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& rt) override {
115
+ auto propertyNames = this->_target.getPropertyNames(rt);
116
+ size_t length = propertyNames.size(rt);
117
+ std::vector<jsi::PropNameID> result{};
118
+
119
+ result.reserve(length);
120
+
121
+ for (size_t i = 0; i < length; i++) {
122
+ result.emplace_back(jsi::PropNameID::forString(rt, propertyNames.getValueAtIndex(rt, i).asString(rt)));
123
+ }
124
+
125
+ return result;
126
+ }
127
+
128
+ private:
129
+ jsi::Object _target;
130
+ DependencyMap _dependencies;
131
+ std::optional<UnistyleDependency> _fallbackDependency;
132
+ std::optional<jsi::Value> _insets = std::nullopt;
133
+
134
+ static jsi::Value wrap(jsi::Runtime& rt, jsi::Object&& target, const DependencyMap& dependencies, std::optional<UnistyleDependency> fallbackDependency) {
135
+ auto trackedObject = std::make_shared<TrackedMiniRuntime>(std::move(target), dependencies, fallbackDependency);
136
+
137
+ return jsi::Object::createFromHostObject(rt, std::move(trackedObject));
138
+ }
139
+
140
+ // the same object for every read, so rt.insets === rt.insets
141
+ jsi::Value getInsets(jsi::Runtime& rt) {
142
+ if (this->_insets.has_value()) {
143
+ return jsi::Value(rt, this->_insets.value());
144
+ }
145
+
146
+ auto insets = this->_target.getProperty(rt, "insets");
147
+
148
+ if (!insets.isObject()) {
149
+ return insets;
150
+ }
151
+
152
+ static const DependencyMap insetsDependencies = {
153
+ {"ime", UnistyleDependency::IME}
154
+ };
155
+
156
+ this->_insets = wrap(rt, insets.asObject(rt), insetsDependencies, UnistyleDependency::INSETS);
157
+
158
+ return jsi::Value(rt, this->_insets.value());
159
+ }
160
+ };
161
+
162
+ }
@@ -25,6 +25,20 @@ struct StyleSheet {
25
25
  StyleSheetType type;
26
26
  jsi::Object rawValue;
27
27
  std::unordered_map<std::string, Unistyle::Shared> unistyles{};
28
+ // without the Babel plugin, every dependency read by the StyleSheet function, shared by all of its unistyles
29
+ std::vector<UnistyleDependency> runtimeDependencies{};
30
+
31
+ inline void addRuntimeDependencies(const std::vector<UnistyleDependency>& dependencies) {
32
+ for (const auto& dependency : dependencies) {
33
+ if (std::find(this->runtimeDependencies.begin(), this->runtimeDependencies.end(), dependency) == this->runtimeDependencies.end()) {
34
+ this->runtimeDependencies.push_back(dependency);
35
+ }
36
+
37
+ for (const auto& [_, unistyle] : this->unistyles) {
38
+ unistyle->addRuntimeDependency(dependency);
39
+ }
40
+ }
41
+ }
28
42
  };
29
43
 
30
44
  }
@@ -53,11 +53,14 @@ struct Unistyle {
53
53
 
54
54
  inline void addBreakpointDependency() {
55
55
  // this dependency can skip sealed check, as useVariants hook is called during React component render
56
- // also this is the only dependency that is not staticly deducted from Babel plugin
57
- auto it = std::find(this->dependencies.begin(), this->dependencies.end(), UnistyleDependency::BREAKPOINTS);
56
+ this->addRuntimeDependency(UnistyleDependency::BREAKPOINTS);
57
+ }
58
+
59
+ inline void addRuntimeDependency(UnistyleDependency dependency) {
60
+ auto it = std::find(this->dependencies.begin(), this->dependencies.end(), dependency);
58
61
 
59
62
  if (it == this->dependencies.end()) {
60
- this->dependencies.push_back(UnistyleDependency::BREAKPOINTS);
63
+ this->dependencies.push_back(dependency);
61
64
  }
62
65
  }
63
66
 
@@ -269,6 +269,10 @@ void core::UnistylesRegistry::queueShadowLeafUpdatesUnsafe(shadow::ShadowLeafUpd
269
269
  continue;
270
270
  }
271
271
 
272
+ if (this->shouldTrackDependencies && !this->trafficController.hasUpdate(family) && shadow::hasCommittedNativeProps(family, props)) {
273
+ continue;
274
+ }
275
+
272
276
  this->trafficController.setUpdate(it->second.family, std::move(props));
273
277
  }
274
278
  }
@@ -435,8 +439,18 @@ size_t core::UnistylesRegistry::getPendingUpdatesCount() {
435
439
  });
436
440
  }
437
441
 
438
- void core::UnistylesRegistry::setCommittedInstanceHandles(std::vector<InstanceHandle::Shared>&& instanceHandles) {
439
- this->_committedInstanceHandles = std::move(instanceHandles);
442
+ void core::UnistylesRegistry::addCommittedInstanceHandles(std::vector<InstanceHandle::Shared>&& instanceHandles) {
443
+ if (this->_committedInstanceHandles.empty()) {
444
+ this->_committedInstanceHandles = std::move(instanceHandles);
445
+
446
+ return;
447
+ }
448
+
449
+ this->_committedInstanceHandles.insert(
450
+ this->_committedInstanceHandles.end(),
451
+ std::make_move_iterator(instanceHandles.begin()),
452
+ std::make_move_iterator(instanceHandles.end())
453
+ );
440
454
  }
441
455
 
442
456
  std::vector<InstanceHandle::Shared> core::UnistylesRegistry::takeCommittedInstanceHandles() {
@@ -44,6 +44,7 @@ struct UnistylesRegistry: public StyleSheetRegistry {
44
44
  UnistylesRegistry(const UnistylesRegistry&&) = delete;
45
45
 
46
46
  bool shouldUsePointsForBreakpoints = false;
47
+ bool shouldTrackDependencies = false;
47
48
 
48
49
  void registerTheme(jsi::Runtime& rt, uint64_t generation, std::string name, jsi::Value& theme);
49
50
  void registerBreakpoints(std::vector<std::pair<std::string, double>>& sortedBreakpoints);
@@ -70,7 +71,7 @@ struct UnistylesRegistry: public StyleSheetRegistry {
70
71
  core::Unistyle::Shared getUnistyleById(std::string unistyleID);
71
72
  std::vector<LinkedFamilySnapshot> getLinkedFamiliesSnapshot();
72
73
  size_t getPendingUpdatesCount();
73
- void setCommittedInstanceHandles(std::vector<InstanceHandle::Shared>&& instanceHandles);
74
+ void addCommittedInstanceHandles(std::vector<InstanceHandle::Shared>&& instanceHandles);
74
75
  std::vector<InstanceHandle::Shared> takeCommittedInstanceHandles();
75
76
 
76
77
  // newest installed runtime owns the registry, generation 0 means no owner
@@ -288,9 +288,11 @@ jsi::Value HybridShadowRegistry::takeCommittedInstanceHandles(jsi::Runtime &rt,
288
288
  }
289
289
 
290
290
  jsi::Value HybridShadowRegistry::refreshReactNodes(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
291
- helpers::assertThat(rt, count == 1 && args[0].isObject(), "Unistyles: refreshReactNodes expected to be called with an array of shadow nodes.");
291
+ helpers::assertThat(rt, count >= 1 && args[0].isObject(), "Unistyles: refreshReactNodes expected to be called with an array of shadow nodes.");
292
292
 
293
293
  auto nodes = args[0].asObject(rt).asArray(rt);
294
+ const bool includeUnpromoted = count > 1 && args[1].isBool() && args[1].getBool();
295
+ std::vector<size_t> unpromotedIndices;
294
296
 
295
297
  UNISTYLES_TRACE_SECTION("refreshReactNodes", "nodes", static_cast<int64_t>(nodes.size(rt)));
296
298
 
@@ -309,9 +311,12 @@ jsi::Value HybridShadowRegistry::refreshReactNodes(jsi::Runtime &rt, const jsi::
309
311
 
310
312
  auto wrapper = node.asObject(rt).getNativeState<ShadowNodeWrapper>(rt);
311
313
 
312
- // never committed, React cloned it in a render that isn't committed yet (eg. a transition that yielded)
313
- // it holds what React rendered since, pointing it at the committed node would drop that
314
- if (!wrapper->shadowNode->getHasBeenPromoted()) {
314
+ // never mounted: React cloned it in a render that isn't committed yet (eg. a transition that yielded),
315
+ // pointing it at the committed node would drop what React rendered since. A commit hook (eg. Reanimated)
316
+ // can also replace the nodes React committed before they're mounted, only the caller can tell them apart
317
+ if (!includeUnpromoted && !wrapper->shadowNode->getHasBeenPromoted()) {
318
+ unpromotedIndices.push_back(i);
319
+
315
320
  continue;
316
321
  }
317
322
 
@@ -334,7 +339,13 @@ jsi::Value HybridShadowRegistry::refreshReactNodes(jsi::Runtime &rt, const jsi::
334
339
  wrapper->shadowNode = parent.get().getChildren().at(index);
335
340
  }
336
341
 
337
- return jsi::Value::undefined();
342
+ auto result = jsi::Array(rt, unpromotedIndices.size());
343
+
344
+ for (size_t i = 0; i < unpromotedIndices.size(); i++) {
345
+ result.setValueAtIndex(rt, i, static_cast<double>(unpromotedIndices[i]));
346
+ }
347
+
348
+ return result;
338
349
  }
339
350
 
340
351
  std::shared_ptr<const core::ShadowNode> HybridShadowRegistry::getShadowNodeFromRef(jsi::Runtime& rt, const jsi::Value& maybeRef) {
@@ -65,7 +65,7 @@ struct HybridShadowRegistry: public HybridUnistylesShadowRegistrySpec {
65
65
  prototype.registerRawHybridMethod("getScopedTheme", 0, &HybridShadowRegistry::getScopedTheme);
66
66
  prototype.registerRawHybridMethod("verify", 0, &HybridShadowRegistry::verify);
67
67
  prototype.registerRawHybridMethod("takeCommittedInstanceHandles", 0, &HybridShadowRegistry::takeCommittedInstanceHandles);
68
- prototype.registerRawHybridMethod("refreshReactNodes", 1, &HybridShadowRegistry::refreshReactNodes);
68
+ prototype.registerRawHybridMethod("refreshReactNodes", 2, &HybridShadowRegistry::refreshReactNodes);
69
69
  });
70
70
  };
71
71
 
@@ -83,6 +83,13 @@ jsi::Value HybridStyleSheet::init(jsi::Runtime &rt, const jsi::Value &thisVal, c
83
83
  auto& registry = core::UnistylesRegistry::get();
84
84
 
85
85
  registry.createState();
86
+ registry.shouldTrackDependencies = false;
87
+
88
+ if (count > 0 && arguments[0].isObject()) {
89
+ auto trackDependencies = arguments[0].asObject(rt).getProperty(rt, "trackDependencies");
90
+
91
+ registry.shouldTrackDependencies = trackDependencies.isBool() && trackDependencies.getBool();
92
+ }
86
93
 
87
94
  loadExternalMethods(thisVal, rt);
88
95
 
@@ -377,7 +384,7 @@ void HybridStyleSheet::applyDependencyChanges(jsi::Runtime& rt, std::vector<Unis
377
384
  }
378
385
 
379
386
  // after the commit, so listeners can point React at the committed nodes
380
- registry.setCommittedInstanceHandles(std::move(committedInstanceHandles));
387
+ registry.addCommittedInstanceHandles(std::move(committedInstanceHandles));
381
388
 
382
389
  UNISTYLES_TRACE_SECTION("notifyListeners");
383
390
 
@@ -44,7 +44,7 @@ std::optional<std::string> HybridUnistylesRuntime::getBreakpoint() {
44
44
  };
45
45
 
46
46
  bool HybridUnistylesRuntime::getRtl() {
47
- return this->_nativePlatform->getPrefersRtlDirection();
47
+ return this->_nativePlatform->getMiniRuntime().rtl;
48
48
  }
49
49
 
50
50
  Insets HybridUnistylesRuntime::getInsets() {