react-native-unistyles 1.1.6 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (394) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +30 -38
  3. package/android/CMakeLists.txt +27 -0
  4. package/android/build.gradle +75 -0
  5. package/android/src/main/cxx/cpp-adapter.cpp +132 -0
  6. package/android/src/main/java/com/unistyles/UnistylesModule.kt +189 -0
  7. package/android/src/main/java/com/unistyles/UnistylesPackage.kt +18 -0
  8. package/cxx/UnistylesRuntime.cpp +318 -0
  9. package/cxx/UnistylesRuntime.h +74 -0
  10. package/ios/UnistylesHelpers.h +3 -0
  11. package/ios/UnistylesHelpers.mm +5 -0
  12. package/ios/UnistylesModule.h +16 -0
  13. package/ios/UnistylesModule.mm +138 -0
  14. package/ios/platform/Platform_iOS.h +21 -0
  15. package/ios/platform/Platform_iOS.mm +132 -0
  16. package/ios/platform/Platform_macOS.h +20 -0
  17. package/ios/platform/Platform_macOS.mm +83 -0
  18. package/lib/commonjs/common.js +60 -0
  19. package/lib/commonjs/common.js.map +1 -0
  20. package/lib/commonjs/core/UnistyleRegistry.js +94 -0
  21. package/lib/commonjs/core/UnistyleRegistry.js.map +1 -0
  22. package/lib/commonjs/core/Unistyles.js +32 -0
  23. package/lib/commonjs/core/Unistyles.js.map +1 -0
  24. package/lib/commonjs/core/UnistylesModule.js +197 -0
  25. package/lib/commonjs/core/UnistylesModule.js.map +1 -0
  26. package/lib/commonjs/core/UnistylesModule.native.js +9 -0
  27. package/lib/commonjs/core/UnistylesModule.native.js.map +1 -0
  28. package/lib/commonjs/core/UnistylesRuntime.js +140 -0
  29. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -0
  30. package/lib/commonjs/core/index.js +13 -0
  31. package/lib/commonjs/core/index.js.map +1 -0
  32. package/lib/commonjs/createStyleSheet.js +14 -0
  33. package/lib/commonjs/createStyleSheet.js.map +1 -0
  34. package/lib/commonjs/global.js +2 -0
  35. package/lib/commonjs/global.js.map +1 -0
  36. package/lib/commonjs/hooks/index.js +24 -3
  37. package/lib/commonjs/hooks/index.js.map +1 -1
  38. package/lib/commonjs/hooks/useCSS.js +44 -0
  39. package/lib/commonjs/hooks/useCSS.js.map +1 -0
  40. package/lib/commonjs/hooks/useCSS.native.js +9 -0
  41. package/lib/commonjs/hooks/useCSS.native.js.map +1 -0
  42. package/lib/commonjs/hooks/useInitialTheme.js +17 -0
  43. package/lib/commonjs/hooks/useInitialTheme.js.map +1 -0
  44. package/lib/commonjs/hooks/useUnistyles.js +64 -0
  45. package/lib/commonjs/hooks/useUnistyles.js.map +1 -0
  46. package/lib/commonjs/hooks/useVariants.js +14 -0
  47. package/lib/commonjs/hooks/useVariants.js.map +1 -0
  48. package/lib/commonjs/index.js +63 -6
  49. package/lib/commonjs/index.js.map +1 -1
  50. package/lib/commonjs/normalizer/index.js +32 -0
  51. package/lib/commonjs/normalizer/index.js.map +1 -0
  52. package/lib/commonjs/normalizer/module.d.js.map +1 -0
  53. package/lib/commonjs/{utils/normalizeStyles.web.js → normalizer/normalizeStyle.js} +5 -5
  54. package/lib/commonjs/normalizer/normalizeStyle.js.map +1 -0
  55. package/lib/commonjs/{utils → normalizer}/normalizer.js +2 -3
  56. package/lib/commonjs/normalizer/normalizer.js.map +1 -0
  57. package/lib/commonjs/normalizer/normalizer.macos.js +3 -0
  58. package/lib/commonjs/normalizer/normalizer.macos.js.map +1 -0
  59. package/lib/commonjs/plugins/cssMediaQueriesPlugin.js +14 -0
  60. package/lib/commonjs/plugins/cssMediaQueriesPlugin.js.map +1 -0
  61. package/lib/commonjs/plugins/index.js +20 -0
  62. package/lib/commonjs/plugins/index.js.map +1 -0
  63. package/lib/commonjs/plugins/normalizeWebStylesPlugin.js +12 -0
  64. package/lib/commonjs/plugins/normalizeWebStylesPlugin.js.map +1 -0
  65. package/lib/commonjs/types/common.js +2 -0
  66. package/lib/{module/utils/module.d.js.map → commonjs/types/common.js.map} +1 -1
  67. package/lib/commonjs/types/index.js +11 -0
  68. package/lib/commonjs/types/index.js.map +1 -1
  69. package/lib/commonjs/types/plugin.js +6 -0
  70. package/lib/commonjs/{utils/module.d.js.map → types/plugin.js.map} +1 -1
  71. package/lib/commonjs/types/stylesheet.js +6 -0
  72. package/lib/commonjs/types/{mediaQueries.js.map → stylesheet.js.map} +1 -1
  73. package/lib/commonjs/types/unistyles.js +6 -0
  74. package/lib/{module/types/mediaQueries.js.map → commonjs/types/unistyles.js.map} +1 -1
  75. package/lib/commonjs/types/variants.js +2 -0
  76. package/lib/commonjs/types/variants.js.map +1 -0
  77. package/lib/commonjs/useStyles.js +47 -0
  78. package/lib/commonjs/useStyles.js.map +1 -0
  79. package/lib/commonjs/utils/breakpoints.js +20 -122
  80. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  81. package/lib/commonjs/utils/cssMediaQuery.js +244 -0
  82. package/lib/commonjs/utils/cssMediaQuery.js.map +1 -0
  83. package/lib/commonjs/utils/generateId.js +15 -0
  84. package/lib/commonjs/utils/generateId.js.map +1 -0
  85. package/lib/commonjs/utils/hash32.js +43 -0
  86. package/lib/commonjs/utils/hash32.js.map +1 -0
  87. package/lib/commonjs/utils/index.js +22 -54
  88. package/lib/commonjs/utils/index.js.map +1 -1
  89. package/lib/commonjs/utils/mq.js +38 -0
  90. package/lib/commonjs/utils/mq.js.map +1 -0
  91. package/lib/commonjs/utils/mqParser.js +89 -0
  92. package/lib/commonjs/utils/mqParser.js.map +1 -0
  93. package/lib/commonjs/utils/styles.js +52 -72
  94. package/lib/commonjs/utils/styles.js.map +1 -1
  95. package/lib/commonjs/utils/withPlugins.js +15 -0
  96. package/lib/commonjs/utils/withPlugins.js.map +1 -0
  97. package/lib/module/common.js +53 -0
  98. package/lib/module/common.js.map +1 -0
  99. package/lib/module/core/UnistyleRegistry.js +87 -0
  100. package/lib/module/core/UnistyleRegistry.js.map +1 -0
  101. package/lib/module/core/Unistyles.js +26 -0
  102. package/lib/module/core/Unistyles.js.map +1 -0
  103. package/lib/module/core/UnistylesModule.js +190 -0
  104. package/lib/module/core/UnistylesModule.js.map +1 -0
  105. package/lib/module/core/UnistylesModule.native.js +3 -0
  106. package/lib/module/core/UnistylesModule.native.js.map +1 -0
  107. package/lib/module/core/UnistylesRuntime.js +133 -0
  108. package/lib/module/core/UnistylesRuntime.js.map +1 -0
  109. package/lib/module/core/index.js +2 -0
  110. package/lib/module/core/index.js.map +1 -0
  111. package/lib/module/createStyleSheet.js +7 -0
  112. package/lib/module/createStyleSheet.js.map +1 -0
  113. package/lib/module/global.js +2 -0
  114. package/lib/module/global.js.map +1 -0
  115. package/lib/module/hooks/index.js +4 -1
  116. package/lib/module/hooks/index.js.map +1 -1
  117. package/lib/module/hooks/useCSS.js +37 -0
  118. package/lib/module/hooks/useCSS.js.map +1 -0
  119. package/lib/module/hooks/useCSS.native.js +2 -0
  120. package/lib/module/hooks/useCSS.native.js.map +1 -0
  121. package/lib/module/hooks/useInitialTheme.js +10 -0
  122. package/lib/module/hooks/useInitialTheme.js.map +1 -0
  123. package/lib/module/hooks/useUnistyles.js +57 -0
  124. package/lib/module/hooks/useUnistyles.js.map +1 -0
  125. package/lib/module/hooks/useVariants.js +7 -0
  126. package/lib/module/hooks/useVariants.js.map +1 -0
  127. package/lib/module/index.js +30 -2
  128. package/lib/module/index.js.map +1 -1
  129. package/lib/module/normalizer/index.js +3 -0
  130. package/lib/module/normalizer/index.js.map +1 -0
  131. package/lib/module/normalizer/module.d.js.map +1 -0
  132. package/lib/module/{utils/normalizeStyles.web.js → normalizer/normalizeStyle.js} +3 -3
  133. package/lib/module/normalizer/normalizeStyle.js.map +1 -0
  134. package/lib/module/{utils → normalizer}/normalizer.js +2 -3
  135. package/lib/module/normalizer/normalizer.js.map +1 -0
  136. package/lib/module/normalizer/normalizer.macos.js +2 -0
  137. package/lib/module/normalizer/normalizer.macos.js.map +1 -0
  138. package/lib/module/plugins/cssMediaQueriesPlugin.js +7 -0
  139. package/lib/module/plugins/cssMediaQueriesPlugin.js.map +1 -0
  140. package/lib/module/plugins/index.js +3 -0
  141. package/lib/module/plugins/index.js.map +1 -0
  142. package/lib/module/plugins/normalizeWebStylesPlugin.js +6 -0
  143. package/lib/module/plugins/normalizeWebStylesPlugin.js.map +1 -0
  144. package/lib/module/types/common.js +2 -0
  145. package/lib/module/types/common.js.map +1 -0
  146. package/lib/module/types/index.js +1 -0
  147. package/lib/module/types/index.js.map +1 -1
  148. package/lib/module/types/plugin.js +2 -0
  149. package/lib/module/types/plugin.js.map +1 -0
  150. package/lib/module/types/stylesheet.js +2 -0
  151. package/lib/module/types/stylesheet.js.map +1 -0
  152. package/lib/module/types/unistyles.js +2 -0
  153. package/lib/module/types/unistyles.js.map +1 -0
  154. package/lib/module/types/variants.js +2 -0
  155. package/lib/module/types/variants.js.map +1 -0
  156. package/lib/module/useStyles.js +40 -0
  157. package/lib/module/useStyles.js.map +1 -0
  158. package/lib/module/utils/breakpoints.js +19 -120
  159. package/lib/module/utils/breakpoints.js.map +1 -1
  160. package/lib/module/utils/cssMediaQuery.js +237 -0
  161. package/lib/module/utils/cssMediaQuery.js.map +1 -0
  162. package/lib/module/utils/generateId.js +7 -0
  163. package/lib/module/utils/generateId.js.map +1 -0
  164. package/lib/module/utils/hash32.js +36 -0
  165. package/lib/module/utils/hash32.js.map +1 -0
  166. package/lib/module/utils/index.js +6 -6
  167. package/lib/module/utils/index.js.map +1 -1
  168. package/lib/module/utils/mq.js +32 -0
  169. package/lib/module/utils/mq.js.map +1 -0
  170. package/lib/module/utils/mqParser.js +78 -0
  171. package/lib/module/utils/mqParser.js.map +1 -0
  172. package/lib/module/utils/styles.js +50 -72
  173. package/lib/module/utils/styles.js.map +1 -1
  174. package/lib/module/utils/withPlugins.js +8 -0
  175. package/lib/module/utils/withPlugins.js.map +1 -0
  176. package/lib/typescript/src/common.d.ts +46 -0
  177. package/lib/typescript/src/common.d.ts.map +1 -0
  178. package/lib/typescript/src/core/UnistyleRegistry.d.ts +56 -0
  179. package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -0
  180. package/lib/typescript/src/core/Unistyles.d.ts +13 -0
  181. package/lib/typescript/src/core/Unistyles.d.ts.map +1 -0
  182. package/lib/typescript/src/core/UnistylesModule.d.ts +19 -0
  183. package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -0
  184. package/lib/typescript/src/core/UnistylesModule.native.d.ts +6 -0
  185. package/lib/typescript/src/core/UnistylesModule.native.d.ts.map +1 -0
  186. package/lib/typescript/src/core/UnistylesRuntime.d.ts +81 -0
  187. package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -0
  188. package/lib/typescript/src/core/index.d.ts +3 -0
  189. package/lib/typescript/src/core/index.d.ts.map +1 -0
  190. package/lib/typescript/src/createStyleSheet.d.ts +8 -0
  191. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -0
  192. package/lib/typescript/src/global.d.ts +7 -0
  193. package/lib/typescript/src/global.d.ts.map +1 -0
  194. package/lib/typescript/src/hooks/index.d.ts +4 -1
  195. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  196. package/lib/typescript/src/hooks/useCSS.d.ts +3 -0
  197. package/lib/typescript/src/hooks/useCSS.d.ts.map +1 -0
  198. package/lib/typescript/src/hooks/useCSS.native.d.ts +3 -0
  199. package/lib/typescript/src/hooks/useCSS.native.d.ts.map +1 -0
  200. package/lib/typescript/src/hooks/useInitialTheme.d.ts +3 -0
  201. package/lib/typescript/src/hooks/useInitialTheme.d.ts.map +1 -0
  202. package/lib/typescript/src/hooks/useUnistyles.d.ts +14 -0
  203. package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -0
  204. package/lib/typescript/src/hooks/useVariants.d.ts +3 -0
  205. package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -0
  206. package/lib/typescript/src/index.d.ts +76 -2
  207. package/lib/typescript/src/index.d.ts.map +1 -1
  208. package/lib/typescript/src/normalizer/index.d.ts +3 -0
  209. package/lib/typescript/src/normalizer/index.d.ts.map +1 -0
  210. package/lib/typescript/src/normalizer/normalizeStyle.d.ts +3 -0
  211. package/lib/typescript/src/normalizer/normalizeStyle.d.ts.map +1 -0
  212. package/lib/typescript/src/normalizer/normalizer.d.ts.map +1 -0
  213. package/lib/typescript/src/normalizer/normalizer.macos.d.ts +1 -0
  214. package/lib/typescript/src/normalizer/normalizer.macos.d.ts.map +1 -0
  215. package/lib/typescript/src/plugins/cssMediaQueriesPlugin.d.ts +3 -0
  216. package/lib/typescript/src/plugins/cssMediaQueriesPlugin.d.ts.map +1 -0
  217. package/lib/typescript/src/plugins/index.d.ts +3 -0
  218. package/lib/typescript/src/plugins/index.d.ts.map +1 -0
  219. package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts +3 -0
  220. package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts.map +1 -0
  221. package/lib/typescript/src/types/breakpoints.d.ts +25 -16
  222. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  223. package/lib/typescript/src/types/common.d.ts +3 -0
  224. package/lib/typescript/src/types/common.d.ts.map +1 -0
  225. package/lib/typescript/src/types/core.d.ts +11 -25
  226. package/lib/typescript/src/types/core.d.ts.map +1 -1
  227. package/lib/typescript/src/types/index.d.ts +7 -2
  228. package/lib/typescript/src/types/index.d.ts.map +1 -1
  229. package/lib/typescript/src/types/plugin.d.ts +7 -0
  230. package/lib/typescript/src/types/plugin.d.ts.map +1 -0
  231. package/lib/typescript/src/types/stylesheet.d.ts +40 -0
  232. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -0
  233. package/lib/typescript/src/types/unistyles.d.ts +53 -0
  234. package/lib/typescript/src/types/unistyles.d.ts.map +1 -0
  235. package/lib/typescript/src/types/variants.d.ts +14 -0
  236. package/lib/typescript/src/types/variants.d.ts.map +1 -0
  237. package/lib/typescript/src/useStyles.d.ts +16 -0
  238. package/lib/typescript/src/useStyles.d.ts.map +1 -0
  239. package/lib/typescript/src/utils/breakpoints.d.ts +2 -62
  240. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  241. package/lib/typescript/src/utils/cssMediaQuery.d.ts +4 -0
  242. package/lib/typescript/src/utils/cssMediaQuery.d.ts.map +1 -0
  243. package/lib/typescript/src/utils/generateId.d.ts +2 -0
  244. package/lib/typescript/src/utils/generateId.d.ts.map +1 -0
  245. package/lib/typescript/src/utils/hash32.d.ts +2 -0
  246. package/lib/typescript/src/utils/hash32.d.ts.map +1 -0
  247. package/lib/typescript/src/utils/index.d.ts +7 -6
  248. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  249. package/lib/typescript/src/utils/mq.d.ts +26 -0
  250. package/lib/typescript/src/utils/mq.d.ts.map +1 -0
  251. package/lib/typescript/src/utils/mqParser.d.ts +16 -0
  252. package/lib/typescript/src/utils/mqParser.d.ts.map +1 -0
  253. package/lib/typescript/src/utils/styles.d.ts +4 -49
  254. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  255. package/lib/typescript/src/utils/withPlugins.d.ts +3 -0
  256. package/lib/typescript/src/utils/withPlugins.d.ts.map +1 -0
  257. package/package.json +46 -25
  258. package/react-native-unistyles.podspec +22 -0
  259. package/src/__tests__/mocks.ts +24 -0
  260. package/src/common.ts +55 -0
  261. package/src/core/UnistyleRegistry.ts +108 -0
  262. package/src/core/Unistyles.ts +35 -0
  263. package/src/core/UnistylesModule.native.ts +7 -0
  264. package/src/core/UnistylesModule.ts +230 -0
  265. package/src/core/UnistylesRuntime.ts +135 -0
  266. package/src/core/index.ts +2 -0
  267. package/src/createStyleSheet.ts +8 -0
  268. package/src/global.ts +6 -0
  269. package/src/hooks/index.ts +4 -1
  270. package/src/hooks/useCSS.native.ts +3 -0
  271. package/src/hooks/useCSS.ts +51 -0
  272. package/src/hooks/useInitialTheme.ts +11 -0
  273. package/src/hooks/useUnistyles.ts +64 -0
  274. package/src/hooks/useVariants.ts +10 -0
  275. package/src/index.ts +50 -2
  276. package/src/normalizer/index.ts +2 -0
  277. package/src/{utils/normalizeStyles.web.ts → normalizer/normalizeStyle.ts} +3 -3
  278. package/src/normalizer/normalizer.macos.ts +1 -0
  279. package/src/{utils → normalizer}/normalizer.ts +5 -4
  280. package/src/plugins/cssMediaQueriesPlugin.ts +8 -0
  281. package/src/plugins/index.ts +2 -0
  282. package/src/plugins/normalizeWebStylesPlugin.ts +7 -0
  283. package/src/types/breakpoints.ts +55 -27
  284. package/src/types/common.ts +2 -0
  285. package/src/types/core.ts +11 -36
  286. package/src/types/index.ts +12 -7
  287. package/src/types/plugin.ts +7 -0
  288. package/src/types/stylesheet.ts +49 -0
  289. package/src/types/unistyles.ts +63 -0
  290. package/src/types/variants.ts +19 -0
  291. package/src/useStyles.ts +60 -0
  292. package/src/utils/breakpoints.ts +28 -124
  293. package/src/utils/cssMediaQuery.ts +268 -0
  294. package/src/utils/generateId.ts +10 -0
  295. package/src/utils/hash32.ts +53 -0
  296. package/src/utils/index.ts +7 -13
  297. package/src/utils/mq.ts +57 -0
  298. package/src/utils/mqParser.ts +99 -0
  299. package/src/utils/styles.ts +62 -102
  300. package/src/utils/withPlugins.ts +13 -0
  301. package/lib/commonjs/UnistylesTheme.js +0 -21
  302. package/lib/commonjs/UnistylesTheme.js.map +0 -1
  303. package/lib/commonjs/createUnistyles.js +0 -57
  304. package/lib/commonjs/createUnistyles.js.map +0 -1
  305. package/lib/commonjs/hooks/useDimensions.js +0 -10
  306. package/lib/commonjs/hooks/useDimensions.js.map +0 -1
  307. package/lib/commonjs/hooks/useDimensions.web.js +0 -34
  308. package/lib/commonjs/hooks/useDimensions.web.js.map +0 -1
  309. package/lib/commonjs/types/mediaQueries.js +0 -2
  310. package/lib/commonjs/utils/common.js +0 -18
  311. package/lib/commonjs/utils/common.js.map +0 -1
  312. package/lib/commonjs/utils/mediaQueries.js +0 -189
  313. package/lib/commonjs/utils/mediaQueries.js.map +0 -1
  314. package/lib/commonjs/utils/normalizeStyles.js +0 -10
  315. package/lib/commonjs/utils/normalizeStyles.js.map +0 -1
  316. package/lib/commonjs/utils/normalizeStyles.web.js.map +0 -1
  317. package/lib/commonjs/utils/normalizer.js.map +0 -1
  318. package/lib/module/UnistylesTheme.js +0 -12
  319. package/lib/module/UnistylesTheme.js.map +0 -1
  320. package/lib/module/createUnistyles.js +0 -50
  321. package/lib/module/createUnistyles.js.map +0 -1
  322. package/lib/module/hooks/useDimensions.js +0 -3
  323. package/lib/module/hooks/useDimensions.js.map +0 -1
  324. package/lib/module/hooks/useDimensions.web.js +0 -27
  325. package/lib/module/hooks/useDimensions.web.js.map +0 -1
  326. package/lib/module/types/mediaQueries.js +0 -2
  327. package/lib/module/utils/common.js +0 -10
  328. package/lib/module/utils/common.js.map +0 -1
  329. package/lib/module/utils/mediaQueries.js +0 -176
  330. package/lib/module/utils/mediaQueries.js.map +0 -1
  331. package/lib/module/utils/normalizeStyles.js +0 -3
  332. package/lib/module/utils/normalizeStyles.js.map +0 -1
  333. package/lib/module/utils/normalizeStyles.web.js.map +0 -1
  334. package/lib/module/utils/normalizer.js.map +0 -1
  335. package/lib/typescript/examples/expo/src/App.d.ts +0 -3
  336. package/lib/typescript/examples/expo/src/App.d.ts.map +0 -1
  337. package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts +0 -3
  338. package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts.map +0 -1
  339. package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts +0 -3
  340. package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts.map +0 -1
  341. package/lib/typescript/examples/expo/src/examples/Extreme.d.ts +0 -7
  342. package/lib/typescript/examples/expo/src/examples/Extreme.d.ts.map +0 -1
  343. package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts +0 -3
  344. package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts.map +0 -1
  345. package/lib/typescript/examples/expo/src/examples/Memoization.d.ts +0 -3
  346. package/lib/typescript/examples/expo/src/examples/Memoization.d.ts.map +0 -1
  347. package/lib/typescript/examples/expo/src/examples/Minimal.d.ts +0 -3
  348. package/lib/typescript/examples/expo/src/examples/Minimal.d.ts.map +0 -1
  349. package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts +0 -3
  350. package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts.map +0 -1
  351. package/lib/typescript/examples/expo/src/examples/Theme.d.ts +0 -3
  352. package/lib/typescript/examples/expo/src/examples/Theme.d.ts.map +0 -1
  353. package/lib/typescript/examples/expo/src/examples/index.d.ts +0 -8
  354. package/lib/typescript/examples/expo/src/examples/index.d.ts.map +0 -1
  355. package/lib/typescript/examples/expo/src/index.d.ts +0 -2
  356. package/lib/typescript/examples/expo/src/index.d.ts.map +0 -1
  357. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts +0 -8
  358. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts.map +0 -1
  359. package/lib/typescript/examples/expo/src/styles/index.d.ts +0 -46
  360. package/lib/typescript/examples/expo/src/styles/index.d.ts.map +0 -1
  361. package/lib/typescript/examples/expo/src/styles/theme.d.ts +0 -24
  362. package/lib/typescript/examples/expo/src/styles/theme.d.ts.map +0 -1
  363. package/lib/typescript/src/UnistylesTheme.d.ts +0 -9
  364. package/lib/typescript/src/UnistylesTheme.d.ts.map +0 -1
  365. package/lib/typescript/src/createUnistyles.d.ts +0 -10
  366. package/lib/typescript/src/createUnistyles.d.ts.map +0 -1
  367. package/lib/typescript/src/hooks/useDimensions.d.ts +0 -3
  368. package/lib/typescript/src/hooks/useDimensions.d.ts.map +0 -1
  369. package/lib/typescript/src/hooks/useDimensions.web.d.ts +0 -3
  370. package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +0 -1
  371. package/lib/typescript/src/types/mediaQueries.d.ts +0 -8
  372. package/lib/typescript/src/types/mediaQueries.d.ts.map +0 -1
  373. package/lib/typescript/src/utils/common.d.ts +0 -5
  374. package/lib/typescript/src/utils/common.d.ts.map +0 -1
  375. package/lib/typescript/src/utils/mediaQueries.d.ts +0 -130
  376. package/lib/typescript/src/utils/mediaQueries.d.ts.map +0 -1
  377. package/lib/typescript/src/utils/normalizeStyles.d.ts +0 -2
  378. package/lib/typescript/src/utils/normalizeStyles.d.ts.map +0 -1
  379. package/lib/typescript/src/utils/normalizeStyles.web.d.ts +0 -5
  380. package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +0 -1
  381. package/lib/typescript/src/utils/normalizer.d.ts.map +0 -1
  382. package/src/UnistylesTheme.tsx +0 -17
  383. package/src/__tests__/createUnistyles.spec.tsx +0 -192
  384. package/src/createUnistyles.ts +0 -70
  385. package/src/hooks/useDimensions.ts +0 -4
  386. package/src/hooks/useDimensions.web.ts +0 -34
  387. package/src/types/mediaQueries.ts +0 -10
  388. package/src/utils/common.ts +0 -12
  389. package/src/utils/mediaQueries.ts +0 -201
  390. package/src/utils/normalizeStyles.ts +0 -2
  391. /package/lib/commonjs/{utils → normalizer}/module.d.js +0 -0
  392. /package/lib/module/{utils → normalizer}/module.d.js +0 -0
  393. /package/lib/typescript/src/{utils → normalizer}/normalizer.d.ts +0 -0
  394. /package/src/{utils → normalizer}/module.d.ts +0 -0
@@ -1,33 +1,61 @@
1
- import type { MediaQueries } from './mediaQueries'
1
+ import type { ColorValue, OpaqueColorValue } from 'react-native'
2
+ import type { UnistylesTheme } from '../types'
3
+ import type { BreakpointsOrMediaQueries, ToDeepUnistyles } from './stylesheet'
4
+ import type { TransformStyles } from './core'
2
5
 
3
- export type Breakpoints = Record<string, number>
4
- export type SortedBreakpointEntries<B extends Breakpoints> = [[keyof B & string, number]]
6
+ type ExtractTransformArray<T> = T extends object
7
+ ? { [K in keyof T]: ExtractBreakpoints<T[K]> }
8
+ : never
5
9
 
6
- export type ScreenSize = {
7
- width: number,
8
- height: number
9
- }
10
+ type ExtractBreakpoints<T> = T extends object
11
+ ? keyof T extends BreakpointsOrMediaQueries
12
+ ? T[keyof T]
13
+ : T extends Array<ToDeepUnistyles<TransformStyles>>
14
+ ? Array<ExtractTransformArray<T[number]>>
15
+ : {
16
+ [K in keyof T]: ExtractBreakpoints<T[K]>
17
+ }
18
+ : T
10
19
 
11
- export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
12
-
13
- type WithEmptyObject<V> = keyof V extends never ? {} : V
14
-
15
- export type ExtractBreakpoints<T, B extends Breakpoints> = T extends Partial<Record<keyof B & string, infer V>>
16
- ? WithEmptyObject<V>
17
- : T extends (...args: infer A) => infer R
18
- ? (...args: A) => ExtractBreakpoints<R, B>
19
- : {
20
- [K in keyof T]: T[K] extends (...args: infer A) => infer R
21
- ? (...args: A) => ExtractBreakpoints<R, B>
22
- : T[K] extends object
23
- ? ExtractBreakpoints<T[K], B>
20
+ type ParseNestedObject<T> = T extends (...args: infer A) => infer R
21
+ ? (...args: A) => ParseNestedObject<R>
22
+ : T extends object
23
+ ? T extends { variants: infer R }
24
+ ? ParseVariants<FlattenVariants<R>> & ParseNestedObject<Omit<T, 'variants'>>
25
+ : {
26
+ [K in keyof T]: T[K] extends object
27
+ ? T[K] extends OpaqueColorValue
28
+ ? ColorValue
29
+ : ExtractBreakpoints<T[K]>
24
30
  : T[K]
25
- }
31
+ }
32
+ : T
26
33
 
27
- export type RemoveKeysWithPrefix<T, B extends Breakpoints> = T extends (...args: Array<any>) => infer R
28
- ? (...args: Parameters<T>) => RemoveKeysWithPrefix<R, B>
29
- : 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> }
34
+ type FlattenVariants<T> = T extends object
35
+ ? {
36
+ [K in keyof T]: T[K] extends object
37
+ ? {
38
+ [key in keyof T[K]]: T[K][key] extends object
39
+ ? ParseNestedObject<T[K][key]>
40
+ : never
41
+ }
42
+ : never
43
+ }
44
+ : never
45
+
46
+ type ParseVariants<T> = T extends object
47
+ ? T[keyof T] extends object
48
+ ? UnionToIntersection<ParseVariants<T[keyof T]>>
33
49
  : T
50
+ : T
51
+
52
+ type UnionToIntersection<U> =
53
+ (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
54
+
55
+ type ParseStyleKeys<T> = T extends object
56
+ ? { [K in keyof T]: ParseNestedObject<T[K]> }
57
+ : never
58
+
59
+ export type ReactNativeStyleSheet<T> = T extends (theme: UnistylesTheme) => infer R
60
+ ? ParseStyleKeys<R>
61
+ : ParseStyleKeys<T>
@@ -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,15 +14,14 @@ 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'
19
18
 
20
- type ShadowOffset = {
19
+ export type ShadowOffset = {
21
20
  width: number,
22
21
  height: number
23
22
  }
24
23
 
25
- type TransformStyles =
24
+ export type TransformStyles =
26
25
  & PerpectiveTransform
27
26
  & RotateTransform
28
27
  & RotateXTransform
@@ -37,37 +36,13 @@ type TransformStyles =
37
36
  & SkewYTransform
38
37
  & MatrixTransform
39
38
 
40
- type UnistyleNested<B> = {
41
- shadowOffset?: DeepUniStyle<ShadowOffset, B>,
42
- textShadowOffset?: DeepUniStyle<ShadowOffset, B>,
43
- transform?: Array<DeepUniStyle<TransformStyles, B>>
44
- }
45
-
46
- type UniStyle<V, B> = {
47
- [innerKey in keyof B]?: V
48
- } | {
49
- [innerKey in MediaQueries]?: V
50
- } | V
51
-
52
- type DeepUniStyle<T, B> = {
53
- [K in keyof T]?: UniStyle<T[K], B>
39
+ export type ScreenSize = {
40
+ width: number,
41
+ height: number
54
42
  }
55
43
 
56
- // these props are treated differently to nest breakpoints and media queries
57
- type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
58
-
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>
62
-
63
- export type StaticStyles<B extends Breakpoints> =
64
- | UnistyleView<B>
65
- | UnistyleText<B>
66
- | UnistyleImage<B>
67
- & UnistyleNested<B>
68
-
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>
73
- }
44
+ export type RNStyle = ViewStyle & TextStyle & ImageStyle
45
+ export type RNValue = ViewStyle[keyof ViewStyle] | TextStyle[keyof TextStyle] | ImageStyle[keyof ImageStyle]
46
+ export type NestedStyle = Record<keyof UnistylesBreakpoints | symbol, RNValue>
47
+ export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | symbol, RNValue]>
48
+ export type UnistylesTheme = UnistylesThemes[keyof UnistylesThemes]
@@ -1,10 +1,15 @@
1
1
  export * from './normalizer'
2
- export type { CustomNamedStyles } from './core'
2
+ export * from './unistyles'
3
+ export type { Optional, Nullable } from './common'
3
4
  export type {
5
+ NestedStylePairs,
6
+ UnistylesTheme,
4
7
  ScreenSize,
5
- Breakpoints,
6
- CreateStylesFactory,
7
- ExtractBreakpoints,
8
- RemoveKeysWithPrefix,
9
- SortedBreakpointEntries
10
- } from './breakpoints'
8
+ NestedStyle,
9
+ RNValue,
10
+ RNStyle
11
+ } from './core'
12
+ export type { StyleSheetWithSuperPowers, StyleSheet, AllAvailableKeys } from './stylesheet'
13
+ export type { ReactNativeStyleSheet } from './breakpoints'
14
+ export type { ExtractVariantNames } from './variants'
15
+ export type { UnistylesPlugin } from './plugin'
@@ -0,0 +1,7 @@
1
+ import type { RNStyle } from './core'
2
+ import type { UnistylesRuntime } from '../core'
3
+
4
+ export type UnistylesPlugin = {
5
+ name: string,
6
+ onParsedStyle?: (styleKey: string, style: RNStyle, runtime: UnistylesRuntime) => RNStyle
7
+ }
@@ -0,0 +1,49 @@
1
+ import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
2
+ import type { ShadowOffset, TransformStyles, UnistylesTheme } from './core'
3
+ import type { UnistylesBreakpoints } from '../global'
4
+
5
+ // these props are treated differently to nest breakpoints and media queries
6
+ type NestedKeys = 'shadowOffset' | 'transform' | 'textShadowOffset'
7
+
8
+ type UnistyleView = Omit<ViewStyle, NestedKeys>
9
+ type UnistyleText = Omit<TextStyle, NestedKeys>
10
+ type UnistyleImage = Omit<ImageStyle, NestedKeys>
11
+
12
+ type UnistyleNestedStyles = {
13
+ shadowOffset?: ToDeepUnistyles<ShadowOffset>,
14
+ textShadowOffset?: ToDeepUnistyles<ShadowOffset>,
15
+ transform?: Array<ToDeepUnistyles<TransformStyles>>
16
+ }
17
+
18
+ type Variants = {
19
+ variants?: {
20
+ [variantName: string]: {
21
+ [variant: string]: Omit<UnistylesValues, 'variants'>
22
+ }
23
+ }
24
+ }
25
+
26
+ export type ToDeepUnistyles<T> = {
27
+ [K in keyof T]?: T[K] | {
28
+ [key in BreakpointsOrMediaQueries]?: T[K]
29
+ }
30
+ }
31
+
32
+ type AllAvailableStyles = UnistyleView & UnistyleText & UnistyleImage & UnistyleNestedStyles
33
+
34
+ export type AllAvailableKeys = keyof (UnistyleView & UnistyleText & UnistyleImage)
35
+ export type BreakpointsOrMediaQueries = keyof UnistylesBreakpoints | symbol
36
+
37
+ export type UnistylesValues = {
38
+ [propName in AllAvailableKeys]?: AllAvailableStyles[propName] | {
39
+ [key in BreakpointsOrMediaQueries]?: AllAvailableStyles[propName]
40
+ }
41
+ } & Variants & {
42
+ [propName in NestedKeys]?: UnistyleNestedStyles[propName]
43
+ }
44
+
45
+ export type StyleSheet = {
46
+ [styleName: string]: UnistylesValues | ((...args: any) => UnistylesValues)
47
+ }
48
+
49
+ export type StyleSheetWithSuperPowers = ((theme: UnistylesTheme) => StyleSheet) | StyleSheet
@@ -0,0 +1,63 @@
1
+ import { UnistylesEventType, ScreenOrientation, IOSContentSizeCategory, AndroidContentSizeCategory } from '../common'
2
+ import type { UnistylesThemes, UnistylesBreakpoints } from '../global'
3
+ import type { ScreenSize } from './core'
4
+ import type { UnistylesPlugin } from './plugin'
5
+
6
+ export type ColorSchemeName = 'light' | 'dark' | 'unspecified'
7
+
8
+ export type UnistylesConfig = {
9
+ adaptiveThemes?: boolean,
10
+ initialTheme?: keyof UnistylesThemes,
11
+ plugins?: Array<UnistylesPlugin>,
12
+ experimentalCSSMediaQueries?: boolean
13
+ }
14
+
15
+ export type UnistylesBridge = {
16
+ // getters
17
+ screenWidth: number,
18
+ screenHeight: number,
19
+ enabledPlugins: Array<string>,
20
+ hasAdaptiveThemes: boolean,
21
+ themeName: keyof UnistylesThemes,
22
+ breakpoint: keyof UnistylesBreakpoints,
23
+ colorScheme: ColorSchemeName,
24
+ contentSizeCategory: IOSContentSizeCategory | AndroidContentSizeCategory,
25
+ sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>,
26
+
27
+ // setters
28
+ themes: Array<keyof UnistylesThemes>,
29
+ useBreakpoints(breakpoints: UnistylesBreakpoints): void,
30
+ useTheme(name: keyof UnistylesThemes): void,
31
+ useAdaptiveThemes(enable: boolean): void,
32
+ addPlugin(pluginName: string, notify: boolean): void,
33
+ removePlugin(pluginName: string): void
34
+ }
35
+
36
+ export type UnistylesThemeEvent = {
37
+ type: UnistylesEventType.Theme,
38
+ payload: {
39
+ themeName: keyof UnistylesThemes
40
+ }
41
+ }
42
+
43
+ export type UnistylesMobileLayoutEvent = {
44
+ type: UnistylesEventType.Layout,
45
+ payload: {
46
+ screen: ScreenSize,
47
+ breakpoint: keyof UnistylesBreakpoints,
48
+ orientation: typeof ScreenOrientation[keyof typeof ScreenOrientation]
49
+ }
50
+ }
51
+
52
+ export type UnistylesPluginEvent = {
53
+ type: UnistylesEventType.Plugin
54
+ }
55
+
56
+ export type UnistylesDynamicTypeSizeEvent = {
57
+ type: UnistylesEventType.DynamicTypeSize,
58
+ payload: {
59
+ contentSizeCategory: IOSContentSizeCategory | AndroidContentSizeCategory
60
+ }
61
+ }
62
+
63
+ export type UnistylesEvents = UnistylesThemeEvent | UnistylesMobileLayoutEvent | UnistylesPluginEvent | UnistylesDynamicTypeSizeEvent
@@ -0,0 +1,19 @@
1
+ export type ExtractVariantNames<T> = T extends (...args: any) => infer R
2
+ ? ExtractVariantKeys<R>
3
+ : ExtractVariantKeys<T>
4
+
5
+ type ExtractVariantKeys<T> = T extends object
6
+ ? ExtractVariant<T[keyof T]>
7
+ : never
8
+
9
+ type ExtractSubVariantKeys<T> = T extends object
10
+ ? keyof Omit<T, 'default'> | undefined
11
+ : never
12
+
13
+ type ExtractVariant<T> = T extends (...args: any) => infer R
14
+ ? R extends { variants: infer V }
15
+ ? { [key in keyof V]?: ExtractSubVariantKeys<V[key]> }
16
+ : never
17
+ : T extends { variants: infer V }
18
+ ? { [key in keyof V]?: ExtractSubVariantKeys<V[key]> }
19
+ : never
@@ -0,0 +1,60 @@
1
+ import { useMemo } from 'react'
2
+ import { StyleSheet } from 'react-native'
3
+ import { parseStyle, proxifyFunction, withPlugins } from './utils'
4
+ import { useCSS, useUnistyles, useVariants } from './hooks'
5
+ import type { UnistylesBreakpoints } from './global'
6
+ import type { ExtractVariantNames, ReactNativeStyleSheet, StyleSheetWithSuperPowers, UnistylesTheme } from './types'
7
+ import { unistyles } from './core'
8
+ import { isWeb } from './common'
9
+
10
+ type ParsedStylesheet<ST extends StyleSheetWithSuperPowers> = {
11
+ theme: UnistylesTheme,
12
+ breakpoint: keyof UnistylesBreakpoints,
13
+ styles: ReactNativeStyleSheet<ST>
14
+ }
15
+
16
+ /**
17
+ * Hook that enables all the features of Unistyles
18
+ * @param stylesheet - The stylesheet with superpowers to be used
19
+ * @param variantsMap - The map of variants to be used
20
+ * @returns - The theme, current breakpoint and RN compatible styles
21
+ */
22
+ export const useStyles = <ST extends StyleSheetWithSuperPowers>(
23
+ stylesheet?: ST,
24
+ variantsMap?: ExtractVariantNames<typeof stylesheet>
25
+ ): ParsedStylesheet<ST> => {
26
+ const { theme, layout, plugins } = useUnistyles()
27
+ const variants = useVariants(variantsMap)
28
+ const parsedStyles = useMemo(() => typeof stylesheet === 'function'
29
+ ? stylesheet(theme)
30
+ : stylesheet, [theme, stylesheet, layout])
31
+
32
+ const dynamicStyleSheet = useMemo(() => Object
33
+ .entries(parsedStyles || {})
34
+ .reduce((acc, [key, value]) => {
35
+ if (typeof value === 'function') {
36
+ return {
37
+ ...acc,
38
+ [key]: proxifyFunction(key, value, variants)
39
+ }
40
+ }
41
+
42
+ return StyleSheet.create({
43
+ ...acc,
44
+ [key]: withPlugins(key, parseStyle(
45
+ value,
46
+ variants,
47
+ !isWeb || !unistyles.registry.config.experimentalCSSMediaQueries)
48
+ )
49
+ })
50
+ }, {}), [parsedStyles, variants, plugins]
51
+ )
52
+
53
+ useCSS(dynamicStyleSheet as ReactNativeStyleSheet<ST>)
54
+
55
+ return {
56
+ theme,
57
+ breakpoint: layout.breakpoint,
58
+ styles: dynamicStyleSheet as ReactNativeStyleSheet<ST>
59
+ }
60
+ }
@@ -1,140 +1,44 @@
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 => {
25
- const sortedPairs = Object
26
- .entries(breakpoints)
27
- .sort((breakpoint1, breakpoint2) => {
28
- const [, value1] = breakpoint1
29
- const [, value2] = breakpoint2
30
-
31
- return value1 - value2
32
- })
33
-
34
- const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs)) as B
35
- const breakpointValues = Object.values(sortedBreakpoints)
36
- const [firstBreakpoint] = breakpointValues
37
-
38
- if (firstBreakpoint !== 0) {
39
- throwError('first breakpoint must start with 0')
40
- }
41
-
42
- if (breakpointValues.length !== new Set(breakpointValues).size) {
43
- throwError('breakpoint values are duplicated')
1
+ import { unistyles } from '../core'
2
+ import type { Optional, RNValue } from '../types'
3
+ import type { UnistylesBreakpoints } from '../global'
4
+ import { ScreenOrientation, isMobile } from '../common'
5
+ import { getKeyForUnistylesMediaQuery } from './mqParser'
6
+
7
+ export const getValueForBreakpoint = (value: Record<string, RNValue>): Optional<RNValue> => {
8
+ const customMediaQueryKey = getKeyForUnistylesMediaQuery(
9
+ Object.entries(value),
10
+ unistyles.runtime.screen
11
+ ) as keyof typeof value
12
+
13
+ if (customMediaQueryKey) {
14
+ return value[customMediaQueryKey]
44
15
  }
45
16
 
46
- return sortedBreakpoints
47
- }
48
-
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 => {
65
- const [key] = breakpointEntries
66
- .find(([, value], index, otherBreakpoints) => {
67
- const minVal = value
68
- const maxVal = otherBreakpoints[index + 1]?.[1]
69
-
70
- if (!maxVal) {
71
- return true
72
- }
73
-
74
- return width >= minVal && width < maxVal
75
- }) as [keyof B & string, number]
17
+ const hasBreakpoints = unistyles.registry.sortedBreakpointPairs.length > 0
76
18
 
77
- return key
78
- }
19
+ if (!hasBreakpoints && isMobile && (ScreenOrientation.Landscape in value || ScreenOrientation.Portrait in value)) {
20
+ return value[unistyles.runtime.orientation]
21
+ }
79
22
 
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)
23
+ const breakpoint = unistyles.runtime.breakpoint
115
24
 
116
- if (customMediaQueryKey && customMediaQueryKey in value) {
117
- return value[customMediaQueryKey]
25
+ if (!breakpoint) {
26
+ return undefined
118
27
  }
119
28
 
120
- // if no custom media query, or didn't match, proceed with defined breakpoints
121
- const unifiedKey = breakpoint
122
- const directBreakpoint = value[unifiedKey]
29
+ const directBreakpoint = value[breakpoint]
123
30
 
124
- // 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)) {
31
+ if (directBreakpoint || (breakpoint in value)) {
126
32
  return directBreakpoint
127
33
  }
128
34
 
129
- // there is no direct hit for breakpoint nor media-query, so let's simulate CSS cascading
130
- const currentBreakpoint = breakpointPairs
131
- .findIndex(([key]) => key === unifiedKey)
35
+ const breakpointPairs = unistyles.registry.sortedBreakpointPairs
36
+ const currentBreakpointIndex = breakpointPairs
37
+ .findIndex(([key]) => key === breakpoint)
132
38
 
133
39
  const availableBreakpoints = breakpointPairs
134
- .filter(([key], index) => index < currentBreakpoint && key && key in value)
40
+ .filter(([key], index) => index < currentBreakpointIndex && key in value)
135
41
  .map(([key]) => key)
136
42
 
137
- return breakpointPairs.length > 0
138
- ? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof B & string]
139
- : undefined
43
+ return value[availableBreakpoints[availableBreakpoints.length - 1] as keyof UnistylesBreakpoints & string]
140
44
  }