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

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. package/android/CMakeLists.txt +28 -0
  2. package/android/build.gradle +40 -0
  3. package/android/src/main/cxx/cpp-adapter.cpp +108 -0
  4. package/android/src/main/java/com/unistyles/UnistylesModule.kt +159 -0
  5. package/android/src/main/java/com/unistyles/UnistylesPackage.kt +18 -0
  6. package/cxx/UnistylesRuntime.cpp +55 -2
  7. package/cxx/UnistylesRuntime.h +15 -9
  8. package/ios/UnistylesModule.mm +16 -6
  9. package/lib/commonjs/common.js +30 -16
  10. package/lib/commonjs/common.js.map +1 -1
  11. package/lib/commonjs/core/UnistyleRegistry.js +65 -3
  12. package/lib/commonjs/core/UnistyleRegistry.js.map +1 -1
  13. package/lib/commonjs/core/Unistyles.js +1 -1
  14. package/lib/commonjs/core/Unistyles.js.map +1 -1
  15. package/lib/commonjs/core/UnistylesModule.web.js +187 -0
  16. package/lib/commonjs/core/UnistylesModule.web.js.map +1 -0
  17. package/lib/commonjs/core/UnistylesRuntime.js +15 -16
  18. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
  19. package/lib/commonjs/createStyleSheet.js +1 -6
  20. package/lib/commonjs/createStyleSheet.js.map +1 -1
  21. package/lib/commonjs/hooks/index.js +7 -0
  22. package/lib/commonjs/hooks/index.js.map +1 -1
  23. package/lib/commonjs/hooks/useUnistyles.js +12 -9
  24. package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
  25. package/lib/commonjs/hooks/useVariants.js +14 -0
  26. package/lib/commonjs/hooks/useVariants.js.map +1 -0
  27. package/lib/commonjs/index.js.map +1 -1
  28. package/lib/commonjs/plugins/index.js +38 -0
  29. package/lib/commonjs/plugins/index.js.map +1 -0
  30. package/lib/commonjs/plugins/normalizeWebStylesPlugin.js +12 -0
  31. package/lib/commonjs/plugins/normalizeWebStylesPlugin.js.map +1 -0
  32. package/lib/commonjs/plugins/normalizer/index.js +32 -0
  33. package/lib/commonjs/plugins/normalizer/index.js.map +1 -0
  34. package/lib/commonjs/plugins/normalizer/module.d.js.map +1 -0
  35. package/lib/commonjs/{utils/normalizeStyles.web.js → plugins/normalizer/normalizeStyle.js} +5 -5
  36. package/lib/commonjs/plugins/normalizer/normalizeStyle.js.map +1 -0
  37. package/lib/commonjs/{utils → plugins/normalizer}/normalizer.js +1 -1
  38. package/lib/commonjs/plugins/normalizer/normalizer.js.map +1 -0
  39. package/lib/commonjs/types/{mq.js → plugin.js} +1 -1
  40. package/lib/commonjs/types/{mq.js.map → plugin.js.map} +1 -1
  41. package/lib/commonjs/types/stylesheet.js +6 -0
  42. package/lib/commonjs/types/stylesheet.js.map +1 -0
  43. package/lib/commonjs/types/variants.js +2 -0
  44. package/lib/commonjs/{utils/module.d.js.map → types/variants.js.map} +1 -1
  45. package/lib/commonjs/useStyles.js +10 -20
  46. package/lib/commonjs/useStyles.js.map +1 -1
  47. package/lib/commonjs/utils/breakpoints.js +13 -59
  48. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  49. package/lib/commonjs/utils/index.js +13 -51
  50. package/lib/commonjs/utils/index.js.map +1 -1
  51. package/lib/commonjs/utils/mq.js +36 -68
  52. package/lib/commonjs/utils/mq.js.map +1 -1
  53. package/lib/commonjs/utils/mqParser.js +4 -1
  54. package/lib/commonjs/utils/mqParser.js.map +1 -1
  55. package/lib/commonjs/utils/styles.js +35 -20
  56. package/lib/commonjs/utils/styles.js.map +1 -1
  57. package/lib/commonjs/utils/withPlugins.js +15 -0
  58. package/lib/commonjs/utils/withPlugins.js.map +1 -0
  59. package/lib/module/common.js +28 -17
  60. package/lib/module/common.js.map +1 -1
  61. package/lib/module/core/UnistyleRegistry.js +65 -3
  62. package/lib/module/core/UnistyleRegistry.js.map +1 -1
  63. package/lib/module/core/Unistyles.js +2 -2
  64. package/lib/module/core/Unistyles.js.map +1 -1
  65. package/lib/module/core/UnistylesModule.web.js +180 -0
  66. package/lib/module/core/UnistylesModule.web.js.map +1 -0
  67. package/lib/module/core/UnistylesRuntime.js +15 -16
  68. package/lib/module/core/UnistylesRuntime.js.map +1 -1
  69. package/lib/module/createStyleSheet.js +1 -6
  70. package/lib/module/createStyleSheet.js.map +1 -1
  71. package/lib/module/hooks/index.js +1 -0
  72. package/lib/module/hooks/index.js.map +1 -1
  73. package/lib/module/hooks/useUnistyles.js +13 -10
  74. package/lib/module/hooks/useUnistyles.js.map +1 -1
  75. package/lib/module/hooks/useVariants.js +7 -0
  76. package/lib/module/hooks/useVariants.js.map +1 -0
  77. package/lib/module/index.js.map +1 -1
  78. package/lib/module/plugins/index.js +3 -0
  79. package/lib/module/plugins/index.js.map +1 -0
  80. package/lib/module/plugins/normalizeWebStylesPlugin.js +6 -0
  81. package/lib/module/plugins/normalizeWebStylesPlugin.js.map +1 -0
  82. package/lib/module/plugins/normalizer/index.js +3 -0
  83. package/lib/module/plugins/normalizer/index.js.map +1 -0
  84. package/lib/module/plugins/normalizer/module.d.js.map +1 -0
  85. package/lib/module/{utils/normalizeStyles.web.js → plugins/normalizer/normalizeStyle.js} +3 -3
  86. package/lib/module/plugins/normalizer/normalizeStyle.js.map +1 -0
  87. package/lib/module/{utils → plugins/normalizer}/normalizer.js +1 -1
  88. package/lib/module/plugins/normalizer/normalizer.js.map +1 -0
  89. package/lib/module/types/plugin.js +2 -0
  90. package/lib/module/types/{mq.js.map → plugin.js.map} +1 -1
  91. package/lib/module/types/stylesheet.js +2 -0
  92. package/lib/module/types/stylesheet.js.map +1 -0
  93. package/lib/module/types/variants.js +2 -0
  94. package/lib/module/{utils/module.d.js.map → types/variants.js.map} +1 -1
  95. package/lib/module/useStyles.js +12 -22
  96. package/lib/module/useStyles.js.map +1 -1
  97. package/lib/module/utils/breakpoints.js +12 -56
  98. package/lib/module/utils/breakpoints.js.map +1 -1
  99. package/lib/module/utils/index.js +4 -6
  100. package/lib/module/utils/index.js.map +1 -1
  101. package/lib/module/utils/mq.js +35 -67
  102. package/lib/module/utils/mq.js.map +1 -1
  103. package/lib/module/utils/mqParser.js +3 -3
  104. package/lib/module/utils/mqParser.js.map +1 -1
  105. package/lib/module/utils/styles.js +35 -20
  106. package/lib/module/utils/styles.js.map +1 -1
  107. package/lib/module/utils/withPlugins.js +8 -0
  108. package/lib/module/utils/withPlugins.js.map +1 -0
  109. package/lib/typescript/src/common.d.ts +23 -12
  110. package/lib/typescript/src/common.d.ts.map +1 -1
  111. package/lib/typescript/src/core/UnistyleRegistry.d.ts +45 -4
  112. package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -1
  113. package/lib/typescript/src/core/UnistylesModule.web.d.ts +19 -0
  114. package/lib/typescript/src/core/UnistylesModule.web.d.ts.map +1 -0
  115. package/lib/typescript/src/core/UnistylesRuntime.d.ts +8 -9
  116. package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -1
  117. package/lib/typescript/src/core/index.d.ts +1 -0
  118. package/lib/typescript/src/core/index.d.ts.map +1 -1
  119. package/lib/typescript/src/createStyleSheet.d.ts +2 -2
  120. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -1
  121. package/lib/typescript/src/global.d.ts.map +1 -1
  122. package/lib/typescript/src/hooks/index.d.ts +1 -0
  123. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  124. package/lib/typescript/src/hooks/useUnistyles.d.ts +2 -1
  125. package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
  126. package/lib/typescript/src/hooks/useVariants.d.ts +3 -0
  127. package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -0
  128. package/lib/typescript/src/index.d.ts +51 -5
  129. package/lib/typescript/src/index.d.ts.map +1 -1
  130. package/lib/typescript/src/plugins/index.d.ts +3 -0
  131. package/lib/typescript/src/plugins/index.d.ts.map +1 -0
  132. package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts +3 -0
  133. package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts.map +1 -0
  134. package/lib/typescript/src/plugins/normalizer/index.d.ts +3 -0
  135. package/lib/typescript/src/plugins/normalizer/index.d.ts.map +1 -0
  136. package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts +3 -0
  137. package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts.map +1 -0
  138. package/lib/typescript/src/{utils → plugins/normalizer}/normalizer.d.ts +1 -1
  139. package/lib/typescript/src/plugins/normalizer/normalizer.d.ts.map +1 -0
  140. package/lib/typescript/src/types/breakpoints.d.ts +25 -12
  141. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  142. package/lib/typescript/src/types/core.d.ts +6 -27
  143. package/lib/typescript/src/types/core.d.ts.map +1 -1
  144. package/lib/typescript/src/types/index.d.ts +4 -2
  145. package/lib/typescript/src/types/index.d.ts.map +1 -1
  146. package/lib/typescript/src/types/plugin.d.ts +7 -0
  147. package/lib/typescript/src/types/plugin.d.ts.map +1 -0
  148. package/lib/typescript/src/types/stylesheet.d.ts +40 -0
  149. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -0
  150. package/lib/typescript/src/types/unistyles.d.ts +16 -11
  151. package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
  152. package/lib/typescript/src/types/variants.d.ts +14 -0
  153. package/lib/typescript/src/types/variants.d.ts.map +1 -0
  154. package/lib/typescript/src/useStyles.d.ts +3 -3
  155. package/lib/typescript/src/useStyles.d.ts.map +1 -1
  156. package/lib/typescript/src/utils/breakpoints.d.ts +2 -5
  157. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  158. package/lib/typescript/src/utils/index.d.ts +4 -6
  159. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  160. package/lib/typescript/src/utils/mq.d.ts +15 -14
  161. package/lib/typescript/src/utils/mq.d.ts.map +1 -1
  162. package/lib/typescript/src/utils/mqParser.d.ts +14 -2
  163. package/lib/typescript/src/utils/mqParser.d.ts.map +1 -1
  164. package/lib/typescript/src/utils/styles.d.ts +3 -4
  165. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  166. package/lib/typescript/src/utils/withPlugins.d.ts +3 -0
  167. package/lib/typescript/src/utils/withPlugins.d.ts.map +1 -0
  168. package/package.json +19 -16
  169. package/src/__tests__/mocks.ts +24 -0
  170. package/src/common.ts +30 -14
  171. package/src/core/UnistyleRegistry.ts +66 -4
  172. package/src/core/Unistyles.ts +2 -2
  173. package/src/core/UnistylesModule.web.ts +214 -0
  174. package/src/core/UnistylesRuntime.ts +18 -20
  175. package/src/core/index.ts +1 -0
  176. package/src/createStyleSheet.ts +2 -8
  177. package/src/global.ts +1 -0
  178. package/src/hooks/index.ts +1 -0
  179. package/src/hooks/useUnistyles.ts +12 -10
  180. package/src/hooks/useVariants.ts +10 -0
  181. package/src/index.ts +3 -1
  182. package/src/plugins/index.ts +2 -0
  183. package/src/plugins/normalizeWebStylesPlugin.ts +7 -0
  184. package/src/plugins/normalizer/index.ts +2 -0
  185. package/src/{utils/normalizeStyles.web.ts → plugins/normalizer/normalizeStyle.ts} +3 -3
  186. package/src/{utils → plugins/normalizer}/normalizer.ts +3 -3
  187. package/src/types/breakpoints.ts +57 -25
  188. package/src/types/core.ts +9 -43
  189. package/src/types/index.ts +11 -2
  190. package/src/types/plugin.ts +7 -0
  191. package/src/types/stylesheet.ts +49 -0
  192. package/src/types/unistyles.ts +18 -13
  193. package/src/types/variants.ts +19 -0
  194. package/src/useStyles.ts +18 -25
  195. package/src/utils/breakpoints.ts +11 -67
  196. package/src/utils/index.ts +4 -6
  197. package/src/utils/mq.ts +31 -84
  198. package/src/utils/mqParser.ts +5 -5
  199. package/src/utils/styles.ts +44 -50
  200. package/src/utils/withPlugins.ts +13 -0
  201. package/lib/commonjs/utils/common.js +0 -25
  202. package/lib/commonjs/utils/common.js.map +0 -1
  203. package/lib/commonjs/utils/normalizeStyles.js +0 -10
  204. package/lib/commonjs/utils/normalizeStyles.js.map +0 -1
  205. package/lib/commonjs/utils/normalizeStyles.web.js.map +0 -1
  206. package/lib/commonjs/utils/normalizer.js.map +0 -1
  207. package/lib/module/types/mq.js +0 -2
  208. package/lib/module/utils/common.js +0 -17
  209. package/lib/module/utils/common.js.map +0 -1
  210. package/lib/module/utils/normalizeStyles.js +0 -3
  211. package/lib/module/utils/normalizeStyles.js.map +0 -1
  212. package/lib/module/utils/normalizeStyles.web.js.map +0 -1
  213. package/lib/module/utils/normalizer.js.map +0 -1
  214. package/lib/typescript/src/types/mq.d.ts +0 -3
  215. package/lib/typescript/src/types/mq.d.ts.map +0 -1
  216. package/lib/typescript/src/utils/common.d.ts +0 -12
  217. package/lib/typescript/src/utils/common.d.ts.map +0 -1
  218. package/lib/typescript/src/utils/normalizeStyles.d.ts +0 -2
  219. package/lib/typescript/src/utils/normalizeStyles.d.ts.map +0 -1
  220. package/lib/typescript/src/utils/normalizeStyles.web.d.ts +0 -5
  221. package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +0 -1
  222. package/lib/typescript/src/utils/normalizer.d.ts.map +0 -1
  223. package/src/types/mq.ts +0 -3
  224. package/src/utils/common.ts +0 -20
  225. package/src/utils/normalizeStyles.ts +0 -2
  226. /package/lib/commonjs/{utils → plugins/normalizer}/module.d.js +0 -0
  227. /package/lib/module/{utils → plugins/normalizer}/module.d.js +0 -0
  228. /package/src/{utils → plugins/normalizer}/module.d.ts +0 -0
@@ -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
@@ -1,18 +1,21 @@
1
- import { CxxUnistylesEventTypes, ScreenOrientation } from '../common'
1
+ import { UnistylesEventType, ScreenOrientation } from '../common'
2
2
  import type { UnistylesThemes, UnistylesBreakpoints } from '../global'
3
- import type { NestedKeys, ScreenSize } from './core'
4
- import type { Optional } from './common'
3
+ import type { ScreenSize } from './core'
4
+ import type { UnistylesPlugin } from './plugin'
5
5
 
6
- export type ColorSchemeName = Optional<'light' | 'dark'>
6
+ export type ColorSchemeName = 'light' | 'dark' | 'unspecified'
7
7
 
8
8
  export type UnistylesConfig = {
9
- adaptiveThemes?: boolean
9
+ adaptiveThemes?: boolean,
10
+ experimentalPlugins?: Array<UnistylesPlugin>,
11
+ initialTheme?: keyof UnistylesThemes
10
12
  }
11
13
 
12
14
  export type UnistylesBridge = {
13
15
  // getters
14
16
  screenWidth: number,
15
17
  screenHeight: number,
18
+ enabledPlugins: Array<string>,
16
19
  hasAdaptiveThemes: boolean,
17
20
  themeName: keyof UnistylesThemes,
18
21
  breakpoint: keyof UnistylesBreakpoints,
@@ -23,27 +26,29 @@ export type UnistylesBridge = {
23
26
  themes: Array<keyof UnistylesThemes>,
24
27
  useBreakpoints(breakpoints: UnistylesBreakpoints): void,
25
28
  useTheme(name: keyof UnistylesThemes): void,
26
- useAdaptiveThemes(enable: boolean): void
29
+ useAdaptiveThemes(enable: boolean): void,
30
+ addPlugin(pluginName: string, notify: boolean): void,
31
+ removePlugin(pluginName: string): void
27
32
  }
28
33
 
29
34
  export type UnistylesThemeEvent = {
30
- type: CxxUnistylesEventTypes.Theme,
35
+ type: UnistylesEventType.Theme,
31
36
  payload: {
32
37
  themeName: keyof UnistylesThemes
33
38
  }
34
39
  }
35
40
 
36
41
  export type UnistylesMobileLayoutEvent = {
37
- type: CxxUnistylesEventTypes.Layout,
42
+ type: UnistylesEventType.Layout,
38
43
  payload: {
39
44
  screen: ScreenSize,
40
45
  breakpoint: keyof UnistylesBreakpoints,
41
- orientation: ScreenOrientation
46
+ orientation: typeof ScreenOrientation[keyof typeof ScreenOrientation]
42
47
  }
43
48
  }
44
49
 
45
- export type UnistylesEvents = UnistylesThemeEvent | UnistylesMobileLayoutEvent
46
-
47
- export interface UnistylesEngine {
48
- didMatchMediaQuery(keys: NestedKeys): Optional<string>,
50
+ export type UnistylesPluginEvent = {
51
+ type: UnistylesEventType.Plugin
49
52
  }
53
+
54
+ export type UnistylesEvents = UnistylesThemeEvent | UnistylesMobileLayoutEvent | UnistylesPluginEvent
@@ -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
package/src/useStyles.ts CHANGED
@@ -1,53 +1,46 @@
1
1
  import { useMemo } from 'react'
2
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'
3
+ import { parseStyle, proxifyFunction, withPlugins } from './utils'
4
+ import { useUnistyles, useVariants } from './hooks'
6
5
  import type { UnistylesBreakpoints } from './global'
6
+ import type { ExtractVariantNames, ReactNativeStyleSheet, StyleSheetWithSuperPowers, UnistylesTheme } from './types'
7
7
 
8
- type ParsedStylesheet<ST extends CustomNamedStyles<ST>> = {
8
+ type ParsedStylesheet<ST extends StyleSheetWithSuperPowers> = {
9
9
  theme: UnistylesTheme,
10
10
  breakpoint: keyof UnistylesBreakpoints,
11
11
  styles: ReactNativeStyleSheet<ST>
12
12
  }
13
13
 
14
- export const useStyles = <ST extends CustomNamedStyles<ST>>(stylesheet?: ST | CreateStylesFactory<ST, UnistylesTheme>): ParsedStylesheet<ST> => {
15
- const { theme, layout } = useUnistyles()
16
- const { screenSize, breakpoint } = layout
17
-
18
- if (!stylesheet) {
19
- return {
20
- theme,
21
- breakpoint,
22
- styles: {} as ReactNativeStyleSheet<ST>
23
- }
24
- }
25
-
14
+ export const useStyles = <ST extends StyleSheetWithSuperPowers>(
15
+ stylesheet?: ST,
16
+ variantsMap?: ExtractVariantNames<typeof stylesheet>
17
+ ): ParsedStylesheet<ST> => {
18
+ const { theme, layout, plugins } = useUnistyles()
19
+ const variants = useVariants(variantsMap)
26
20
  const parsedStyles = useMemo(() => typeof stylesheet === 'function'
27
21
  ? stylesheet(theme)
28
- : stylesheet, [theme, stylesheet])
22
+ : stylesheet, [theme, stylesheet, layout])
29
23
 
30
24
  const dynamicStyleSheet = useMemo(() => Object
31
- .entries(parsedStyles)
25
+ .entries(parsedStyles || {})
32
26
  .reduce((acc, [key, value]) => {
33
- const style = value as CustomNamedStyles<ST>
34
-
35
27
  if (typeof value === 'function') {
36
28
  return {
37
29
  ...acc,
38
- [key]: proxifyFunction(value, breakpoint, screenSize)
30
+ [key]: proxifyFunction(key, value)
39
31
  }
40
32
  }
41
33
 
42
34
  return StyleSheet.create({
43
35
  ...acc,
44
- [key]: parseStyle<ST>(style, breakpoint, screenSize)
36
+ [key]: withPlugins(key, parseStyle(value, variants))
45
37
  })
46
- }, {} as ST), [breakpoint, screenSize, parsedStyles]) as ReactNativeStyleSheet<ST>
38
+ }, {}), [parsedStyles, variants, plugins]
39
+ )
47
40
 
48
41
  return {
49
42
  theme,
50
- breakpoint,
51
- styles: dynamicStyleSheet
43
+ breakpoint: layout.breakpoint,
44
+ styles: dynamicStyleSheet as ReactNativeStyleSheet<ST>
52
45
  }
53
46
  }
@@ -1,55 +1,12 @@
1
1
  import { unistyles } from '../core'
2
- import { isMobile, Orientation, throwError } from './common'
3
- import type { MediaQuery, NestedKeys } from '../types'
2
+ import type { Optional, RNValue } from '../types'
4
3
  import type { UnistylesBreakpoints } from '../global'
5
- import { ScreenOrientation } from '../common'
4
+ import { ScreenOrientation, isMobile } from '../common'
6
5
  import { getKeyForUnistylesMediaQuery } from './mqParser'
7
6
 
8
- export const sortAndValidateBreakpoints = (breakpoints: UnistylesBreakpoints): UnistylesBreakpoints => {
9
- const sortedPairs = Object
10
- .entries(breakpoints)
11
- .sort((breakpoint1, breakpoint2) => {
12
- const [, value1] = breakpoint1
13
- const [, value2] = breakpoint2
14
-
15
- return (value1 as number) - (value2 as number)
16
- })
17
-
18
- const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs)) as UnistylesBreakpoints
19
- const breakpointValues = Object.values(sortedBreakpoints)
20
- const [firstBreakpoint] = breakpointValues
21
-
22
- if (firstBreakpoint !== 0) {
23
- throwError('first breakpoint must start with 0')
24
- }
25
-
26
- if (breakpointValues.length !== new Set(breakpointValues).size) {
27
- throwError('breakpoint values are duplicated')
28
- }
29
-
30
- return sortedBreakpoints
31
- }
32
-
33
- export const getBreakpointFromScreenWidth = (width: number, breakpointEntries: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>): keyof UnistylesBreakpoints & string => {
34
- const [key] = breakpointEntries
35
- .find(([, value], index, otherBreakpoints) => {
36
- const minVal = value as number
37
- const maxVal = otherBreakpoints[index + 1]?.[1]
38
-
39
- if (!maxVal) {
40
- return true
41
- }
42
-
43
- return width >= minVal && width < maxVal
44
- }) as [keyof UnistylesBreakpoints & string, number]
45
-
46
- return key
47
- }
48
-
49
- export const getValueForBreakpoint = (value: Record<keyof UnistylesBreakpoints | MediaQuery, string | number | undefined>): string | number | undefined => {
50
- // the highest priority is for custom media queries
7
+ export const getValueForBreakpoint = (value: Record<string, RNValue>): Optional<RNValue> => {
51
8
  const customMediaQueryKey = getKeyForUnistylesMediaQuery(
52
- Object.entries(value) as NestedKeys,
9
+ Object.entries(value),
53
10
  unistyles.runtime.screen
54
11
  ) as keyof typeof value
55
12
 
@@ -57,44 +14,31 @@ export const getValueForBreakpoint = (value: Record<keyof UnistylesBreakpoints |
57
14
  return value[customMediaQueryKey]
58
15
  }
59
16
 
60
- // at this point user didn't use custom media queries (:w, :h)
61
- // check if user defined any breakpoints
62
- const hasBreakpoints = unistyles.runtime.sortedBreakpoints.length > 0
17
+ const hasBreakpoints = unistyles.registry.sortedBreakpointPairs.length > 0
63
18
 
64
- // if not then we can fall back to horizontal and portrait (mobile only)
65
- if (!hasBreakpoints && isMobile && (Orientation.Landscape in value || Orientation.Portrait in value)) {
66
- return value[
67
- unistyles.runtime.orientation === ScreenOrientation.Portrait
68
- ? Orientation.Portrait
69
- : Orientation.Landscape
70
- ]
19
+ if (!hasBreakpoints && isMobile && (ScreenOrientation.Landscape in value || ScreenOrientation.Portrait in value)) {
20
+ return value[unistyles.runtime.orientation]
71
21
  }
72
22
 
73
- // let's get the current breakpoint
74
23
  const breakpoint = unistyles.runtime.breakpoint
75
24
 
76
25
  if (!breakpoint) {
77
26
  return undefined
78
27
  }
79
28
 
80
- // if user defined breakpoints, then we look for the valid one
81
29
  const directBreakpoint = value[breakpoint]
82
30
 
83
- // if there is a direct key like 'sm' or 'md', or value for this key exists but its undefined
84
31
  if (directBreakpoint || (breakpoint in value)) {
85
32
  return directBreakpoint
86
33
  }
87
34
 
88
- // there is no direct hit for breakpoint nor media-query, let's simulate CSS cascading
89
- const breakpointPairs = unistyles.runtime.sortedBreakpoints
90
- const currentBreakpoint = breakpointPairs
35
+ const breakpointPairs = unistyles.registry.sortedBreakpointPairs
36
+ const currentBreakpointIndex = breakpointPairs
91
37
  .findIndex(([key]) => key === breakpoint)
92
38
 
93
39
  const availableBreakpoints = breakpointPairs
94
- .filter(([key], index) => index < currentBreakpoint && key && key in value)
40
+ .filter(([key], index) => index < currentBreakpointIndex && key in value)
95
41
  .map(([key]) => key)
96
42
 
97
- return breakpointPairs.length > 0
98
- ? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof UnistylesBreakpoints & string]
99
- : undefined
43
+ return value[availableBreakpoints[availableBreakpoints.length - 1] as keyof UnistylesBreakpoints & string]
100
44
  }
@@ -1,7 +1,5 @@
1
- export { normalizeStyles } from './normalizeStyles'
2
- export * from './normalizer'
3
- export { mq, MQSymbol } from './mq'
4
- export { getKeyForUnistylesMediaQuery } from './mqParser'
5
- export { getBreakpointFromScreenWidth, sortAndValidateBreakpoints, getValueForBreakpoint } from './breakpoints'
1
+ export { mq } from './mq'
2
+ export { getKeyForUnistylesMediaQuery, isWithinTheWidthAndHeight, isValidMq, parseMq } from './mqParser'
3
+ export { getValueForBreakpoint } from './breakpoints'
6
4
  export { proxifyFunction, parseStyle } from './styles'
7
- export { isServer, Orientation } from './common'
5
+ export { withPlugins } from './withPlugins'
package/src/utils/mq.ts CHANGED
@@ -1,38 +1,24 @@
1
- import type { MediaQuery, Nullable } from '../types'
1
+ import type { Nullable } from '../types'
2
2
  import type { UnistylesBreakpoints } from '../global'
3
3
  import { unistyles } from '../core'
4
4
 
5
- export const MQSymbol = Symbol('unistyles-mq')
6
-
7
5
  type MQValue = keyof UnistylesBreakpoints | number
8
6
 
9
7
  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'
8
+ only: {
9
+ width(wMin?: Nullable<MQValue>, wMax?: MQValue): symbol,
10
+ height(hMin?: Nullable<MQValue>, hMax?: MQValue): symbol,
11
+ },
12
+ width(wMin?: Nullable<MQValue>, wMax?: MQValue): {
13
+ and: {
14
+ height(hMin?: Nullable<MQValue>, hMax?: MQValue): symbol
15
+ }
16
+ },
17
+ height(hMin?: Nullable<MQValue>, hMax?: MQValue): {
18
+ and: {
19
+ width(wMin?: Nullable<MQValue>, wMax?: MQValue): symbol
20
+ }
21
+ }
36
22
  }
37
23
 
38
24
  const getMQValue = (value: Nullable<MQValue>) => {
@@ -47,60 +33,21 @@ const getMQValue = (value: Nullable<MQValue>) => {
47
33
  return unistyles.registry.breakpoints[value] ?? 0
48
34
  }
49
35
 
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
36
+ export const mq: MQHandler = {
37
+ only: {
38
+ width: (wMin: Nullable<MQValue> = 0, wMax: MQValue = Infinity) => (`:w[${getMQValue(wMin)}, ${getMQValue(wMax)}]` as unknown as symbol),
39
+ height: (hMin: Nullable<MQValue> = 0, hMax: MQValue = Infinity) => (`:h[${getMQValue(hMin)}, ${getMQValue(hMax)}]` as unknown as symbol)
40
+ },
41
+ width: (wMin: Nullable<MQValue> = 0, wMax: MQValue = Infinity) => ({
42
+ and: {
43
+ height: (hMin: Nullable<MQValue> = 0, hMax: MQValue = Infinity) =>
44
+ (`:w[${getMQValue(wMin)}, ${getMQValue(wMax)}]:h[${getMQValue(hMin)}, ${getMQValue(hMax)}]` as unknown as symbol)
98
45
  }
99
-
100
- if (prop === MQProp.shortH || prop === MQProp.height) {
101
- return widthHandler
46
+ }),
47
+ height: (hMin: Nullable<MQValue> = 0, hMax: MQValue = Infinity) => ({
48
+ and: {
49
+ width: (wMin: Nullable<MQValue> = 0, wMax: MQValue = Infinity) =>
50
+ (`:w[${getMQValue(wMin)}, ${getMQValue(wMax)}]:h[${getMQValue(hMin)}, ${getMQValue(hMax)}]` as unknown as symbol)
102
51
  }
103
-
104
- return Reflect.get(target, prop, receiver)
105
- }
106
- })
52
+ })
53
+ }
@@ -1,4 +1,4 @@
1
- import type { NestedKeys, Optional, ScreenSize } from '../types'
1
+ import type { Optional, RNValue, ScreenSize } from '../types'
2
2
 
3
3
  const IS_UNISTYLES_REGEX = /:([hw])\[(\d+)(?:,\s*(\d+|Infinity))?]/
4
4
  const UNISTYLES_WIDTH_REGEX = /:(w)\[(\d+)(?:,\s*(\d+|Infinity))?]/
@@ -14,7 +14,7 @@ type UnistylesParsedMq = {
14
14
  height?: ParsedMqDimension
15
15
  }
16
16
 
17
- const parseMq = (mq: string): UnistylesParsedMq => {
17
+ export const parseMq = (mq: string): UnistylesParsedMq => {
18
18
  const [, width, fromW, toW] = UNISTYLES_WIDTH_REGEX.exec(mq) || []
19
19
  const [, height, fromH, toH] = UNISTYLES_HEIGHT_REGEX.exec(mq) || []
20
20
 
@@ -32,7 +32,7 @@ const parseMq = (mq: string): UnistylesParsedMq => {
32
32
 
33
33
  const isUnistylesMq = (mq: string) => IS_UNISTYLES_REGEX.test(mq)
34
34
 
35
- const isValidMq = (parsedMq: UnistylesParsedMq) => {
35
+ export const isValidMq = (parsedMq: UnistylesParsedMq) => {
36
36
  const { width, height } = parsedMq
37
37
 
38
38
  if (width && height) {
@@ -50,7 +50,7 @@ const isValidMq = (parsedMq: UnistylesParsedMq) => {
50
50
  return false
51
51
  }
52
52
 
53
- const isWithinTheWidthAndHeight = (parsedMq: UnistylesParsedMq, screenSize: ScreenSize): boolean => {
53
+ export const isWithinTheWidthAndHeight = (parsedMq: UnistylesParsedMq, screenSize: ScreenSize): boolean => {
54
54
  const { width, height } = parsedMq
55
55
 
56
56
  if (width && height) {
@@ -80,7 +80,7 @@ const isWithinTheHeight = (height: UnistylesParsedMq['height'], screenHeight: nu
80
80
  return screenHeight >= from && screenHeight <= to
81
81
  }
82
82
 
83
- export const getKeyForUnistylesMediaQuery = (mediaQueries: NestedKeys, screenSize: ScreenSize) => {
83
+ export const getKeyForUnistylesMediaQuery = (mediaQueries: Array<[string, RNValue]>, screenSize: ScreenSize): Optional<string> => {
84
84
  const mq = mediaQueries.find(([key]) => {
85
85
  if (!isUnistylesMq(key as string)) {
86
86
  return false
@@ -1,15 +1,14 @@
1
- import type { CustomNamedStyles, ScreenSize, MediaQuery } from '../types'
1
+ import type { Optional, RNStyle, RNValue } from '../types'
2
2
  import { getValueForBreakpoint } from './breakpoints'
3
- import { normalizeStyles } from './normalizeStyles'
4
- import type { UnistylesBreakpoints } from '../global'
5
- import { isAndroid, isIOS, isWeb } from './common'
3
+ import { isAndroid, isIOS } from '../common'
4
+ import { withPlugins } from './withPlugins'
6
5
 
7
6
  export const proxifyFunction = (
8
- fn: Function, breakpoint: keyof UnistylesBreakpoints & string,
9
- screenSize: ScreenSize
7
+ key: string,
8
+ fn: Function,
9
+ variant?: Record<string, Optional<string>>
10
10
  ): Function => new Proxy(fn, {
11
- apply: (target, thisArg, argumentsList) =>
12
- parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize)
11
+ apply: (target, thisArg, argumentsList) => withPlugins(key, parseStyle(target.apply(thisArg, argumentsList), variant))
13
12
  })
14
13
 
15
14
  export const isPlatformColor = <T extends {}>(value: T): boolean => {
@@ -20,52 +19,47 @@ export const isPlatformColor = <T extends {}>(value: T): boolean => {
20
19
  return isAndroid && 'resource_paths' in value && typeof value.resource_paths === 'object'
21
20
  }
22
21
 
23
- export const parseStyle = <T>(
24
- style: CustomNamedStyles<T>,
25
- breakpoint: keyof UnistylesBreakpoints & string,
26
- screenSize: ScreenSize
27
- ): T => {
28
- const entries = Object.entries(style || {}) as [[
29
- keyof T,
30
- CustomNamedStyles<T> | Record<keyof UnistylesBreakpoints & string, string | number | undefined>]
31
- ]
22
+ export const parseStyle = <T extends RNStyle>(
23
+ style: T,
24
+ variant: Record<string, Optional<string>> = {}
25
+ ): T => Object
26
+ .entries(style || {})
27
+ .reduce((acc, [key, value]) => {
28
+ // nested objects
29
+ if (key === 'shadowOffset' || key === 'textShadowOffset') {
30
+ acc[key] = parseStyle(value, variant)
32
31
 
33
- const parsedStyles = Object
34
- .fromEntries(entries
35
- .map(([key, value]) => {
36
- const hasNestedProperties = key === 'shadowOffset' || key === 'textShadowOffset'
32
+ return acc
33
+ }
37
34
 
38
- if (hasNestedProperties) {
39
- return [
40
- key,
41
- parseStyle(value as CustomNamedStyles<T>, breakpoint, screenSize)
42
- ]
43
- }
35
+ // transforms
36
+ if (key === 'transform' && Array.isArray(value)) {
37
+ acc[key] = value.map(value => parseStyle(value, variant))
44
38
 
45
- const isTransform = key === 'transform'
39
+ return acc
40
+ }
46
41
 
47
- if (isTransform && Array.isArray(value)) {
48
- return [
49
- key,
50
- value.map(value => parseStyle(value, breakpoint, screenSize))
51
- ]
52
- }
42
+ // values or platform colors
43
+ if (typeof value !== 'object' || isPlatformColor(value)) {
44
+ acc[key as keyof T] = value
53
45
 
54
- const isDynamicFunction = typeof value === 'function'
55
- const isValidStyle = typeof value !== 'object' || isPlatformColor(value)
46
+ return acc
47
+ }
56
48
 
57
- if (isDynamicFunction || isValidStyle) {
58
- return [key, value]
59
- }
49
+ if (key === 'variants') {
50
+ return {
51
+ ...acc,
52
+ ...(Object
53
+ .keys(value) as Array<keyof typeof value>)
54
+ .reduce((acc, key) => ({
55
+ ...acc,
56
+ ...parseStyle((value)[key][variant[key as keyof typeof variant] || 'default'] ?? {})
57
+ }), {})
58
+ }
59
+ }
60
60
 
61
- return [
62
- key,
63
- getValueForBreakpoint(value as Record<keyof UnistylesBreakpoints | MediaQuery, string | number | undefined>)
64
- ]
65
- })
66
- )
67
-
68
- return isWeb
69
- ? normalizeStyles(parsedStyles)
70
- : parsedStyles
71
- }
61
+ return {
62
+ ...acc,
63
+ [key]: getValueForBreakpoint(value as Record<string, RNValue>)
64
+ }
65
+ }, {} as T)
@@ -0,0 +1,13 @@
1
+ import type { RNStyle } from '../types'
2
+ import { unistyles } from '../core'
3
+
4
+ export const withPlugins = (
5
+ key: string,
6
+ style: RNStyle
7
+ ) => unistyles.registry.plugins.reduce((acc, plugin) => {
8
+ if (plugin.onParsedStyle) {
9
+ return plugin.onParsedStyle(key, acc, unistyles.runtime)
10
+ }
11
+
12
+ return acc
13
+ }, style)
@@ -1,25 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.warn = exports.throwError = exports.isWeb = exports.isServer = exports.isMobile = exports.isIOS = exports.isAndroid = exports.Orientation = void 0;
7
- var _reactNative = require("react-native");
8
- const throwError = message => {
9
- throw new Error(`🦄 [react-native-unistyles]: ${message}`);
10
- };
11
- exports.throwError = throwError;
12
- const warn = message => {
13
- console.warn(`🦄 [react-native-unistyles]: ${message}`);
14
- };
15
- exports.warn = warn;
16
- const isMobile = exports.isMobile = _reactNative.Platform.OS === 'android' || _reactNative.Platform.OS === 'ios';
17
- const isWeb = exports.isWeb = _reactNative.Platform.OS === 'web';
18
- const isIOS = exports.isIOS = _reactNative.Platform.OS === 'ios';
19
- const isAndroid = exports.isAndroid = _reactNative.Platform.OS === 'android';
20
- const isServer = exports.isServer = typeof window === 'undefined';
21
- const Orientation = exports.Orientation = {
22
- Landscape: 'landscape',
23
- Portrait: 'portrait'
24
- };
25
- //# sourceMappingURL=common.js.map