react-native-unistyles 2.0.0-alpha.9 → 2.0.0-beta.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. package/android/CMakeLists.txt +28 -0
  2. package/android/build.gradle +40 -0
  3. package/android/src/main/cxx/cpp-adapter.cpp +108 -0
  4. package/android/src/main/java/com/unistyles/UnistylesModule.kt +159 -0
  5. package/android/src/main/java/com/unistyles/UnistylesPackage.kt +18 -0
  6. package/cxx/UnistylesRuntime.cpp +55 -2
  7. package/cxx/UnistylesRuntime.h +15 -9
  8. package/ios/UnistylesModule.mm +16 -6
  9. package/lib/commonjs/common.js +30 -16
  10. package/lib/commonjs/common.js.map +1 -1
  11. package/lib/commonjs/core/UnistyleRegistry.js +65 -3
  12. package/lib/commonjs/core/UnistyleRegistry.js.map +1 -1
  13. package/lib/commonjs/core/Unistyles.js +1 -1
  14. package/lib/commonjs/core/Unistyles.js.map +1 -1
  15. package/lib/commonjs/core/UnistylesModule.web.js +187 -0
  16. package/lib/commonjs/core/UnistylesModule.web.js.map +1 -0
  17. package/lib/commonjs/core/UnistylesRuntime.js +15 -16
  18. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
  19. package/lib/commonjs/createStyleSheet.js +1 -6
  20. package/lib/commonjs/createStyleSheet.js.map +1 -1
  21. package/lib/commonjs/hooks/index.js +7 -0
  22. package/lib/commonjs/hooks/index.js.map +1 -1
  23. package/lib/commonjs/hooks/useUnistyles.js +12 -9
  24. package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
  25. package/lib/commonjs/hooks/useVariants.js +14 -0
  26. package/lib/commonjs/hooks/useVariants.js.map +1 -0
  27. package/lib/commonjs/index.js.map +1 -1
  28. package/lib/commonjs/plugins/index.js +38 -0
  29. package/lib/commonjs/plugins/index.js.map +1 -0
  30. package/lib/commonjs/plugins/normalizeWebStylesPlugin.js +12 -0
  31. package/lib/commonjs/plugins/normalizeWebStylesPlugin.js.map +1 -0
  32. package/lib/commonjs/plugins/normalizer/index.js +32 -0
  33. package/lib/commonjs/plugins/normalizer/index.js.map +1 -0
  34. package/lib/commonjs/plugins/normalizer/module.d.js.map +1 -0
  35. package/lib/commonjs/{utils/normalizeStyles.web.js → plugins/normalizer/normalizeStyle.js} +5 -5
  36. package/lib/commonjs/plugins/normalizer/normalizeStyle.js.map +1 -0
  37. package/lib/commonjs/{utils → plugins/normalizer}/normalizer.js +1 -1
  38. package/lib/commonjs/plugins/normalizer/normalizer.js.map +1 -0
  39. package/lib/commonjs/types/{mq.js → plugin.js} +1 -1
  40. package/lib/commonjs/types/{mq.js.map → plugin.js.map} +1 -1
  41. package/lib/commonjs/types/stylesheet.js +6 -0
  42. package/lib/commonjs/types/stylesheet.js.map +1 -0
  43. package/lib/commonjs/types/variants.js +2 -0
  44. package/lib/commonjs/{utils/module.d.js.map → types/variants.js.map} +1 -1
  45. package/lib/commonjs/useStyles.js +10 -20
  46. package/lib/commonjs/useStyles.js.map +1 -1
  47. package/lib/commonjs/utils/breakpoints.js +13 -59
  48. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  49. package/lib/commonjs/utils/index.js +13 -51
  50. package/lib/commonjs/utils/index.js.map +1 -1
  51. package/lib/commonjs/utils/mq.js +36 -68
  52. package/lib/commonjs/utils/mq.js.map +1 -1
  53. package/lib/commonjs/utils/mqParser.js +4 -1
  54. package/lib/commonjs/utils/mqParser.js.map +1 -1
  55. package/lib/commonjs/utils/styles.js +35 -20
  56. package/lib/commonjs/utils/styles.js.map +1 -1
  57. package/lib/commonjs/utils/withPlugins.js +15 -0
  58. package/lib/commonjs/utils/withPlugins.js.map +1 -0
  59. package/lib/module/common.js +28 -17
  60. package/lib/module/common.js.map +1 -1
  61. package/lib/module/core/UnistyleRegistry.js +65 -3
  62. package/lib/module/core/UnistyleRegistry.js.map +1 -1
  63. package/lib/module/core/Unistyles.js +2 -2
  64. package/lib/module/core/Unistyles.js.map +1 -1
  65. package/lib/module/core/UnistylesModule.web.js +180 -0
  66. package/lib/module/core/UnistylesModule.web.js.map +1 -0
  67. package/lib/module/core/UnistylesRuntime.js +15 -16
  68. package/lib/module/core/UnistylesRuntime.js.map +1 -1
  69. package/lib/module/createStyleSheet.js +1 -6
  70. package/lib/module/createStyleSheet.js.map +1 -1
  71. package/lib/module/hooks/index.js +1 -0
  72. package/lib/module/hooks/index.js.map +1 -1
  73. package/lib/module/hooks/useUnistyles.js +13 -10
  74. package/lib/module/hooks/useUnistyles.js.map +1 -1
  75. package/lib/module/hooks/useVariants.js +7 -0
  76. package/lib/module/hooks/useVariants.js.map +1 -0
  77. package/lib/module/index.js.map +1 -1
  78. package/lib/module/plugins/index.js +3 -0
  79. package/lib/module/plugins/index.js.map +1 -0
  80. package/lib/module/plugins/normalizeWebStylesPlugin.js +6 -0
  81. package/lib/module/plugins/normalizeWebStylesPlugin.js.map +1 -0
  82. package/lib/module/plugins/normalizer/index.js +3 -0
  83. package/lib/module/plugins/normalizer/index.js.map +1 -0
  84. package/lib/module/plugins/normalizer/module.d.js.map +1 -0
  85. package/lib/module/{utils/normalizeStyles.web.js → plugins/normalizer/normalizeStyle.js} +3 -3
  86. package/lib/module/plugins/normalizer/normalizeStyle.js.map +1 -0
  87. package/lib/module/{utils → plugins/normalizer}/normalizer.js +1 -1
  88. package/lib/module/plugins/normalizer/normalizer.js.map +1 -0
  89. package/lib/module/types/plugin.js +2 -0
  90. package/lib/module/types/{mq.js.map → plugin.js.map} +1 -1
  91. package/lib/module/types/stylesheet.js +2 -0
  92. package/lib/module/types/stylesheet.js.map +1 -0
  93. package/lib/module/types/variants.js +2 -0
  94. package/lib/module/{utils/module.d.js.map → types/variants.js.map} +1 -1
  95. package/lib/module/useStyles.js +12 -22
  96. package/lib/module/useStyles.js.map +1 -1
  97. package/lib/module/utils/breakpoints.js +12 -56
  98. package/lib/module/utils/breakpoints.js.map +1 -1
  99. package/lib/module/utils/index.js +4 -6
  100. package/lib/module/utils/index.js.map +1 -1
  101. package/lib/module/utils/mq.js +35 -67
  102. package/lib/module/utils/mq.js.map +1 -1
  103. package/lib/module/utils/mqParser.js +3 -3
  104. package/lib/module/utils/mqParser.js.map +1 -1
  105. package/lib/module/utils/styles.js +35 -20
  106. package/lib/module/utils/styles.js.map +1 -1
  107. package/lib/module/utils/withPlugins.js +8 -0
  108. package/lib/module/utils/withPlugins.js.map +1 -0
  109. package/lib/typescript/src/common.d.ts +23 -12
  110. package/lib/typescript/src/common.d.ts.map +1 -1
  111. package/lib/typescript/src/core/UnistyleRegistry.d.ts +45 -4
  112. package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -1
  113. package/lib/typescript/src/core/UnistylesModule.web.d.ts +19 -0
  114. package/lib/typescript/src/core/UnistylesModule.web.d.ts.map +1 -0
  115. package/lib/typescript/src/core/UnistylesRuntime.d.ts +8 -9
  116. package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -1
  117. package/lib/typescript/src/core/index.d.ts +1 -0
  118. package/lib/typescript/src/core/index.d.ts.map +1 -1
  119. package/lib/typescript/src/createStyleSheet.d.ts +2 -2
  120. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -1
  121. package/lib/typescript/src/global.d.ts.map +1 -1
  122. package/lib/typescript/src/hooks/index.d.ts +1 -0
  123. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  124. package/lib/typescript/src/hooks/useUnistyles.d.ts +2 -1
  125. package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
  126. package/lib/typescript/src/hooks/useVariants.d.ts +3 -0
  127. package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -0
  128. package/lib/typescript/src/index.d.ts +51 -5
  129. package/lib/typescript/src/index.d.ts.map +1 -1
  130. package/lib/typescript/src/plugins/index.d.ts +3 -0
  131. package/lib/typescript/src/plugins/index.d.ts.map +1 -0
  132. package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts +3 -0
  133. package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts.map +1 -0
  134. package/lib/typescript/src/plugins/normalizer/index.d.ts +3 -0
  135. package/lib/typescript/src/plugins/normalizer/index.d.ts.map +1 -0
  136. package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts +3 -0
  137. package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts.map +1 -0
  138. package/lib/typescript/src/{utils → plugins/normalizer}/normalizer.d.ts +1 -1
  139. package/lib/typescript/src/plugins/normalizer/normalizer.d.ts.map +1 -0
  140. package/lib/typescript/src/types/breakpoints.d.ts +25 -12
  141. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  142. package/lib/typescript/src/types/core.d.ts +6 -27
  143. package/lib/typescript/src/types/core.d.ts.map +1 -1
  144. package/lib/typescript/src/types/index.d.ts +4 -2
  145. package/lib/typescript/src/types/index.d.ts.map +1 -1
  146. package/lib/typescript/src/types/plugin.d.ts +7 -0
  147. package/lib/typescript/src/types/plugin.d.ts.map +1 -0
  148. package/lib/typescript/src/types/stylesheet.d.ts +40 -0
  149. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -0
  150. package/lib/typescript/src/types/unistyles.d.ts +16 -11
  151. package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
  152. package/lib/typescript/src/types/variants.d.ts +14 -0
  153. package/lib/typescript/src/types/variants.d.ts.map +1 -0
  154. package/lib/typescript/src/useStyles.d.ts +3 -3
  155. package/lib/typescript/src/useStyles.d.ts.map +1 -1
  156. package/lib/typescript/src/utils/breakpoints.d.ts +2 -5
  157. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  158. package/lib/typescript/src/utils/index.d.ts +4 -6
  159. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  160. package/lib/typescript/src/utils/mq.d.ts +15 -14
  161. package/lib/typescript/src/utils/mq.d.ts.map +1 -1
  162. package/lib/typescript/src/utils/mqParser.d.ts +14 -2
  163. package/lib/typescript/src/utils/mqParser.d.ts.map +1 -1
  164. package/lib/typescript/src/utils/styles.d.ts +3 -4
  165. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  166. package/lib/typescript/src/utils/withPlugins.d.ts +3 -0
  167. package/lib/typescript/src/utils/withPlugins.d.ts.map +1 -0
  168. package/package.json +19 -16
  169. package/src/__tests__/mocks.ts +24 -0
  170. package/src/common.ts +30 -14
  171. package/src/core/UnistyleRegistry.ts +66 -4
  172. package/src/core/Unistyles.ts +2 -2
  173. package/src/core/UnistylesModule.web.ts +214 -0
  174. package/src/core/UnistylesRuntime.ts +18 -20
  175. package/src/core/index.ts +1 -0
  176. package/src/createStyleSheet.ts +2 -8
  177. package/src/global.ts +1 -0
  178. package/src/hooks/index.ts +1 -0
  179. package/src/hooks/useUnistyles.ts +12 -10
  180. package/src/hooks/useVariants.ts +10 -0
  181. package/src/index.ts +3 -1
  182. package/src/plugins/index.ts +2 -0
  183. package/src/plugins/normalizeWebStylesPlugin.ts +7 -0
  184. package/src/plugins/normalizer/index.ts +2 -0
  185. package/src/{utils/normalizeStyles.web.ts → plugins/normalizer/normalizeStyle.ts} +3 -3
  186. package/src/{utils → plugins/normalizer}/normalizer.ts +3 -3
  187. package/src/types/breakpoints.ts +57 -25
  188. package/src/types/core.ts +9 -43
  189. package/src/types/index.ts +11 -2
  190. package/src/types/plugin.ts +7 -0
  191. package/src/types/stylesheet.ts +49 -0
  192. package/src/types/unistyles.ts +18 -13
  193. package/src/types/variants.ts +19 -0
  194. package/src/useStyles.ts +18 -25
  195. package/src/utils/breakpoints.ts +11 -67
  196. package/src/utils/index.ts +4 -6
  197. package/src/utils/mq.ts +31 -84
  198. package/src/utils/mqParser.ts +5 -5
  199. package/src/utils/styles.ts +44 -50
  200. package/src/utils/withPlugins.ts +13 -0
  201. package/lib/commonjs/utils/common.js +0 -25
  202. package/lib/commonjs/utils/common.js.map +0 -1
  203. package/lib/commonjs/utils/normalizeStyles.js +0 -10
  204. package/lib/commonjs/utils/normalizeStyles.js.map +0 -1
  205. package/lib/commonjs/utils/normalizeStyles.web.js.map +0 -1
  206. package/lib/commonjs/utils/normalizer.js.map +0 -1
  207. package/lib/module/types/mq.js +0 -2
  208. package/lib/module/utils/common.js +0 -17
  209. package/lib/module/utils/common.js.map +0 -1
  210. package/lib/module/utils/normalizeStyles.js +0 -3
  211. package/lib/module/utils/normalizeStyles.js.map +0 -1
  212. package/lib/module/utils/normalizeStyles.web.js.map +0 -1
  213. package/lib/module/utils/normalizer.js.map +0 -1
  214. package/lib/typescript/src/types/mq.d.ts +0 -3
  215. package/lib/typescript/src/types/mq.d.ts.map +0 -1
  216. package/lib/typescript/src/utils/common.d.ts +0 -12
  217. package/lib/typescript/src/utils/common.d.ts.map +0 -1
  218. package/lib/typescript/src/utils/normalizeStyles.d.ts +0 -2
  219. package/lib/typescript/src/utils/normalizeStyles.d.ts.map +0 -1
  220. package/lib/typescript/src/utils/normalizeStyles.web.d.ts +0 -5
  221. package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +0 -1
  222. package/lib/typescript/src/utils/normalizer.d.ts.map +0 -1
  223. package/src/types/mq.ts +0 -3
  224. package/src/utils/common.ts +0 -20
  225. package/src/utils/normalizeStyles.ts +0 -2
  226. /package/lib/commonjs/{utils → plugins/normalizer}/module.d.js +0 -0
  227. /package/lib/module/{utils → plugins/normalizer}/module.d.js +0 -0
  228. /package/src/{utils → plugins/normalizer}/module.d.ts +0 -0
@@ -0,0 +1,214 @@
1
+ import { NativeEventEmitter, NativeModules } from 'react-native'
2
+ import type { UnistylesThemes, UnistylesBreakpoints } from 'react-native-unistyles'
3
+ import type { ColorSchemeName } from '../types'
4
+ import { normalizeWebStylesPlugin } from '../plugins'
5
+
6
+ export class UnistylesBridgeWeb {
7
+ #timerRef?: ReturnType<typeof setTimeout> = undefined
8
+ #hasAdaptiveThemes: boolean = false
9
+ #supportsAutomaticColorScheme = false
10
+ #screenWidth: number = window.innerWidth
11
+ #screenHeight: number = window.innerHeight
12
+ #themes: Array<keyof UnistylesThemes> = []
13
+ #colorScheme: ColorSchemeName = this.getPreferredColorScheme()
14
+ #themeName: keyof UnistylesThemes = '' as keyof UnistylesThemes
15
+ #enabledPlugins: Array<string> = [normalizeWebStylesPlugin.name]
16
+ #unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
17
+ #sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, number]> = []
18
+ #breakpoint: keyof UnistylesBreakpoints = '' as keyof UnistylesBreakpoints
19
+
20
+ constructor() {
21
+ this.setupListeners()
22
+ }
23
+
24
+ public install() {
25
+ // @ts-ignore
26
+ window.__UNISTYLES__ = new Proxy({}, {
27
+ get: (_target, prop) => {
28
+ switch (prop) {
29
+ case 'themeName':
30
+ return this.getTheme()
31
+ case 'screenWidth':
32
+ return this.#screenWidth
33
+ case 'screenHeight':
34
+ return this.#screenHeight
35
+ case 'breakpoint':
36
+ return this.#breakpoint || undefined
37
+ case 'hasAdaptiveThemes':
38
+ return this.#hasAdaptiveThemes
39
+ case 'sortedBreakpointPairs':
40
+ return this.#sortedBreakpointPairs
41
+ case 'enabledPlugins':
42
+ return this.#enabledPlugins
43
+ case 'colorScheme':
44
+ return this.#colorScheme
45
+ case 'useTheme':
46
+ return (themeName: keyof UnistylesThemes) => this.useTheme(themeName)
47
+ case 'useBreakpoints':
48
+ return (breakpoints: UnistylesBreakpoints) => this.useBreakpoints(breakpoints)
49
+ case 'useAdaptiveThemes':
50
+ return (enable: boolean) => this.useAdaptiveThemes(enable)
51
+ case 'addPlugin':
52
+ return (pluginName: string, notify: boolean) => this.addPlugin(pluginName, notify)
53
+ case 'removePlugin':
54
+ return (pluginName: string) => this.removePlugin(pluginName)
55
+ default:
56
+ return Reflect.get(this, prop)
57
+ }
58
+ },
59
+ set: (target, prop, newValue, receiver) => {
60
+ switch (prop) {
61
+ case 'themes': {
62
+ this.#themes = newValue
63
+ this.#supportsAutomaticColorScheme = newValue.includes('light') && newValue.includes('dark')
64
+
65
+ return true
66
+ }
67
+ case 'themeName': {
68
+ this.#themeName = newValue as keyof UnistylesThemes
69
+ this.emitThemeChange()
70
+
71
+ return true
72
+ }
73
+ default:
74
+ return Reflect.set(target, prop, newValue, receiver)
75
+ }
76
+ }
77
+ })
78
+
79
+ return true
80
+ }
81
+
82
+ private useTheme(themeName: keyof UnistylesThemes) {
83
+ this.#themeName = themeName
84
+ this.emitThemeChange()
85
+ }
86
+
87
+ private useBreakpoints(breakpoints: UnistylesBreakpoints) {
88
+ this.#sortedBreakpointPairs = Object
89
+ .entries(breakpoints)
90
+ .sort(([, a], [, b]) => (a ?? 0) - (b ?? 0)) as Array<[keyof UnistylesBreakpoints, number]>
91
+ this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth)
92
+ }
93
+
94
+ private useAdaptiveThemes(enable: boolean) {
95
+ this.#hasAdaptiveThemes = enable
96
+
97
+ if (!this.#hasAdaptiveThemes || !this.#supportsAutomaticColorScheme) {
98
+ return
99
+ }
100
+
101
+ if (this.#themeName !== this.#colorScheme) {
102
+ this.#themeName = this.#colorScheme as keyof UnistylesThemes
103
+ this.emitThemeChange()
104
+ }
105
+ }
106
+
107
+ private addPlugin(pluginName: string, notify: boolean) {
108
+ this.#enabledPlugins = [pluginName].concat(this.#enabledPlugins)
109
+
110
+ if (notify) {
111
+ this.emitPluginChange()
112
+ }
113
+ }
114
+
115
+ private removePlugin(pluginName: string) {
116
+ this.#enabledPlugins = this.#enabledPlugins.filter(name => name !== pluginName)
117
+ this.emitPluginChange()
118
+ }
119
+
120
+ private getTheme(): keyof UnistylesThemes {
121
+
122
+ if (this.#themes.length === 1) {
123
+ return this.#themes.at(0) as keyof UnistylesThemes
124
+ }
125
+
126
+ return this.#themeName
127
+ }
128
+
129
+ private setupListeners() {
130
+ window.addEventListener('resize', () => {
131
+ clearTimeout(this.#timerRef)
132
+
133
+ this.#timerRef = setTimeout(() => {
134
+ this.#screenWidth = window.innerWidth
135
+ this.#screenHeight = window.innerHeight
136
+ this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth)
137
+
138
+ this.emitLayoutChange()
139
+ }, 100)
140
+ })
141
+
142
+ window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
143
+ this.#colorScheme = event.matches
144
+ ? 'dark'
145
+ : 'light'
146
+
147
+ if (!this.#supportsAutomaticColorScheme || !this.#hasAdaptiveThemes) {
148
+ return
149
+ }
150
+
151
+ if (this.#colorScheme !== this.#themeName) {
152
+ this.#themeName = this.#colorScheme as keyof UnistylesThemes
153
+ this.emitThemeChange()
154
+ }
155
+ })
156
+ }
157
+
158
+ private getBreakpointFromScreenWidth(width: number): keyof UnistylesBreakpoints {
159
+ const breakpoint = this.#sortedBreakpointPairs
160
+ .find(([, value], index, otherBreakpoints) => {
161
+ const minVal = value
162
+ const maxVal = otherBreakpoints[index + 1]?.[1]
163
+
164
+ if (!maxVal) {
165
+ return true
166
+ }
167
+
168
+ return width >= minVal && width < maxVal
169
+ })
170
+
171
+ return breakpoint?.at(0) as keyof UnistylesBreakpoints
172
+ }
173
+
174
+ private getPreferredColorScheme() {
175
+ if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
176
+ return 'dark'
177
+ }
178
+
179
+ return 'light'
180
+ }
181
+
182
+ private emitPluginChange() {
183
+ this.#unistylesEvents.emit('__unistylesOnChange', {
184
+ type: 'plugin'
185
+ })
186
+ }
187
+
188
+ private emitThemeChange() {
189
+ this.#unistylesEvents.emit('__unistylesOnChange', {
190
+ type: 'theme',
191
+ payload: {
192
+ themeName: this.#themeName
193
+ }
194
+ })
195
+ }
196
+
197
+ private emitLayoutChange() {
198
+ this.#unistylesEvents.emit('__unistylesOnChange', {
199
+ type: 'layout',
200
+ payload: {
201
+ breakpoint: this.#breakpoint,
202
+ orientation: this.#screenWidth > this.#screenHeight
203
+ ? 'landscape'
204
+ : 'portrait',
205
+ screen: {
206
+ width: this.#screenWidth,
207
+ height: this.#screenHeight
208
+ }
209
+ }
210
+ })
211
+ }
212
+ }
213
+
214
+ export const UnistylesModule = new UnistylesBridgeWeb()
@@ -1,10 +1,10 @@
1
1
  import { ScreenOrientation, UnistylesError } from '../common'
2
- import type { UnistylesBridge } from '../types'
2
+ import type { UnistylesBridge, UnistylesPlugin } from '../types'
3
3
  import type { UnistylesThemes } from '../global'
4
4
  import type { UnistyleRegistry } from './UnistyleRegistry'
5
5
 
6
6
  export class UnistylesRuntime {
7
- constructor(private unistylesBridge: UnistylesBridge, private registry: UnistyleRegistry) {}
7
+ constructor(private unistylesBridge: UnistylesBridge, private unistylesRegistry: UnistyleRegistry) {}
8
8
 
9
9
  public get colorScheme() {
10
10
  return this.unistylesBridge.colorScheme
@@ -14,10 +14,6 @@ export class UnistylesRuntime {
14
14
  return this.unistylesBridge.hasAdaptiveThemes
15
15
  }
16
16
 
17
- public get sortedBreakpoints() {
18
- return this.registry.sortedBreakpointPairs
19
- }
20
-
21
17
  public get themeName() {
22
18
  return this.unistylesBridge.themeName
23
19
  }
@@ -26,6 +22,10 @@ export class UnistylesRuntime {
26
22
  return this.unistylesBridge.breakpoint
27
23
  }
28
24
 
25
+ public get enabledPlugins() {
26
+ return this.unistylesBridge.enabledPlugins
27
+ }
28
+
29
29
  public get screen() {
30
30
  return {
31
31
  width: this.unistylesBridge.screenWidth,
@@ -44,7 +44,11 @@ export class UnistylesRuntime {
44
44
  }
45
45
 
46
46
  public setTheme = (name: keyof UnistylesThemes) => {
47
- if (this.hasTheme(name)) {
47
+ if (name === this.themeName) {
48
+ return
49
+ }
50
+
51
+ if (this.unistylesRegistry.hasTheme(name)) {
48
52
  this.unistylesBridge.useTheme(name)
49
53
 
50
54
  return true
@@ -53,21 +57,15 @@ export class UnistylesRuntime {
53
57
  throw new Error(UnistylesError.ThemeNotRegistered)
54
58
  }
55
59
 
56
- public getTheme = (forName: keyof UnistylesThemes) => {
57
- if (this.registry.themeNames.length === 0) {
58
- return {} as UnistylesThemes[keyof UnistylesThemes]
59
- }
60
-
61
- if (!this.hasTheme(forName)) {
62
- throw new Error(UnistylesError.ThemeNotFound)
63
- }
64
-
65
- return this.registry.themes[forName]
66
- }
67
-
68
60
  public setAdaptiveThemes = (enable: boolean) => {
69
61
  this.unistylesBridge.useAdaptiveThemes(enable)
70
62
  }
71
63
 
72
- private hasTheme = (name: keyof UnistylesThemes) => name in this.registry.themes
64
+ public addPlugin = (plugin: UnistylesPlugin) => {
65
+ this.unistylesRegistry.addPlugin(plugin)
66
+ }
67
+
68
+ public removePlugin = (plugin: UnistylesPlugin) => {
69
+ this.unistylesRegistry.removePlugin(plugin)
70
+ }
73
71
  }
package/src/core/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export { unistyles } from './Unistyles'
2
+ export type { UnistylesRuntime } from './UnistylesRuntime'
@@ -1,9 +1,3 @@
1
- import type { CustomNamedStyles, UnistylesTheme } from './types'
1
+ import type { StyleSheetWithSuperPowers } from './types'
2
2
 
3
- export const createStyleSheet = <S, X>(styles: S | CustomNamedStyles<S> | X | ((theme: UnistylesTheme) => X | CustomNamedStyles<X>)): S | X => {
4
- if (typeof styles === 'function') {
5
- return styles as X
6
- }
7
-
8
- return styles as S
9
- }
3
+ export const createStyleSheet = <S extends StyleSheetWithSuperPowers>(stylesheet: S): S => stylesheet
package/src/global.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export interface UnistylesThemes {}
2
2
  export interface UnistylesBreakpoints {
3
+ // these breakpoints are only available on mobile, when you didn't specify any breakpoints
3
4
  landscape?: number,
4
5
  portrait?: number,
5
6
  }
@@ -1,2 +1,3 @@
1
1
  export { useInitialTheme } from './useInitialTheme'
2
2
  export { useUnistyles } from './useUnistyles'
3
+ export { useVariants } from './useVariants'
@@ -1,13 +1,14 @@
1
1
  import { NativeEventEmitter, NativeModules } from 'react-native'
2
2
  import { useEffect, useState } from 'react'
3
3
  import { unistyles } from '../core'
4
- import { CxxUnistylesEventTypes } from '../common'
4
+ import { UnistylesEventType } from '../common'
5
5
  import type { UnistylesEvents, UnistylesMobileLayoutEvent, UnistylesThemeEvent } from '../types'
6
6
 
7
7
  const unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
8
8
 
9
9
  export const useUnistyles = () => {
10
- const [theme, setTheme] = useState(unistyles.runtime.getTheme(unistyles.runtime.themeName))
10
+ const [plugins, setPlugins] = useState(unistyles.runtime.enabledPlugins)
11
+ const [theme, setTheme] = useState(unistyles.registry.getTheme(unistyles.runtime.themeName))
11
12
  const [layout, setLayout] = useState({
12
13
  breakpoint: unistyles.runtime.breakpoint,
13
14
  orientation: unistyles.runtime.orientation,
@@ -19,26 +20,26 @@ export const useUnistyles = () => {
19
20
 
20
21
  useEffect(() => {
21
22
  const subscription = unistylesEvents.addListener(
22
- 'onChange',
23
+ '__unistylesOnChange',
23
24
  (event: UnistylesEvents) => {
24
25
  switch (event.type) {
25
- case CxxUnistylesEventTypes.Theme: {
26
+ case UnistylesEventType.Theme: {
26
27
  const themeEvent = event as UnistylesThemeEvent
27
28
 
28
- return setTheme(unistyles.runtime.getTheme(themeEvent.payload.themeName))
29
+ return setTheme(unistyles.registry.getTheme(themeEvent.payload.themeName))
29
30
  }
30
- case CxxUnistylesEventTypes.Layout: {
31
+ case UnistylesEventType.Layout: {
31
32
  const layoutEvent = event as UnistylesMobileLayoutEvent
32
33
 
33
34
  return setLayout({
34
35
  breakpoint: layoutEvent.payload.breakpoint,
35
36
  orientation: layoutEvent.payload.orientation,
36
- screenSize: {
37
- width: layoutEvent.payload.screen.width,
38
- height: layoutEvent.payload.screen.height
39
- }
37
+ screenSize: layoutEvent.payload.screen
40
38
  })
41
39
  }
40
+ case UnistylesEventType.Plugin: {
41
+ return setPlugins(unistyles.runtime.enabledPlugins)
42
+ }
42
43
  default:
43
44
  return
44
45
  }
@@ -49,6 +50,7 @@ export const useUnistyles = () => {
49
50
  }, [])
50
51
 
51
52
  return {
53
+ plugins,
52
54
  theme,
53
55
  layout
54
56
  }
@@ -0,0 +1,10 @@
1
+ import { useRef } from 'react'
2
+ import type { Optional } from '../types'
3
+
4
+ export const useVariants = (variantsMap?: Record<string, Optional<string>>) => {
5
+ const variantsRef = useRef<Optional<Record<string, Optional<string>>>>(variantsMap)
6
+
7
+ variantsRef.current = variantsMap
8
+
9
+ return variantsRef.current
10
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { unistyles } from './core'
2
2
  import { mq } from './utils'
3
3
  import { useInitialTheme } from './hooks'
4
+ import type { UnistylesPlugin } from './types'
4
5
  import type { UnistylesThemes, UnistylesBreakpoints } from './global'
5
6
  import { ScreenOrientation } from './common'
6
7
  import { useStyles } from './useStyles'
@@ -26,5 +27,6 @@ export {
26
27
 
27
28
  export type {
28
29
  UnistylesThemes,
29
- UnistylesBreakpoints
30
+ UnistylesBreakpoints,
31
+ UnistylesPlugin
30
32
  }
@@ -0,0 +1,2 @@
1
+ export { normalizeWebStylesPlugin } from './normalizeWebStylesPlugin'
2
+ export { preprocessor, normalizeNumericValue, normalizeColor, normalizeStyle } from './normalizer'
@@ -0,0 +1,7 @@
1
+ import type { UnistylesPlugin } from '../types'
2
+ import { normalizeStyle } from './normalizer'
3
+
4
+ export const normalizeWebStylesPlugin: UnistylesPlugin = {
5
+ name: '__unistylesNormalizeWebStyles',
6
+ onParsedStyle: (_key, styles) => normalizeStyle(styles)
7
+ }
@@ -0,0 +1,2 @@
1
+ export { normalizeStyle } from './normalizeStyle'
2
+ export { preprocessor, normalizeNumericValue, normalizeColor } from './normalizer'
@@ -1,6 +1,6 @@
1
- import { warn } from './common'
2
1
  import { preprocessor } from './normalizer'
3
- import type { NormalizedBoxShadow, NormalizedTextShadow, BoxShadow, TextShadow, Transforms } from '../types'
2
+ import { warn } from '../../common'
3
+ import type { NormalizedBoxShadow, NormalizedTextShadow, BoxShadow, TextShadow, RNStyle } from '../../types'
4
4
 
5
5
  const normalizeBoxShadow = <T extends BoxShadow>(style: T): NormalizedBoxShadow => {
6
6
  const requiredBoxShadowProperties = [
@@ -55,7 +55,7 @@ const normalizeTextShadow = <T extends TextShadow>(style: T): NormalizedTextShad
55
55
  }
56
56
  }
57
57
 
58
- export const normalizeStyles = <T extends BoxShadow | TextShadow | { transform: Transforms }>(style: T): T => {
58
+ export const normalizeStyle = <T extends RNStyle>(style: T): T => {
59
59
  const normalizedTransform = ('transform' in style && Array.isArray(style.transform))
60
60
  ? { transform: preprocessor.createTransformValue(style.transform) }
61
61
  : {}
@@ -1,7 +1,7 @@
1
1
  // based on react-native-web normalizer
2
2
  // https://github.com/necolas/react-native-web
3
3
  import normalizeColors from '@react-native/normalize-colors'
4
- import type { TextShadow, Transforms, BoxShadow } from '../types'
4
+ import type { TextShadow, Transforms, BoxShadow, Nullable } from '../../types'
5
5
 
6
6
  type Preprocessor = {
7
7
  createTextShadowValue(style: TextShadow): string,
@@ -15,9 +15,9 @@ export const normalizeColor = (color: string, opacity: number = 1) => {
15
15
  return color
16
16
  }
17
17
 
18
- const integer = normalizeColors(color) as number | null
18
+ const integer = normalizeColors(color) as Nullable<number>
19
19
 
20
- // If the colour is an unknown format, the return value is null
20
+ // If the color is an unknown format, the return value is null
21
21
  if (integer === null) {
22
22
  return color
23
23
  }
@@ -1,29 +1,61 @@
1
- import type { OpaqueColorValue } from 'react-native'
2
- import type { UnistylesBreakpoints } from '../global'
3
- import type { MediaQuery } from './mq'
4
-
5
- type WithEmptyObject<V> = keyof V extends never ? {} : V
6
-
7
- type ExtractBreakpoints<T> = T extends Partial<Record<keyof UnistylesBreakpoints & string, infer V>>
8
- ? WithEmptyObject<V>
9
- : T extends (...args: infer A) => infer R
10
- ? (...args: A) => ExtractBreakpoints<R>
11
- : {
12
- [K in keyof T]: T[K] extends (...args: infer A) => infer R
13
- ? (...args: A) => ExtractBreakpoints<R>
14
- : T[K] extends object
15
- ? ExtractBreakpoints<T[K]>
16
- : T[K]
17
- }
1
+ import type { ColorValue, OpaqueColorValue } from 'react-native'
2
+ import type { UnistylesTheme } from '../types'
3
+ import type { BreakpointsOrMediaQueries, ToDeepUnistyles } from './stylesheet'
4
+ import type { TransformStyles } from './core'
5
+
6
+ type ExtractTransformArray<T> = T extends object
7
+ ? { [K in keyof T]: ExtractBreakpoints<T[K]> }
8
+ : never
9
+
10
+ type ExtractBreakpoints<T> = T extends object
11
+ ? keyof T extends BreakpointsOrMediaQueries
12
+ ? T[keyof T]
13
+ : T extends Array<ToDeepUnistyles<TransformStyles>>
14
+ ? Array<ExtractTransformArray<T[number]>>
15
+ : {
16
+ [K in keyof T]: ExtractBreakpoints<T[K]>
17
+ }
18
+ : T
18
19
 
19
- type RemoveKeysWithPrefix<T> = T extends (...args: Array<any>) => infer R
20
- ? (...args: Parameters<T>) => RemoveKeysWithPrefix<R>
20
+ type ParseNestedObject<T> = T extends (...args: infer A) => infer R
21
+ ? (...args: A) => ParseNestedObject<R>
21
22
  : T extends object
22
- ? T extends OpaqueColorValue
23
- ? string
24
- : T extends Record<string, infer _V>
25
- ? { [K in keyof T as K extends MediaQuery ? keyof UnistylesBreakpoints & string : K]: RemoveKeysWithPrefix<T[K]> }
26
- : { [K in keyof T]: RemoveKeysWithPrefix<T[K]> }
23
+ ? T extends { variants: infer R }
24
+ ? ParseVariants<FlattenVariants<R>> & ParseNestedObject<Omit<T, 'variants'>>
25
+ : {
26
+ [K in keyof T]: T[K] extends object
27
+ ? T[K] extends OpaqueColorValue
28
+ ? ColorValue
29
+ : ExtractBreakpoints<T[K]>
30
+ : T[K]
31
+ }
27
32
  : T
28
33
 
29
- export type ReactNativeStyleSheet<T> = ExtractBreakpoints<RemoveKeysWithPrefix<T>>
34
+ type FlattenVariants<T> = T extends object
35
+ ? {
36
+ [K in keyof T]: T[K] extends object
37
+ ? {
38
+ [key in keyof T[K]]: T[K][key] extends object
39
+ ? ParseNestedObject<T[K][key]>
40
+ : never
41
+ }
42
+ : never
43
+ }
44
+ : never
45
+
46
+ type ParseVariants<T> = T extends object
47
+ ? T[keyof T] extends object
48
+ ? UnionToIntersection<ParseVariants<T[keyof T]>>
49
+ : T
50
+ : T
51
+
52
+ type UnionToIntersection<U> =
53
+ (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
54
+
55
+ type ParseStyleKeys<T> = T extends object
56
+ ? { [K in keyof T]: ParseNestedObject<T[K]> }
57
+ : never
58
+
59
+ export type ReactNativeStyleSheet<T> = T extends (theme: UnistylesTheme) => infer R
60
+ ? ParseStyleKeys<R>
61
+ : ParseStyleKeys<T>
package/src/types/core.ts CHANGED
@@ -15,19 +15,13 @@ import type {
15
15
  } from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
16
16
  import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
17
17
  import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
18
- import type { MediaQuery } from './mq'
19
18
 
20
- type ShadowOffset = {
19
+ export type ShadowOffset = {
21
20
  width: number,
22
21
  height: number
23
22
  }
24
23
 
25
- export type ScreenSize = {
26
- width: number,
27
- height: number
28
- }
29
-
30
- type TransformStyles =
24
+ export type TransformStyles =
31
25
  & PerpectiveTransform
32
26
  & RotateTransform
33
27
  & RotateXTransform
@@ -42,41 +36,13 @@ type TransformStyles =
42
36
  & SkewYTransform
43
37
  & MatrixTransform
44
38
 
45
- type UnistyleNested = {
46
- shadowOffset?: DeepUniStyle<ShadowOffset>,
47
- textShadowOffset?: DeepUniStyle<ShadowOffset>,
48
- transform?: Array<DeepUniStyle<TransformStyles>>
49
- }
50
-
51
- type UniStyle<V> = {
52
- [innerKey in keyof UnistylesBreakpoints]?: V
53
- } | {
54
- [innerKey in MediaQuery]: V
55
- }
56
-
57
- type DeepUniStyle<T> = {
58
- [K in keyof T]?: UniStyle<T[K]> | T[K]
59
- }
60
-
61
- // these props are treated differently to nest breakpoints and media queries
62
- type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
63
-
64
- type UnistyleView = DeepUniStyle<Omit<ViewStyle, NestedTypes>>
65
- type UnistyleText = DeepUniStyle<Omit<TextStyle, NestedTypes>>
66
- type UnistyleImage = DeepUniStyle<Omit<ImageStyle, NestedTypes>>
67
-
68
- export type StaticStyles =
69
- | UnistyleView
70
- | UnistyleText
71
- | UnistyleImage
72
- & UnistyleNested
73
-
74
- export type CustomNamedStyles<T> = {
75
- [K in keyof T]: T[K] extends (...args: infer A) => StaticStyles
76
- ? (...args: A) => StaticStyles
77
- : StaticStyles
39
+ export type ScreenSize = {
40
+ width: number,
41
+ height: number
78
42
  }
79
43
 
80
- export type NestedKeys = Array<[keyof UnistylesBreakpoints | MediaQuery, string | number | undefined]>
44
+ export type RNStyle = ViewStyle & TextStyle & ImageStyle
45
+ export type RNValue = ViewStyle[keyof ViewStyle] | TextStyle[keyof TextStyle] | ImageStyle[keyof ImageStyle]
46
+ export type NestedStyle = Record<keyof UnistylesBreakpoints | symbol, RNValue>
47
+ export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | symbol, RNValue]>
81
48
  export type UnistylesTheme = UnistylesThemes[keyof UnistylesThemes]
82
- export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
@@ -1,6 +1,15 @@
1
1
  export * from './normalizer'
2
2
  export * from './unistyles'
3
3
  export type { Optional, Nullable } from './common'
4
- export type { MediaQuery } from './mq'
5
- export type { CustomNamedStyles, NestedKeys, UnistylesTheme, CreateStylesFactory, ScreenSize } from './core'
4
+ export type {
5
+ NestedStylePairs,
6
+ UnistylesTheme,
7
+ ScreenSize,
8
+ NestedStyle,
9
+ RNValue,
10
+ RNStyle
11
+ } from './core'
12
+ export type { StyleSheetWithSuperPowers, StyleSheet, AllAvailableKeys } from './stylesheet'
6
13
  export type { ReactNativeStyleSheet } from './breakpoints'
14
+ export type { ExtractVariantNames } from './variants'
15
+ export type { UnistylesPlugin } from './plugin'
@@ -0,0 +1,7 @@
1
+ import type { RNStyle } from './core'
2
+ import type { UnistylesRuntime } from '../core'
3
+
4
+ export type UnistylesPlugin = {
5
+ name: string,
6
+ onParsedStyle?: (styleKey: string, style: RNStyle, runtime: UnistylesRuntime) => RNStyle
7
+ }