react-native-unistyles 2.0.0-alpha.1 → 2.0.0-alpha.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (250) hide show
  1. package/README.md +4 -4
  2. package/cxx/UnistylesRuntime.cpp +255 -0
  3. package/cxx/UnistylesRuntime.h +60 -0
  4. package/ios/UnistylesHelpers.h +4 -0
  5. package/ios/UnistylesHelpers.mm +20 -0
  6. package/ios/UnistylesModule.h +10 -0
  7. package/ios/UnistylesModule.mm +158 -0
  8. package/lib/commonjs/common.js +26 -0
  9. package/lib/commonjs/common.js.map +1 -0
  10. package/lib/commonjs/core/UnistyleRegistry.js +38 -0
  11. package/lib/commonjs/core/UnistyleRegistry.js.map +1 -0
  12. package/lib/commonjs/core/Unistyles.js +31 -0
  13. package/lib/commonjs/core/Unistyles.js.map +1 -0
  14. package/lib/commonjs/core/UnistylesModule.js +9 -0
  15. package/lib/commonjs/core/UnistylesModule.js.map +1 -0
  16. package/lib/commonjs/core/UnistylesRuntime.js +66 -0
  17. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -0
  18. package/lib/commonjs/core/index.js +13 -0
  19. package/lib/commonjs/core/index.js.map +1 -0
  20. package/lib/commonjs/createStyleSheet.js +14 -0
  21. package/lib/commonjs/createStyleSheet.js.map +1 -0
  22. package/lib/commonjs/global.js +2 -0
  23. package/lib/commonjs/global.js.map +1 -0
  24. package/lib/commonjs/hooks/index.js +10 -3
  25. package/lib/commonjs/hooks/index.js.map +1 -1
  26. package/lib/commonjs/hooks/useInitialTheme.js +17 -0
  27. package/lib/commonjs/hooks/useInitialTheme.js.map +1 -0
  28. package/lib/commonjs/hooks/useUnistyles.js +54 -0
  29. package/lib/commonjs/hooks/useUnistyles.js.map +1 -0
  30. package/lib/commonjs/index.js +35 -6
  31. package/lib/commonjs/index.js.map +1 -1
  32. package/lib/commonjs/types/common.js +2 -0
  33. package/lib/commonjs/types/{mediaQueries.js.map → common.js.map} +1 -1
  34. package/lib/commonjs/types/index.js +26 -0
  35. package/lib/commonjs/types/index.js.map +1 -1
  36. package/lib/commonjs/types/mq.js +6 -0
  37. package/lib/{module/types/mediaQueries.js.map → commonjs/types/mq.js.map} +1 -1
  38. package/lib/commonjs/types/normalizer.js +6 -0
  39. package/lib/commonjs/types/normalizer.js.map +1 -0
  40. package/lib/commonjs/types/unistyles.js +6 -0
  41. package/lib/commonjs/types/unistyles.js.map +1 -0
  42. package/lib/commonjs/useStyles.js +49 -0
  43. package/lib/commonjs/useStyles.js.map +1 -0
  44. package/lib/commonjs/utils/breakpoints.js +44 -82
  45. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  46. package/lib/commonjs/utils/common.js +10 -3
  47. package/lib/commonjs/utils/common.js.map +1 -1
  48. package/lib/commonjs/utils/index.js +46 -17
  49. package/lib/commonjs/utils/index.js.map +1 -1
  50. package/lib/commonjs/utils/module.d.js +2 -0
  51. package/lib/commonjs/utils/module.d.js.map +1 -0
  52. package/lib/commonjs/utils/mq.js +89 -0
  53. package/lib/commonjs/utils/mq.js.map +1 -0
  54. package/lib/commonjs/utils/mqParser.js +86 -0
  55. package/lib/commonjs/utils/mqParser.js.map +1 -0
  56. package/lib/commonjs/utils/normalizeStyles.web.js +13 -13
  57. package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -1
  58. package/lib/commonjs/utils/normalizer.js +89 -0
  59. package/lib/commonjs/utils/normalizer.js.map +1 -0
  60. package/lib/commonjs/utils/styles.js +17 -57
  61. package/lib/commonjs/utils/styles.js.map +1 -1
  62. package/lib/commonjs/utils/variants.js +20 -0
  63. package/lib/commonjs/utils/variants.js.map +1 -0
  64. package/lib/module/common.js +22 -0
  65. package/lib/module/common.js.map +1 -0
  66. package/lib/module/core/UnistyleRegistry.js +31 -0
  67. package/lib/module/core/UnistyleRegistry.js.map +1 -0
  68. package/lib/module/core/Unistyles.js +25 -0
  69. package/lib/module/core/Unistyles.js.map +1 -0
  70. package/lib/module/core/UnistylesModule.js +3 -0
  71. package/lib/module/core/UnistylesModule.js.map +1 -0
  72. package/lib/module/core/UnistylesRuntime.js +59 -0
  73. package/lib/module/core/UnistylesRuntime.js.map +1 -0
  74. package/lib/module/core/index.js +2 -0
  75. package/lib/module/core/index.js.map +1 -0
  76. package/lib/module/createStyleSheet.js +7 -0
  77. package/lib/module/createStyleSheet.js.map +1 -0
  78. package/lib/module/global.js +2 -0
  79. package/lib/module/global.js.map +1 -0
  80. package/lib/module/hooks/index.js +2 -1
  81. package/lib/module/hooks/index.js.map +1 -1
  82. package/lib/module/hooks/useInitialTheme.js +10 -0
  83. package/lib/module/hooks/useInitialTheme.js.map +1 -0
  84. package/lib/module/hooks/useUnistyles.js +47 -0
  85. package/lib/module/hooks/useUnistyles.js.map +1 -0
  86. package/lib/module/index.js +13 -2
  87. package/lib/module/index.js.map +1 -1
  88. package/lib/module/types/common.js +2 -0
  89. package/lib/module/types/common.js.map +1 -0
  90. package/lib/module/types/index.js +2 -1
  91. package/lib/module/types/index.js.map +1 -1
  92. package/lib/module/types/mq.js +2 -0
  93. package/lib/module/types/mq.js.map +1 -0
  94. package/lib/module/types/normalizer.js +2 -0
  95. package/lib/module/types/normalizer.js.map +1 -0
  96. package/lib/module/types/unistyles.js +2 -0
  97. package/lib/module/types/unistyles.js.map +1 -0
  98. package/lib/module/useStyles.js +42 -0
  99. package/lib/module/useStyles.js.map +1 -0
  100. package/lib/module/utils/breakpoints.js +43 -82
  101. package/lib/module/utils/breakpoints.js.map +1 -1
  102. package/lib/module/utils/common.js +9 -1
  103. package/lib/module/utils/common.js.map +1 -1
  104. package/lib/module/utils/index.js +6 -2
  105. package/lib/module/utils/index.js.map +1 -1
  106. package/lib/module/utils/module.d.js +2 -0
  107. package/lib/module/utils/module.d.js.map +1 -0
  108. package/lib/module/utils/mq.js +83 -0
  109. package/lib/module/utils/mq.js.map +1 -0
  110. package/lib/module/utils/mqParser.js +79 -0
  111. package/lib/module/utils/mqParser.js.map +1 -0
  112. package/lib/module/utils/normalizeStyles.web.js +13 -13
  113. package/lib/module/utils/normalizeStyles.web.js.map +1 -1
  114. package/lib/module/utils/normalizer.js +79 -0
  115. package/lib/module/utils/normalizer.js.map +1 -0
  116. package/lib/module/utils/styles.js +17 -59
  117. package/lib/module/utils/styles.js.map +1 -1
  118. package/lib/module/utils/variants.js +13 -0
  119. package/lib/module/utils/variants.js.map +1 -0
  120. package/lib/typescript/src/common.d.ts +17 -0
  121. package/lib/typescript/src/common.d.ts.map +1 -0
  122. package/lib/typescript/src/core/UnistyleRegistry.d.ts +15 -0
  123. package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -0
  124. package/lib/typescript/src/core/Unistyles.d.ts +13 -0
  125. package/lib/typescript/src/core/Unistyles.d.ts.map +1 -0
  126. package/lib/typescript/src/core/UnistylesModule.d.ts +6 -0
  127. package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -0
  128. package/lib/typescript/src/core/UnistylesRuntime.d.ts +24 -0
  129. package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -0
  130. package/lib/typescript/src/core/index.d.ts +2 -0
  131. package/lib/typescript/src/core/index.d.ts.map +1 -0
  132. package/lib/typescript/src/createStyleSheet.d.ts +3 -0
  133. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -0
  134. package/lib/typescript/src/global.d.ts +7 -0
  135. package/lib/typescript/src/global.d.ts.map +1 -0
  136. package/lib/typescript/src/hooks/index.d.ts +2 -1
  137. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  138. package/lib/typescript/src/hooks/useInitialTheme.d.ts +3 -0
  139. package/lib/typescript/src/hooks/useInitialTheme.d.ts.map +1 -0
  140. package/lib/typescript/src/hooks/useUnistyles.d.ts +12 -0
  141. package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -0
  142. package/lib/typescript/src/index.d.ts +14 -2
  143. package/lib/typescript/src/index.d.ts.map +1 -1
  144. package/lib/typescript/src/types/breakpoints.d.ts +13 -13
  145. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  146. package/lib/typescript/src/types/common.d.ts +3 -0
  147. package/lib/typescript/src/types/common.d.ts.map +1 -0
  148. package/lib/typescript/src/types/core.d.ts +34 -18
  149. package/lib/typescript/src/types/core.d.ts.map +1 -1
  150. package/lib/typescript/src/types/index.d.ts +6 -2
  151. package/lib/typescript/src/types/index.d.ts.map +1 -1
  152. package/lib/typescript/src/types/mq.d.ts +3 -0
  153. package/lib/typescript/src/types/mq.d.ts.map +1 -0
  154. package/lib/typescript/src/types/normalizer.d.ts +20 -0
  155. package/lib/typescript/src/types/normalizer.d.ts.map +1 -0
  156. package/lib/typescript/src/types/unistyles.d.ts +37 -0
  157. package/lib/typescript/src/types/unistyles.d.ts.map +1 -0
  158. package/lib/typescript/src/useStyles.d.ts +10 -0
  159. package/lib/typescript/src/useStyles.d.ts.map +1 -0
  160. package/lib/typescript/src/utils/breakpoints.d.ts +5 -62
  161. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  162. package/lib/typescript/src/utils/common.d.ts +9 -1
  163. package/lib/typescript/src/utils/common.d.ts.map +1 -1
  164. package/lib/typescript/src/utils/index.d.ts +6 -2
  165. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  166. package/lib/typescript/src/utils/mq.d.ts +21 -0
  167. package/lib/typescript/src/utils/mq.d.ts.map +1 -0
  168. package/lib/typescript/src/utils/mqParser.d.ts +3 -0
  169. package/lib/typescript/src/utils/mqParser.d.ts.map +1 -0
  170. package/lib/typescript/src/utils/normalizeStyles.web.d.ts +4 -1
  171. package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -1
  172. package/lib/typescript/src/utils/normalizer.d.ts +11 -0
  173. package/lib/typescript/src/utils/normalizer.d.ts.map +1 -0
  174. package/lib/typescript/src/utils/styles.d.ts +5 -49
  175. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  176. package/lib/typescript/src/utils/variants.d.ts +3 -0
  177. package/lib/typescript/src/utils/variants.d.ts.map +1 -0
  178. package/package.json +33 -15
  179. package/react-native-unistyles.podspec +22 -0
  180. package/src/common.ts +19 -0
  181. package/src/core/UnistyleRegistry.ts +41 -0
  182. package/src/core/Unistyles.ts +34 -0
  183. package/src/core/UnistylesModule.ts +7 -0
  184. package/src/core/UnistylesRuntime.ts +73 -0
  185. package/src/core/index.ts +1 -0
  186. package/src/createStyleSheet.ts +9 -0
  187. package/src/global.ts +5 -0
  188. package/src/hooks/index.ts +2 -1
  189. package/src/hooks/useInitialTheme.ts +11 -0
  190. package/src/hooks/useUnistyles.ts +55 -0
  191. package/src/index.ts +30 -2
  192. package/src/types/breakpoints.ts +20 -20
  193. package/src/types/common.ts +2 -0
  194. package/src/types/core.ts +42 -24
  195. package/src/types/index.ts +6 -9
  196. package/src/types/mq.ts +3 -0
  197. package/src/types/normalizer.ts +29 -0
  198. package/src/types/unistyles.ts +45 -0
  199. package/src/useStyles.ts +56 -0
  200. package/src/utils/breakpoints.ts +57 -88
  201. package/src/utils/common.ts +10 -1
  202. package/src/utils/index.ts +6 -9
  203. package/src/utils/module.d.ts +3 -0
  204. package/src/utils/mq.ts +106 -0
  205. package/src/utils/mqParser.ts +99 -0
  206. package/src/utils/normalizeStyles.web.ts +21 -42
  207. package/src/utils/normalizer.ts +99 -0
  208. package/src/utils/styles.ts +29 -68
  209. package/src/utils/variants.ts +17 -0
  210. package/lib/commonjs/UnistylesTheme.js +0 -21
  211. package/lib/commonjs/UnistylesTheme.js.map +0 -1
  212. package/lib/commonjs/createUnistyles.js +0 -57
  213. package/lib/commonjs/createUnistyles.js.map +0 -1
  214. package/lib/commonjs/hooks/useDimensions.js +0 -10
  215. package/lib/commonjs/hooks/useDimensions.js.map +0 -1
  216. package/lib/commonjs/hooks/useDimensions.web.js +0 -31
  217. package/lib/commonjs/hooks/useDimensions.web.js.map +0 -1
  218. package/lib/commonjs/types/mediaQueries.js +0 -2
  219. package/lib/commonjs/utils/mediaQueries.js +0 -189
  220. package/lib/commonjs/utils/mediaQueries.js.map +0 -1
  221. package/lib/module/UnistylesTheme.js +0 -12
  222. package/lib/module/UnistylesTheme.js.map +0 -1
  223. package/lib/module/createUnistyles.js +0 -50
  224. package/lib/module/createUnistyles.js.map +0 -1
  225. package/lib/module/hooks/useDimensions.js +0 -3
  226. package/lib/module/hooks/useDimensions.js.map +0 -1
  227. package/lib/module/hooks/useDimensions.web.js +0 -24
  228. package/lib/module/hooks/useDimensions.web.js.map +0 -1
  229. package/lib/module/types/mediaQueries.js +0 -2
  230. package/lib/module/utils/mediaQueries.js +0 -176
  231. package/lib/module/utils/mediaQueries.js.map +0 -1
  232. package/lib/typescript/src/UnistylesTheme.d.ts +0 -9
  233. package/lib/typescript/src/UnistylesTheme.d.ts.map +0 -1
  234. package/lib/typescript/src/createUnistyles.d.ts +0 -10
  235. package/lib/typescript/src/createUnistyles.d.ts.map +0 -1
  236. package/lib/typescript/src/hooks/useDimensions.d.ts +0 -3
  237. package/lib/typescript/src/hooks/useDimensions.d.ts.map +0 -1
  238. package/lib/typescript/src/hooks/useDimensions.web.d.ts +0 -3
  239. package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +0 -1
  240. package/lib/typescript/src/types/mediaQueries.d.ts +0 -8
  241. package/lib/typescript/src/types/mediaQueries.d.ts.map +0 -1
  242. package/lib/typescript/src/utils/mediaQueries.d.ts +0 -130
  243. package/lib/typescript/src/utils/mediaQueries.d.ts.map +0 -1
  244. package/src/UnistylesTheme.tsx +0 -17
  245. package/src/__tests__/createUnistyles.spec.tsx +0 -192
  246. package/src/createUnistyles.ts +0 -70
  247. package/src/hooks/useDimensions.ts +0 -4
  248. package/src/hooks/useDimensions.web.ts +0 -30
  249. package/src/types/mediaQueries.ts +0 -10
  250. package/src/utils/mediaQueries.ts +0 -201
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "2.0.0-alpha.1",
3
+ "version": "2.0.0-alpha.10",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",
7
+ "test:coverage": "jest --coverage",
7
8
  "tsc": "node_modules/typescript/bin/tsc --noEmit",
8
9
  "lint": "eslint . --ext .ts,.tsx",
9
10
  "prepare": "husky install && bob build",
@@ -18,7 +19,19 @@
18
19
  "files": [
19
20
  "src",
20
21
  "lib",
22
+ "ios",
23
+ "android",
24
+ "cxx",
25
+ "react-native-unistyles.podspec",
26
+ "!lib/typescript/examples",
27
+ "!ios/build",
28
+ "!android/build",
29
+ "!android/gradle",
30
+ "!android/gradlew",
31
+ "!android/gradlew.bat",
32
+ "!android/local.properties",
21
33
  "!**/*.spec.ts",
34
+ "!**/*.spec.tsx",
22
35
  "!**/.*"
23
36
  ],
24
37
  "keywords": [
@@ -37,24 +50,24 @@
37
50
  "registry": "https://registry.npmjs.org/"
38
51
  },
39
52
  "devDependencies": {
40
- "@commitlint/config-conventional": "17.8.0",
53
+ "@commitlint/config-conventional": "18.1.0",
41
54
  "@react-native/eslint-config": "0.74.0",
55
+ "@react-native/normalize-colors": "0.74.1",
42
56
  "@release-it/conventional-changelog": "7.0.2",
43
57
  "@testing-library/react-hooks": "8.0.1",
44
- "@types/jest": "29.5.6",
45
- "@types/react": "18.2.28",
46
- "@types/react-native": "0.72.3",
47
- "@typescript-eslint/eslint-plugin": "6.8.0",
48
- "@typescript-eslint/eslint-plugin-tslint": "6.8.0",
49
- "@typescript-eslint/parser": "6.8.0",
50
- "commitlint": "17.8.0",
51
- "concurrently": "8.2.1",
52
- "eslint": "8.51.0",
58
+ "@types/jest": "29.5.7",
59
+ "@types/react": "18.2.34",
60
+ "@typescript-eslint/eslint-plugin": "6.9.1",
61
+ "@typescript-eslint/eslint-plugin-tslint": "6.9.1",
62
+ "@typescript-eslint/parser": "6.9.1",
63
+ "commitlint": "18.2.0",
64
+ "concurrently": "8.2.2",
65
+ "eslint": "8.53.0",
53
66
  "eslint-config-codemask": "1.1.7",
54
67
  "eslint-plugin-functional": "6.0.0",
55
- "eslint-plugin-import": "2.28.1",
68
+ "eslint-plugin-import": "2.29.0",
56
69
  "eslint-plugin-jsdoc": "46.8.2",
57
- "eslint-plugin-jsx-a11y": "6.7.1",
70
+ "eslint-plugin-jsx-a11y": "6.8.0",
58
71
  "eslint-plugin-nested-if": "1.0.0",
59
72
  "eslint-plugin-no-else": "0.2.2",
60
73
  "eslint-plugin-no-loops": "0.3.0",
@@ -72,6 +85,7 @@
72
85
  "typescript": "5.2.2"
73
86
  },
74
87
  "peerDependencies": {
88
+ "@react-native/normalize-colors": "*",
75
89
  "react": "*",
76
90
  "react-native": "*",
77
91
  "react-native-web": "*"
@@ -82,7 +96,7 @@
82
96
  }
83
97
  },
84
98
  "workspaces": [
85
- "example",
99
+ "examples/expo",
86
100
  "docs"
87
101
  ],
88
102
  "packageManager": "yarn@3.6.1",
@@ -92,9 +106,13 @@
92
106
  "jest": {
93
107
  "preset": "react-native",
94
108
  "modulePathIgnorePatterns": [
95
- "<rootDir>/example/node_modules",
109
+ "<rootDir>/examples/expo/node_modules",
110
+ "<rootDir>/examples/ssr/node_modules",
96
111
  "<rootDir>/docs/node_modules",
97
112
  "<rootDir>/lib/"
113
+ ],
114
+ "coverageReporters": [
115
+ "html"
98
116
  ]
99
117
  },
100
118
  "commitlint": {
@@ -0,0 +1,22 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = package["name"]
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => min_ios_version_supported }
14
+ s.source = { :git => package["repository"], :tag => "#{s.version}" }
15
+
16
+ s.source_files = [
17
+ "ios/*.{h,mm}",
18
+ "cxx/*.{h,cpp}"
19
+ ]
20
+
21
+ s.dependency "React-Core"
22
+ end
package/src/common.ts ADDED
@@ -0,0 +1,19 @@
1
+ export enum CxxUnistylesEventTypes {
2
+ Theme = 'theme',
3
+ Layout = 'layout'
4
+ }
5
+
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',
13
+ }
14
+
15
+ // todo to string?
16
+ export enum ScreenOrientation {
17
+ Portrait = 1,
18
+ Landscape = 2
19
+ }
@@ -0,0 +1,41 @@
1
+ import type { UnistylesBridge, UnistylesConfig } from '../types'
2
+ import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
3
+
4
+ export class UnistyleRegistry {
5
+ public config: UnistylesConfig = {}
6
+ public themeNames: Array<keyof UnistylesThemes> = []
7
+ public themes: UnistylesThemes = {} as UnistylesThemes
8
+ public breakpoints: UnistylesBreakpoints = {} as UnistylesBreakpoints
9
+ public sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]> = []
10
+
11
+ constructor(private unistylesBridge: UnistylesBridge) {}
12
+
13
+ public addThemes = (themes: UnistylesThemes) => {
14
+ this.themes = themes
15
+
16
+ const keys = Object.keys(themes) as Array<keyof UnistylesThemes>
17
+
18
+ this.unistylesBridge.themes = keys
19
+ this.themeNames = keys
20
+
21
+ return this
22
+ }
23
+
24
+ public addBreakpoints = (breakpoints: UnistylesBreakpoints) => {
25
+ this.breakpoints = breakpoints
26
+ this.unistylesBridge.useBreakpoints(breakpoints)
27
+ this.sortedBreakpointPairs = this.unistylesBridge.sortedBreakpointPairs
28
+
29
+ return this
30
+ }
31
+
32
+ public addConfig = (config: UnistylesConfig) => {
33
+ this.config = config
34
+
35
+ if (config.adaptiveThemes) {
36
+ this.unistylesBridge.useAdaptiveThemes(config.adaptiveThemes)
37
+ }
38
+
39
+ return this
40
+ }
41
+ }
@@ -0,0 +1,34 @@
1
+ import { UnistylesModule } from './UnistylesModule'
2
+ import { UnistylesRuntime } from './UnistylesRuntime'
3
+ import { UnistyleRegistry } from './UnistyleRegistry'
4
+ import type { UnistylesBridge } from '../types'
5
+ import { UnistylesError } from '../common'
6
+
7
+ class Unistyles {
8
+ private _runtime: UnistylesRuntime
9
+ private _registry: UnistyleRegistry
10
+ private _bridge: UnistylesBridge
11
+
12
+ constructor() {
13
+ const isInstalled = UnistylesModule?.install() ?? false
14
+
15
+ if (!isInstalled) {
16
+ throw new Error(UnistylesError.RuntimeUnavailable)
17
+ }
18
+
19
+ // @ts-ignore
20
+ this._bridge = global.__UNISTYLES__ as UnistylesBridge
21
+ this._registry = new UnistyleRegistry(this._bridge)
22
+ this._runtime = new UnistylesRuntime(this._bridge, this._registry)
23
+ }
24
+
25
+ public get registry() {
26
+ return this._registry
27
+ }
28
+
29
+ public get runtime() {
30
+ return this._runtime
31
+ }
32
+ }
33
+
34
+ export const unistyles = new Unistyles()
@@ -0,0 +1,7 @@
1
+ import { NativeModules } from 'react-native'
2
+
3
+ type UnistylesNativeModule = {
4
+ install(): boolean
5
+ }
6
+
7
+ export const UnistylesModule = NativeModules?.Unistyles as UnistylesNativeModule
@@ -0,0 +1,73 @@
1
+ import { ScreenOrientation, UnistylesError } from '../common'
2
+ import type { UnistylesBridge } from '../types'
3
+ import type { UnistylesThemes } from '../global'
4
+ import type { UnistyleRegistry } from './UnistyleRegistry'
5
+
6
+ export class UnistylesRuntime {
7
+ constructor(private unistylesBridge: UnistylesBridge, private registry: UnistyleRegistry) {}
8
+
9
+ public get colorScheme() {
10
+ return this.unistylesBridge.colorScheme
11
+ }
12
+
13
+ public get hasAdaptiveThemes() {
14
+ return this.unistylesBridge.hasAdaptiveThemes
15
+ }
16
+
17
+ public get sortedBreakpoints() {
18
+ return this.registry.sortedBreakpointPairs
19
+ }
20
+
21
+ public get themeName() {
22
+ return this.unistylesBridge.themeName
23
+ }
24
+
25
+ public get breakpoint() {
26
+ return this.unistylesBridge.breakpoint
27
+ }
28
+
29
+ public get screen() {
30
+ return {
31
+ width: this.unistylesBridge.screenWidth,
32
+ height: this.unistylesBridge.screenHeight
33
+ }
34
+ }
35
+
36
+ public get orientation() {
37
+ const { width, height } = this.screen
38
+
39
+ if (width > height) {
40
+ return ScreenOrientation.Landscape
41
+ }
42
+
43
+ return ScreenOrientation.Portrait
44
+ }
45
+
46
+ public setTheme = (name: keyof UnistylesThemes) => {
47
+ if (this.hasTheme(name)) {
48
+ this.unistylesBridge.useTheme(name)
49
+
50
+ return true
51
+ }
52
+
53
+ throw new Error(UnistylesError.ThemeNotRegistered)
54
+ }
55
+
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
+ public setAdaptiveThemes = (enable: boolean) => {
69
+ this.unistylesBridge.useAdaptiveThemes(enable)
70
+ }
71
+
72
+ private hasTheme = (name: keyof UnistylesThemes) => name in this.registry.themes
73
+ }
@@ -0,0 +1 @@
1
+ export { unistyles } from './Unistyles'
@@ -0,0 +1,9 @@
1
+ import type { CustomNamedStyles, UnistylesTheme } from './types'
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
+ }
package/src/global.ts ADDED
@@ -0,0 +1,5 @@
1
+ export interface UnistylesThemes {}
2
+ export interface UnistylesBreakpoints {
3
+ landscape?: number,
4
+ portrait?: number,
5
+ }
@@ -1 +1,2 @@
1
- export { useDimensions } from './useDimensions'
1
+ export { useInitialTheme } from './useInitialTheme'
2
+ export { useUnistyles } from './useUnistyles'
@@ -0,0 +1,11 @@
1
+ import { useMemo } from 'react'
2
+ import { unistyles } from '../core'
3
+ import type { UnistylesThemes } from '../global'
4
+
5
+ export const useInitialTheme = (forName: keyof UnistylesThemes) => {
6
+ useMemo(() => {
7
+ if (!unistyles.runtime.themeName) {
8
+ unistyles.runtime.setTheme(forName)
9
+ }
10
+ }, [])
11
+ }
@@ -0,0 +1,55 @@
1
+ import { NativeEventEmitter, NativeModules } from 'react-native'
2
+ import { useEffect, useState } from 'react'
3
+ import { unistyles } from '../core'
4
+ import { CxxUnistylesEventTypes } from '../common'
5
+ import type { UnistylesEvents, UnistylesMobileLayoutEvent, UnistylesThemeEvent } from '../types'
6
+
7
+ const unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
8
+
9
+ export const useUnistyles = () => {
10
+ const [theme, setTheme] = useState(unistyles.runtime.getTheme(unistyles.runtime.themeName))
11
+ const [layout, setLayout] = useState({
12
+ breakpoint: unistyles.runtime.breakpoint,
13
+ orientation: unistyles.runtime.orientation,
14
+ screenSize: {
15
+ width: unistyles.runtime.screen.width,
16
+ height: unistyles.runtime.screen.height
17
+ }
18
+ })
19
+
20
+ useEffect(() => {
21
+ const subscription = unistylesEvents.addListener(
22
+ 'onChange',
23
+ (event: UnistylesEvents) => {
24
+ switch (event.type) {
25
+ case CxxUnistylesEventTypes.Theme: {
26
+ const themeEvent = event as UnistylesThemeEvent
27
+
28
+ return setTheme(unistyles.runtime.getTheme(themeEvent.payload.themeName))
29
+ }
30
+ case CxxUnistylesEventTypes.Layout: {
31
+ const layoutEvent = event as UnistylesMobileLayoutEvent
32
+
33
+ return setLayout({
34
+ breakpoint: layoutEvent.payload.breakpoint,
35
+ orientation: layoutEvent.payload.orientation,
36
+ screenSize: {
37
+ width: layoutEvent.payload.screen.width,
38
+ height: layoutEvent.payload.screen.height
39
+ }
40
+ })
41
+ }
42
+ default:
43
+ return
44
+ }
45
+ }
46
+ )
47
+
48
+ return subscription.remove
49
+ }, [])
50
+
51
+ return {
52
+ theme,
53
+ layout
54
+ }
55
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,30 @@
1
- export { UnistylesTheme } from './UnistylesTheme'
2
- export { createUnistyles } from './createUnistyles'
1
+ import { unistyles } from './core'
2
+ import { mq } from './utils'
3
+ import { useInitialTheme } from './hooks'
4
+ import type { UnistylesThemes, UnistylesBreakpoints } from './global'
5
+ import { ScreenOrientation } from './common'
6
+ import { useStyles } from './useStyles'
7
+ import { createStyleSheet } from './createStyleSheet'
8
+
9
+ const UnistylesRegistry = {
10
+ addThemes: unistyles.registry.addThemes,
11
+ addBreakpoints: unistyles.registry.addBreakpoints,
12
+ addConfig: unistyles.registry.addConfig
13
+ }
14
+
15
+ const UnistylesRuntime = unistyles.runtime
16
+
17
+ export {
18
+ mq,
19
+ useStyles,
20
+ useInitialTheme,
21
+ createStyleSheet,
22
+ ScreenOrientation,
23
+ UnistylesRegistry,
24
+ UnistylesRuntime
25
+ }
26
+
27
+ export type {
28
+ UnistylesThemes,
29
+ UnistylesBreakpoints
30
+ }
@@ -1,33 +1,33 @@
1
- import type { MediaQueries } from './mediaQueries'
2
-
3
- export type Breakpoints = Record<string, number>
4
- export type SortedBreakpointEntries<B extends Breakpoints> = [[keyof B & string, number]]
5
-
6
- export type ScreenSize = {
7
- width: number,
8
- height: number
9
- }
10
-
11
- export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
1
+ import type { OpaqueColorValue } from 'react-native'
2
+ import type { UnistylesBreakpoints } from '../global'
3
+ import type { MediaQuery } from './mq'
4
+ import type { VariantValue } from './core'
12
5
 
13
6
  type WithEmptyObject<V> = keyof V extends never ? {} : V
14
7
 
15
- export type ExtractBreakpoints<T, B extends Breakpoints> = T extends Partial<Record<keyof B & string, infer V>>
8
+ type ExtractBreakpoints<T> = T extends Partial<Record<keyof UnistylesBreakpoints & string, infer V>>
16
9
  ? WithEmptyObject<V>
17
10
  : T extends (...args: infer A) => infer R
18
- ? (...args: A) => ExtractBreakpoints<R, B>
11
+ ? (...args: A) => ExtractBreakpoints<R>
19
12
  : {
20
13
  [K in keyof T]: T[K] extends (...args: infer A) => infer R
21
- ? (...args: A) => ExtractBreakpoints<R, B>
14
+ ? (...args: A) => ExtractBreakpoints<R>
22
15
  : T[K] extends object
23
- ? ExtractBreakpoints<T[K], B>
16
+ ? ExtractBreakpoints<T[K]>
24
17
  : T[K]
25
18
  }
26
19
 
27
- export type RemoveKeysWithPrefix<T, B extends Breakpoints> = T extends (...args: Array<any>) => infer R
28
- ? (...args: Parameters<T>) => RemoveKeysWithPrefix<R, B>
20
+ type FlattenObjectTypes<T> = T[keyof T]
21
+ type RemoveKeysWithPrefix<T> = T extends (...args: Array<any>) => infer R
22
+ ? (...args: Parameters<T>) => RemoveKeysWithPrefix<R>
29
23
  : T extends object
30
- ? T extends Record<string, infer _V>
31
- ? { [K in keyof T as K extends MediaQueries ? keyof B & string : K]: RemoveKeysWithPrefix<T[K], B> }
32
- : { [K in keyof T]: RemoveKeysWithPrefix<T[K], B> }
24
+ ? T extends OpaqueColorValue
25
+ ? string
26
+ : T extends Record<string, infer _V>
27
+ ? T extends { variants: VariantValue }
28
+ ? RemoveKeysWithPrefix<FlattenObjectTypes<T['variants']>>
29
+ : { [K in keyof T as K extends MediaQuery ? keyof UnistylesBreakpoints & string : K]: RemoveKeysWithPrefix<T[K]> }
30
+ : { [K in keyof T]: RemoveKeysWithPrefix<T[K]> }
33
31
  : T
32
+
33
+ export type ReactNativeStyleSheet<T> = ExtractBreakpoints<RemoveKeysWithPrefix<T>>
@@ -0,0 +1,2 @@
1
+ export type Optional<T> = T | undefined
2
+ export type Nullable<T> = T | null
package/src/types/core.ts CHANGED
@@ -14,14 +14,19 @@ import type {
14
14
  TranslateYTransform
15
15
  } from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
16
16
  import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
17
- import type { Breakpoints } from './breakpoints'
18
- import type { MediaQueries } from './mediaQueries'
17
+ import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
18
+ import type { MediaQuery } from './mq'
19
19
 
20
20
  type ShadowOffset = {
21
21
  width: number,
22
22
  height: number
23
23
  }
24
24
 
25
+ export type ScreenSize = {
26
+ width: number,
27
+ height: number
28
+ }
29
+
25
30
  type TransformStyles =
26
31
  & PerpectiveTransform
27
32
  & RotateTransform
@@ -37,37 +42,50 @@ type TransformStyles =
37
42
  & SkewYTransform
38
43
  & MatrixTransform
39
44
 
40
- type UnistyleNested<B> = {
41
- shadowOffset?: DeepUniStyle<ShadowOffset, B>,
42
- textShadowOffset?: DeepUniStyle<ShadowOffset, B>,
43
- transform?: Array<DeepUniStyle<TransformStyles, B>>
45
+ type UnistyleNested = {
46
+ shadowOffset?: DeepUniStyle<ShadowOffset>,
47
+ textShadowOffset?: DeepUniStyle<ShadowOffset>,
48
+ transform?: Array<DeepUniStyle<TransformStyles>>
44
49
  }
45
50
 
46
- type UniStyle<V, B> = {
47
- [innerKey in keyof B]?: V
51
+ type UniStyle<V> = {
52
+ [innerKey in keyof UnistylesBreakpoints]?: V
48
53
  } | {
49
- [innerKey in MediaQueries]?: V
50
- } | V
54
+ [innerKey in MediaQuery]: V
55
+ } | {
56
+ [innerKey in 'variants']?: {
57
+ [variant: string]: V
58
+ }
59
+ }
51
60
 
52
- type DeepUniStyle<T, B> = {
53
- [K in keyof T]?: UniStyle<T[K], B>
61
+ type DeepUniStyle<T> = {
62
+ [K in keyof T]?: UniStyle<T[K]> | T[K]
54
63
  }
55
64
 
56
65
  // these props are treated differently to nest breakpoints and media queries
57
66
  type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
58
67
 
59
- type UnistyleView<B> = DeepUniStyle<Omit<ViewStyle, NestedTypes>, B>
60
- type UnistyleText<B> = DeepUniStyle<Omit<TextStyle, NestedTypes>, B>
61
- type UnistyleImage<B> = DeepUniStyle<Omit<ImageStyle, NestedTypes>, B>
68
+ type UnistyleView = DeepUniStyle<Omit<ViewStyle, NestedTypes>>
69
+ type UnistyleText = DeepUniStyle<Omit<TextStyle, NestedTypes>>
70
+ type UnistyleImage = DeepUniStyle<Omit<ImageStyle, NestedTypes>>
62
71
 
63
- export type StaticStyles<B extends Breakpoints> =
64
- | UnistyleView<B>
65
- | UnistyleText<B>
66
- | UnistyleImage<B>
67
- & UnistyleNested<B>
72
+ export type StaticStyles =
73
+ | UnistyleView
74
+ | UnistyleText
75
+ | UnistyleImage
76
+ & UnistyleNested
77
+
78
+ export type CustomNamedStyles<T> = {
79
+ [K in keyof T]: T[K] extends (...args: infer A) => StaticStyles
80
+ ? (...args: A) => StaticStyles
81
+ : StaticStyles
82
+ }
68
83
 
69
- export type CustomNamedStyles<T, B extends Breakpoints> = {
70
- [K in keyof T]: T[K] extends (...args: infer A) => StaticStyles<B>
71
- ? (...args: A) => StaticStyles<B>
72
- : StaticStyles<B>
84
+ export type RNValue = number | string | undefined
85
+ export type VariantValue = Record<string, RNValue>
86
+ export type NestedStyle = Record<keyof UnistylesBreakpoints | MediaQuery | 'variants', RNValue> & {
87
+ variants: VariantValue
73
88
  }
89
+ export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | MediaQuery | 'variants', RNValue | VariantValue]>
90
+ export type UnistylesTheme = UnistylesThemes[keyof UnistylesThemes]
91
+ export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
@@ -1,9 +1,6 @@
1
- export type { CustomNamedStyles } from './core'
2
- export type {
3
- ScreenSize,
4
- Breakpoints,
5
- CreateStylesFactory,
6
- ExtractBreakpoints,
7
- RemoveKeysWithPrefix,
8
- SortedBreakpointEntries
9
- } from './breakpoints'
1
+ export * from './normalizer'
2
+ export * from './unistyles'
3
+ export type { Optional, Nullable } from './common'
4
+ export type { MediaQuery } from './mq'
5
+ export type { CustomNamedStyles, NestedStylePairs, UnistylesTheme, CreateStylesFactory, ScreenSize, NestedStyle, RNValue } from './core'
6
+ export type { ReactNativeStyleSheet } from './breakpoints'
@@ -0,0 +1,3 @@
1
+ import { MQSymbol } from '../utils'
2
+
3
+ export type MediaQuery = typeof MQSymbol
@@ -0,0 +1,29 @@
1
+ import type { ShadowStyleIOS, TextStyle, TransformsStyle } from 'react-native'
2
+
3
+ type TransformArrayElement<T> = T extends Array<infer U> ? U : never
4
+ type BoxShadow = Required<ShadowStyleIOS>
5
+ type TextShadow = Required<Pick<TextStyle, 'textShadowColor' | 'textShadowOffset' | 'textShadowRadius'>>
6
+ type Transforms = Array<TransformArrayElement<TransformsStyle['transform']>>
7
+
8
+ type NormalizedBoxShadow = {
9
+ shadowColor: undefined,
10
+ shadowOffset: undefined,
11
+ shadowOpacity: undefined,
12
+ shadowRadius: undefined,
13
+ boxShadow?: string
14
+ }
15
+
16
+ type NormalizedTextShadow = {
17
+ textShadowColor: undefined
18
+ textShadowOffset: undefined
19
+ textShadowRadius: undefined,
20
+ textShadow?: string
21
+ }
22
+
23
+ export type {
24
+ BoxShadow,
25
+ TextShadow,
26
+ Transforms,
27
+ NormalizedBoxShadow,
28
+ NormalizedTextShadow
29
+ }