react-native-unistyles 3.0.0-alpha.32 → 3.0.0-alpha.34

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. package/cxx/common/Helpers.h +24 -2
  2. package/cxx/core/HostStyle.cpp +1 -1
  3. package/cxx/core/UnistyleWrapper.h +31 -1
  4. package/cxx/core/UnistylesMountHook.cpp +1 -1
  5. package/cxx/core/UnistylesMountHook.h +4 -2
  6. package/cxx/core/UnistylesRegistry.cpp +7 -2
  7. package/cxx/hybridObjects/HybridShadowRegistry.cpp +1 -2
  8. package/cxx/hybridObjects/HybridStyleSheet.cpp +4 -1
  9. package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +12 -0
  10. package/cxx/hybridObjects/HybridUnistylesRuntime.h +2 -0
  11. package/cxx/parser/Parser.cpp +6 -6
  12. package/cxx/parser/Parser.h +1 -1
  13. package/cxx/shadowTree/ShadowTrafficController.h +5 -8
  14. package/lib/commonjs/components/Display.js +18 -0
  15. package/lib/commonjs/components/Display.js.map +1 -0
  16. package/lib/commonjs/components/Hide.js +18 -0
  17. package/lib/commonjs/components/Hide.js.map +1 -0
  18. package/lib/commonjs/components/index.js +20 -0
  19. package/lib/commonjs/components/index.js.map +1 -0
  20. package/lib/commonjs/components/useMedia.js +59 -0
  21. package/lib/commonjs/components/useMedia.js.map +1 -0
  22. package/lib/commonjs/core/createUnistylesComponent.js +2 -1
  23. package/lib/commonjs/core/createUnistylesComponent.js.map +1 -1
  24. package/lib/commonjs/core/createUnistylesComponent.native.js +43 -4
  25. package/lib/commonjs/core/createUnistylesComponent.native.js.map +1 -1
  26. package/lib/commonjs/core/index.js +0 -14
  27. package/lib/commonjs/core/index.js.map +1 -1
  28. package/lib/commonjs/global.js +0 -4
  29. package/lib/commonjs/global.js.map +1 -1
  30. package/lib/commonjs/index.js +12 -14
  31. package/lib/commonjs/index.js.map +1 -1
  32. package/lib/commonjs/mq.js +25 -12
  33. package/lib/commonjs/mq.js.map +1 -1
  34. package/lib/commonjs/specs/NativePlatform/NativePlatform.nitro.js +4 -5
  35. package/lib/commonjs/specs/NativePlatform/NativePlatform.nitro.js.map +0 -1
  36. package/lib/commonjs/specs/ShadowRegistry/ShadowRegistry.nitro.js.map +1 -1
  37. package/lib/commonjs/specs/ShadowRegistry/types.js +0 -4
  38. package/lib/commonjs/specs/ShadowRegistry/types.js.map +1 -1
  39. package/lib/commonjs/specs/StatusBar/UnistylesStatusBar.nitro.js.map +1 -1
  40. package/lib/commonjs/specs/StyleSheet/UnistylesStyleSheet.nitro.js +4 -2
  41. package/lib/commonjs/specs/StyleSheet/UnistylesStyleSheet.nitro.js.map +1 -1
  42. package/lib/commonjs/specs/TurboUnistyles/NativeTurboUnistyles.js +3 -1
  43. package/lib/commonjs/specs/TurboUnistyles/NativeTurboUnistyles.js.map +1 -2
  44. package/lib/commonjs/specs/types.js.map +0 -1
  45. package/lib/commonjs/types/accessibility.js +1 -1
  46. package/lib/commonjs/types/accessibility.js.map +0 -1
  47. package/lib/commonjs/types/common.js +0 -3
  48. package/lib/commonjs/types/stylesheet.js +2 -1
  49. package/lib/commonjs/web/convert/index.js +7 -1
  50. package/lib/commonjs/web/convert/index.js.map +1 -2
  51. package/lib/commonjs/web/convert/pseudo.js.map +1 -2
  52. package/lib/commonjs/web/convert/shadow.js +5 -1
  53. package/lib/commonjs/web/convert/shadow.js.map +1 -2
  54. package/lib/commonjs/web/convert/style.js.map +1 -2
  55. package/lib/commonjs/web/convert/textShadow.js +4 -1
  56. package/lib/commonjs/web/convert/textShadow.js.map +1 -2
  57. package/lib/commonjs/web/convert/transform.js.map +1 -2
  58. package/lib/commonjs/web/convert/types.js.map +1 -1
  59. package/lib/commonjs/web/convert/utils.js +2 -1
  60. package/lib/commonjs/web/convert/utils.js.map +1 -2
  61. package/lib/commonjs/web/listener.js.map +1 -2
  62. package/lib/commonjs/web/mock.js.map +1 -2
  63. package/lib/commonjs/web/shadowRegistry.js +9 -1
  64. package/lib/commonjs/web/shadowRegistry.js.map +1 -2
  65. package/lib/commonjs/web/utils/unistyle.js +1 -1
  66. package/lib/commonjs/web/variants/index.js.map +1 -1
  67. package/lib/commonjs/web/variants/useVariants.js +2 -1
  68. package/lib/commonjs/web/variants/useVariants.js.map +1 -2
  69. package/lib/module/common.js.map +1 -1
  70. package/lib/module/components/Display.js +13 -0
  71. package/lib/module/components/Display.js.map +1 -0
  72. package/lib/module/components/Hide.js +13 -0
  73. package/lib/module/components/Hide.js.map +1 -0
  74. package/lib/module/components/index.js +5 -0
  75. package/lib/module/components/index.js.map +1 -0
  76. package/lib/module/components/useMedia.js +54 -0
  77. package/lib/module/components/useMedia.js.map +1 -0
  78. package/lib/module/core/createUnistylesComponent.js +2 -1
  79. package/lib/module/core/createUnistylesComponent.js.map +1 -1
  80. package/lib/module/core/createUnistylesComponent.native.js +41 -3
  81. package/lib/module/core/createUnistylesComponent.native.js.map +1 -1
  82. package/lib/module/core/index.js +0 -2
  83. package/lib/module/core/index.js.map +1 -1
  84. package/lib/module/global.js +1 -1
  85. package/lib/module/global.js.map +0 -1
  86. package/lib/module/index.js +2 -1
  87. package/lib/module/index.js.map +1 -1
  88. package/lib/module/mq.js +21 -11
  89. package/lib/module/mq.js.map +1 -1
  90. package/lib/module/specs/NativePlatform/NativePlatform.nitro.js +6 -5
  91. package/lib/module/specs/NativePlatform/NativePlatform.nitro.js.map +0 -1
  92. package/lib/module/specs/NavigtionBar/UnistylesNavigationBar.nitro.js.map +1 -1
  93. package/lib/module/specs/ShadowRegistry/ShadowRegistry.nitro.js.map +0 -1
  94. package/lib/module/specs/ShadowRegistry/types.js +0 -2
  95. package/lib/module/specs/ShadowRegistry/types.js.map +0 -1
  96. package/lib/module/specs/StatusBar/UnistylesStatusBar.nitro.js.map +0 -1
  97. package/lib/module/specs/StyleSheet/UnistylesStyleSheet.nitro.js +1 -1
  98. package/lib/module/specs/StyleSheet/UnistylesStyleSheet.nitro.js.map +0 -1
  99. package/lib/module/specs/StyleSheet/index.js.map +1 -2
  100. package/lib/module/specs/TurboUnistyles/NativeTurboUnistyles.js.map +1 -2
  101. package/lib/module/specs/UnistylesRuntime/UnistylesRuntime.nitro.js.map +0 -1
  102. package/lib/module/specs/UnistylesRuntime/index.js.map +1 -2
  103. package/lib/module/specs/types.js.map +0 -1
  104. package/lib/module/types/accessibility.js +9 -9
  105. package/lib/module/types/accessibility.js.map +1 -1
  106. package/lib/module/types/breakpoints.js.map +0 -1
  107. package/lib/module/types/common.js +0 -2
  108. package/lib/module/types/common.js.map +1 -1
  109. package/lib/module/types/core.js.map +1 -1
  110. package/lib/module/types/stylesheet.js +2 -2
  111. package/lib/module/types/variants.js.map +1 -1
  112. package/lib/module/web/convert/boxShadow.js +4 -1
  113. package/lib/module/web/convert/boxShadow.js.map +1 -2
  114. package/lib/module/web/convert/pseudo.js.map +1 -2
  115. package/lib/module/web/convert/textShadow.js +4 -1
  116. package/lib/module/web/convert/transform.js.map +1 -2
  117. package/lib/module/web/convert/types.js.map +1 -1
  118. package/lib/module/web/convert/utils.js +2 -1
  119. package/lib/module/web/registry.js +3 -1
  120. package/lib/module/web/registry.js.map +1 -2
  121. package/lib/module/web/runtime.js.map +1 -2
  122. package/lib/module/web/shadowRegistry.js +9 -1
  123. package/lib/module/web/utils/common.js +3 -1
  124. package/lib/module/web/utils/common.js.map +1 -2
  125. package/lib/module/web/utils/index.js.map +1 -1
  126. package/lib/module/web/utils/unistyle.js +1 -1
  127. package/lib/module/web/utils/unistyle.js.map +1 -1
  128. package/lib/module/web/variants/index.js.map +1 -1
  129. package/lib/module/web/variants/useVariants.js +2 -1
  130. package/lib/module/web/variants/useVariants.js.map +1 -2
  131. package/lib/typescript/src/components/Display.d.ts +7 -0
  132. package/lib/typescript/src/components/Display.d.ts.map +1 -0
  133. package/lib/typescript/src/components/Hide.d.ts +7 -0
  134. package/lib/typescript/src/components/Hide.d.ts.map +1 -0
  135. package/lib/typescript/src/components/index.d.ts +3 -0
  136. package/lib/typescript/src/components/index.d.ts.map +1 -0
  137. package/lib/typescript/src/components/useMedia.d.ts +6 -0
  138. package/lib/typescript/src/components/useMedia.d.ts.map +1 -0
  139. package/lib/typescript/src/core/createUnistylesComponent.d.ts +3 -2
  140. package/lib/typescript/src/core/createUnistylesComponent.d.ts.map +1 -1
  141. package/lib/typescript/src/core/createUnistylesComponent.native.d.ts +3 -2
  142. package/lib/typescript/src/core/createUnistylesComponent.native.d.ts.map +1 -1
  143. package/lib/typescript/src/core/index.d.ts +0 -2
  144. package/lib/typescript/src/core/index.d.ts.map +1 -1
  145. package/lib/typescript/src/index.d.ts +2 -1
  146. package/lib/typescript/src/index.d.ts.map +1 -1
  147. package/lib/typescript/src/mq.d.ts +8 -0
  148. package/lib/typescript/src/mq.d.ts.map +1 -1
  149. package/lib/typescript/src/specs/UnistylesRuntime/UnistylesRuntime.nitro.d.ts +1 -0
  150. package/lib/typescript/src/specs/UnistylesRuntime/UnistylesRuntime.nitro.d.ts.map +1 -1
  151. package/nitrogen/generated/shared/c++/HybridUnistylesRuntimeSpec.cpp +1 -0
  152. package/nitrogen/generated/shared/c++/HybridUnistylesRuntimeSpec.hpp +2 -0
  153. package/package.json +1 -1
  154. package/plugin/ref.js +4 -4
  155. package/plugin/style.js +15 -3
  156. package/src/components/Display.tsx +12 -0
  157. package/src/components/Hide.tsx +12 -0
  158. package/src/components/index.ts +2 -0
  159. package/src/components/useMedia.ts +64 -0
  160. package/src/core/createUnistylesComponent.native.tsx +48 -4
  161. package/src/core/createUnistylesComponent.tsx +4 -2
  162. package/src/core/index.ts +0 -2
  163. package/src/index.ts +2 -1
  164. package/src/mq.ts +27 -9
  165. package/src/specs/UnistylesRuntime/UnistylesRuntime.nitro.ts +1 -0
  166. package/src/web/utils/unistyle.ts +1 -1
  167. package/lib/commonjs/core/useBreakpoint.js +0 -22
  168. package/lib/commonjs/core/useBreakpoint.js.map +0 -1
  169. package/lib/commonjs/core/useBreakpoint.native.js +0 -24
  170. package/lib/commonjs/core/useBreakpoint.native.js.map +0 -2
  171. package/lib/commonjs/core/useTheme.js +0 -22
  172. package/lib/commonjs/core/useTheme.js.map +0 -1
  173. package/lib/commonjs/core/useTheme.native.js +0 -24
  174. package/lib/commonjs/core/useTheme.native.js.map +0 -2
  175. package/lib/commonjs/web/mq.js +0 -23
  176. package/lib/commonjs/web/mq.js.map +0 -1
  177. package/lib/module/core/useBreakpoint.js +0 -17
  178. package/lib/module/core/useBreakpoint.js.map +0 -1
  179. package/lib/module/core/useBreakpoint.native.js +0 -19
  180. package/lib/module/core/useBreakpoint.native.js.map +0 -1
  181. package/lib/module/core/useTheme.js +0 -17
  182. package/lib/module/core/useTheme.js.map +0 -2
  183. package/lib/module/core/useTheme.native.js +0 -19
  184. package/lib/module/core/useTheme.native.js.map +0 -1
  185. package/lib/module/web/mq.js +0 -17
  186. package/lib/module/web/mq.js.map +0 -1
  187. package/lib/typescript/src/core/useBreakpoint.d.ts +0 -2
  188. package/lib/typescript/src/core/useBreakpoint.d.ts.map +0 -1
  189. package/lib/typescript/src/core/useBreakpoint.native.d.ts +0 -2
  190. package/lib/typescript/src/core/useBreakpoint.native.d.ts.map +0 -1
  191. package/lib/typescript/src/core/useTheme.d.ts +0 -3
  192. package/lib/typescript/src/core/useTheme.d.ts.map +0 -1
  193. package/lib/typescript/src/core/useTheme.native.d.ts +0 -3
  194. package/lib/typescript/src/core/useTheme.native.d.ts.map +0 -1
  195. package/lib/typescript/src/web/mq.d.ts +0 -8
  196. package/lib/typescript/src/web/mq.d.ts.map +0 -1
  197. package/src/common.js +0 -4
  198. package/src/core/createUnistylesComponent.js +0 -6
  199. package/src/core/createUnistylesComponent.native.js +0 -31
  200. package/src/core/index.js +0 -3
  201. package/src/core/useBreakpoint.js +0 -14
  202. package/src/core/useBreakpoint.native.js +0 -16
  203. package/src/core/useBreakpoint.native.ts +0 -21
  204. package/src/core/useBreakpoint.ts +0 -18
  205. package/src/core/useTheme.js +0 -14
  206. package/src/core/useTheme.native.js +0 -16
  207. package/src/core/useTheme.native.ts +0 -22
  208. package/src/core/useTheme.ts +0 -19
  209. package/src/global.js +0 -1
  210. package/src/index.js +0 -3
  211. package/src/mq.js +0 -38
  212. package/src/specs/NativePlatform/NativePlatform.nitro.js +0 -19
  213. package/src/specs/NativePlatform/index.js +0 -1
  214. package/src/specs/NavigtionBar/UnistylesNavigationBar.nitro.js +0 -1
  215. package/src/specs/NavigtionBar/index.js +0 -9
  216. package/src/specs/ShadowRegistry/ShadowRegistry.nitro.js +0 -1
  217. package/src/specs/ShadowRegistry/index.js +0 -26
  218. package/src/specs/ShadowRegistry/types.js +0 -1
  219. package/src/specs/StatusBar/UnistylesStatusBar.nitro.js +0 -1
  220. package/src/specs/StatusBar/index.js +0 -25
  221. package/src/specs/StyleSheet/UnistylesStyleSheet.nitro.js +0 -1
  222. package/src/specs/StyleSheet/index.js +0 -13
  223. package/src/specs/TurboUnistyles/NativeTurboUnistyles.js +0 -2
  224. package/src/specs/TurboUnistyles/index.js +0 -1
  225. package/src/specs/UnistylesRuntime/UnistylesRuntime.nitro.js +0 -1
  226. package/src/specs/UnistylesRuntime/index.js +0 -20
  227. package/src/specs/index.js +0 -8
  228. package/src/specs/index.web.js +0 -2
  229. package/src/specs/types.js +0 -17
  230. package/src/types/accessibility.js +0 -30
  231. package/src/types/breakpoints.js +0 -1
  232. package/src/types/common.js +0 -1
  233. package/src/types/core.js +0 -1
  234. package/src/types/index.js +0 -1
  235. package/src/types/stylesheet.js +0 -2
  236. package/src/types/variants.js +0 -1
  237. package/src/web/convert/boxShadow.js +0 -59
  238. package/src/web/convert/index.js +0 -56
  239. package/src/web/convert/pseudo.js +0 -131
  240. package/src/web/convert/shadow.js +0 -50
  241. package/src/web/convert/style.js +0 -137
  242. package/src/web/convert/textShadow.js +0 -56
  243. package/src/web/convert/transform.js +0 -65
  244. package/src/web/convert/types.js +0 -2
  245. package/src/web/convert/utils.js +0 -39
  246. package/src/web/create.js +0 -36
  247. package/src/web/index.js +0 -22
  248. package/src/web/listener.js +0 -26
  249. package/src/web/mock.js +0 -21
  250. package/src/web/mq.js +0 -14
  251. package/src/web/mq.ts +0 -17
  252. package/src/web/registry.js +0 -165
  253. package/src/web/runtime.js +0 -160
  254. package/src/web/shadowRegistry.js +0 -96
  255. package/src/web/state.js +0 -68
  256. package/src/web/utils/common.js +0 -79
  257. package/src/web/utils/index.js +0 -2
  258. package/src/web/utils/unistyle.js +0 -72
  259. package/src/web/variants/getVariants.js +0 -26
  260. package/src/web/variants/index.js +0 -2
  261. package/src/web/variants/useVariants.js +0 -50
@@ -1,165 +0,0 @@
1
- import { UnistylesRuntime } from './runtime';
2
- import { extractMediaQueryValue, keyInObject, getMediaQuery, generateHash, extractUnistyleDependencies, deepMergeObjects } from './utils';
3
- import { UnistylesListener } from './listener';
4
- import { convertUnistyles } from './convert';
5
- import { getVariants } from './variants';
6
- class UnistylesRegistryBuilder {
7
- stylesheets = new Map();
8
- stylesCounter = new Map();
9
- #styleTag = null;
10
- disposeListenersMap = new Map();
11
- dependenciesMap = new Map();
12
- get styleTag() {
13
- const tag = this.#styleTag;
14
- if (!tag) {
15
- const newTag = document.createElement('style');
16
- newTag.id = 'unistyles-web';
17
- this.#styleTag = newTag;
18
- document.head.appendChild(newTag);
19
- return newTag;
20
- }
21
- return tag;
22
- }
23
- getComputedStylesheet = (stylesheet) => {
24
- if (typeof stylesheet !== 'function') {
25
- return stylesheet;
26
- }
27
- const computedStylesheet = this.stylesheets.get(stylesheet);
28
- if (computedStylesheet) {
29
- return computedStylesheet;
30
- }
31
- const createdStylesheet = stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime);
32
- const dependencies = Object.values(createdStylesheet).flatMap(value => extractUnistyleDependencies(value));
33
- this.addDependenciesToStylesheet(stylesheet, dependencies);
34
- this.stylesheets.set(stylesheet, createdStylesheet);
35
- return createdStylesheet;
36
- };
37
- addDependenciesToStylesheet = (stylesheet, dependencies) => {
38
- this.disposeListenersMap.get(stylesheet)?.();
39
- const dependenciesMap = this.dependenciesMap.get(stylesheet) ?? new Set(dependencies);
40
- dependencies.forEach(dependency => dependenciesMap.add(dependency));
41
- const dispose = UnistylesListener.addListeners(Array.from(dependenciesMap), () => {
42
- const newComputedStylesheet = stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime);
43
- this.stylesheets.set(stylesheet, newComputedStylesheet);
44
- });
45
- this.dependenciesMap.set(stylesheet, dependenciesMap);
46
- this.disposeListenersMap.set(stylesheet, dispose);
47
- };
48
- add = ({ key, value, stylesheet, args, variants }) => {
49
- const hash = generateHash(value);
50
- const existingCounter = this.stylesCounter.get(hash);
51
- if (!existingCounter || existingCounter.size === 0) {
52
- const counter = new Set();
53
- const dependencies = extractUnistyleDependencies(value);
54
- counter.add(value);
55
- this.stylesCounter.set(hash, counter);
56
- this.applyStyles(hash, convertUnistyles(value));
57
- UnistylesListener.addListeners(dependencies, async () => {
58
- // Move this callback to the end of the event loop
59
- await Promise.resolve();
60
- const newComputedStyleSheet = this.getComputedStylesheet(stylesheet);
61
- const newValue = newComputedStyleSheet[key];
62
- const result = typeof newValue === 'function'
63
- ? newValue(...args)
64
- : newValue;
65
- const { variantsResult } = Object.fromEntries(getVariants({ variantsResult: result }, variants));
66
- const resultWithVariants = deepMergeObjects(result, variantsResult ?? {});
67
- this.applyStyles(hash, convertUnistyles(resultWithVariants));
68
- });
69
- return hash;
70
- }
71
- existingCounter.add(value);
72
- return hash;
73
- };
74
- applyStyles = (hash, styles) => {
75
- Object.entries(styles).forEach(([key, value]) => {
76
- if (!this.styleTag.sheet) {
77
- return;
78
- }
79
- if (typeof value === 'object' && !key.startsWith('_')) {
80
- const mediaQuery = getMediaQuery(key);
81
- const cssRules = Array.from(this.styleTag.sheet.cssRules);
82
- let queryRule = cssRules.find(rule => {
83
- if (!(rule instanceof CSSMediaRule)) {
84
- return false;
85
- }
86
- return rule.media.item(0)?.includes(mediaQuery);
87
- }) ?? null;
88
- if (!queryRule) {
89
- const mediaQueryValue = extractMediaQueryValue(mediaQuery);
90
- const ruleIndex = mediaQueryValue
91
- ? cssRules.reduce((acc, rule, ruleIndex) => {
92
- if (!(rule instanceof CSSMediaRule)) {
93
- return acc;
94
- }
95
- const ruleMediaQueryValue = extractMediaQueryValue(rule.conditionText);
96
- if (ruleMediaQueryValue === undefined) {
97
- return;
98
- }
99
- return ruleMediaQueryValue > mediaQueryValue ? ruleIndex : acc;
100
- }, cssRules.length)
101
- : undefined;
102
- queryRule = this.styleTag.sheet.cssRules.item(this.styleTag.sheet.insertRule(`@media ${mediaQuery} {.${hash} {}}`, ruleIndex));
103
- }
104
- if (queryRule instanceof CSSMediaRule) {
105
- Object.entries(value).forEach(([mqKey, mqValue]) => {
106
- this.applyRule({
107
- hash,
108
- key: mqKey,
109
- value: mqValue,
110
- sheet: queryRule
111
- });
112
- });
113
- }
114
- return;
115
- }
116
- // Pseudo
117
- if (typeof value === 'object') {
118
- Object.entries(value).forEach(([pseudoKey, pseudoValue]) => {
119
- this.applyRule({
120
- hash: `${hash}${key.replace('_', ':')}`,
121
- key: pseudoKey,
122
- value: pseudoValue,
123
- sheet: this.styleTag.sheet
124
- });
125
- });
126
- return;
127
- }
128
- this.applyRule({
129
- hash,
130
- key,
131
- value,
132
- sheet: this.styleTag.sheet
133
- });
134
- });
135
- };
136
- applyRule = ({ hash, key, value, sheet }) => {
137
- let rule = Array.from(sheet.cssRules).find(rule => {
138
- if (!(rule instanceof CSSStyleRule)) {
139
- return false;
140
- }
141
- // In unistyles pseudos are prefixed with ':' but in css some of them are prefixed with '::'
142
- return rule.selectorText.replace('::', ':').includes(hash);
143
- }) ?? null;
144
- if (!rule) {
145
- rule = sheet.cssRules.item(sheet.insertRule(`.${hash} {}`));
146
- }
147
- if (!(rule instanceof CSSStyleRule) || !keyInObject(rule.style, key)) {
148
- return;
149
- }
150
- rule.style[key] = value;
151
- };
152
- remove = (value) => {
153
- const hash = generateHash(value);
154
- const existingStyles = this.stylesCounter.get(hash);
155
- if (!existingStyles) {
156
- return;
157
- }
158
- existingStyles.delete(value);
159
- if (existingStyles.size === 0) {
160
- const ruleIndex = Array.from(this.styleTag.sheet?.cssRules ?? []).findIndex(rule => rule.cssText.includes(`.${hash}`));
161
- this.styleTag.sheet?.deleteRule(ruleIndex);
162
- }
163
- };
164
- }
165
- export const UnistylesRegistry = new UnistylesRegistryBuilder();
@@ -1,160 +0,0 @@
1
- import { UnistyleDependency } from '../specs/NativePlatform';
2
- import { ColorScheme, Orientation } from '../specs/types';
3
- import { WebContentSizeCategory } from '../types';
4
- import { UnistylesListener } from './listener';
5
- import { NavigationBar, StatusBar } from './mock';
6
- import { UnistylesState } from './state';
7
- import { hexToRGBA, isServer, schemeToTheme } from './utils';
8
- class UnistylesRuntimeBuilder {
9
- lightMedia = this.getLightMedia();
10
- darkMedia = this.getDarkMedia();
11
- getLightMedia() {
12
- if (isServer()) {
13
- return null;
14
- }
15
- if (!this.lightMedia) {
16
- this.lightMedia = window.matchMedia('(prefers-color-scheme: light)');
17
- }
18
- return this.lightMedia;
19
- }
20
- getDarkMedia() {
21
- if (isServer()) {
22
- return null;
23
- }
24
- if (!this.darkMedia) {
25
- this.darkMedia = window.matchMedia('(prefers-color-scheme: dark)');
26
- }
27
- return this.darkMedia;
28
- }
29
- get colorScheme() {
30
- switch (true) {
31
- case this.getLightMedia()?.matches:
32
- return ColorScheme.Light;
33
- case this.getDarkMedia()?.matches:
34
- return ColorScheme.Dark;
35
- default:
36
- return ColorScheme.Unspecified;
37
- }
38
- }
39
- get themeName() {
40
- return UnistylesState.themeName;
41
- }
42
- get contentSizeCategory() {
43
- return WebContentSizeCategory.Unspecified;
44
- }
45
- get breakpoint() {
46
- return UnistylesState.breakpoint;
47
- }
48
- get orientation() {
49
- if (isServer()) {
50
- return Orientation.Portrait;
51
- }
52
- return screen.orientation.type.includes('portrait') ? Orientation.Portrait : Orientation.Landscape;
53
- }
54
- get theme() {
55
- return this.getTheme(this.themeName);
56
- }
57
- get pixelRatio() {
58
- return isServer() ? 1 : window.devicePixelRatio;
59
- }
60
- get screen() {
61
- if (isServer()) {
62
- return {
63
- width: 0,
64
- height: 0
65
- };
66
- }
67
- return {
68
- width: window.innerWidth,
69
- height: window.innerHeight
70
- };
71
- }
72
- get fontScale() {
73
- return 1;
74
- }
75
- get insets() {
76
- return {
77
- top: 0,
78
- bottom: 0,
79
- left: 0,
80
- right: 0,
81
- ime: 0
82
- };
83
- }
84
- get statusBar() {
85
- return StatusBar;
86
- }
87
- get rtl() {
88
- return isServer() ? true : document.documentElement.dir === 'rtl';
89
- }
90
- get hasAdaptiveThemes() {
91
- return UnistylesState.hasAdaptiveThemes;
92
- }
93
- get navigationBar() {
94
- return NavigationBar;
95
- }
96
- get miniRuntime() {
97
- return {
98
- colorScheme: this.colorScheme,
99
- themeName: this.themeName,
100
- contentSizeCategory: this.contentSizeCategory,
101
- breakpoint: this.breakpoint,
102
- isLandscape: this.orientation === Orientation.Landscape,
103
- isPortrait: this.orientation === Orientation.Portrait,
104
- pixelRatio: this.pixelRatio,
105
- screen: this.screen,
106
- fontScale: this.fontScale,
107
- insets: this.insets,
108
- statusBar: {
109
- width: this.statusBar.width,
110
- height: this.statusBar.height
111
- },
112
- navigationBar: {
113
- width: this.navigationBar.width,
114
- height: this.navigationBar.height,
115
- },
116
- rtl: this.rtl,
117
- hasAdaptiveThemes: this.hasAdaptiveThemes
118
- };
119
- }
120
- setTheme = (themeName) => {
121
- if (themeName === UnistylesRuntime.themeName) {
122
- return;
123
- }
124
- UnistylesState.themeName = themeName;
125
- UnistylesListener.emitChange(UnistyleDependency.Theme);
126
- UnistylesListener.emitChange(UnistyleDependency.ThemeName);
127
- };
128
- setAdaptiveThemes = (isEnabled) => {
129
- UnistylesState.hasAdaptiveThemes = isEnabled;
130
- if (!isEnabled) {
131
- return;
132
- }
133
- this.setTheme(schemeToTheme(UnistylesRuntime.colorScheme));
134
- };
135
- setRootViewBackgroundColor = (hex, alpha) => {
136
- if (isServer()) {
137
- return;
138
- }
139
- document.documentElement.style.backgroundColor = alpha ? hexToRGBA(hex, alpha) : hex;
140
- };
141
- setImmersiveMode = () => { };
142
- updateTheme = (themeName, updater) => {
143
- const oldTheme = UnistylesState.themes.get(themeName);
144
- if (!oldTheme) {
145
- throw new Error(`🦄 Theme "${themeName}" is not registered!`);
146
- }
147
- UnistylesState.themes.set(themeName, updater(oldTheme));
148
- };
149
- getTheme = (themeName = this.themeName) => {
150
- if (!themeName) {
151
- throw new Error('🦄 No theme selected!');
152
- }
153
- const theme = UnistylesState.themes.get(themeName);
154
- if (!theme) {
155
- throw new Error(`🦄 Theme "${this.themeName}" is not registered!`);
156
- }
157
- return theme;
158
- };
159
- }
160
- export const UnistylesRuntime = new UnistylesRuntimeBuilder();
@@ -1,96 +0,0 @@
1
- import { UnistylesRegistry } from './registry';
2
- import { createDoubleMap, deepMergeObjects, equal, extractSecrets, extractUnistyleDependencies, isInDocument } from './utils';
3
- import { getVariants } from './variants';
4
- class UnistylesShadowRegistryBuilder {
5
- // MOCKS
6
- name = 'UnistylesShadowRegistry';
7
- __type = 'web';
8
- equals = () => true;
9
- toString = () => 'UnistylesShadowRegistry';
10
- dispose = () => { };
11
- // END MOCKS
12
- resultsMap = createDoubleMap();
13
- classNamesMap = createDoubleMap();
14
- add = (ref, _style, _variants, _args) => {
15
- // Style is not provided
16
- if (!_style) {
17
- return;
18
- }
19
- // Array of styles
20
- if (Array.isArray(_style)) {
21
- _style.forEach(style => this.add(ref, style, _variants, _args));
22
- return;
23
- }
24
- // Not a unistyle
25
- if (!Object.keys(_style).some(key => key.startsWith('__uni__'))) {
26
- return;
27
- }
28
- // Ref is unmounted, remove style tags from the document
29
- if (ref === null) {
30
- const secrets = extractSecrets(_style);
31
- secrets.forEach(({ __uni__refs, __uni__key }) => {
32
- __uni__refs.forEach(ref => {
33
- if (isInDocument(ref)) {
34
- return;
35
- }
36
- const oldResult = this.resultsMap.get(ref, __uni__key);
37
- this.resultsMap.delete(ref, __uni__key);
38
- this.classNamesMap.delete(ref, __uni__key);
39
- if (oldResult) {
40
- UnistylesRegistry.remove(oldResult);
41
- }
42
- });
43
- });
44
- return;
45
- }
46
- // Ref is not an HTMLElement
47
- if (!(ref instanceof HTMLElement)) {
48
- return;
49
- }
50
- extractSecrets(_style).forEach(secret => {
51
- const { __uni__key, __uni__stylesheet, __uni__variants, __uni__args = [], __uni__refs } = secret;
52
- const newComputedStylesheet = UnistylesRegistry.getComputedStylesheet(__uni__stylesheet);
53
- const style = newComputedStylesheet[__uni__key];
54
- const args = _args ?? __uni__args;
55
- const variants = _variants && Object.keys(_variants).length > 0 ? _variants : __uni__variants;
56
- const result = typeof style === 'function'
57
- ? style(...args)
58
- : style;
59
- const { variantsResult } = Object.fromEntries(getVariants({ variantsResult: result }, variants));
60
- const resultWithVariants = deepMergeObjects(result, variantsResult ?? {});
61
- const oldResult = this.resultsMap.get(ref, __uni__key);
62
- // If results are the same do nothing
63
- if (equal(oldResult, resultWithVariants)) {
64
- return;
65
- }
66
- const oldClassNames = this.classNamesMap.get(ref, __uni__key);
67
- // Remove old styles
68
- if (oldResult) {
69
- UnistylesRegistry.remove(oldResult);
70
- }
71
- // Remove old classnames from the ref
72
- oldClassNames?.forEach(className => ref.classList.remove(className));
73
- this.resultsMap.set(ref, __uni__key, resultWithVariants);
74
- const className = UnistylesRegistry.add({
75
- key: __uni__key,
76
- args,
77
- stylesheet: __uni__stylesheet,
78
- value: resultWithVariants,
79
- variants
80
- });
81
- const injectedClassNames = resultWithVariants?._web?._classNames ?? [];
82
- const newClassNames = injectedClassNames.concat(className);
83
- const dependencies = extractUnistyleDependencies(resultWithVariants);
84
- if (typeof __uni__stylesheet === 'function') {
85
- // Add dependencies from dynamic styles to stylesheet
86
- UnistylesRegistry.addDependenciesToStylesheet(__uni__stylesheet, dependencies);
87
- }
88
- __uni__refs.add(ref);
89
- this.classNamesMap.set(ref, __uni__key, newClassNames);
90
- // Add new classnames to the ref
91
- ref.classList.add(...newClassNames);
92
- });
93
- };
94
- remove = () => { };
95
- }
96
- export const UnistylesShadowRegistry = new UnistylesShadowRegistryBuilder();
package/src/web/state.js DELETED
@@ -1,68 +0,0 @@
1
- import { UnistylesRuntime } from './runtime';
2
- import { isServer, schemeToTheme } from './utils';
3
- import { UnistylesListener } from './listener';
4
- import { UnistyleDependency } from '../specs/NativePlatform';
5
- class UnistylesStateBuilder {
6
- themes = new Map();
7
- themeName;
8
- matchingBreakpoints = new Map();
9
- get breakpoint() {
10
- const [currentBreakpoint] = Array.from(this.matchingBreakpoints)
11
- .reverse()
12
- .find(([_key, value]) => value) ?? [];
13
- return currentBreakpoint;
14
- }
15
- breakpoints;
16
- hasAdaptiveThemes = false;
17
- init = (config) => {
18
- this.initThemes(config.themes);
19
- this.initBreakpoints(config.breakpoints);
20
- this.initSettings(config.settings);
21
- if (isServer()) {
22
- return;
23
- }
24
- UnistylesListener.initListeners();
25
- };
26
- initThemes = (themes = {}) => {
27
- Object.entries(themes).forEach(([themeName, theme]) => {
28
- this.themes.set(themeName, theme);
29
- });
30
- };
31
- initSettings = (settings) => {
32
- this.hasAdaptiveThemes = settings?.adaptiveThemes ?? false;
33
- const themeNames = Array.from(this.themes.keys());
34
- // Single theme + no settings
35
- if (!settings?.adaptiveThemes && !settings?.initialTheme && themeNames.length === 1) {
36
- this.themeName = themeNames[0];
37
- return;
38
- }
39
- // No settings
40
- if (!settings?.adaptiveThemes && !settings?.initialTheme) {
41
- throw new Error('🦄 You need to specify either "initialTheme" or "adaptiveThemes" in your config');
42
- }
43
- // Adaptive themes
44
- if (settings.adaptiveThemes) {
45
- this.themeName = schemeToTheme(UnistylesRuntime.colorScheme);
46
- return;
47
- }
48
- // Initial theme
49
- this.themeName = typeof settings.initialTheme === 'function' ? settings.initialTheme() : settings.initialTheme;
50
- };
51
- initBreakpoints = (breakpoints = {}) => {
52
- this.breakpoints = breakpoints;
53
- Object.entries(breakpoints)
54
- .sort(([, a], [, b]) => a - b)
55
- .forEach(([breakpoint, value]) => {
56
- if (isServer()) {
57
- return;
58
- }
59
- const mediaQuery = window.matchMedia(`(min-width: ${value}px)`);
60
- this.matchingBreakpoints.set(breakpoint, mediaQuery.matches);
61
- mediaQuery.addEventListener('change', event => {
62
- this.matchingBreakpoints.set(breakpoint, event.matches);
63
- UnistylesListener.emitChange(UnistyleDependency.Breakpoints);
64
- });
65
- });
66
- };
67
- }
68
- export const UnistylesState = new UnistylesStateBuilder();
@@ -1,79 +0,0 @@
1
- export const reduceObject = (obj, reducer) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, reducer(value, key)]));
2
- export const keyInObject = (obj, key) => key in obj;
3
- export const isServer = () => typeof window === 'undefined';
4
- /**
5
- * Deeply merges properties of passed object
6
- */
7
- export const deepMergeObjects = (...sources) => {
8
- const target = {};
9
- sources.forEach(source => {
10
- Object.keys(source).forEach(key => {
11
- const sourceValue = source[key];
12
- const targetValue = target[key];
13
- if (Object(sourceValue) == sourceValue && Object(targetValue) === targetValue) {
14
- // @ts-expect-error - can't assign to generic
15
- target[key] = deepMergeObjects(targetValue, sourceValue);
16
- return;
17
- }
18
- // @ts-expect-error - can't assign to generic
19
- target[key] = sourceValue;
20
- });
21
- });
22
- return target;
23
- };
24
- export const warn = (message) => console.warn(`🦄 [react-native-unistyles] ${message}`);
25
- export const equal = (a, b) => {
26
- if (Object.is(a, b)) {
27
- return true;
28
- }
29
- if (typeof a !== 'object'
30
- || a === null
31
- || typeof b !== 'object'
32
- || b === null) {
33
- return false;
34
- }
35
- const keysA = Object.keys(a);
36
- if (keysA.length !== Object.keys(b).length) {
37
- return false;
38
- }
39
- return keysA.every(key => Object.is(a[key], b[key]) && Object.prototype.hasOwnProperty.call(b, key));
40
- };
41
- export const generateHash = (value) => {
42
- const str = JSON.stringify(value);
43
- let hasher = 5381;
44
- let length = str.length;
45
- while (length--)
46
- hasher = (hasher * 33) ^ str.charCodeAt(length);
47
- return `unistyles-${(hasher >>> 0).toString(36)}`;
48
- };
49
- export const createDoubleMap = () => {
50
- const map = new Map();
51
- return {
52
- get: (key, secondKey) => {
53
- const mapForKey = map.get(key);
54
- if (!mapForKey) {
55
- return undefined;
56
- }
57
- return mapForKey.get(secondKey);
58
- },
59
- set: (key, secondKey, value) => {
60
- const mapForKey = map.get(key) ?? new Map();
61
- map.set(key, mapForKey);
62
- mapForKey.set(secondKey, value);
63
- },
64
- delete: (key, secondKey) => {
65
- const mapForKey = map.get(key);
66
- if (!mapForKey) {
67
- return;
68
- }
69
- mapForKey.delete(secondKey);
70
- },
71
- forEach: (callback) => {
72
- map.forEach((mapForKey, key) => {
73
- mapForKey.forEach((value, secondKey) => {
74
- callback(key, secondKey, value);
75
- });
76
- });
77
- }
78
- };
79
- };
@@ -1,2 +0,0 @@
1
- export * from './common';
2
- export * from './unistyle';
@@ -1,72 +0,0 @@
1
- import { ColorScheme, Orientation } from '../../specs/types';
2
- import { isUnistylesMq, parseMq } from '../mq';
3
- import { UnistylesState } from '../state';
4
- import { keyInObject, reduceObject } from './common';
5
- export const schemeToTheme = (scheme) => {
6
- switch (scheme) {
7
- case ColorScheme.Dark:
8
- return 'dark';
9
- case ColorScheme.Light:
10
- default:
11
- return 'light';
12
- }
13
- };
14
- export const hexToRGBA = (hex, opacity) => hex
15
- .replace('#', '')
16
- .split(/(?=(?:..)*$)/)
17
- .map(x => parseInt(x, 16))
18
- .filter(num => !isNaN(num))
19
- .reduce((acc, color) => `${acc}${color},`, 'rgba(')
20
- .concat(`${opacity})`);
21
- export const assignSecrets = (object, secrets) => {
22
- // @ts-expect-error - assign secrets to object
23
- object[`__uni__secrets__${Math.random().toString(16).slice(2)}`] = secrets;
24
- return object;
25
- };
26
- export const extractSecrets = (object) => {
27
- const secrets = Object.entries(object).reduce((acc, [key, value]) => {
28
- if (key.startsWith('__uni__secrets__')) {
29
- acc.push(value);
30
- }
31
- return acc;
32
- }, []);
33
- return secrets;
34
- };
35
- export const getStyles = (values) => {
36
- const returnValue = {};
37
- Object.defineProperties(returnValue, reduceObject(values, value => ({
38
- value,
39
- enumerable: false,
40
- configurable: true
41
- })));
42
- return returnValue;
43
- };
44
- export const isInDocument = (element) => document.body.contains(element);
45
- export const extractMediaQueryValue = (query) => {
46
- const [_, px] = query.match(/(\d+)px/) ?? [];
47
- if (!px) {
48
- return undefined;
49
- }
50
- const value = Number(px);
51
- return isNaN(value) ? undefined : value;
52
- };
53
- export const getMediaQuery = (query) => {
54
- if (Object.values(Orientation).includes(query)) {
55
- return `(orientation: ${query})`;
56
- }
57
- if (isUnistylesMq(query)) {
58
- const { minWidth, maxWidth, minHeight, maxHeight } = parseMq(query);
59
- return [
60
- minWidth ? `(min-width: ${minWidth}px)` : undefined,
61
- maxWidth ? `(max-width: ${maxWidth}px)` : undefined,
62
- minHeight ? `(min-height: ${minHeight}px)` : undefined,
63
- maxHeight ? `(max-height: ${maxHeight}px)` : undefined
64
- ].filter(Boolean).join(' and ');
65
- }
66
- const minWidth = UnistylesState.breakpoints && keyInObject(UnistylesState.breakpoints, query) ? UnistylesState.breakpoints[query] : undefined;
67
- return `(min-width: ${minWidth ?? 0}px)`;
68
- };
69
- export const extractUnistyleDependencies = (value) => {
70
- const dependencies = keyInObject(value, 'uni__dependencies') ? value.uni__dependencies : [];
71
- return dependencies;
72
- };
@@ -1,26 +0,0 @@
1
- import { deepMergeObjects } from '../utils';
2
- const hasVariants = (value) => 'variants' in value[1];
3
- export const getVariants = (styles, selectedVariants) => {
4
- return Object.entries(styles)
5
- .filter(hasVariants)
6
- .filter(([_key, { variants }]) => Object.keys(variants).some(variant => variant in variants))
7
- .map(([key, { variants, compoundVariants = [] }]) => {
8
- const variantStyles = Object.entries(variants).flatMap(([variant, styles]) => {
9
- const selectedVariant = selectedVariants[variant];
10
- const selectedVariantStyles = styles[selectedVariant] ?? styles['default'];
11
- if (!selectedVariantStyles) {
12
- return [];
13
- }
14
- return selectedVariantStyles;
15
- });
16
- const compoundVariantStyles = compoundVariants.flatMap(compoundVariant => {
17
- const { styles, ...conditions } = compoundVariant;
18
- if (Object.entries(conditions).some(([variant, value]) => String(selectedVariants[variant]) !== String(value))) {
19
- return [];
20
- }
21
- return styles;
22
- });
23
- const mergedVariantStyles = deepMergeObjects(...variantStyles, ...compoundVariantStyles);
24
- return [key, mergedVariantStyles];
25
- });
26
- };
@@ -1,2 +0,0 @@
1
- export * from './useVariants';
2
- export * from './getVariants';