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

Sign up to get free protection for your applications and to get access to all the features.
Files changed (253) hide show
  1. package/cxx/UnistylesRuntime.cpp +255 -0
  2. package/cxx/UnistylesRuntime.h +60 -0
  3. package/ios/UnistylesHelpers.h +4 -0
  4. package/ios/UnistylesHelpers.mm +20 -0
  5. package/ios/UnistylesModule.h +10 -0
  6. package/ios/UnistylesModule.mm +158 -0
  7. package/lib/commonjs/common.js +26 -0
  8. package/lib/commonjs/common.js.map +1 -0
  9. package/lib/commonjs/core/UnistyleRegistry.js +38 -0
  10. package/lib/commonjs/core/UnistyleRegistry.js.map +1 -0
  11. package/lib/commonjs/core/Unistyles.js +31 -0
  12. package/lib/commonjs/core/Unistyles.js.map +1 -0
  13. package/lib/commonjs/core/UnistylesModule.js +9 -0
  14. package/lib/commonjs/core/UnistylesModule.js.map +1 -0
  15. package/lib/commonjs/core/UnistylesRuntime.js +66 -0
  16. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -0
  17. package/lib/commonjs/core/index.js +13 -0
  18. package/lib/commonjs/core/index.js.map +1 -0
  19. package/lib/commonjs/createStyleSheet.js +14 -0
  20. package/lib/commonjs/createStyleSheet.js.map +1 -0
  21. package/lib/commonjs/global.js +2 -0
  22. package/lib/commonjs/global.js.map +1 -0
  23. package/lib/commonjs/hooks/index.js +10 -3
  24. package/lib/commonjs/hooks/index.js.map +1 -1
  25. package/lib/commonjs/hooks/useInitialTheme.js +17 -0
  26. package/lib/commonjs/hooks/useInitialTheme.js.map +1 -0
  27. package/lib/commonjs/hooks/useUnistyles.js +54 -0
  28. package/lib/commonjs/hooks/useUnistyles.js.map +1 -0
  29. package/lib/commonjs/index.js +35 -6
  30. package/lib/commonjs/index.js.map +1 -1
  31. package/lib/commonjs/types/common.js +2 -0
  32. package/lib/commonjs/types/{mediaQueries.js.map → common.js.map} +1 -1
  33. package/lib/commonjs/types/index.js +11 -0
  34. package/lib/commonjs/types/index.js.map +1 -1
  35. package/lib/commonjs/types/mq.js +6 -0
  36. package/lib/{module/types/mediaQueries.js.map → commonjs/types/mq.js.map} +1 -1
  37. package/lib/commonjs/types/unistyles.js +6 -0
  38. package/lib/commonjs/types/unistyles.js.map +1 -0
  39. package/lib/commonjs/useStyles.js +49 -0
  40. package/lib/commonjs/useStyles.js.map +1 -0
  41. package/lib/commonjs/utils/breakpoints.js +44 -82
  42. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  43. package/lib/commonjs/utils/common.js +6 -1
  44. package/lib/commonjs/utils/common.js.map +1 -1
  45. package/lib/commonjs/utils/index.js +25 -30
  46. package/lib/commonjs/utils/index.js.map +1 -1
  47. package/lib/commonjs/utils/mq.js +89 -0
  48. package/lib/commonjs/utils/mq.js.map +1 -0
  49. package/lib/commonjs/utils/mqParser.js +86 -0
  50. package/lib/commonjs/utils/mqParser.js.map +1 -0
  51. package/lib/commonjs/utils/styles.js +6 -53
  52. package/lib/commonjs/utils/styles.js.map +1 -1
  53. package/lib/commonjs/utils/variants.js +20 -0
  54. package/lib/commonjs/utils/variants.js.map +1 -0
  55. package/lib/module/common.js +22 -0
  56. package/lib/module/common.js.map +1 -0
  57. package/lib/module/core/UnistyleRegistry.js +31 -0
  58. package/lib/module/core/UnistyleRegistry.js.map +1 -0
  59. package/lib/module/core/Unistyles.js +25 -0
  60. package/lib/module/core/Unistyles.js.map +1 -0
  61. package/lib/module/core/UnistylesModule.js +3 -0
  62. package/lib/module/core/UnistylesModule.js.map +1 -0
  63. package/lib/module/core/UnistylesRuntime.js +59 -0
  64. package/lib/module/core/UnistylesRuntime.js.map +1 -0
  65. package/lib/module/core/index.js +2 -0
  66. package/lib/module/core/index.js.map +1 -0
  67. package/lib/module/createStyleSheet.js +7 -0
  68. package/lib/module/createStyleSheet.js.map +1 -0
  69. package/lib/module/global.js +2 -0
  70. package/lib/module/global.js.map +1 -0
  71. package/lib/module/hooks/index.js +2 -1
  72. package/lib/module/hooks/index.js.map +1 -1
  73. package/lib/module/hooks/useInitialTheme.js +10 -0
  74. package/lib/module/hooks/useInitialTheme.js.map +1 -0
  75. package/lib/module/hooks/useUnistyles.js +47 -0
  76. package/lib/module/hooks/useUnistyles.js.map +1 -0
  77. package/lib/module/index.js +13 -2
  78. package/lib/module/index.js.map +1 -1
  79. package/lib/module/types/common.js +2 -0
  80. package/lib/module/types/common.js.map +1 -0
  81. package/lib/module/types/index.js +1 -0
  82. package/lib/module/types/index.js.map +1 -1
  83. package/lib/module/types/mq.js +2 -0
  84. package/lib/module/types/mq.js.map +1 -0
  85. package/lib/module/types/unistyles.js +2 -0
  86. package/lib/module/types/unistyles.js.map +1 -0
  87. package/lib/module/useStyles.js +42 -0
  88. package/lib/module/useStyles.js.map +1 -0
  89. package/lib/module/utils/breakpoints.js +43 -82
  90. package/lib/module/utils/breakpoints.js.map +1 -1
  91. package/lib/module/utils/common.js +5 -0
  92. package/lib/module/utils/common.js.map +1 -1
  93. package/lib/module/utils/index.js +5 -3
  94. package/lib/module/utils/index.js.map +1 -1
  95. package/lib/module/utils/mq.js +83 -0
  96. package/lib/module/utils/mq.js.map +1 -0
  97. package/lib/module/utils/mqParser.js +79 -0
  98. package/lib/module/utils/mqParser.js.map +1 -0
  99. package/lib/module/utils/styles.js +7 -55
  100. package/lib/module/utils/styles.js.map +1 -1
  101. package/lib/module/utils/variants.js +13 -0
  102. package/lib/module/utils/variants.js.map +1 -0
  103. package/lib/typescript/src/common.d.ts +17 -0
  104. package/lib/typescript/src/common.d.ts.map +1 -0
  105. package/lib/typescript/src/core/UnistyleRegistry.d.ts +15 -0
  106. package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -0
  107. package/lib/typescript/src/core/Unistyles.d.ts +13 -0
  108. package/lib/typescript/src/core/Unistyles.d.ts.map +1 -0
  109. package/lib/typescript/src/core/UnistylesModule.d.ts +6 -0
  110. package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -0
  111. package/lib/typescript/src/core/UnistylesRuntime.d.ts +24 -0
  112. package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -0
  113. package/lib/typescript/src/core/index.d.ts +2 -0
  114. package/lib/typescript/src/core/index.d.ts.map +1 -0
  115. package/lib/typescript/src/createStyleSheet.d.ts +3 -0
  116. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -0
  117. package/lib/typescript/src/global.d.ts +7 -0
  118. package/lib/typescript/src/global.d.ts.map +1 -0
  119. package/lib/typescript/src/hooks/index.d.ts +2 -1
  120. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  121. package/lib/typescript/src/hooks/useInitialTheme.d.ts +3 -0
  122. package/lib/typescript/src/hooks/useInitialTheme.d.ts.map +1 -0
  123. package/lib/typescript/src/hooks/useUnistyles.d.ts +12 -0
  124. package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -0
  125. package/lib/typescript/src/index.d.ts +14 -2
  126. package/lib/typescript/src/index.d.ts.map +1 -1
  127. package/lib/typescript/src/types/breakpoints.d.ts +12 -13
  128. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  129. package/lib/typescript/src/types/common.d.ts +3 -0
  130. package/lib/typescript/src/types/common.d.ts.map +1 -0
  131. package/lib/typescript/src/types/core.d.ts +34 -18
  132. package/lib/typescript/src/types/core.d.ts.map +1 -1
  133. package/lib/typescript/src/types/index.d.ts +5 -2
  134. package/lib/typescript/src/types/index.d.ts.map +1 -1
  135. package/lib/typescript/src/types/mq.d.ts +3 -0
  136. package/lib/typescript/src/types/mq.d.ts.map +1 -0
  137. package/lib/typescript/src/types/unistyles.d.ts +37 -0
  138. package/lib/typescript/src/types/unistyles.d.ts.map +1 -0
  139. package/lib/typescript/src/useStyles.d.ts +10 -0
  140. package/lib/typescript/src/useStyles.d.ts.map +1 -0
  141. package/lib/typescript/src/utils/breakpoints.d.ts +5 -62
  142. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  143. package/lib/typescript/src/utils/common.d.ts +5 -0
  144. package/lib/typescript/src/utils/common.d.ts.map +1 -1
  145. package/lib/typescript/src/utils/index.d.ts +5 -3
  146. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  147. package/lib/typescript/src/utils/mq.d.ts +21 -0
  148. package/lib/typescript/src/utils/mq.d.ts.map +1 -0
  149. package/lib/typescript/src/utils/mqParser.d.ts +3 -0
  150. package/lib/typescript/src/utils/mqParser.d.ts.map +1 -0
  151. package/lib/typescript/src/utils/styles.d.ts +4 -49
  152. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  153. package/lib/typescript/src/utils/variants.d.ts +3 -0
  154. package/lib/typescript/src/utils/variants.d.ts.map +1 -0
  155. package/package.json +30 -19
  156. package/react-native-unistyles.podspec +22 -0
  157. package/src/common.ts +19 -0
  158. package/src/core/UnistyleRegistry.ts +41 -0
  159. package/src/core/Unistyles.ts +34 -0
  160. package/src/core/UnistylesModule.ts +7 -0
  161. package/src/core/UnistylesRuntime.ts +73 -0
  162. package/src/core/index.ts +1 -0
  163. package/src/createStyleSheet.ts +9 -0
  164. package/src/global.ts +5 -0
  165. package/src/hooks/index.ts +2 -1
  166. package/src/hooks/useInitialTheme.ts +11 -0
  167. package/src/hooks/useUnistyles.ts +55 -0
  168. package/src/index.ts +30 -2
  169. package/src/types/breakpoints.ts +16 -19
  170. package/src/types/common.ts +2 -0
  171. package/src/types/core.ts +42 -24
  172. package/src/types/index.ts +5 -9
  173. package/src/types/mq.ts +3 -0
  174. package/src/types/unistyles.ts +45 -0
  175. package/src/useStyles.ts +56 -0
  176. package/src/utils/breakpoints.ts +57 -88
  177. package/src/utils/common.ts +6 -0
  178. package/src/utils/index.ts +5 -10
  179. package/src/utils/mq.ts +106 -0
  180. package/src/utils/mqParser.ts +99 -0
  181. package/src/utils/styles.ts +17 -64
  182. package/src/utils/variants.ts +17 -0
  183. package/lib/commonjs/UnistylesTheme.js +0 -21
  184. package/lib/commonjs/UnistylesTheme.js.map +0 -1
  185. package/lib/commonjs/createUnistyles.js +0 -57
  186. package/lib/commonjs/createUnistyles.js.map +0 -1
  187. package/lib/commonjs/hooks/useDimensions.js +0 -10
  188. package/lib/commonjs/hooks/useDimensions.js.map +0 -1
  189. package/lib/commonjs/hooks/useDimensions.web.js +0 -34
  190. package/lib/commonjs/hooks/useDimensions.web.js.map +0 -1
  191. package/lib/commonjs/types/mediaQueries.js +0 -2
  192. package/lib/commonjs/utils/mediaQueries.js +0 -189
  193. package/lib/commonjs/utils/mediaQueries.js.map +0 -1
  194. package/lib/module/UnistylesTheme.js +0 -12
  195. package/lib/module/UnistylesTheme.js.map +0 -1
  196. package/lib/module/createUnistyles.js +0 -50
  197. package/lib/module/createUnistyles.js.map +0 -1
  198. package/lib/module/hooks/useDimensions.js +0 -3
  199. package/lib/module/hooks/useDimensions.js.map +0 -1
  200. package/lib/module/hooks/useDimensions.web.js +0 -27
  201. package/lib/module/hooks/useDimensions.web.js.map +0 -1
  202. package/lib/module/types/mediaQueries.js +0 -2
  203. package/lib/module/utils/mediaQueries.js +0 -176
  204. package/lib/module/utils/mediaQueries.js.map +0 -1
  205. package/lib/typescript/examples/expo/src/App.d.ts +0 -3
  206. package/lib/typescript/examples/expo/src/App.d.ts.map +0 -1
  207. package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts +0 -3
  208. package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts.map +0 -1
  209. package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts +0 -3
  210. package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts.map +0 -1
  211. package/lib/typescript/examples/expo/src/examples/Extreme.d.ts +0 -7
  212. package/lib/typescript/examples/expo/src/examples/Extreme.d.ts.map +0 -1
  213. package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts +0 -3
  214. package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts.map +0 -1
  215. package/lib/typescript/examples/expo/src/examples/Memoization.d.ts +0 -3
  216. package/lib/typescript/examples/expo/src/examples/Memoization.d.ts.map +0 -1
  217. package/lib/typescript/examples/expo/src/examples/Minimal.d.ts +0 -3
  218. package/lib/typescript/examples/expo/src/examples/Minimal.d.ts.map +0 -1
  219. package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts +0 -3
  220. package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts.map +0 -1
  221. package/lib/typescript/examples/expo/src/examples/PlatformColors.d.ts +0 -3
  222. package/lib/typescript/examples/expo/src/examples/PlatformColors.d.ts.map +0 -1
  223. package/lib/typescript/examples/expo/src/examples/Theme.d.ts +0 -3
  224. package/lib/typescript/examples/expo/src/examples/Theme.d.ts.map +0 -1
  225. package/lib/typescript/examples/expo/src/examples/index.d.ts +0 -9
  226. package/lib/typescript/examples/expo/src/examples/index.d.ts.map +0 -1
  227. package/lib/typescript/examples/expo/src/index.d.ts +0 -2
  228. package/lib/typescript/examples/expo/src/index.d.ts.map +0 -1
  229. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts +0 -8
  230. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts.map +0 -1
  231. package/lib/typescript/examples/expo/src/styles/index.d.ts +0 -46
  232. package/lib/typescript/examples/expo/src/styles/index.d.ts.map +0 -1
  233. package/lib/typescript/examples/expo/src/styles/theme.d.ts +0 -24
  234. package/lib/typescript/examples/expo/src/styles/theme.d.ts.map +0 -1
  235. package/lib/typescript/src/UnistylesTheme.d.ts +0 -9
  236. package/lib/typescript/src/UnistylesTheme.d.ts.map +0 -1
  237. package/lib/typescript/src/createUnistyles.d.ts +0 -10
  238. package/lib/typescript/src/createUnistyles.d.ts.map +0 -1
  239. package/lib/typescript/src/hooks/useDimensions.d.ts +0 -3
  240. package/lib/typescript/src/hooks/useDimensions.d.ts.map +0 -1
  241. package/lib/typescript/src/hooks/useDimensions.web.d.ts +0 -3
  242. package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +0 -1
  243. package/lib/typescript/src/types/mediaQueries.d.ts +0 -8
  244. package/lib/typescript/src/types/mediaQueries.d.ts.map +0 -1
  245. package/lib/typescript/src/utils/mediaQueries.d.ts +0 -130
  246. package/lib/typescript/src/utils/mediaQueries.d.ts.map +0 -1
  247. package/src/UnistylesTheme.tsx +0 -17
  248. package/src/__tests__/createUnistyles.spec.tsx +0 -192
  249. package/src/createUnistyles.ts +0 -70
  250. package/src/hooks/useDimensions.ts +0 -4
  251. package/src/hooks/useDimensions.web.ts +0 -34
  252. package/src/types/mediaQueries.ts +0 -10
  253. package/src/utils/mediaQueries.ts +0 -201
@@ -1,36 +1,33 @@
1
1
  import type { OpaqueColorValue } from 'react-native'
2
- import type { MediaQueries } from './mediaQueries'
3
-
4
- export type Breakpoints = Record<string, number>
5
- export type SortedBreakpointEntries<B extends Breakpoints> = [[keyof B & string, number]]
6
-
7
- export type ScreenSize = {
8
- width: number,
9
- height: number
10
- }
11
-
12
- export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
2
+ import type { UnistylesBreakpoints } from '../global'
3
+ import type { MediaQuery } from './mq'
4
+ import type { VariantValue } from './core'
13
5
 
14
6
  type WithEmptyObject<V> = keyof V extends never ? {} : V
15
7
 
16
- 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>>
17
9
  ? WithEmptyObject<V>
18
10
  : T extends (...args: infer A) => infer R
19
- ? (...args: A) => ExtractBreakpoints<R, B>
11
+ ? (...args: A) => ExtractBreakpoints<R>
20
12
  : {
21
13
  [K in keyof T]: T[K] extends (...args: infer A) => infer R
22
- ? (...args: A) => ExtractBreakpoints<R, B>
14
+ ? (...args: A) => ExtractBreakpoints<R>
23
15
  : T[K] extends object
24
- ? ExtractBreakpoints<T[K], B>
16
+ ? ExtractBreakpoints<T[K]>
25
17
  : T[K]
26
18
  }
27
19
 
28
- export type RemoveKeysWithPrefix<T, B extends Breakpoints> = T extends (...args: Array<any>) => infer R
29
- ? (...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>
30
23
  : T extends object
31
24
  ? T extends OpaqueColorValue
32
25
  ? string
33
26
  : T extends Record<string, infer _V>
34
- ? { [K in keyof T as K extends MediaQueries ? keyof B & string : K]: RemoveKeysWithPrefix<T[K], B> }
35
- : { [K in keyof T]: RemoveKeysWithPrefix<T[K], B> }
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]> }
36
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,10 +1,6 @@
1
1
  export * from './normalizer'
2
- export type { CustomNamedStyles } from './core'
3
- export type {
4
- ScreenSize,
5
- Breakpoints,
6
- CreateStylesFactory,
7
- ExtractBreakpoints,
8
- RemoveKeysWithPrefix,
9
- SortedBreakpointEntries
10
- } from './breakpoints'
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,45 @@
1
+ import { CxxUnistylesEventTypes, ScreenOrientation } from '../common'
2
+ import type { UnistylesThemes, UnistylesBreakpoints } from '../global'
3
+ import type { ScreenSize } from './core'
4
+ import type { Optional } from './common'
5
+
6
+ export type ColorSchemeName = Optional<'light' | 'dark'>
7
+
8
+ export type UnistylesConfig = {
9
+ adaptiveThemes?: boolean
10
+ }
11
+
12
+ export type UnistylesBridge = {
13
+ // getters
14
+ screenWidth: number,
15
+ screenHeight: number,
16
+ hasAdaptiveThemes: boolean,
17
+ themeName: keyof UnistylesThemes,
18
+ breakpoint: keyof UnistylesBreakpoints,
19
+ colorScheme: ColorSchemeName,
20
+ sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>,
21
+
22
+ // setters
23
+ themes: Array<keyof UnistylesThemes>,
24
+ useBreakpoints(breakpoints: UnistylesBreakpoints): void,
25
+ useTheme(name: keyof UnistylesThemes): void,
26
+ useAdaptiveThemes(enable: boolean): void
27
+ }
28
+
29
+ export type UnistylesThemeEvent = {
30
+ type: CxxUnistylesEventTypes.Theme,
31
+ payload: {
32
+ themeName: keyof UnistylesThemes
33
+ }
34
+ }
35
+
36
+ export type UnistylesMobileLayoutEvent = {
37
+ type: CxxUnistylesEventTypes.Layout,
38
+ payload: {
39
+ screen: ScreenSize,
40
+ breakpoint: keyof UnistylesBreakpoints,
41
+ orientation: ScreenOrientation
42
+ }
43
+ }
44
+
45
+ export type UnistylesEvents = UnistylesThemeEvent | UnistylesMobileLayoutEvent
@@ -0,0 +1,56 @@
1
+ import { useMemo } from 'react'
2
+ import { StyleSheet } from 'react-native'
3
+ import { parseStyle, proxifyFunction } from './utils'
4
+ import type { CreateStylesFactory, CustomNamedStyles, ReactNativeStyleSheet, UnistylesTheme } from './types'
5
+ import { useUnistyles } from './hooks'
6
+ import type { UnistylesBreakpoints } from './global'
7
+
8
+ type ParsedStylesheet<ST extends CustomNamedStyles<ST>> = {
9
+ theme: UnistylesTheme,
10
+ breakpoint: keyof UnistylesBreakpoints,
11
+ styles: ReactNativeStyleSheet<ST>
12
+ }
13
+
14
+ export const useStyles = <ST extends CustomNamedStyles<ST>>(
15
+ stylesheet?: ST | CreateStylesFactory<ST, UnistylesTheme>,
16
+ variant?: string
17
+ ): ParsedStylesheet<ST> => {
18
+ const { theme, layout } = useUnistyles()
19
+ const { screenSize, breakpoint } = layout
20
+
21
+ if (!stylesheet) {
22
+ return {
23
+ theme,
24
+ breakpoint,
25
+ styles: {} as ReactNativeStyleSheet<ST>
26
+ }
27
+ }
28
+
29
+ const parsedStyles = useMemo(() => typeof stylesheet === 'function'
30
+ ? stylesheet(theme)
31
+ : stylesheet, [theme, stylesheet])
32
+
33
+ const dynamicStyleSheet = useMemo(() => Object
34
+ .entries(parsedStyles)
35
+ .reduce((acc, [key, value]) => {
36
+ const style = value as CustomNamedStyles<ST>
37
+
38
+ if (typeof value === 'function') {
39
+ return {
40
+ ...acc,
41
+ [key]: proxifyFunction(value, breakpoint, screenSize, variant)
42
+ }
43
+ }
44
+
45
+ return StyleSheet.create({
46
+ ...acc,
47
+ [key]: parseStyle<ST>(style, breakpoint, screenSize, variant)
48
+ })
49
+ }, {} as ST), [breakpoint, screenSize, parsedStyles, variant]) as ReactNativeStyleSheet<ST>
50
+
51
+ return {
52
+ theme,
53
+ breakpoint,
54
+ styles: dynamicStyleSheet
55
+ }
56
+ }
@@ -1,37 +1,22 @@
1
- import { throwError } from './common'
2
- import type { Breakpoints, ScreenSize, SortedBreakpointEntries } from '../types'
3
- import { getKeyForCustomMediaQuery, isMediaQuery } from './mediaQueries'
4
-
5
- /**
6
- * Sorts the breakpoints object based on its numeric values in ascending order and validates them.
7
- *
8
- * This function takes an object where keys represent breakpoint names and values are numeric.
9
- * It returns a new object with the same keys but sorted based on their corresponding numeric values.
10
- * Additionally, it validates that:
11
- * 1. The first breakpoint starts with a value of 0.
12
- * 2. No duplicate breakpoint values exist.
13
- *
14
- * If the validation fails, appropriate error messages are logged to the console.
15
- *
16
- * @template B - An object type where keys are strings and values are numbers.
17
- * @param {B} breakpoints - The breakpoints object to be sorted and validated.
18
- * @returns {B} A new object with sorted and validated breakpoints.
19
- *
20
- * @example
21
- * const input = { md: 768, lg: 1024, sm: 0 }
22
- * sortAndValidateBreakpoints(input) // returns { sm: 0, md: 768, lg: 1024 }
23
- */
24
- export const sortAndValidateBreakpoints = <B extends Breakpoints>(breakpoints: B): B => {
1
+ import { unistyles } from '../core'
2
+ import { isMobile, Orientation, throwError } from './common'
3
+ import type { NestedStyle, NestedStylePairs, RNValue } from '../types'
4
+ import type { UnistylesBreakpoints } from '../global'
5
+ import { ScreenOrientation } from '../common'
6
+ import { getKeyForUnistylesMediaQuery } from './mqParser'
7
+ import { getKeyForVariant } from './variants'
8
+
9
+ export const sortAndValidateBreakpoints = (breakpoints: UnistylesBreakpoints): UnistylesBreakpoints => {
25
10
  const sortedPairs = Object
26
11
  .entries(breakpoints)
27
12
  .sort((breakpoint1, breakpoint2) => {
28
13
  const [, value1] = breakpoint1
29
14
  const [, value2] = breakpoint2
30
15
 
31
- return value1 - value2
16
+ return (value1 as number) - (value2 as number)
32
17
  })
33
18
 
34
- const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs)) as B
19
+ const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs)) as UnistylesBreakpoints
35
20
  const breakpointValues = Object.values(sortedBreakpoints)
36
21
  const [firstBreakpoint] = breakpointValues
37
22
 
@@ -46,25 +31,10 @@ export const sortAndValidateBreakpoints = <B extends Breakpoints>(breakpoints: B
46
31
  return sortedBreakpoints
47
32
  }
48
33
 
49
- /**
50
- * Determines the appropriate breakpoint key for a given screen width based on provided breakpoints.
51
- *
52
- * This function takes a screen width and an object of breakpoints. It returns the key of the breakpoint
53
- * that the screen width falls into. The breakpoints are assumed to be sorted in ascending order.
54
- *
55
- * @template B - An object type where keys are strings and values are numbers representing screen widths.
56
- * @param {number} width - The screen width to determine the breakpoint for.
57
- * @param breakpointEntries - sorted pairs of breakpoints
58
- * @returns {keyof B & string} The key of the breakpoint that the screen width falls into.
59
- *
60
- * @example
61
- * const breakpoints = { sm: 0, md: 768, lg: 1024 }
62
- * getBreakpointFromScreenWidth(800, breakpoints) // returns 'md'
63
- */
64
- export const getBreakpointFromScreenWidth = <B extends Breakpoints>(width: number, breakpointEntries: SortedBreakpointEntries<B>): keyof B & string => {
34
+ export const getBreakpointFromScreenWidth = (width: number, breakpointEntries: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>): keyof UnistylesBreakpoints & string => {
65
35
  const [key] = breakpointEntries
66
36
  .find(([, value], index, otherBreakpoints) => {
67
- const minVal = value
37
+ const minVal = value as number
68
38
  const maxVal = otherBreakpoints[index + 1]?.[1]
69
39
 
70
40
  if (!maxVal) {
@@ -72,69 +42,68 @@ export const getBreakpointFromScreenWidth = <B extends Breakpoints>(width: numbe
72
42
  }
73
43
 
74
44
  return width >= minVal && width < maxVal
75
- }) as [keyof B & string, number]
45
+ }) as [keyof UnistylesBreakpoints & string, number]
76
46
 
77
47
  return key
78
48
  }
79
49
 
80
- /**
81
- * Retrieves the value associated with a given breakpoint or custom media query based on the provided screen size.
82
- *
83
- * The function first checks for custom media queries. If a matching custom media query is found, its associated value is returned.
84
- * If no custom media query matches, the function then checks for a direct breakpoint match.
85
- * If there's no direct breakpoint match, the function simulates CSS cascading to find the closest matching breakpoint.
86
- *
87
- * @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
88
- *
89
- * @param {Record<keyof B & string, string | number>} value - An object containing values associated with breakpoints or custom media queries.
90
- * @param {keyof B & string} breakpoint - The breakpoint name to check against.
91
- * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
92
- * @param breakpointPairs - sorted pairs of breakpoints
93
- *
94
- * @returns {string | number | undefined} Returns the value associated with the matching breakpoint or custom media query, or `undefined` if no match is found.
95
- *
96
- * @example
97
- *
98
- * const values = { ':w[200]': 'value1', sm: 'value2', md: 'value3' }
99
- * const screenSize = { width: 250, height: 400 }
100
- * const breakpoints = { sm: 300, md: 600, lg: 900 }
101
- *
102
- * getValueForBreakpoint(values, 'sm', screenSize, breakpoints); // 'value1'
103
- */
104
- export const getValueForBreakpoint = <B extends Breakpoints>(
105
- value: Record<keyof B & string, string | number | undefined>,
106
- breakpoint: keyof B & string,
107
- screenSize: ScreenSize,
108
- breakpointPairs: SortedBreakpointEntries<B>
109
- ): string | number | undefined => {
110
- // the highest priority is for custom media queries
111
- const customMediaQueries = Object
112
- .entries(value)
113
- .filter(([key]) => isMediaQuery(key))
114
- const customMediaQueryKey = getKeyForCustomMediaQuery(customMediaQueries, screenSize)
115
-
116
- if (customMediaQueryKey && customMediaQueryKey in value) {
50
+ // todo move it to different file
51
+ export const getValueForNestedStyle = (value: NestedStyle, variant?: string): RNValue => {
52
+ // the highest priority is for variants
53
+ const variantKey = getKeyForVariant(value, variant)
54
+
55
+ if (variantKey) {
56
+ return value.variants[variantKey as string]
57
+ }
58
+
59
+ // then custom media queries
60
+ const customMediaQueryKey = getKeyForUnistylesMediaQuery(
61
+ Object.entries(value) as NestedStylePairs,
62
+ unistyles.runtime.screen
63
+ ) as keyof typeof value
64
+
65
+ if (customMediaQueryKey) {
117
66
  return value[customMediaQueryKey]
118
67
  }
119
68
 
120
- // if no custom media query, or didn't match, proceed with defined breakpoints
121
- const unifiedKey = breakpoint
122
- const directBreakpoint = value[unifiedKey]
69
+ // at this point user didn't use custom media queries (:w, :h)
70
+ // check if user defined any breakpoints
71
+ const hasBreakpoints = unistyles.runtime.sortedBreakpoints.length > 0
72
+
73
+ // if not then we can fall back to horizontal and portrait (mobile only)
74
+ if (!hasBreakpoints && isMobile && (Orientation.Landscape in value || Orientation.Portrait in value)) {
75
+ return value[
76
+ unistyles.runtime.orientation === ScreenOrientation.Portrait
77
+ ? Orientation.Portrait
78
+ : Orientation.Landscape
79
+ ]
80
+ }
81
+
82
+ // let's get the current breakpoint
83
+ const breakpoint = unistyles.runtime.breakpoint
84
+
85
+ if (!breakpoint) {
86
+ return undefined
87
+ }
88
+
89
+ // if user defined breakpoints, then we look for the valid one
90
+ const directBreakpoint = value[breakpoint]
123
91
 
124
92
  // if there is a direct key like 'sm' or 'md', or value for this key exists but its undefined
125
- if (directBreakpoint || (unifiedKey in value)) {
93
+ if (directBreakpoint || (breakpoint in value)) {
126
94
  return directBreakpoint
127
95
  }
128
96
 
129
- // there is no direct hit for breakpoint nor media-query, so let's simulate CSS cascading
97
+ // there is no direct hit for breakpoint nor media-query, let's simulate CSS cascading
98
+ const breakpointPairs = unistyles.runtime.sortedBreakpoints
130
99
  const currentBreakpoint = breakpointPairs
131
- .findIndex(([key]) => key === unifiedKey)
100
+ .findIndex(([key]) => key === breakpoint)
132
101
 
133
102
  const availableBreakpoints = breakpointPairs
134
103
  .filter(([key], index) => index < currentBreakpoint && key && key in value)
135
104
  .map(([key]) => key)
136
105
 
137
106
  return breakpointPairs.length > 0
138
- ? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof B & string]
107
+ ? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof UnistylesBreakpoints & string]
139
108
  : undefined
140
109
  }
@@ -8,7 +8,13 @@ export const warn = (message: string) => {
8
8
  console.warn(`🦄 [react-native-unistyles]: ${message}`)
9
9
  }
10
10
 
11
+ export const isMobile = Platform.OS === 'android' || Platform.OS === 'ios'
11
12
  export const isWeb = Platform.OS === 'web'
12
13
  export const isIOS = Platform.OS === 'ios'
13
14
  export const isAndroid = Platform.OS === 'android'
14
15
  export const isServer = typeof window === 'undefined'
16
+
17
+ export const Orientation = {
18
+ Landscape: 'landscape',
19
+ Portrait: 'portrait'
20
+ } as const
@@ -1,13 +1,8 @@
1
1
  export { normalizeStyles } from './normalizeStyles'
2
2
  export * from './normalizer'
3
- export { getBreakpointFromScreenWidth, sortAndValidateBreakpoints, getValueForBreakpoint } from './breakpoints'
3
+ export { mq, MQSymbol } from './mq'
4
+ export { getKeyForVariant } from './variants'
5
+ export { getKeyForUnistylesMediaQuery } from './mqParser'
6
+ export { getBreakpointFromScreenWidth, sortAndValidateBreakpoints, getValueForNestedStyle } from './breakpoints'
4
7
  export { proxifyFunction, parseStyle } from './styles'
5
- export { isServer } from './common'
6
- export {
7
- extractValues,
8
- getKeyForCustomMediaQuery,
9
- isMediaQuery,
10
- isWithinTheHeight,
11
- isWithinTheWidth,
12
- isWithinTheWidthAndHeight
13
- } from './mediaQueries'
8
+ export { isServer, Orientation } from './common'
@@ -0,0 +1,106 @@
1
+ import type { MediaQuery, Nullable } from '../types'
2
+ import type { UnistylesBreakpoints } from '../global'
3
+ import { unistyles } from '../core'
4
+
5
+ export const MQSymbol = Symbol('unistyles-mq')
6
+
7
+ type MQValue = keyof UnistylesBreakpoints | number
8
+
9
+ type MQHandler = {
10
+ w(wMin?: Nullable<MQValue>, wMax?: MQValue): WidthHandler,
11
+ width(wMin?: Nullable<MQValue>, wMax?: MQValue): WidthHandler,
12
+ h(hMin?: Nullable<MQValue>, hMax?: MQValue): HeightHandler,
13
+ height(hMin?: Nullable<MQValue>, hMax?: MQValue): HeightHandler
14
+ }
15
+
16
+ type HeightHandler = {
17
+ w(wMin?: Nullable<MQValue>, wMax?: MQValue): MediaQuery,
18
+ width(wMin?: Nullable<MQValue>, wMax?: MQValue): MediaQuery
19
+ } & MediaQuery
20
+
21
+ type WidthHandler = {
22
+ h(hMin?: Nullable<MQValue>, hMax?: MQValue): MediaQuery,
23
+ height(hMin?: Nullable<MQValue>, hMax?: MQValue): MediaQuery
24
+ } & MediaQuery
25
+
26
+ type FinalHandler = {
27
+ [MQSymbol]: true
28
+ }
29
+
30
+ enum MQProp {
31
+ toString = 'toString',
32
+ width = 'width',
33
+ height = 'height',
34
+ shortW = 'w',
35
+ shortH = 'h'
36
+ }
37
+
38
+ const getMQValue = (value: Nullable<MQValue>) => {
39
+ if (typeof value === 'number') {
40
+ return value
41
+ }
42
+
43
+ if (value === null) {
44
+ return 0
45
+ }
46
+
47
+ return unistyles.registry.breakpoints[value] ?? 0
48
+ }
49
+
50
+ const widthHandler = (hMin: Nullable<MQValue> = 0, hMax: MQValue = Infinity) => new Proxy<HeightHandler>({} as HeightHandler, {
51
+ get: (target, prop, receiver) => {
52
+ if (prop === Symbol.toPrimitive || prop === MQProp.toString) {
53
+ return () => `:h[${getMQValue(hMin)}, ${getMQValue(hMax)}]`
54
+ }
55
+
56
+ if (prop === MQProp.width || prop === MQProp.shortW) {
57
+ return (wMin: MQValue = 0, wMax: MQValue = Infinity) => new Proxy<FinalHandler>({} as FinalHandler, {
58
+ get: (target, prop, receiver) => {
59
+ if (prop === Symbol.toPrimitive || prop === MQProp.toString) {
60
+ return () => `:w[${getMQValue(wMin)}, ${getMQValue(wMax)}]:h[${getMQValue(hMin)}, ${getMQValue(hMax)}]`
61
+ }
62
+
63
+ return Reflect.get(target, prop, receiver)
64
+ }
65
+ })
66
+ }
67
+
68
+ return Reflect.get(target, prop, receiver)
69
+ }
70
+ })
71
+
72
+ const heightHandler = (wMin: Nullable<MQValue> = 0, wMax: MQValue = Infinity) => new Proxy({} as WidthHandler, {
73
+ get: (target, prop, receiver) => {
74
+ if (prop === Symbol.toPrimitive || prop === MQProp.toString) {
75
+ return () => `:w[${getMQValue(wMin)}, ${getMQValue(wMax)}]`
76
+ }
77
+
78
+ if (prop === MQProp.height || prop === MQProp.shortH) {
79
+ return (hMin: MQValue = 0, hMax: MQValue = Infinity) => new Proxy<FinalHandler>({} as FinalHandler, {
80
+ get: (target, prop, receiver) => {
81
+ if (prop === Symbol.toPrimitive || MQProp.toString) {
82
+ return () => `:w[${getMQValue(wMin)}, ${getMQValue(wMax)}]:h[${getMQValue(hMin)}, ${getMQValue(hMax)}]`
83
+ }
84
+
85
+ return Reflect.get(target, prop, receiver)
86
+ }
87
+ })
88
+ }
89
+
90
+ return Reflect.get(target, prop, receiver)
91
+ }
92
+ })
93
+
94
+ export const mq = new Proxy({} as MQHandler, {
95
+ get: (target, prop, receiver) => {
96
+ if (prop === MQProp.shortW || prop === MQProp.width) {
97
+ return heightHandler
98
+ }
99
+
100
+ if (prop === MQProp.shortH || prop === MQProp.height) {
101
+ return widthHandler
102
+ }
103
+
104
+ return Reflect.get(target, prop, receiver)
105
+ }
106
+ })