react-native-unistyles 3.0.0-alpha.1 → 3.0.0-alpha.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. package/Unistyles.podspec +33 -0
  2. package/cxx/common/Constants.h +1 -0
  3. package/cxx/common/Helpers.h +92 -0
  4. package/cxx/core/HostStyle.cpp +18 -12
  5. package/cxx/core/HostStyle.h +3 -0
  6. package/cxx/core/StyleSheet.h +0 -1
  7. package/cxx/core/StyleSheetRegistry.cpp +10 -13
  8. package/cxx/core/StyleSheetRegistry.h +4 -4
  9. package/cxx/core/Unistyle.h +7 -10
  10. package/cxx/core/UnistyleData.h +23 -0
  11. package/cxx/core/UnistyleWrapper.h +25 -7
  12. package/cxx/core/UnistylesCommitHook.cpp +8 -7
  13. package/cxx/core/UnistylesCommitHook.h +6 -1
  14. package/cxx/core/UnistylesCommitShadowNode.h +13 -0
  15. package/cxx/core/UnistylesMountHook.cpp +40 -0
  16. package/cxx/core/UnistylesMountHook.h +30 -0
  17. package/cxx/core/UnistylesRegistry.cpp +44 -75
  18. package/cxx/core/UnistylesRegistry.h +10 -11
  19. package/cxx/core/UnistylesState.cpp +4 -4
  20. package/cxx/hybridObjects/HybridShadowRegistry.cpp +13 -6
  21. package/cxx/hybridObjects/HybridStyleSheet.cpp +35 -39
  22. package/cxx/hybridObjects/HybridStyleSheet.h +9 -6
  23. package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +34 -15
  24. package/cxx/hybridObjects/HybridUnistylesRuntime.h +2 -0
  25. package/cxx/parser/Parser.cpp +87 -165
  26. package/cxx/parser/Parser.h +8 -14
  27. package/cxx/shadowTree/ShadowLeafUpdate.h +1 -1
  28. package/cxx/shadowTree/ShadowTreeManager.cpp +3 -8
  29. package/ios/UnistylesModuleOnLoad.h +2 -0
  30. package/ios/UnistylesModuleOnLoad.mm +8 -1
  31. package/lib/commonjs/specs/NavigtionBar/index.js +1 -1
  32. package/lib/commonjs/specs/NavigtionBar/index.js.map +1 -1
  33. package/lib/commonjs/specs/ShadowRegistry/index.js +3 -3
  34. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  35. package/lib/commonjs/specs/StatusBar/index.js +1 -1
  36. package/lib/commonjs/specs/StatusBar/index.js.map +1 -1
  37. package/lib/commonjs/specs/StyleSheet/index.js.map +1 -1
  38. package/lib/commonjs/specs/UnistylesRuntime/index.js +1 -1
  39. package/lib/commonjs/specs/UnistylesRuntime/index.js.map +1 -1
  40. package/lib/commonjs/specs/index.web.js +1 -1
  41. package/lib/commonjs/web/convert/boxShadow.js +77 -0
  42. package/lib/commonjs/web/convert/boxShadow.js.map +1 -0
  43. package/lib/commonjs/web/convert/breakpoint.js +25 -0
  44. package/lib/commonjs/web/convert/breakpoint.js.map +1 -0
  45. package/lib/commonjs/web/convert/index.js +76 -0
  46. package/lib/commonjs/web/convert/index.js.map +1 -0
  47. package/lib/commonjs/web/convert/module.d.js +2 -0
  48. package/lib/commonjs/web/convert/module.d.js.map +1 -0
  49. package/lib/commonjs/web/convert/shadow.js +68 -0
  50. package/lib/commonjs/web/convert/shadow.js.map +1 -0
  51. package/lib/commonjs/web/convert/style.js +89 -0
  52. package/lib/commonjs/web/convert/style.js.map +1 -0
  53. package/lib/commonjs/web/convert/textShadow.js +73 -0
  54. package/lib/commonjs/web/convert/textShadow.js.map +1 -0
  55. package/lib/commonjs/web/convert/transform.js +72 -0
  56. package/lib/commonjs/web/convert/transform.js.map +1 -0
  57. package/lib/commonjs/web/convert/types.js +9 -0
  58. package/lib/commonjs/web/convert/types.js.map +1 -0
  59. package/lib/commonjs/web/convert/utils.js +55 -0
  60. package/lib/commonjs/web/convert/utils.js.map +1 -0
  61. package/lib/commonjs/web/create.js +89 -0
  62. package/lib/commonjs/web/create.js.map +1 -0
  63. package/lib/commonjs/web/index.js +51 -0
  64. package/lib/commonjs/web/index.js.map +1 -0
  65. package/lib/commonjs/web/listener/index.js +13 -0
  66. package/lib/commonjs/web/listener/index.js.map +1 -0
  67. package/lib/commonjs/web/listener/listener.js +36 -0
  68. package/lib/commonjs/web/listener/listener.js.map +1 -0
  69. package/lib/commonjs/web/mock.js +37 -0
  70. package/lib/commonjs/web/mock.js.map +1 -0
  71. package/lib/commonjs/web/mq.js +23 -0
  72. package/lib/commonjs/web/mq.js.map +1 -0
  73. package/lib/commonjs/web/pseudo.js +11 -0
  74. package/lib/commonjs/web/pseudo.js.map +1 -0
  75. package/lib/commonjs/web/registry.js +37 -0
  76. package/lib/commonjs/web/registry.js.map +1 -0
  77. package/lib/commonjs/web/runtime.js +164 -0
  78. package/lib/commonjs/web/runtime.js.map +1 -0
  79. package/lib/commonjs/web/state.js +121 -0
  80. package/lib/commonjs/web/state.js.map +1 -0
  81. package/lib/commonjs/web/utils.js +78 -0
  82. package/lib/commonjs/web/utils.js.map +1 -0
  83. package/lib/commonjs/web/variants/getVariants.js +39 -0
  84. package/lib/commonjs/web/variants/getVariants.js.map +1 -0
  85. package/lib/commonjs/web/variants/index.js +28 -0
  86. package/lib/commonjs/web/variants/index.js.map +1 -0
  87. package/lib/commonjs/web/variants/useVariants.js +75 -0
  88. package/lib/commonjs/web/variants/useVariants.js.map +1 -0
  89. package/lib/module/specs/NavigtionBar/index.js +1 -1
  90. package/lib/module/specs/NavigtionBar/index.js.map +1 -1
  91. package/lib/module/specs/ShadowRegistry/index.js +3 -3
  92. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  93. package/lib/module/specs/StatusBar/index.js +1 -1
  94. package/lib/module/specs/StatusBar/index.js.map +1 -1
  95. package/lib/module/specs/StyleSheet/index.js.map +1 -1
  96. package/lib/module/specs/UnistylesRuntime/index.js +1 -1
  97. package/lib/module/specs/UnistylesRuntime/index.js.map +1 -1
  98. package/lib/module/specs/index.web.js +1 -1
  99. package/lib/module/specs/index.web.js.map +1 -1
  100. package/lib/module/web/convert/boxShadow.js +72 -0
  101. package/lib/module/web/convert/boxShadow.js.map +1 -0
  102. package/lib/module/web/convert/breakpoint.js +20 -0
  103. package/lib/module/web/convert/breakpoint.js.map +1 -0
  104. package/lib/module/web/convert/index.js +71 -0
  105. package/lib/module/web/convert/index.js.map +1 -0
  106. package/lib/module/web/convert/module.d.js +2 -0
  107. package/lib/module/web/convert/module.d.js.map +1 -0
  108. package/lib/module/web/convert/shadow.js +63 -0
  109. package/lib/module/web/convert/shadow.js.map +1 -0
  110. package/lib/module/web/convert/style.js +84 -0
  111. package/lib/module/web/convert/style.js.map +1 -0
  112. package/lib/module/web/convert/textShadow.js +68 -0
  113. package/lib/module/web/convert/textShadow.js.map +1 -0
  114. package/lib/module/web/convert/transform.js +67 -0
  115. package/lib/module/web/convert/transform.js.map +1 -0
  116. package/lib/module/web/convert/types.js +5 -0
  117. package/lib/module/web/convert/types.js.map +1 -0
  118. package/lib/module/web/convert/utils.js +43 -0
  119. package/lib/module/web/convert/utils.js.map +1 -0
  120. package/lib/module/web/create.js +84 -0
  121. package/lib/module/web/create.js.map +1 -0
  122. package/lib/module/web/index.js +24 -0
  123. package/lib/module/web/index.js.map +1 -0
  124. package/lib/module/web/listener/index.js +4 -0
  125. package/lib/module/web/listener/index.js.map +1 -0
  126. package/lib/module/web/listener/listener.js +31 -0
  127. package/lib/module/web/listener/listener.js.map +1 -0
  128. package/lib/module/web/mock.js +33 -0
  129. package/lib/module/web/mock.js.map +1 -0
  130. package/lib/module/web/mq.js +17 -0
  131. package/lib/module/web/mq.js.map +1 -0
  132. package/lib/module/web/pseudo.js +6 -0
  133. package/lib/module/web/pseudo.js.map +1 -0
  134. package/lib/module/web/registry.js +33 -0
  135. package/lib/module/web/registry.js.map +1 -0
  136. package/lib/module/web/runtime.js +160 -0
  137. package/lib/module/web/runtime.js.map +1 -0
  138. package/lib/module/web/state.js +117 -0
  139. package/lib/module/web/state.js.map +1 -0
  140. package/lib/module/web/utils.js +65 -0
  141. package/lib/module/web/utils.js.map +1 -0
  142. package/lib/module/web/variants/getVariants.js +34 -0
  143. package/lib/module/web/variants/getVariants.js.map +1 -0
  144. package/lib/module/web/variants/index.js +5 -0
  145. package/lib/module/web/variants/index.js.map +1 -0
  146. package/lib/module/web/variants/useVariants.js +70 -0
  147. package/lib/module/web/variants/useVariants.js.map +1 -0
  148. package/lib/typescript/example/App.d.ts.map +1 -1
  149. package/lib/typescript/example/Typography.d.ts +12 -0
  150. package/lib/typescript/example/Typography.d.ts.map +1 -0
  151. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +2 -2
  152. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  153. package/lib/typescript/src/specs/StyleSheet/index.d.ts +2 -1
  154. package/lib/typescript/src/specs/StyleSheet/index.d.ts.map +1 -1
  155. package/lib/typescript/src/specs/index.web.d.ts +1 -1
  156. package/lib/typescript/src/specs/index.web.d.ts.map +1 -1
  157. package/lib/typescript/src/types/stylesheet.d.ts +1 -1
  158. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
  159. package/lib/typescript/src/web/convert/boxShadow.d.ts.map +1 -0
  160. package/lib/typescript/src/web/convert/breakpoint.d.ts.map +1 -0
  161. package/lib/typescript/{web → src/web}/convert/index.d.ts +1 -1
  162. package/lib/typescript/src/web/convert/index.d.ts.map +1 -0
  163. package/lib/typescript/src/web/convert/shadow.d.ts.map +1 -0
  164. package/lib/typescript/src/web/convert/style.d.ts.map +1 -0
  165. package/lib/typescript/src/web/convert/textShadow.d.ts.map +1 -0
  166. package/lib/typescript/src/web/convert/transform.d.ts.map +1 -0
  167. package/lib/typescript/{web → src/web}/convert/types.d.ts +1 -1
  168. package/lib/typescript/src/web/convert/types.d.ts.map +1 -0
  169. package/lib/typescript/src/web/convert/utils.d.ts.map +1 -0
  170. package/lib/typescript/{web → src/web}/create.d.ts +11 -11
  171. package/lib/typescript/{web → src/web}/create.d.ts.map +1 -1
  172. package/lib/typescript/{web → src/web}/index.d.ts +12 -12
  173. package/lib/typescript/{web → src/web}/index.d.ts.map +1 -1
  174. package/lib/typescript/src/web/listener/index.d.ts.map +1 -0
  175. package/lib/typescript/{web → src/web}/listener/listener.d.ts +1 -1
  176. package/lib/typescript/src/web/listener/listener.d.ts.map +1 -0
  177. package/lib/typescript/{web → src/web}/mock.d.ts +2 -2
  178. package/lib/typescript/src/web/mock.d.ts.map +1 -0
  179. package/lib/typescript/src/web/mq.d.ts.map +1 -0
  180. package/lib/typescript/src/web/pseudo.d.ts.map +1 -0
  181. package/lib/typescript/{web → src/web}/registry.d.ts +1 -1
  182. package/lib/typescript/src/web/registry.d.ts.map +1 -0
  183. package/lib/typescript/{web → src/web}/runtime.d.ts +6 -6
  184. package/lib/typescript/src/web/runtime.d.ts.map +1 -0
  185. package/lib/typescript/{web → src/web}/state.d.ts +4 -4
  186. package/lib/typescript/src/web/state.d.ts.map +1 -0
  187. package/lib/typescript/{web → src/web}/utils.d.ts +5 -3
  188. package/lib/typescript/src/web/utils.d.ts.map +1 -0
  189. package/lib/typescript/src/web/variants/getVariants.d.ts +3 -0
  190. package/lib/typescript/src/web/variants/getVariants.d.ts.map +1 -0
  191. package/lib/typescript/src/web/variants/index.d.ts +3 -0
  192. package/lib/typescript/src/web/variants/index.d.ts.map +1 -0
  193. package/lib/typescript/src/web/variants/useVariants.d.ts +3 -0
  194. package/lib/typescript/src/web/variants/useVariants.d.ts.map +1 -0
  195. package/package.json +2 -2
  196. package/plugin/__tests__/dependencies.spec.js +195 -107
  197. package/plugin/__tests__/ref.spec.js +537 -179
  198. package/plugin/__tests__/stylesheet.spec.js +174 -63
  199. package/plugin/index.js +68 -23
  200. package/plugin/ref.js +74 -12
  201. package/plugin/style.js +57 -24
  202. package/plugin/stylesheet.js +44 -1
  203. package/plugin/variants.js +33 -0
  204. package/src/specs/NavigtionBar/index.ts +1 -1
  205. package/src/specs/ShadowRegistry/index.ts +5 -5
  206. package/src/specs/StatusBar/index.ts +1 -1
  207. package/src/specs/StyleSheet/index.ts +3 -1
  208. package/src/specs/UnistylesRuntime/index.ts +1 -1
  209. package/src/specs/index.web.ts +1 -1
  210. package/src/types/stylesheet.ts +1 -1
  211. package/{web → src/web}/convert/breakpoint.ts +1 -1
  212. package/{web → src/web}/convert/index.ts +1 -1
  213. package/{web → src/web}/convert/types.ts +1 -1
  214. package/{web → src/web}/create.ts +36 -33
  215. package/{web → src/web}/listener/listener.ts +1 -1
  216. package/{web → src/web}/mock.ts +2 -2
  217. package/{web → src/web}/registry.ts +1 -1
  218. package/{web → src/web}/runtime.ts +4 -4
  219. package/{web → src/web}/state.ts +5 -5
  220. package/{web → src/web}/utils.ts +5 -5
  221. package/src/web/variants/getVariants.ts +42 -0
  222. package/src/web/variants/index.ts +2 -0
  223. package/{web → src/web/variants}/useVariants.ts +24 -44
  224. package/lib/typescript/web/convert/boxShadow.d.ts.map +0 -1
  225. package/lib/typescript/web/convert/breakpoint.d.ts.map +0 -1
  226. package/lib/typescript/web/convert/index.d.ts.map +0 -1
  227. package/lib/typescript/web/convert/shadow.d.ts.map +0 -1
  228. package/lib/typescript/web/convert/style.d.ts.map +0 -1
  229. package/lib/typescript/web/convert/textShadow.d.ts.map +0 -1
  230. package/lib/typescript/web/convert/transform.d.ts.map +0 -1
  231. package/lib/typescript/web/convert/types.d.ts.map +0 -1
  232. package/lib/typescript/web/convert/utils.d.ts.map +0 -1
  233. package/lib/typescript/web/listener/index.d.ts.map +0 -1
  234. package/lib/typescript/web/listener/listener.d.ts.map +0 -1
  235. package/lib/typescript/web/mock.d.ts.map +0 -1
  236. package/lib/typescript/web/mq.d.ts.map +0 -1
  237. package/lib/typescript/web/pseudo.d.ts.map +0 -1
  238. package/lib/typescript/web/registry.d.ts.map +0 -1
  239. package/lib/typescript/web/runtime.d.ts.map +0 -1
  240. package/lib/typescript/web/state.d.ts.map +0 -1
  241. package/lib/typescript/web/useVariants.d.ts +0 -3
  242. package/lib/typescript/web/useVariants.d.ts.map +0 -1
  243. package/lib/typescript/web/utils.d.ts.map +0 -1
  244. /package/lib/typescript/{web → src/web}/convert/boxShadow.d.ts +0 -0
  245. /package/lib/typescript/{web → src/web}/convert/breakpoint.d.ts +0 -0
  246. /package/lib/typescript/{web → src/web}/convert/shadow.d.ts +0 -0
  247. /package/lib/typescript/{web → src/web}/convert/style.d.ts +0 -0
  248. /package/lib/typescript/{web → src/web}/convert/textShadow.d.ts +0 -0
  249. /package/lib/typescript/{web → src/web}/convert/transform.d.ts +0 -0
  250. /package/lib/typescript/{web → src/web}/convert/utils.d.ts +0 -0
  251. /package/lib/typescript/{web → src/web}/listener/index.d.ts +0 -0
  252. /package/lib/typescript/{web → src/web}/mq.d.ts +0 -0
  253. /package/lib/typescript/{web → src/web}/pseudo.d.ts +0 -0
  254. /package/{web → src/web}/convert/boxShadow.ts +0 -0
  255. /package/{web → src/web}/convert/module.d.ts +0 -0
  256. /package/{web → src/web}/convert/shadow.ts +0 -0
  257. /package/{web → src/web}/convert/style.ts +0 -0
  258. /package/{web → src/web}/convert/textShadow.ts +0 -0
  259. /package/{web → src/web}/convert/transform.ts +0 -0
  260. /package/{web → src/web}/convert/utils.ts +0 -0
  261. /package/{web → src/web}/index.ts +0 -0
  262. /package/{web → src/web}/listener/index.ts +0 -0
  263. /package/{web → src/web}/mq.ts +0 -0
  264. /package/{web → src/web}/pseudo.ts +0 -0
@@ -0,0 +1,33 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "Unistyles"
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => min_ios_version_supported }
14
+ s.source = { :git => package["repository"], :tag => "#{s.version}" }
15
+
16
+ s.source_files = [
17
+ "ios/**/*.{swift,h,mm}",
18
+ "cxx/**/*.{h,cpp,hpp}"
19
+ ]
20
+ s.pod_target_xcconfig = {
21
+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
22
+ "GCC_PREPROCESSOR_DEFINITIONS" => "$(inherited) FOLLY_NO_CONFIG FOLLY_CFG_NO_COROUTINES"
23
+ }
24
+
25
+ s.public_header_files = [
26
+ "ios/Unistyles.h"
27
+ ]
28
+
29
+ load 'nitrogen/generated/ios/Unistyles+autolinking.rb'
30
+ add_nitrogen_files(s)
31
+
32
+ install_modules_dependencies(s)
33
+ end
@@ -5,6 +5,7 @@ namespace margelo::nitro::unistyles::helpers {
5
5
  static const std::string UNISTYLES_ID = "__unid";
6
6
  static const std::string ADD_VARIANTS_FN = "useVariants";
7
7
  static const std::string STYLE_DEPENDENCIES = "uni__dependencies";
8
+ static const std::string STYLE_VARIANTS = "uni__variants";
8
9
  static const std::string WEB_STYLE_KEY = "_web";
9
10
 
10
11
  }
@@ -1,12 +1,16 @@
1
1
  #pragma once
2
2
 
3
3
  #include <jsi/jsi.h>
4
+ #include <jsi/JSIDynamic.h>
5
+ #include <folly/dynamic.h>
4
6
  #include <unordered_set>
5
7
 
6
8
  using namespace facebook;
7
9
 
8
10
  namespace margelo::nitro::unistyles::helpers {
9
11
 
12
+ using Variants = std::vector<std::pair<std::string, std::string>>;
13
+
10
14
  inline void assertThat(jsi::Runtime& rt, bool condition, const std::string& message) {
11
15
  if (!condition) {
12
16
  throw jsi::JSError(rt, message);
@@ -90,4 +94,92 @@ inline bool isPlatformColor(jsi::Runtime& rt, jsi::Object& maybePlatformColor) {
90
94
  return maybePlatformColor.hasProperty(rt, "resource_paths") && maybePlatformColor.getProperty(rt, "resource_paths").isObject();
91
95
  }
92
96
 
97
+ inline Variants variantsToPairs(jsi::Runtime& rt, jsi::Object&& variants) {
98
+ Variants pairs{};
99
+
100
+ helpers::enumerateJSIObject(rt, variants, [&](const std::string& variantName, jsi::Value& variantValue){
101
+ if (variantValue.isUndefined() || variantValue.isNull()) {
102
+ return;
103
+ }
104
+
105
+ if (variantValue.isBool()) {
106
+ pairs.emplace_back(std::make_pair(variantName, variantValue.asBool() ? "true" : "false"));
107
+
108
+ return;
109
+ }
110
+
111
+ if (variantValue.isString()) {
112
+ pairs.emplace_back(std::make_pair(variantName, variantValue.asString(rt).utf8(rt)));
113
+ }
114
+ });
115
+
116
+ return pairs;
117
+ }
118
+
119
+ inline jsi::Object variantsToValue(jsi::Runtime& rt, Variants& variants) {
120
+ jsi::Object rawVariants = jsi::Object(rt);
121
+
122
+ std::for_each(variants.begin(), variants.end(), [&](std::pair<std::string, std::string>& pair){
123
+ rawVariants.setProperty(rt, pair.first.c_str(), jsi::String::createFromUtf8(rt, pair.second));
124
+ });
125
+
126
+ return rawVariants;
127
+ }
128
+
129
+ inline std::vector<folly::dynamic> parseDynamicFunctionArguments(jsi::Runtime& rt, jsi::Array& arguments) {
130
+ std::vector<folly::dynamic> parsedArgument{};
131
+ size_t count = arguments.size(rt);
132
+
133
+ parsedArgument.reserve(count);
134
+
135
+ for (size_t i = 0; i < count; i++) {
136
+ jsi::Value arg = arguments.getValueAtIndex(rt, i);
137
+
138
+ if (arg.isBool()) {
139
+ parsedArgument.push_back(folly::dynamic(arg.asBool()));
140
+
141
+ continue;
142
+ }
143
+
144
+ if (arg.isNumber()) {
145
+ parsedArgument.push_back(folly::dynamic(arg.asNumber()));
146
+
147
+ continue;
148
+ }
149
+
150
+ if (arg.isString()) {
151
+ parsedArgument.push_back(folly::dynamic(arg.asString(rt).utf8(rt)));
152
+
153
+ continue;
154
+ }
155
+
156
+ if (arg.isUndefined()) {
157
+ parsedArgument.push_back(folly::dynamic());
158
+
159
+ continue;
160
+ }
161
+
162
+ if (arg.isNull()) {
163
+ parsedArgument.push_back(folly::dynamic(nullptr));
164
+
165
+ continue;
166
+ }
167
+
168
+ if (!arg.isObject()) {
169
+ continue;;
170
+ }
171
+
172
+ auto argObj = arg.asObject(rt);
173
+
174
+ // allow arrays and objects too
175
+ if (!argObj.isFunction(rt) && !argObj.isArrayBuffer(rt)) {
176
+ parsedArgument.push_back(jsi::dynamicFromValue(rt, arg));
177
+
178
+ continue;
179
+ }
180
+ }
181
+
182
+ return parsedArgument;
183
+ }
184
+
93
185
  }
@@ -7,10 +7,12 @@ using namespace facebook;
7
7
  std::vector<jsi::PropNameID> HostStyle::getPropertyNames(jsi::Runtime& rt) {
8
8
  auto propertyNames = std::vector<jsi::PropNameID> {};
9
9
 
10
+ propertyNames.reserve(8);
11
+
10
12
  for (const auto& pair : this->_styleSheet->unistyles) {
11
13
  propertyNames.emplace_back(jsi::PropNameID::forUtf8(rt, pair.first));
12
14
  }
13
-
15
+
14
16
  return propertyNames;
15
17
  }
16
18
 
@@ -20,7 +22,7 @@ jsi::Value HostStyle::get(jsi::Runtime& rt, const jsi::PropNameID& propNameId) {
20
22
  if (propertyName == helpers::UNISTYLES_ID) {
21
23
  return jsi::Value(this->_styleSheet->tag);
22
24
  }
23
-
25
+
24
26
  if (propertyName == helpers::ADD_VARIANTS_FN) {
25
27
  return this->createAddVariantsProxyFunction(rt);
26
28
  }
@@ -29,6 +31,10 @@ jsi::Value HostStyle::get(jsi::Runtime& rt, const jsi::PropNameID& propNameId) {
29
31
  return valueFromUnistyle(rt, this->_styleSheet->unistyles[propertyName]);
30
32
  }
31
33
 
34
+ if (propertyName == helpers::STYLE_VARIANTS) {
35
+ return helpers::variantsToValue(rt, this->_variants);
36
+ }
37
+
32
38
  return jsi::Value::undefined();
33
39
  }
34
40
 
@@ -36,20 +42,20 @@ jsi::Function HostStyle::createAddVariantsProxyFunction(jsi::Runtime& rt) {
36
42
  auto useVariantsFnName = jsi::PropNameID::forUtf8(rt, helpers::ADD_VARIANTS_FN);
37
43
 
38
44
  return jsi::Function::createFromHostFunction(rt, useVariantsFnName, 1, [&](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count){
39
- helpers::assertThat(rt, count == 1, "useVariants expected to be called with one argument.");
40
- helpers::assertThat(rt, arguments[0].isObject(), "useVariants expected to be called with object.");
45
+ helpers::assertThat(rt, count == 1, "Unistyles: useVariants expected to be called with one argument.");
46
+ helpers::assertThat(rt, arguments[0].isObject(), "Unistyles: useVariants expected to be called with object.");
41
47
 
42
48
  auto parser = parser::Parser(this->_unistylesRuntime);
43
- auto pairs = parser.variantsToPairs(rt, arguments[0].asObject(rt));
44
-
45
- if (pairs == this->_styleSheet->variants) {
49
+ auto pairs = helpers::variantsToPairs(rt, arguments[0].asObject(rt));
50
+
51
+ if (pairs == this->_variants) {
46
52
  return jsi::Value::undefined();
47
53
  }
48
-
49
- this->_styleSheet->variants = pairs;
50
-
51
- parser.rebuildUnistylesWithVariants(rt, this->_styleSheet);
52
-
54
+
55
+ this->_variants = pairs;
56
+
57
+ parser.rebuildUnistylesWithVariants(rt, this->_styleSheet, this->_variants);
58
+
53
59
  return jsi::Value::undefined();
54
60
  });
55
61
  }
@@ -8,6 +8,8 @@
8
8
 
9
9
  namespace margelo::nitro::unistyles::core {
10
10
 
11
+ using Variants = std::vector<std::pair<std::string, std::string>>;
12
+
11
13
  struct JSI_EXPORT HostStyle : public jsi::HostObject {
12
14
  HostStyle(std::shared_ptr<StyleSheet> styleSheet, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime)
13
15
  : _styleSheet{styleSheet}, _unistylesRuntime{unistylesRuntime} {};
@@ -20,6 +22,7 @@ struct JSI_EXPORT HostStyle : public jsi::HostObject {
20
22
  private:
21
23
  std::shared_ptr<StyleSheet> _styleSheet;
22
24
  std::shared_ptr<HybridUnistylesRuntime> _unistylesRuntime;
25
+ std::vector<std::pair<std::string, std::string>> _variants{};
23
26
  };
24
27
 
25
28
  }
@@ -26,7 +26,6 @@ struct StyleSheet {
26
26
  StyleSheetType type;
27
27
  jsi::Object rawValue;
28
28
  std::unordered_map<std::string, Unistyle::Shared> unistyles{};
29
- std::vector<std::pair<std::string, std::string>> variants{};
30
29
  };
31
30
 
32
31
  }
@@ -4,20 +4,18 @@
4
4
  using namespace margelo::nitro::unistyles::core;
5
5
  using namespace facebook;
6
6
 
7
- std::shared_ptr<StyleSheet> StyleSheetRegistry::addStyleSheetFromValue(jsi::Runtime& rt, jsi::Object rawStyleSheet) {
8
- static unsigned int tag = 0;
9
-
7
+ std::shared_ptr<StyleSheet> StyleSheetRegistry::addStyleSheetFromValue(jsi::Runtime& rt, jsi::Object rawStyleSheet, int unid) {
10
8
  if (rawStyleSheet.isFunction(rt)) {
11
- return this->addFromFunction(rt, ++tag, rawStyleSheet.asFunction(rt));
9
+ return this->addFromFunction(rt, unid, rawStyleSheet.asFunction(rt));
12
10
  }
13
11
 
14
- return this->addFromObject(rt, ++tag, std::move(rawStyleSheet));
12
+ return this->addFromObject(rt, unid, std::move(rawStyleSheet));
15
13
  }
16
14
 
17
- std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromFunction(jsi::Runtime& rt, unsigned int tag, jsi::Function styleSheetFn) {
15
+ std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromFunction(jsi::Runtime& rt, int unid, jsi::Function styleSheetFn) {
18
16
  auto numberOfArgs = styleSheetFn.getProperty(rt, "length").getNumber();
19
17
 
20
- helpers::assertThat(rt, numberOfArgs <= 2, "expected up to 2 arguments.");
18
+ helpers::assertThat(rt, numberOfArgs <= 2, "StyleSheet.create expected up to 2 arguments.");
21
19
 
22
20
  auto& registry = UnistylesRegistry::get();
23
21
 
@@ -25,21 +23,20 @@ std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromFunction(jsi::Runtime& rt
25
23
  if (numberOfArgs == 0) {
26
24
  auto staticStyleSheet = styleSheetFn.call(rt).asObject(rt);
27
25
 
28
- return registry.addStyleSheet(tag, core::StyleSheetType::Static, std::move(staticStyleSheet));
26
+ return registry.addStyleSheet(rt, unid, core::StyleSheetType::Static, std::move(staticStyleSheet));
29
27
  }
30
28
 
31
29
  // stylesheet depends only on theme
32
30
  if (numberOfArgs == 1) {
33
- return registry.addStyleSheet(tag, core::StyleSheetType::Themable, std::move(styleSheetFn));
31
+ return registry.addStyleSheet(rt, unid, core::StyleSheetType::Themable, std::move(styleSheetFn));
34
32
  }
35
33
 
36
34
  // stylesheet depends on theme and mini runtime
37
- return registry.addStyleSheet(tag, core::StyleSheetType::ThemableWithMiniRuntime, std::move(styleSheetFn));
35
+ return registry.addStyleSheet(rt, unid, core::StyleSheetType::ThemableWithMiniRuntime, std::move(styleSheetFn));
38
36
  }
39
37
 
40
- std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromObject(jsi::Runtime& rt, unsigned int tag, jsi::Object rawStyleSheet) {
38
+ std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromObject(jsi::Runtime& rt, int tag, jsi::Object rawStyleSheet) {
41
39
  auto& registry = UnistylesRegistry::get();
42
40
 
43
- return registry.addStyleSheet(tag, core::StyleSheetType::Static, std::move(rawStyleSheet));
41
+ return registry.addStyleSheet(rt, tag, core::StyleSheetType::Static, std::move(rawStyleSheet));
44
42
  }
45
-
@@ -16,11 +16,11 @@ struct StyleSheetRegistry {
16
16
  StyleSheetRegistry(const StyleSheetRegistry&) = delete;
17
17
  StyleSheetRegistry(StyleSheetRegistry&&) = delete;
18
18
 
19
- virtual std::shared_ptr<StyleSheet> addStyleSheetFromValue(jsi::Runtime& rt, jsi::Object rawStyleSheet);
20
-
19
+ virtual std::shared_ptr<StyleSheet> addStyleSheetFromValue(jsi::Runtime& rt, jsi::Object rawStyleSheet, int unid);
20
+
21
21
  private:
22
- virtual std::shared_ptr<StyleSheet> addFromFunction(jsi::Runtime& rt, unsigned int tag, jsi::Function styleSheetFn);
23
- virtual std::shared_ptr<StyleSheet> addFromObject(jsi::Runtime& rt, unsigned int tag, jsi::Object rawStyleSheet);
22
+ virtual std::shared_ptr<StyleSheet> addFromFunction(jsi::Runtime& rt, int unid, jsi::Function styleSheetFn);
23
+ virtual std::shared_ptr<StyleSheet> addFromObject(jsi::Runtime& rt, int unid, jsi::Object rawStyleSheet);
24
24
  };
25
25
 
26
26
  }
@@ -7,6 +7,8 @@
7
7
 
8
8
  namespace margelo::nitro::unistyles::core {
9
9
 
10
+ class StyleSheet;
11
+
10
12
  using namespace facebook;
11
13
 
12
14
  enum class UnistyleType {
@@ -14,16 +16,11 @@ enum class UnistyleType {
14
16
  DynamicFunction
15
17
  };
16
18
 
17
- struct DynamicFunctionMetadata {
18
- size_t count;
19
- std::vector<folly::dynamic> arguments;
20
- };
21
-
22
19
  struct Unistyle {
23
20
  using Shared = std::shared_ptr<Unistyle>;
24
21
 
25
- Unistyle(UnistyleType type, std::string styleKey, jsi::Object& rawObject)
26
- : styleKey{styleKey}, type{type}, rawValue{std::move(rawObject)} {}
22
+ Unistyle(UnistyleType type, std::string styleKey, jsi::Object& rawObject, std::shared_ptr<StyleSheet> styleSheet)
23
+ : styleKey{styleKey}, type{type}, rawValue{std::move(rawObject)}, parent{styleSheet} {}
27
24
  virtual ~Unistyle() = default;
28
25
 
29
26
  Unistyle(const Unistyle&) = delete;
@@ -34,6 +31,7 @@ struct Unistyle {
34
31
  jsi::Object rawValue;
35
32
  std::optional<jsi::Object> parsedStyle;
36
33
  std::vector<UnistyleDependency> dependencies{};
34
+ std::shared_ptr<StyleSheet> parent;
37
35
 
38
36
  inline void addDependency(UnistyleDependency dependency) {
39
37
  // we can't add dependencies if unistyle is sealed
@@ -67,15 +65,14 @@ struct UnistyleDynamicFunction: public Unistyle {
67
65
  // unprocessedValue <- object generated after calling proxy and user's original function
68
66
  // parsedStyle <- parsed with Unistyle's parser
69
67
 
70
- UnistyleDynamicFunction(UnistyleType type, std::string styleKey, jsi::Object& rawObject)
71
- : Unistyle(type, styleKey, rawObject) {}
68
+ UnistyleDynamicFunction(UnistyleType type, std::string styleKey, jsi::Object& rawObject, std::shared_ptr<StyleSheet> styleSheet)
69
+ : Unistyle(type, styleKey, rawObject, styleSheet) {}
72
70
 
73
71
  UnistyleDynamicFunction(const UnistyleDynamicFunction&) = delete;
74
72
  UnistyleDynamicFunction(UnistyleDynamicFunction&& other) = delete;
75
73
 
76
74
  std::optional<jsi::Object> unprocessedValue;
77
75
  std::optional<jsi::Function> proxiedFunction = std::nullopt;
78
- std::optional<DynamicFunctionMetadata> dynamicFunctionMetadata = std::nullopt;
79
76
  };
80
77
 
81
78
  }
@@ -0,0 +1,23 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include "Unistyle.h"
5
+
6
+ namespace margelo::nitro::unistyles::core {
7
+
8
+ using Variants = std::vector<std::pair<std::string, std::string>>;
9
+
10
+ struct UnistyleData {
11
+ UnistyleData(Unistyle::Shared unistyle, const Variants& variants, std::vector<folly::dynamic>& arguments)
12
+ : unistyle{unistyle}, variants(std::move(variants)), dynamicFunctionMetadata{std::move(arguments)} {}
13
+
14
+ UnistyleData(const UnistyleData&) = delete;
15
+ UnistyleData(UnistyleData&& other): unistyle{other.unistyle}, variants(std::move(other.variants)) {}
16
+
17
+ core::Unistyle::Shared unistyle;
18
+ core::Variants variants;
19
+ std::optional<jsi::Object> parsedStyle = std::nullopt;
20
+ std::optional<std::vector<folly::dynamic>> dynamicFunctionMetadata = std::nullopt;
21
+ };
22
+
23
+ }
@@ -21,29 +21,47 @@ inline static Unistyle::Shared unistyleFromValue(jsi::Runtime& rt, const jsi::Va
21
21
  return nullptr;
22
22
  }
23
23
 
24
- return value.getObject(rt).getNativeState<UnistyleWrapper>(rt)->unistyle;
24
+ auto obj = value.getObject(rt);
25
+
26
+ if (!obj.hasNativeState(rt)) {
27
+ throw jsi::JSError(rt, R"(Unistyles: Style is not bound!
28
+
29
+ Potential reasons:
30
+ - You likely used the spread operator on a Unistyle style outside of a JSX component
31
+ - You're mixing React Native's StyleSheet styles with Unistyles styles
32
+
33
+ If you need to merge styles, do it within the style prop of your JSX component:
34
+
35
+ style={{...styles.container, ...styles.otherProp}} or style={[styles.container, styles.otherProp]}
36
+
37
+ Copying a Unistyle style outside of a JSX element will remove its internal C++ state, leading to unexpected behavior.
38
+
39
+ If you're mixing React Native and Unistyle StyleSheet styles, move your static styles into Unistyles to avoid conflicts.)");
40
+ }
41
+
42
+ return obj.getNativeState<UnistyleWrapper>(rt)->unistyle;
25
43
  }
26
44
 
27
45
  inline static jsi::Value valueFromUnistyle(jsi::Runtime& rt, Unistyle::Shared unistyle) {
28
46
  auto wrappedUnistyle = std::make_shared<UnistyleWrapper>(unistyle);
29
-
47
+
30
48
  if (unistyle->type == UnistyleType::Object) {
31
49
  jsi::Object obj = jsi::Object(rt);
32
-
50
+
33
51
  obj.setNativeState(rt, std::move(wrappedUnistyle));
34
52
  helpers::defineHiddenProperty(rt, obj, helpers::UNISTYLES_ID.c_str(), unistyle->styleKey);
35
-
53
+
36
54
  helpers::mergeJSIObjects(rt, obj, unistyle->parsedStyle.value());
37
-
55
+
38
56
  return obj;
39
57
  }
40
-
58
+
41
59
  auto unistyleFn = std::dynamic_pointer_cast<UnistyleDynamicFunction>(unistyle);
42
60
  auto hostFn = jsi::Value(rt, unistyleFn->proxiedFunction.value()).asObject(rt).asFunction(rt);
43
61
 
44
62
  hostFn.setNativeState(rt, std::move(wrappedUnistyle));
45
63
  hostFn.setProperty(rt, helpers::UNISTYLES_ID.c_str(), unistyleFn->styleKey);
46
-
64
+
47
65
  return std::move(hostFn);
48
66
  }
49
67
 
@@ -3,12 +3,9 @@
3
3
  using namespace margelo::nitro::unistyles;
4
4
  using namespace facebook::react;
5
5
 
6
- core::UnistylesCommitHook::UnistylesCommitHook(UIManager& uiManager, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime)
7
- : _unistylesRuntime{unistylesRuntime} {
8
- uiManager.registerCommitHook(*this);
9
- };
10
-
11
- core::UnistylesCommitHook::~UnistylesCommitHook() noexcept {}
6
+ core::UnistylesCommitHook::~UnistylesCommitHook() noexcept {
7
+ _uiManager->unregisterCommitHook(*this);
8
+ }
12
9
 
13
10
  void core::UnistylesCommitHook::commitHookWasRegistered(const UIManager &uiManager) noexcept {}
14
11
  void core::UnistylesCommitHook::commitHookWasUnregistered(const UIManager &uiManager) noexcept {}
@@ -24,6 +21,7 @@ RootShadowNode::Unshared core::UnistylesCommitHook::shadowTreeWillCommit(
24
21
  // skip only unistyles commits
25
22
  if (unistylesRootNode->hasUnistylesCommitTrait()) {
26
23
  unistylesRootNode->removeUnistylesCommitTrait();
24
+ unistylesRootNode->addUnistylesMountTrait();
27
25
 
28
26
  return newRootShadowNode;
29
27
  }
@@ -46,8 +44,11 @@ RootShadowNode::Unshared core::UnistylesCommitHook::shadowTreeWillCommit(
46
44
 
47
45
  shadow::ShadowLeafUpdates core::UnistylesCommitHook::getUnistylesUpdates() {
48
46
  auto& registry = core::UnistylesRegistry::get();
47
+ auto& rt = this->_unistylesRuntime->getRuntime();
49
48
  auto parser = parser::Parser(this->_unistylesRuntime);
50
- auto dependencyMap = registry.buildDependencyMap();
49
+ auto dependencyMap = registry.buildDependencyMap(rt);
50
+
51
+ parser.rebuildUnistylesInDependencyMap(rt, dependencyMap);
51
52
 
52
53
  return parser.dependencyMapToShadowLeafUpdates(dependencyMap);
53
54
  }
@@ -11,7 +11,11 @@ namespace margelo::nitro::unistyles::core {
11
11
  using namespace facebook::react;
12
12
 
13
13
  struct UnistylesCommitHook : public UIManagerCommitHook {
14
- UnistylesCommitHook(UIManager& uiManager, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime);
14
+ UnistylesCommitHook(std::shared_ptr<UIManager> uiManager, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime)
15
+ : _unistylesRuntime{unistylesRuntime}, _uiManager{uiManager} {
16
+ _uiManager->registerCommitHook(*this);
17
+ }
18
+
15
19
  ~UnistylesCommitHook() noexcept override;
16
20
 
17
21
  void commitHookWasRegistered(const UIManager &uiManager) noexcept override;
@@ -22,6 +26,7 @@ struct UnistylesCommitHook : public UIManagerCommitHook {
22
26
 
23
27
  private:
24
28
  std::shared_ptr<HybridUnistylesRuntime> _unistylesRuntime;
29
+ std::shared_ptr<UIManager> _uiManager;
25
30
  };
26
31
 
27
32
  }
@@ -8,6 +8,7 @@ namespace margelo::nitro::unistyles::core {
8
8
  // React Native uses 0-10
9
9
  // Reanimated uses 27-28
10
10
  constexpr shadow::ShadowNodeTraits::Trait UnistylesCommitTrait{1 << 30};
11
+ constexpr shadow::ShadowNodeTraits::Trait UnistylesMountTrait{1 << 31};
11
12
 
12
13
  struct UnistylesCommitShadowNode: public shadow::ShadowNode {
13
14
  inline void addUnistylesCommitTrait() {
@@ -21,6 +22,18 @@ struct UnistylesCommitShadowNode: public shadow::ShadowNode {
21
22
  inline bool hasUnistylesCommitTrait() {
22
23
  return traits_.check(UnistylesCommitTrait);
23
24
  }
25
+
26
+ inline void addUnistylesMountTrait() {
27
+ traits_.set(UnistylesMountTrait);
28
+ }
29
+
30
+ inline void removeUnistylesMountTrait() {
31
+ traits_.unset(UnistylesMountTrait);
32
+ }
33
+
34
+ inline bool hasUnistylesMountTrait() {
35
+ return traits_.check(UnistylesMountTrait);
36
+ }
24
37
  };
25
38
 
26
39
  }
@@ -0,0 +1,40 @@
1
+ #include "UnistylesMountHook.h"
2
+ #include "ShadowTreeManager.h"
3
+
4
+ using namespace margelo::nitro::unistyles;
5
+ using namespace facebook::react;
6
+
7
+ core::UnistylesMountHook::~UnistylesMountHook() noexcept {
8
+ _uiManager->unregisterMountHook(*this);
9
+ }
10
+
11
+ void core::UnistylesMountHook::shadowTreeDidMount(RootShadowNode::Shared const &rootShadowNode, double mountTime) noexcept {
12
+ auto rootNode = std::const_pointer_cast<RootShadowNode>(rootShadowNode);
13
+ auto unistylesRootNode = std::reinterpret_pointer_cast<core::UnistylesCommitShadowNode>(rootNode);
14
+
15
+ // skip only unistyles commits
16
+ if (unistylesRootNode->hasUnistylesMountTrait()) {
17
+ unistylesRootNode->removeUnistylesMountTrait();
18
+
19
+ return;
20
+ }
21
+
22
+ auto shadowLeafUpdates = this->getUnistylesUpdates();
23
+
24
+ if (shadowLeafUpdates.size() == 0) {
25
+ return;
26
+ }
27
+
28
+ shadow::ShadowTreeManager::updateShadowTree(this->_unistylesRuntime->getRuntime(), shadowLeafUpdates);
29
+ }
30
+
31
+ shadow::ShadowLeafUpdates core::UnistylesMountHook::getUnistylesUpdates() {
32
+ auto& registry = core::UnistylesRegistry::get();
33
+ auto& rt = this->_unistylesRuntime->getRuntime();
34
+ auto parser = parser::Parser(this->_unistylesRuntime);
35
+ auto dependencyMap = registry.buildDependencyMap(rt);
36
+
37
+ parser.rebuildUnistylesInDependencyMap(rt, dependencyMap);
38
+
39
+ return parser.dependencyMapToShadowLeafUpdates(dependencyMap);
40
+ }
@@ -0,0 +1,30 @@
1
+ #pragma once
2
+
3
+ #include <react/renderer/uimanager/UIManager.h>
4
+ #include <react/renderer/uimanager/UIManagerMountHook.h>
5
+ #include "HybridUnistylesRuntime.h"
6
+ #include "Parser.h"
7
+ #include "ShadowTreeManager.h"
8
+
9
+ namespace margelo::nitro::unistyles::core {
10
+
11
+ using namespace facebook::react;
12
+
13
+ struct UnistylesMountHook : public UIManagerMountHook {
14
+ UnistylesMountHook(std::shared_ptr<UIManager> uiManager, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime)
15
+ : _unistylesRuntime{unistylesRuntime}, _uiManager{uiManager} {
16
+ _uiManager->registerMountHook(*this);
17
+ }
18
+
19
+ ~UnistylesMountHook() noexcept override;
20
+
21
+ void shadowTreeDidMount(RootShadowNode::Shared const &rootShadowNode, double mountTime) noexcept override;
22
+
23
+ shadow::ShadowLeafUpdates getUnistylesUpdates();
24
+
25
+ private:
26
+ std::shared_ptr<HybridUnistylesRuntime> _unistylesRuntime;
27
+ std::shared_ptr<UIManager> _uiManager;
28
+ };
29
+
30
+ }