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,130 +0,0 @@
1
- import type { ScreenSize } from '../types';
2
- /**
3
- * Extracts numeric values from a coded string.
4
- *
5
- * The function is designed to process strings that have a format like "w[100,200]" or "h[300]".
6
- * It removes characters 'w', 'h', '[', and ']' from the input string and then extracts the numbers.
7
- *
8
- * @param {string} codedValue - The input string to extract values from.
9
- * @returns {Array<number>} An array of extracted numbers. Can contain one or two numbers based on the input format.
10
- *
11
- * @example
12
- * extractValues("w[100,200]") // returns [100, 200]
13
- * extractValues("h[300]") // returns [300]
14
- * extractValues("h[,300]") // returns [0,300]
15
- * extractValues("h[100,]") // returns [100]
16
- */
17
- export declare const extractValues: (codedValue: string) => Array<number>;
18
- /**
19
- * Determines if the given screen size matches the specified breakpoint query.
20
- *
21
- * The function checks if the screen size (width and/or height) falls within the range
22
- * specified by the breakpoint query. The query can specify width (using 'w'), height (using 'h'),
23
- * or both.
24
- *
25
- * @param {string} query - The breakpoint query string. Examples: 'w[100,200]', 'h[300]', 'w[100,200]h[300,400]'.
26
- * @param {ScreenSize} screenSize - The screen size to check against the breakpoint query.
27
- * @returns {boolean} True if the screen size matches the breakpoint query, false otherwise.
28
- *
29
- * @example
30
- * const screenSize = { width: 150, height: 350 }
31
- * isWithinBreakpoint('w[100,200]', screenSize) // returns true
32
- * isWithinBreakpoint('h[400]', screenSize) // returns false
33
- */
34
- export declare const isWithinBreakpoint: (query: string, screenSize: ScreenSize) => boolean;
35
- /**
36
- * Determines if the given width matches the specified width range in the query.
37
- *
38
- * The function checks if the provided width falls within the range specified by the query.
39
- * The query specifies a width range using a format like 'w[100,200]'. If only one value is provided,
40
- * it's treated as a minimum width.
41
- *
42
- * @param {string} query - The width query string. Examples: 'w[100,200]' or 'w[100]'.
43
- * @param {number} width - The width to check against the query.
44
- * @returns {boolean} True if the width matches the query range, false otherwise.
45
- *
46
- * @example
47
- * isWithinTheWidth('w[100,200]', 150) // returns true
48
- * isWithinTheWidth('w[100]', 50) // returns false
49
- * isWithinTheWidth('w[100]', 150) // returns true
50
- */
51
- export declare const isWithinTheWidth: (query: string, width: number) => boolean;
52
- /**
53
- * Determines if the given height matches the specified height range in the query.
54
- *
55
- * The function checks if the provided height falls within the range specified by the query.
56
- * The query specifies a height range using a format like 'h[100,200]'. If only one value is provided,
57
- * it's treated as a minimum height.
58
- *
59
- * @param {string} query - The height query string. Examples: 'h[100,200]' or 'h[100]'.
60
- * @param {number} height - The height to check against the query.
61
- * @returns {boolean} True if the height matches the query range, false otherwise.
62
- *
63
- * @example
64
- * isWithinTheHeight('h[100,200]', 150) // returns true
65
- * isWithinTheHeight('h[100]', 50) // returns false
66
- * isWithinTheHeight('h[100]', 150) // returns true
67
- */
68
- export declare const isWithinTheHeight: (query: string, height: number) => boolean;
69
- /**
70
- * Determines if the given screen size matches both the specified width and height ranges in the query.
71
- *
72
- * The function checks if the provided screen size (both width and height) falls within the ranges
73
- * specified by the query. The query can specify both width and height using a format like 'w[100,200]:h[300,400]'.
74
- *
75
- * @param {string} query - The combined width and height query string. Example: 'w[100,200]:h[300,400]'.
76
- * @param {ScreenSize} screenSize - The screen size to check against the query.
77
- * @returns {boolean} True if the screen size matches both the width and height ranges in the query, false otherwise.
78
- *
79
- * @example
80
- * const screenSize = { width: 150, height: 350 }
81
- * isWithinTheWidthAndHeight('w[100,200]:h[300,400]', screenSize) // returns true
82
- * isWithinTheWidthAndHeight('w[100,200]:h[400,500]', screenSize) // returns false
83
- */
84
- export declare const isWithinTheWidthAndHeight: (query: string, screenSize: ScreenSize) => boolean;
85
- /**
86
- * Checks if the given query string is a valid custom media query.
87
- *
88
- * The valid custom media query formats include:
89
- * - :w[200]
90
- * - :w[0, 200]
91
- * - :w[, 300]
92
- * - :h[200]
93
- * - :h[0, 500]
94
- * - :h[,200]
95
- * - :w[100, 300]:h[200,500]
96
- * - :h[200,500]:w[100, 300]
97
- *
98
- * @param {string} query - The query string to be checked.
99
- * @returns {boolean} Returns `true` if the query is a valid custom media query, otherwise `false`.
100
- * @example
101
- *
102
- * isMediaQuery(':w[200]') // true
103
- * isMediaQuery(':w100]') // false
104
- */
105
- export declare const isMediaQuery: (query: string) => boolean;
106
- /**
107
- * Retrieves the first matching custom media query key based on the provided screen size.
108
- *
109
- * The function processes an array of media queries and returns the first query that matches
110
- * the given screen size. The media queries can be in formats like:
111
- * - w[200]
112
- * - w[0, 200]
113
- * - w[, 300]
114
- * - h[200]
115
- * - h[0, 500]
116
- * - h[,200]
117
- * - w[100, 300]:h[200,500]
118
- * - h[200,500]:w[100, 300]
119
- *
120
- * @param {Array<[string, string | number]>} mediaQueries - An array of tuples containing media query keys and associated values.
121
- * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
122
- * @returns {string | undefined} Returns the first matching media query key or `undefined` if no match is found.
123
- * @example
124
- *
125
- * const queries = [[':w[200]', 'value1'], [':h[300,500]', 'value2']]
126
- * const size = { width: 250, height: 400 }
127
- * getKeyForCustomMediaQuery(queries, size) // ':w[200]
128
- */
129
- export declare const getKeyForCustomMediaQuery: (mediaQueries: Array<[string, string | number | undefined]>, screenSize: ScreenSize) => string | undefined;
130
- //# sourceMappingURL=mediaQueries.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mediaQueries.d.ts","sourceRoot":"","sources":["../../../../src/utils/mediaQueries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAE1C;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,aAAa,eAAgB,MAAM,KAAG,MAAM,MAAM,CAQ9D,CAAA;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,kBAAkB,UAAW,MAAM,cAAc,UAAU,KAAG,OAc1E,CAAA;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,gBAAgB,UAAW,MAAM,SAAS,MAAM,KAAG,OAQ/D,CAAA;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,iBAAiB,UAAW,MAAM,UAAU,MAAM,KAAG,OAQjE,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,yBAAyB,UAAW,MAAM,cAAc,UAAU,KAAG,OAQjF,CAAA;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,OAI5C,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,yBAAyB,iBAAkB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAC,cAAc,UAAU,KAAG,MAAM,GAAG,SAevI,CAAA"}
@@ -1,17 +0,0 @@
1
- import React, { createContext } from 'react'
2
- import type { PropsWithChildren } from 'react'
3
-
4
- interface UnistylesThemeProps extends PropsWithChildren {
5
- theme: any
6
- }
7
-
8
- export const UnistylesContext = createContext({})
9
-
10
- export const UnistylesTheme: React.FunctionComponent<UnistylesThemeProps> = ({
11
- theme,
12
- children
13
- }) => (
14
- <UnistylesContext.Provider value={theme}>
15
- {children}
16
- </UnistylesContext.Provider>
17
- )
@@ -1,192 +0,0 @@
1
- import React from 'react'
2
- import { StyleSheet } from 'react-native'
3
- import type { ViewStyle } from 'react-native'
4
- import { renderHook } from '@testing-library/react-hooks'
5
- import { createUnistyles } from '../createUnistyles'
6
- import { UnistylesTheme } from '../UnistylesTheme'
7
- import type { CustomNamedStyles } from '../types'
8
-
9
- jest.mock('../hooks', () => ({
10
- useDimensions: jest.fn(() => ({
11
- width: 500,
12
- height: 1000
13
- }))
14
- }))
15
-
16
- describe('createUnistyles', () => {
17
- describe('createStyleSheet', () => {
18
- it('should work exactly the same like StyleSheet.create', () => {
19
- const breakpoints = {
20
- xs: 0
21
- }
22
- const { createStyleSheet } = createUnistyles(breakpoints)
23
-
24
- const styles = {
25
- container: {
26
- flex: 1,
27
- justifyContent: 'center',
28
- alignItems: 'center'
29
- },
30
- text: {
31
- fontWeight: 'bold',
32
- fontSize: 32
33
- }
34
- } as const
35
-
36
- expect(createStyleSheet(styles)).toEqual(StyleSheet.create(styles))
37
- })
38
-
39
- it('should inject the theme to the createStyleSheet', () => {
40
- const theme = {
41
- colors: {
42
- barbie: '#ff9ff3',
43
- oak: '#1dd1a1',
44
- sky: '#48dbfb',
45
- fog: '#c8d6e5',
46
- aloes: '#00d2d3'
47
- }
48
- }
49
- const breakpoints = {
50
- xs: 0
51
- }
52
- const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, typeof theme>(breakpoints)
53
- const stylesheet = createStyleSheet(theme => ({
54
- container: {
55
- backgroundColor: theme.colors.barbie
56
- }
57
- }))
58
- const { result } = renderHook(() => useStyles(stylesheet), {
59
- // @ts-ignore
60
- wrapper: ({ children }) => (
61
- <UnistylesTheme theme={theme}>
62
- {children}
63
- </UnistylesTheme>
64
- )
65
- })
66
-
67
- expect(result.current.theme).toEqual(theme)
68
- expect(result.current.styles).not.toBe(Function)
69
- expect(result.current.styles.container.backgroundColor).toEqual(theme.colors.barbie)
70
- })
71
- })
72
-
73
- describe('useStyles', () => {
74
- it('should return empty object for optional stylesheet', () => {
75
- const breakpoints = {
76
- xs: 0
77
- }
78
- const { useStyles } = createUnistyles(breakpoints)
79
- const { result } = renderHook(() => useStyles())
80
-
81
- expect(result.current.styles).toEqual({})
82
- expect(result.current.theme).toEqual({})
83
- })
84
-
85
- it ('should choose single value from breakpoints', () => {
86
- const breakpoints = {
87
- xs: 0,
88
- sm: 200,
89
- md: 500,
90
- lg: 1000
91
- }
92
-
93
- const { useStyles, createStyleSheet } = createUnistyles(breakpoints)
94
- const stylesheet = createStyleSheet({
95
- container: {
96
- flex: 1,
97
- justifyContent: 'center',
98
- alignItems: {
99
- xs: 'row',
100
- md: 'column'
101
- }
102
- }
103
- })
104
- const { result } = renderHook(() => useStyles(stylesheet))
105
-
106
- expect(result.current.styles.container.alignItems).toEqual('column')
107
- })
108
-
109
- it ('should choose single value from media queries', () => {
110
- const breakpoints = {
111
- xs: 0
112
- }
113
-
114
- const { useStyles, createStyleSheet } = createUnistyles(breakpoints)
115
- const stylesheet = createStyleSheet({
116
- container: {
117
- flex: 1,
118
- justifyContent: 'center',
119
- alignItems: {
120
- xs: 'row',
121
- ':w[300, 490]': 'column',
122
- ':w[491]': 'row'
123
- }
124
- }
125
- })
126
- const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))
127
-
128
- expect(result.current.styles.container.alignItems).toEqual('row')
129
- })
130
-
131
- it ('should choose wrap dynamic functions in Proxy', () => {
132
- const breakpoints = {
133
- xs: 0
134
- }
135
-
136
- const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, {}>(breakpoints)
137
- const stylesheet = createStyleSheet({
138
- container: (flex: number) => ({
139
- flex,
140
- justifyContent: 'center',
141
- alignItems: {
142
- xs: 'row',
143
- ':w[300, 490]': 'column',
144
- ':w[491]': 'row'
145
- }
146
- })
147
- })
148
- const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))
149
-
150
- expect(result.current.styles.container).toBeInstanceOf(Function)
151
- expect((result.current.styles.container as (flex: number) => ViewStyle)(2)).toEqual({
152
- flex: 2,
153
- justifyContent: 'center',
154
- alignItems: 'row'
155
- })
156
- })
157
-
158
- it ('should return breakpoint even if no stylesheet has been provided', () => {
159
- const breakpoints = {
160
- xs: 0,
161
- sm: 200,
162
- md: 500,
163
- lg: 1000
164
- }
165
-
166
- const { useStyles } = createUnistyles<typeof breakpoints, {}>(breakpoints)
167
- const { result } = renderHook(() => useStyles())
168
-
169
- expect(result.current.breakpoint).toEqual('md')
170
- })
171
-
172
- it ('should return breakpoint for stylesheet', () => {
173
- const breakpoints = {
174
- xs: 0,
175
- sm: 200,
176
- lg: 1000
177
- }
178
-
179
- const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, {}>(breakpoints)
180
- const stylesheet = createStyleSheet({
181
- container: {
182
- flex: 1,
183
- justifyContent: 'center',
184
- alignItems: 'center'
185
- }
186
- })
187
- const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))
188
-
189
- expect(result.current.breakpoint).toEqual('sm')
190
- })
191
- })
192
- })
@@ -1,70 +0,0 @@
1
- import { useContext, useMemo } from 'react'
2
- import { StyleSheet } from 'react-native'
3
- import type {
4
- Breakpoints,
5
- CreateStylesFactory,
6
- CustomNamedStyles,
7
- ExtractBreakpoints,
8
- RemoveKeysWithPrefix,
9
- SortedBreakpointEntries
10
- } from './types'
11
- import { UnistylesContext } from './UnistylesTheme'
12
- import { useDimensions } from './hooks'
13
- import { getBreakpointFromScreenWidth, proxifyFunction, parseStyle, sortAndValidateBreakpoints } from './utils'
14
-
15
- export const createUnistyles = <B extends Breakpoints, T = {}>(breakpoints: B) => {
16
- const sortedBreakpoints = sortAndValidateBreakpoints(breakpoints)
17
- const sortedBreakpointEntries = Object
18
- .entries(sortedBreakpoints) as SortedBreakpointEntries<B>
19
-
20
- return {
21
- createStyleSheet: <S extends CustomNamedStyles<S, B>, X>(styles: S | CustomNamedStyles<S, B> | X | ((theme: T) => X | CustomNamedStyles<X, B>)): S | X => {
22
- if (typeof styles === 'function') {
23
- return styles as X
24
- }
25
-
26
- return styles as S
27
- },
28
- useStyles: <ST extends CustomNamedStyles<ST, B>>(stylesheet?: ST | CreateStylesFactory<ST, T>) => {
29
- const theme = useContext(UnistylesContext) as T
30
- const screenSize = useDimensions()
31
- const breakpoint = getBreakpointFromScreenWidth<B>(screenSize.width, sortedBreakpointEntries)
32
-
33
- if (!stylesheet) {
34
- return {
35
- theme,
36
- breakpoint,
37
- styles: {} as ExtractBreakpoints<RemoveKeysWithPrefix<ST, B>, B>
38
- }
39
- }
40
-
41
- const parsedStyles = useMemo(() => typeof stylesheet === 'function'
42
- ? stylesheet(theme)
43
- : stylesheet, [theme, stylesheet])
44
-
45
- const dynamicStyleSheet = useMemo(() => Object
46
- .entries(parsedStyles)
47
- .reduce((acc, [key, value]) => {
48
- const style = value as CustomNamedStyles<ST, B>
49
-
50
- if (typeof value === 'function') {
51
- return {
52
- ...acc,
53
- [key]: proxifyFunction<B>(value, breakpoint, screenSize, sortedBreakpointEntries)
54
- }
55
- }
56
-
57
- return StyleSheet.create({
58
- ...acc,
59
- [key]: parseStyle<ST, B>(style, breakpoint, screenSize, sortedBreakpointEntries)
60
- })
61
- }, {} as ST), [breakpoint, screenSize, parsedStyles])
62
-
63
- return {
64
- theme,
65
- breakpoint,
66
- styles: dynamicStyleSheet as ExtractBreakpoints<RemoveKeysWithPrefix<ST, B>, B>
67
- }
68
- }
69
- }
70
- }
@@ -1,4 +0,0 @@
1
- import { useWindowDimensions } from 'react-native'
2
- import type { ScreenSize } from '../types'
3
-
4
- export const useDimensions = (): ScreenSize => useWindowDimensions()
@@ -1,34 +0,0 @@
1
- import { useEffect, useLayoutEffect, useRef, useState } from 'react'
2
- import type { ScreenSize } from '../types'
3
-
4
- export const useDimensions = (): ScreenSize => {
5
- const timerRef = useRef<ReturnType<typeof setTimeout>>()
6
- const [screenSize, setScreenSize] = useState<ScreenSize>({} as ScreenSize)
7
-
8
- useLayoutEffect(() => {
9
- setScreenSize({
10
- width: window.innerWidth,
11
- height: window.innerHeight
12
- })
13
- }, [])
14
-
15
- useEffect(() => {
16
- const handleResize = () => {
17
- clearTimeout(timerRef.current)
18
-
19
- timerRef.current = setTimeout(() => setScreenSize({
20
- width: window.innerWidth,
21
- height: window.innerHeight
22
- }), 100)
23
- }
24
-
25
- window.addEventListener('resize', handleResize)
26
-
27
- return () => {
28
- window.removeEventListener('resize', handleResize)
29
- clearTimeout(timerRef.current)
30
- }
31
- }, [])
32
-
33
- return screenSize
34
- }
@@ -1,10 +0,0 @@
1
- type Optional<TOptional extends string> = TOptional | ''
2
- type Separator = ',' | ', '
3
- // this is super weird, but number passes empty string and bigint does not
4
- type MediaQuery = `[${Separator}${bigint}]` | `[${bigint}${Optional<`${Separator}${bigint}`>}]`
5
- type WidthMediaQuery = `:w${MediaQuery}`
6
- type HeightMediaQuery = `:h${MediaQuery}`
7
-
8
- export type MediaQueries =
9
- | `${WidthMediaQuery}${Optional<HeightMediaQuery>}`
10
- | `${HeightMediaQuery}${Optional<WidthMediaQuery>}`
@@ -1,201 +0,0 @@
1
- import type { ScreenSize } from '../types'
2
-
3
- /**
4
- * Extracts numeric values from a coded string.
5
- *
6
- * The function is designed to process strings that have a format like "w[100,200]" or "h[300]".
7
- * It removes characters 'w', 'h', '[', and ']' from the input string and then extracts the numbers.
8
- *
9
- * @param {string} codedValue - The input string to extract values from.
10
- * @returns {Array<number>} An array of extracted numbers. Can contain one or two numbers based on the input format.
11
- *
12
- * @example
13
- * extractValues("w[100,200]") // returns [100, 200]
14
- * extractValues("h[300]") // returns [300]
15
- * extractValues("h[,300]") // returns [0,300]
16
- * extractValues("h[100,]") // returns [100]
17
- */
18
- export const extractValues = (codedValue: string): Array<number> => {
19
- const [lh, rh] = codedValue
20
- .replace(/[wh[\]]/g, '')
21
- .split(',')
22
-
23
- return rh
24
- ? [Number(lh), Number(rh)]
25
- : [Number(lh)]
26
- }
27
-
28
- /**
29
- * Determines if the given screen size matches the specified breakpoint query.
30
- *
31
- * The function checks if the screen size (width and/or height) falls within the range
32
- * specified by the breakpoint query. The query can specify width (using 'w'), height (using 'h'),
33
- * or both.
34
- *
35
- * @param {string} query - The breakpoint query string. Examples: 'w[100,200]', 'h[300]', 'w[100,200]h[300,400]'.
36
- * @param {ScreenSize} screenSize - The screen size to check against the breakpoint query.
37
- * @returns {boolean} True if the screen size matches the breakpoint query, false otherwise.
38
- *
39
- * @example
40
- * const screenSize = { width: 150, height: 350 }
41
- * isWithinBreakpoint('w[100,200]', screenSize) // returns true
42
- * isWithinBreakpoint('h[400]', screenSize) // returns false
43
- */
44
- export const isWithinBreakpoint = (query: string, screenSize: ScreenSize): boolean => {
45
- if (query.includes('w') && query.includes('h')) {
46
- return isWithinTheWidthAndHeight(query, screenSize)
47
- }
48
-
49
- if (query.charAt(0) === 'w') {
50
- return isWithinTheWidth(query, screenSize.width)
51
- }
52
-
53
- if (query.charAt(0) === 'h') {
54
- return isWithinTheHeight(query, screenSize.height)
55
- }
56
-
57
- return false
58
- }
59
-
60
- /**
61
- * Determines if the given width matches the specified width range in the query.
62
- *
63
- * The function checks if the provided width falls within the range specified by the query.
64
- * The query specifies a width range using a format like 'w[100,200]'. If only one value is provided,
65
- * it's treated as a minimum width.
66
- *
67
- * @param {string} query - The width query string. Examples: 'w[100,200]' or 'w[100]'.
68
- * @param {number} width - The width to check against the query.
69
- * @returns {boolean} True if the width matches the query range, false otherwise.
70
- *
71
- * @example
72
- * isWithinTheWidth('w[100,200]', 150) // returns true
73
- * isWithinTheWidth('w[100]', 50) // returns false
74
- * isWithinTheWidth('w[100]', 150) // returns true
75
- */
76
- export const isWithinTheWidth = (query: string, width: number): boolean => {
77
- const [minWidth, maxWidth] = extractValues(query) as [number, number | undefined]
78
-
79
- if (maxWidth && width >= minWidth && width <= maxWidth) {
80
- return true
81
- }
82
-
83
- return !maxWidth && width >= minWidth
84
- }
85
-
86
- /**
87
- * Determines if the given height matches the specified height range in the query.
88
- *
89
- * The function checks if the provided height falls within the range specified by the query.
90
- * The query specifies a height range using a format like 'h[100,200]'. If only one value is provided,
91
- * it's treated as a minimum height.
92
- *
93
- * @param {string} query - The height query string. Examples: 'h[100,200]' or 'h[100]'.
94
- * @param {number} height - The height to check against the query.
95
- * @returns {boolean} True if the height matches the query range, false otherwise.
96
- *
97
- * @example
98
- * isWithinTheHeight('h[100,200]', 150) // returns true
99
- * isWithinTheHeight('h[100]', 50) // returns false
100
- * isWithinTheHeight('h[100]', 150) // returns true
101
- */
102
- export const isWithinTheHeight = (query: string, height: number): boolean => {
103
- const [minHeight, maxHeight] = extractValues(query) as [number, number | undefined]
104
-
105
- if (maxHeight && height >= minHeight && height <= maxHeight) {
106
- return true
107
- }
108
-
109
- return !maxHeight && height >= minHeight
110
- }
111
-
112
- /**
113
- * Determines if the given screen size matches both the specified width and height ranges in the query.
114
- *
115
- * The function checks if the provided screen size (both width and height) falls within the ranges
116
- * specified by the query. The query can specify both width and height using a format like 'w[100,200]:h[300,400]'.
117
- *
118
- * @param {string} query - The combined width and height query string. Example: 'w[100,200]:h[300,400]'.
119
- * @param {ScreenSize} screenSize - The screen size to check against the query.
120
- * @returns {boolean} True if the screen size matches both the width and height ranges in the query, false otherwise.
121
- *
122
- * @example
123
- * const screenSize = { width: 150, height: 350 }
124
- * isWithinTheWidthAndHeight('w[100,200]:h[300,400]', screenSize) // returns true
125
- * isWithinTheWidthAndHeight('w[100,200]:h[400,500]', screenSize) // returns false
126
- */
127
- export const isWithinTheWidthAndHeight = (query: string, screenSize: ScreenSize): boolean => {
128
- const result = query
129
- .split(':')
130
- .filter(Boolean)
131
- .map(q => isWithinBreakpoint(q, screenSize))
132
- .filter(Boolean)
133
-
134
- return result.length === 2
135
- }
136
-
137
- /**
138
- * Checks if the given query string is a valid custom media query.
139
- *
140
- * The valid custom media query formats include:
141
- * - :w[200]
142
- * - :w[0, 200]
143
- * - :w[, 300]
144
- * - :h[200]
145
- * - :h[0, 500]
146
- * - :h[,200]
147
- * - :w[100, 300]:h[200,500]
148
- * - :h[200,500]:w[100, 300]
149
- *
150
- * @param {string} query - The query string to be checked.
151
- * @returns {boolean} Returns `true` if the query is a valid custom media query, otherwise `false`.
152
- * @example
153
- *
154
- * isMediaQuery(':w[200]') // true
155
- * isMediaQuery(':w100]') // false
156
- */
157
- export const isMediaQuery = (query: string): boolean => {
158
- const regex = /^(?:(:w\[\d*(?:,\s?\d+)?])?(:h\[\d*(?:,\s?\d+)?])?|(:h\[\d*(?:,\s?\d+)?])?(:w\[\d*(?:,\s?\d+)?])?)$/
159
-
160
- return query.length > 0 && regex.test(query)
161
- }
162
-
163
- /**
164
- * Retrieves the first matching custom media query key based on the provided screen size.
165
- *
166
- * The function processes an array of media queries and returns the first query that matches
167
- * the given screen size. The media queries can be in formats like:
168
- * - w[200]
169
- * - w[0, 200]
170
- * - w[, 300]
171
- * - h[200]
172
- * - h[0, 500]
173
- * - h[,200]
174
- * - w[100, 300]:h[200,500]
175
- * - h[200,500]:w[100, 300]
176
- *
177
- * @param {Array<[string, string | number]>} mediaQueries - An array of tuples containing media query keys and associated values.
178
- * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
179
- * @returns {string | undefined} Returns the first matching media query key or `undefined` if no match is found.
180
- * @example
181
- *
182
- * const queries = [[':w[200]', 'value1'], [':h[300,500]', 'value2']]
183
- * const size = { width: 250, height: 400 }
184
- * getKeyForCustomMediaQuery(queries, size) // ':w[200]
185
- */
186
- export const getKeyForCustomMediaQuery = (mediaQueries: Array<[string, string | number | undefined]>, screenSize: ScreenSize): string | undefined => {
187
- const [matchedQuery] = mediaQueries
188
- .flatMap(([key]) => {
189
- if (key.includes('w') && key.includes('h')) {
190
- return isWithinBreakpoint(key, screenSize) ? key : undefined
191
- }
192
-
193
- return key
194
- .split(':')
195
- .filter(Boolean)
196
- .map(query => isWithinBreakpoint(query, screenSize) ? key : undefined)
197
- })
198
- .filter(Boolean)
199
-
200
- return matchedQuery
201
- }