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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) 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 +12 -2
  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/UnistylesRuntime.js +15 -16
  14. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
  15. package/lib/commonjs/createStyleSheet.js +1 -6
  16. package/lib/commonjs/createStyleSheet.js.map +1 -1
  17. package/lib/commonjs/hooks/index.js +7 -0
  18. package/lib/commonjs/hooks/index.js.map +1 -1
  19. package/lib/commonjs/hooks/useUnistyles.js +11 -8
  20. package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
  21. package/lib/commonjs/hooks/useVariants.js +14 -0
  22. package/lib/commonjs/hooks/useVariants.js.map +1 -0
  23. package/lib/commonjs/index.js.map +1 -1
  24. package/lib/commonjs/plugins/index.js +38 -0
  25. package/lib/commonjs/plugins/index.js.map +1 -0
  26. package/lib/commonjs/plugins/normalizeWebStylesPlugin.js +12 -0
  27. package/lib/commonjs/plugins/normalizeWebStylesPlugin.js.map +1 -0
  28. package/lib/commonjs/plugins/normalizer/index.js +32 -0
  29. package/lib/commonjs/plugins/normalizer/index.js.map +1 -0
  30. package/lib/commonjs/plugins/normalizer/module.d.js.map +1 -0
  31. package/lib/commonjs/{utils/normalizeStyles.web.js → plugins/normalizer/normalizeStyle.js} +5 -5
  32. package/lib/commonjs/plugins/normalizer/normalizeStyle.js.map +1 -0
  33. package/lib/commonjs/{utils → plugins/normalizer}/normalizer.js +1 -1
  34. package/lib/commonjs/plugins/normalizer/normalizer.js.map +1 -0
  35. package/lib/commonjs/types/{mq.js → plugin.js} +1 -1
  36. package/lib/commonjs/types/{mq.js.map → plugin.js.map} +1 -1
  37. package/lib/commonjs/types/stylesheet.js +6 -0
  38. package/lib/commonjs/types/stylesheet.js.map +1 -0
  39. package/lib/commonjs/types/variants.js +2 -0
  40. package/lib/commonjs/{utils/module.d.js.map → types/variants.js.map} +1 -1
  41. package/lib/commonjs/useStyles.js +10 -20
  42. package/lib/commonjs/useStyles.js.map +1 -1
  43. package/lib/commonjs/utils/breakpoints.js +13 -59
  44. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  45. package/lib/commonjs/utils/index.js +13 -51
  46. package/lib/commonjs/utils/index.js.map +1 -1
  47. package/lib/commonjs/utils/mq.js +36 -68
  48. package/lib/commonjs/utils/mq.js.map +1 -1
  49. package/lib/commonjs/utils/mqParser.js +4 -1
  50. package/lib/commonjs/utils/mqParser.js.map +1 -1
  51. package/lib/commonjs/utils/styles.js +35 -20
  52. package/lib/commonjs/utils/styles.js.map +1 -1
  53. package/lib/commonjs/utils/withPlugins.js +15 -0
  54. package/lib/commonjs/utils/withPlugins.js.map +1 -0
  55. package/lib/module/common.js +28 -17
  56. package/lib/module/common.js.map +1 -1
  57. package/lib/module/core/UnistyleRegistry.js +65 -3
  58. package/lib/module/core/UnistyleRegistry.js.map +1 -1
  59. package/lib/module/core/UnistylesRuntime.js +15 -16
  60. package/lib/module/core/UnistylesRuntime.js.map +1 -1
  61. package/lib/module/createStyleSheet.js +1 -6
  62. package/lib/module/createStyleSheet.js.map +1 -1
  63. package/lib/module/hooks/index.js +1 -0
  64. package/lib/module/hooks/index.js.map +1 -1
  65. package/lib/module/hooks/useUnistyles.js +12 -9
  66. package/lib/module/hooks/useUnistyles.js.map +1 -1
  67. package/lib/module/hooks/useVariants.js +7 -0
  68. package/lib/module/hooks/useVariants.js.map +1 -0
  69. package/lib/module/index.js.map +1 -1
  70. package/lib/module/plugins/index.js +3 -0
  71. package/lib/module/plugins/index.js.map +1 -0
  72. package/lib/module/plugins/normalizeWebStylesPlugin.js +6 -0
  73. package/lib/module/plugins/normalizeWebStylesPlugin.js.map +1 -0
  74. package/lib/module/plugins/normalizer/index.js +3 -0
  75. package/lib/module/plugins/normalizer/index.js.map +1 -0
  76. package/lib/module/plugins/normalizer/module.d.js.map +1 -0
  77. package/lib/module/{utils/normalizeStyles.web.js → plugins/normalizer/normalizeStyle.js} +3 -3
  78. package/lib/module/plugins/normalizer/normalizeStyle.js.map +1 -0
  79. package/lib/module/{utils → plugins/normalizer}/normalizer.js +1 -1
  80. package/lib/module/plugins/normalizer/normalizer.js.map +1 -0
  81. package/lib/module/types/plugin.js +2 -0
  82. package/lib/module/types/{mq.js.map → plugin.js.map} +1 -1
  83. package/lib/module/types/stylesheet.js +2 -0
  84. package/lib/module/types/stylesheet.js.map +1 -0
  85. package/lib/module/types/variants.js +2 -0
  86. package/lib/module/{utils/module.d.js.map → types/variants.js.map} +1 -1
  87. package/lib/module/useStyles.js +12 -22
  88. package/lib/module/useStyles.js.map +1 -1
  89. package/lib/module/utils/breakpoints.js +12 -56
  90. package/lib/module/utils/breakpoints.js.map +1 -1
  91. package/lib/module/utils/index.js +4 -6
  92. package/lib/module/utils/index.js.map +1 -1
  93. package/lib/module/utils/mq.js +35 -67
  94. package/lib/module/utils/mq.js.map +1 -1
  95. package/lib/module/utils/mqParser.js +3 -3
  96. package/lib/module/utils/mqParser.js.map +1 -1
  97. package/lib/module/utils/styles.js +35 -20
  98. package/lib/module/utils/styles.js.map +1 -1
  99. package/lib/module/utils/withPlugins.js +8 -0
  100. package/lib/module/utils/withPlugins.js.map +1 -0
  101. package/lib/typescript/src/common.d.ts +23 -12
  102. package/lib/typescript/src/common.d.ts.map +1 -1
  103. package/lib/typescript/src/core/UnistyleRegistry.d.ts +45 -4
  104. package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -1
  105. package/lib/typescript/src/core/UnistylesRuntime.d.ts +8 -9
  106. package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -1
  107. package/lib/typescript/src/core/index.d.ts +1 -0
  108. package/lib/typescript/src/core/index.d.ts.map +1 -1
  109. package/lib/typescript/src/createStyleSheet.d.ts +2 -2
  110. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -1
  111. package/lib/typescript/src/global.d.ts.map +1 -1
  112. package/lib/typescript/src/hooks/index.d.ts +1 -0
  113. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  114. package/lib/typescript/src/hooks/useUnistyles.d.ts +2 -1
  115. package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
  116. package/lib/typescript/src/hooks/useVariants.d.ts +3 -0
  117. package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -0
  118. package/lib/typescript/src/index.d.ts +51 -5
  119. package/lib/typescript/src/index.d.ts.map +1 -1
  120. package/lib/typescript/src/plugins/index.d.ts +3 -0
  121. package/lib/typescript/src/plugins/index.d.ts.map +1 -0
  122. package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts +3 -0
  123. package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts.map +1 -0
  124. package/lib/typescript/src/plugins/normalizer/index.d.ts +3 -0
  125. package/lib/typescript/src/plugins/normalizer/index.d.ts.map +1 -0
  126. package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts +3 -0
  127. package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts.map +1 -0
  128. package/lib/typescript/src/{utils → plugins/normalizer}/normalizer.d.ts +1 -1
  129. package/lib/typescript/src/plugins/normalizer/normalizer.d.ts.map +1 -0
  130. package/lib/typescript/src/types/breakpoints.d.ts +25 -12
  131. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  132. package/lib/typescript/src/types/core.d.ts +6 -27
  133. package/lib/typescript/src/types/core.d.ts.map +1 -1
  134. package/lib/typescript/src/types/index.d.ts +4 -2
  135. package/lib/typescript/src/types/index.d.ts.map +1 -1
  136. package/lib/typescript/src/types/plugin.d.ts +7 -0
  137. package/lib/typescript/src/types/plugin.d.ts.map +1 -0
  138. package/lib/typescript/src/types/stylesheet.d.ts +40 -0
  139. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -0
  140. package/lib/typescript/src/types/unistyles.d.ts +16 -11
  141. package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
  142. package/lib/typescript/src/types/variants.d.ts +14 -0
  143. package/lib/typescript/src/types/variants.d.ts.map +1 -0
  144. package/lib/typescript/src/useStyles.d.ts +3 -3
  145. package/lib/typescript/src/useStyles.d.ts.map +1 -1
  146. package/lib/typescript/src/utils/breakpoints.d.ts +2 -5
  147. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  148. package/lib/typescript/src/utils/index.d.ts +4 -6
  149. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  150. package/lib/typescript/src/utils/mq.d.ts +15 -14
  151. package/lib/typescript/src/utils/mq.d.ts.map +1 -1
  152. package/lib/typescript/src/utils/mqParser.d.ts +14 -2
  153. package/lib/typescript/src/utils/mqParser.d.ts.map +1 -1
  154. package/lib/typescript/src/utils/styles.d.ts +3 -4
  155. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  156. package/lib/typescript/src/utils/withPlugins.d.ts +3 -0
  157. package/lib/typescript/src/utils/withPlugins.d.ts.map +1 -0
  158. package/package.json +19 -16
  159. package/src/__tests__/mocks.ts +24 -0
  160. package/src/common.ts +30 -14
  161. package/src/core/UnistyleRegistry.ts +66 -4
  162. package/src/core/UnistylesRuntime.ts +18 -20
  163. package/src/core/index.ts +1 -0
  164. package/src/createStyleSheet.ts +2 -8
  165. package/src/global.ts +1 -0
  166. package/src/hooks/index.ts +1 -0
  167. package/src/hooks/useUnistyles.ts +11 -9
  168. package/src/hooks/useVariants.ts +10 -0
  169. package/src/index.ts +3 -1
  170. package/src/plugins/index.ts +2 -0
  171. package/src/plugins/normalizeWebStylesPlugin.ts +7 -0
  172. package/src/plugins/normalizer/index.ts +2 -0
  173. package/src/{utils/normalizeStyles.web.ts → plugins/normalizer/normalizeStyle.ts} +3 -3
  174. package/src/{utils → plugins/normalizer}/normalizer.ts +3 -3
  175. package/src/types/breakpoints.ts +57 -25
  176. package/src/types/core.ts +9 -43
  177. package/src/types/index.ts +11 -2
  178. package/src/types/plugin.ts +7 -0
  179. package/src/types/stylesheet.ts +49 -0
  180. package/src/types/unistyles.ts +18 -13
  181. package/src/types/variants.ts +19 -0
  182. package/src/useStyles.ts +18 -25
  183. package/src/utils/breakpoints.ts +11 -67
  184. package/src/utils/index.ts +4 -6
  185. package/src/utils/mq.ts +31 -84
  186. package/src/utils/mqParser.ts +5 -5
  187. package/src/utils/styles.ts +44 -50
  188. package/src/utils/withPlugins.ts +13 -0
  189. package/lib/commonjs/utils/common.js +0 -25
  190. package/lib/commonjs/utils/common.js.map +0 -1
  191. package/lib/commonjs/utils/normalizeStyles.js +0 -10
  192. package/lib/commonjs/utils/normalizeStyles.js.map +0 -1
  193. package/lib/commonjs/utils/normalizeStyles.web.js.map +0 -1
  194. package/lib/commonjs/utils/normalizer.js.map +0 -1
  195. package/lib/module/types/mq.js +0 -2
  196. package/lib/module/utils/common.js +0 -17
  197. package/lib/module/utils/common.js.map +0 -1
  198. package/lib/module/utils/normalizeStyles.js +0 -3
  199. package/lib/module/utils/normalizeStyles.js.map +0 -1
  200. package/lib/module/utils/normalizeStyles.web.js.map +0 -1
  201. package/lib/module/utils/normalizer.js.map +0 -1
  202. package/lib/typescript/src/types/mq.d.ts +0 -3
  203. package/lib/typescript/src/types/mq.d.ts.map +0 -1
  204. package/lib/typescript/src/utils/common.d.ts +0 -12
  205. package/lib/typescript/src/utils/common.d.ts.map +0 -1
  206. package/lib/typescript/src/utils/normalizeStyles.d.ts +0 -2
  207. package/lib/typescript/src/utils/normalizeStyles.d.ts.map +0 -1
  208. package/lib/typescript/src/utils/normalizeStyles.web.d.ts +0 -5
  209. package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +0 -1
  210. package/lib/typescript/src/utils/normalizer.d.ts.map +0 -1
  211. package/src/types/mq.ts +0 -3
  212. package/src/utils/common.ts +0 -20
  213. package/src/utils/normalizeStyles.ts +0 -2
  214. /package/lib/commonjs/{utils → plugins/normalizer}/module.d.js +0 -0
  215. /package/lib/module/{utils → plugins/normalizer}/module.d.js +0 -0
  216. /package/src/{utils → plugins/normalizer}/module.d.ts +0 -0
package/src/common.ts CHANGED
@@ -1,19 +1,35 @@
1
- export enum CxxUnistylesEventTypes {
2
- Theme = 'theme',
3
- Layout = 'layout'
1
+ import { Platform } from 'react-native'
2
+
3
+ export const warn = (message: string) => {
4
+ console.warn(`🦄 [react-native-unistyles]: ${message}`)
4
5
  }
5
6
 
6
- export enum UnistylesError {
7
- RuntimeUnavailable = 'UNISTYLES_ERROR_RUNTIME_UNAVAILABLE',
8
- ThemeNotFound = 'UNISTYLES_ERROR_THEME_NOT_FOUND',
9
- ThemeNotRegistered = 'UNISTYLES_ERROR_THEME_NOT_REGISTERED',
10
- ThemesCannotBeEmpty = 'UNISTYLES_ERROR_THEMES_CANNOT_BE_EMPTY',
11
- BreakpointsCannotBeEmpty = 'UNISTYLES_ERROR_BREAKPOINTS_CANNOT_BE_EMPTY',
12
- BreakpointsMustStartFromZero = 'UNISTYLES_ERROR_BREAKPOINTS_MUST_START_FROM_ZERO',
7
+ export const isWeb = Platform.OS === 'web'
8
+ export const isIOS = Platform.OS === 'ios'
9
+ export const isAndroid = Platform.OS === 'android'
10
+ export const isMobile = isIOS || isAndroid
11
+ export const isServer = typeof window === 'undefined'
12
+
13
+ export const ScreenOrientation = {
14
+ Landscape: 'landscape',
15
+ Portrait: 'portrait'
16
+ } as const
17
+
18
+ export enum UnistylesEventType {
19
+ Theme = 'theme',
20
+ Layout = 'layout',
21
+ Plugin = 'plugin'
13
22
  }
14
23
 
15
- // todo to string?
16
- export enum ScreenOrientation {
17
- Portrait = 1,
18
- Landscape = 2
24
+ export enum UnistylesError {
25
+ RuntimeUnavailable = 'Unistyles runtime is not available. Make sure you followed the installation instructions',
26
+ ThemeNotFound = 'You are trying to get a theme that is not registered with UnistylesRegistry',
27
+ ThemeNotRegistered = 'You are trying to set a theme that was not registered with UnistylesRegistry',
28
+ ThemeNotSelected = 'Your themes are registered, but you didn\'t select the initial theme',
29
+ ThemesCannotBeEmpty = 'You are trying to register empty themes object',
30
+ BreakpointsCannotBeEmpty = 'You are trying to register empty breakpoints object',
31
+ BreakpointsMustStartFromZero = 'You are trying to register breakpoints that don\'t start from 0',
32
+ InvalidPluginName = 'Plugin name can\'t start from reserved prefix __unistyles',
33
+ DuplicatePluginName = 'You are trying to register a plugin with a name that is already registered',
34
+ CantRemoveInternalPlugin = 'You are trying to remove an internal unistyles plugin'
19
35
  }
@@ -1,8 +1,13 @@
1
- import type { UnistylesBridge, UnistylesConfig } from '../types'
1
+ import type { UnistylesBridge, UnistylesConfig, UnistylesPlugin } from '../types'
2
2
  import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
3
+ import { isWeb, UnistylesError } from '../common'
4
+ import { normalizeWebStylesPlugin } from '../plugins'
3
5
 
4
6
  export class UnistyleRegistry {
5
7
  public config: UnistylesConfig = {}
8
+ public plugins: Array<UnistylesPlugin> = isWeb
9
+ ? [normalizeWebStylesPlugin]
10
+ : []
6
11
  public themeNames: Array<keyof UnistylesThemes> = []
7
12
  public themes: UnistylesThemes = {} as UnistylesThemes
8
13
  public breakpoints: UnistylesBreakpoints = {} as UnistylesBreakpoints
@@ -18,7 +23,10 @@ export class UnistyleRegistry {
18
23
  this.unistylesBridge.themes = keys
19
24
  this.themeNames = keys
20
25
 
21
- return this
26
+ return {
27
+ addBreakpoints: this.addBreakpoints,
28
+ addConfig: this.addConfig
29
+ }
22
30
  }
23
31
 
24
32
  public addBreakpoints = (breakpoints: UnistylesBreakpoints) => {
@@ -26,7 +34,10 @@ export class UnistyleRegistry {
26
34
  this.unistylesBridge.useBreakpoints(breakpoints)
27
35
  this.sortedBreakpointPairs = this.unistylesBridge.sortedBreakpointPairs
28
36
 
29
- return this
37
+ return {
38
+ addThemes: this.addThemes,
39
+ addConfig: this.addConfig
40
+ }
30
41
  }
31
42
 
32
43
  public addConfig = (config: UnistylesConfig) => {
@@ -36,6 +47,57 @@ export class UnistyleRegistry {
36
47
  this.unistylesBridge.useAdaptiveThemes(config.adaptiveThemes)
37
48
  }
38
49
 
39
- return this
50
+ if (config.experimentalPlugins) {
51
+ config.experimentalPlugins.forEach(plugin => this.addPlugin(plugin, false))
52
+ }
53
+
54
+ if (config.initialTheme) {
55
+ this.unistylesBridge.useTheme(config.initialTheme)
56
+ }
57
+
58
+ return {
59
+ addBreakpoints: this.addBreakpoints,
60
+ addThemes: this.addThemes
61
+ }
62
+ }
63
+
64
+ public getTheme = (forName: keyof UnistylesThemes) => {
65
+ if (this.themeNames.length === 0) {
66
+ return {} as UnistylesThemes[keyof UnistylesThemes]
67
+ }
68
+
69
+ if (this.hasTheme(forName)) {
70
+ return this.themes[forName]
71
+ }
72
+
73
+ if (this.unistylesBridge.themeName) {
74
+ throw new Error(UnistylesError.ThemeNotFound)
75
+ }
76
+
77
+ throw new Error(UnistylesError.ThemeNotSelected)
40
78
  }
79
+
80
+ public addPlugin = (plugin: UnistylesPlugin, notify: boolean = true) => {
81
+ if (plugin.name.startsWith('__unistyles')) {
82
+ throw new Error(UnistylesError.InvalidPluginName)
83
+ }
84
+
85
+ if (this.plugins.some(({ name }) => name === plugin.name)) {
86
+ throw new Error(UnistylesError.DuplicatePluginName)
87
+ }
88
+
89
+ this.plugins = [plugin].concat(this.plugins)
90
+ this.unistylesBridge.addPlugin(plugin.name, notify)
91
+ }
92
+
93
+ public removePlugin = (plugin: UnistylesPlugin) => {
94
+ if (plugin.name.startsWith('__unistyles')) {
95
+ throw new Error(UnistylesError.CantRemoveInternalPlugin)
96
+ }
97
+
98
+ this.plugins = this.plugins.filter(({ name }) => name !== plugin.name)
99
+ this.unistylesBridge.removePlugin(plugin.name)
100
+ }
101
+
102
+ public hasTheme = (name: keyof UnistylesThemes) => name in this.themes
41
103
  }
@@ -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,
@@ -22,23 +23,23 @@ export const useUnistyles = () => {
22
23
  'onChange',
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
+ }
@@ -0,0 +1,49 @@
1
+ import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
2
+ import type { ShadowOffset, TransformStyles, UnistylesTheme } from './core'
3
+ import type { UnistylesBreakpoints } from '../global'
4
+
5
+ // these props are treated differently to nest breakpoints and media queries
6
+ type NestedKeys = 'shadowOffset' | 'transform' | 'textShadowOffset'
7
+
8
+ type UnistyleView = Omit<ViewStyle, NestedKeys>
9
+ type UnistyleText = Omit<TextStyle, NestedKeys>
10
+ type UnistyleImage = Omit<ImageStyle, NestedKeys>
11
+
12
+ type UnistyleNestedStyles = {
13
+ shadowOffset?: ToDeepUnistyles<ShadowOffset>,
14
+ textShadowOffset?: ToDeepUnistyles<ShadowOffset>,
15
+ transform?: Array<ToDeepUnistyles<TransformStyles>>
16
+ }
17
+
18
+ type Variants = {
19
+ variants?: {
20
+ [variantName: string]: {
21
+ [variant: string]: Omit<UnistylesValues, 'variants'>
22
+ }
23
+ }
24
+ }
25
+
26
+ export type ToDeepUnistyles<T> = {
27
+ [K in keyof T]?: T[K] | {
28
+ [key in BreakpointsOrMediaQueries]?: T[K]
29
+ }
30
+ }
31
+
32
+ type AllAvailableStyles = UnistyleView & UnistyleText & UnistyleImage & UnistyleNestedStyles
33
+
34
+ export type AllAvailableKeys = keyof (UnistyleView & UnistyleText & UnistyleImage)
35
+ export type BreakpointsOrMediaQueries = keyof UnistylesBreakpoints | symbol
36
+
37
+ export type UnistylesValues = {
38
+ [propName in AllAvailableKeys]?: AllAvailableStyles[propName] | {
39
+ [key in BreakpointsOrMediaQueries]?: AllAvailableStyles[propName]
40
+ }
41
+ } & Variants & {
42
+ [propName in NestedKeys]?: UnistyleNestedStyles[propName]
43
+ }
44
+
45
+ export type StyleSheet = {
46
+ [styleName: string]: UnistylesValues | ((...args: any) => UnistylesValues)
47
+ }
48
+
49
+ export type StyleSheetWithSuperPowers = ((theme: UnistylesTheme) => StyleSheet) | StyleSheet