react-native-unistyles 2.0.0-alpha.16 → 2.0.0-alpha.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/cxx/UnistylesRuntime.h +3 -3
  2. package/lib/commonjs/common.js +11 -10
  3. package/lib/commonjs/common.js.map +1 -1
  4. package/lib/commonjs/core/UnistyleRegistry.js +17 -4
  5. package/lib/commonjs/core/UnistyleRegistry.js.map +1 -1
  6. package/lib/commonjs/core/UnistylesRuntime.js +4 -1
  7. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
  8. package/lib/commonjs/hooks/useVariants.js +1 -11
  9. package/lib/commonjs/hooks/useVariants.js.map +1 -1
  10. package/lib/commonjs/plugins/index.js +0 -7
  11. package/lib/commonjs/plugins/index.js.map +1 -1
  12. package/lib/commonjs/useStyles.js +4 -12
  13. package/lib/commonjs/useStyles.js.map +1 -1
  14. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  15. package/lib/commonjs/utils/index.js +7 -0
  16. package/lib/commonjs/utils/index.js.map +1 -1
  17. package/lib/commonjs/utils/mqParser.js.map +1 -1
  18. package/lib/commonjs/utils/styles.js +26 -18
  19. package/lib/commonjs/utils/styles.js.map +1 -1
  20. package/lib/commonjs/utils/withPlugins.js +15 -0
  21. package/lib/commonjs/utils/withPlugins.js.map +1 -0
  22. package/lib/module/common.js +11 -10
  23. package/lib/module/common.js.map +1 -1
  24. package/lib/module/core/UnistyleRegistry.js +17 -4
  25. package/lib/module/core/UnistyleRegistry.js.map +1 -1
  26. package/lib/module/core/UnistylesRuntime.js +4 -1
  27. package/lib/module/core/UnistylesRuntime.js.map +1 -1
  28. package/lib/module/hooks/useVariants.js +1 -11
  29. package/lib/module/hooks/useVariants.js.map +1 -1
  30. package/lib/module/plugins/index.js +0 -1
  31. package/lib/module/plugins/index.js.map +1 -1
  32. package/lib/module/useStyles.js +5 -13
  33. package/lib/module/useStyles.js.map +1 -1
  34. package/lib/module/utils/breakpoints.js.map +1 -1
  35. package/lib/module/utils/index.js +1 -0
  36. package/lib/module/utils/index.js.map +1 -1
  37. package/lib/module/utils/mqParser.js.map +1 -1
  38. package/lib/module/utils/styles.js +26 -18
  39. package/lib/module/utils/styles.js.map +1 -1
  40. package/lib/module/utils/withPlugins.js +8 -0
  41. package/lib/module/utils/withPlugins.js.map +1 -0
  42. package/lib/typescript/src/common.d.ts +11 -10
  43. package/lib/typescript/src/common.d.ts.map +1 -1
  44. package/lib/typescript/src/core/UnistyleRegistry.d.ts +39 -3
  45. package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -1
  46. package/lib/typescript/src/core/UnistylesRuntime.d.ts +1 -1
  47. package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -1
  48. package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -1
  49. package/lib/typescript/src/index.d.ts +48 -3
  50. package/lib/typescript/src/index.d.ts.map +1 -1
  51. package/lib/typescript/src/plugins/index.d.ts +0 -1
  52. package/lib/typescript/src/plugins/index.d.ts.map +1 -1
  53. package/lib/typescript/src/types/breakpoints.d.ts +2 -1
  54. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  55. package/lib/typescript/src/types/core.d.ts +2 -3
  56. package/lib/typescript/src/types/core.d.ts.map +1 -1
  57. package/lib/typescript/src/types/index.d.ts +2 -2
  58. package/lib/typescript/src/types/index.d.ts.map +1 -1
  59. package/lib/typescript/src/types/stylesheet.d.ts +5 -5
  60. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
  61. package/lib/typescript/src/useStyles.d.ts.map +1 -1
  62. package/lib/typescript/src/utils/breakpoints.d.ts +2 -2
  63. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  64. package/lib/typescript/src/utils/index.d.ts +1 -0
  65. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  66. package/lib/typescript/src/utils/mqParser.d.ts +2 -2
  67. package/lib/typescript/src/utils/mqParser.d.ts.map +1 -1
  68. package/lib/typescript/src/utils/styles.d.ts +3 -4
  69. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  70. package/lib/typescript/src/utils/withPlugins.d.ts +3 -0
  71. package/lib/typescript/src/utils/withPlugins.d.ts.map +1 -0
  72. package/package.json +1 -1
  73. package/src/common.ts +11 -10
  74. package/src/core/UnistyleRegistry.ts +19 -4
  75. package/src/core/UnistylesRuntime.ts +5 -1
  76. package/src/hooks/useVariants.ts +1 -14
  77. package/src/plugins/index.ts +0 -1
  78. package/src/types/breakpoints.ts +4 -1
  79. package/src/types/core.ts +2 -3
  80. package/src/types/index.ts +1 -2
  81. package/src/types/stylesheet.ts +5 -5
  82. package/src/useStyles.ts +8 -30
  83. package/src/utils/breakpoints.ts +3 -3
  84. package/src/utils/index.ts +1 -0
  85. package/src/utils/mqParser.ts +2 -2
  86. package/src/utils/styles.ts +40 -49
  87. package/src/utils/withPlugins.ts +13 -0
  88. package/lib/commonjs/plugins/withPlugins.js +0 -14
  89. package/lib/commonjs/plugins/withPlugins.js.map +0 -1
  90. package/lib/commonjs/utils/variants.js +0 -52
  91. package/lib/commonjs/utils/variants.js.map +0 -1
  92. package/lib/module/plugins/withPlugins.js +0 -7
  93. package/lib/module/plugins/withPlugins.js.map +0 -1
  94. package/lib/module/utils/variants.js +0 -45
  95. package/lib/module/utils/variants.js.map +0 -1
  96. package/lib/typescript/src/plugins/withPlugins.d.ts +0 -4
  97. package/lib/typescript/src/plugins/withPlugins.d.ts.map +0 -1
  98. package/lib/typescript/src/utils/variants.d.ts +0 -3
  99. package/lib/typescript/src/utils/variants.d.ts.map +0 -1
  100. package/src/plugins/withPlugins.ts +0 -15
  101. package/src/utils/variants.ts +0 -61
@@ -1 +1 @@
1
- {"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../../../../src/utils/styles.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,eAAe,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAElF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAK/C,eAAO,MAAM,eAAe,QACnB,MAAM,MACP,QAAQ,WACH,MAAM,eAAe,CAAC,WACtB,gBAAgB,YACf,OAAO,MAAM,EAAE,SAAS,MAAM,CAAC,CAAC,KAC3C,QAGD,CAAA;AAEF,eAAO,MAAM,eAAe,8BAA6B,OAMxD,CAAA;AAED,eAAO,MAAM,UAAU,8BACd,MAAM,qBAEF,MAAM,eAAe,CAAC,WACtB,gBAAgB,YACf,OAAO,MAAM,EAAE,SAAS,MAAM,CAAC,CAAC,MA0C7C,CAAA"}
1
+ {"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../../../../src/utils/styles.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAW,MAAM,UAAU,CAAA;AAK1D,eAAO,MAAM,eAAe,QACnB,MAAM,MACP,QAAQ,YACF,OAAO,MAAM,EAAE,SAAS,MAAM,CAAC,CAAC,KAC3C,QAED,CAAA;AAEF,eAAO,MAAM,eAAe,8BAA6B,OAMxD,CAAA;AAED,eAAO,MAAM,UAAU,0CAEV,OAAO,MAAM,EAAE,SAAS,MAAM,CAAC,CAAC,MAyC9B,CAAA"}
@@ -0,0 +1,3 @@
1
+ import type { RNStyle } from '../types';
2
+ export declare const withPlugins: (key: string, style: RNStyle) => RNStyle;
3
+ //# sourceMappingURL=withPlugins.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withPlugins.d.ts","sourceRoot":"","sources":["../../../../src/utils/withPlugins.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAGvC,eAAO,MAAM,WAAW,QACf,MAAM,SACJ,OAAO,YAOT,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "2.0.0-alpha.16",
3
+ "version": "2.0.0-alpha.18",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/common.ts CHANGED
@@ -4,10 +4,10 @@ export const warn = (message: string) => {
4
4
  console.warn(`🦄 [react-native-unistyles]: ${message}`)
5
5
  }
6
6
 
7
- export const isMobile = Platform.OS === 'android' || Platform.OS === 'ios'
8
7
  export const isWeb = Platform.OS === 'web'
9
8
  export const isIOS = Platform.OS === 'ios'
10
9
  export const isAndroid = Platform.OS === 'android'
10
+ export const isMobile = isIOS || isAndroid
11
11
  export const isServer = typeof window === 'undefined'
12
12
 
13
13
  export const ScreenOrientation = {
@@ -22,13 +22,14 @@ export enum UnistylesEventType {
22
22
  }
23
23
 
24
24
  export enum UnistylesError {
25
- RuntimeUnavailable = 'Unistyles runtime is not available. Make sure you followed the installation instructions.',
26
- ThemeNotFound = 'You are trying to get a theme that is not registered.',
27
- ThemeNotRegistered = 'You are trying to set a theme that is not registered.',
28
- ThemesCannotBeEmpty = 'You are trying to register an empty themes object.',
29
- BreakpointsCannotBeEmpty = 'You are trying to register an empty breakpoints object.',
30
- BreakpointsMustStartFromZero = 'One breakpoint must start from 0.',
31
- InvalidPluginName = 'Plugin name can\'t start from reserved prefix. Use another name.',
32
- DuplicatePluginName = 'You are trying to register a plugin with a name that is already registered.',
33
- CantRemoveInternalPlugin = 'You are trying to remove an internal unistyles plugin.'
25
+ RuntimeUnavailable = 'Unistyles runtime is not available. Make sure you followed the installation instructions',
26
+ ThemeNotFound = 'You are trying to get a theme that is not registered with UnistylesRegistry',
27
+ ThemeNotRegistered = 'You are trying to set a theme that was not registered with UnistylesRegistry',
28
+ ThemeNotSelected = 'Your themes are registered, but you didn\'t select the initial theme',
29
+ ThemesCannotBeEmpty = 'You are trying to register empty themes object',
30
+ BreakpointsCannotBeEmpty = 'You are trying to register empty breakpoints object',
31
+ BreakpointsMustStartFromZero = 'You are trying to register breakpoints that don\'t start from 0',
32
+ InvalidPluginName = 'Plugin name can\'t start from reserved prefix __unistyles',
33
+ DuplicatePluginName = 'You are trying to register a plugin with a name that is already registered',
34
+ CantRemoveInternalPlugin = 'You are trying to remove an internal unistyles plugin'
34
35
  }
@@ -23,7 +23,10 @@ export class UnistyleRegistry {
23
23
  this.unistylesBridge.themes = keys
24
24
  this.themeNames = keys
25
25
 
26
- return this
26
+ return {
27
+ addBreakpoints: this.addBreakpoints,
28
+ addConfig: this.addConfig
29
+ }
27
30
  }
28
31
 
29
32
  public addBreakpoints = (breakpoints: UnistylesBreakpoints) => {
@@ -31,7 +34,10 @@ export class UnistyleRegistry {
31
34
  this.unistylesBridge.useBreakpoints(breakpoints)
32
35
  this.sortedBreakpointPairs = this.unistylesBridge.sortedBreakpointPairs
33
36
 
34
- return this
37
+ return {
38
+ addThemes: this.addThemes,
39
+ addConfig: this.addConfig
40
+ }
35
41
  }
36
42
 
37
43
  public addConfig = (config: UnistylesConfig) => {
@@ -48,6 +54,11 @@ export class UnistyleRegistry {
48
54
  if (config.initialTheme) {
49
55
  this.unistylesBridge.useTheme(config.initialTheme)
50
56
  }
57
+
58
+ return {
59
+ addBreakpoints: this.addBreakpoints,
60
+ addThemes: this.addThemes
61
+ }
51
62
  }
52
63
 
53
64
  public getTheme = (forName: keyof UnistylesThemes) => {
@@ -55,11 +66,15 @@ export class UnistyleRegistry {
55
66
  return {} as UnistylesThemes[keyof UnistylesThemes]
56
67
  }
57
68
 
58
- if (!this.hasTheme(forName)) {
69
+ if (this.hasTheme(forName)) {
70
+ return this.themes[forName]
71
+ }
72
+
73
+ if (this.unistylesBridge.themeName) {
59
74
  throw new Error(UnistylesError.ThemeNotFound)
60
75
  }
61
76
 
62
- return this.themes[forName]
77
+ throw new Error(UnistylesError.ThemeNotSelected)
63
78
  }
64
79
 
65
80
  public addPlugin = (plugin: UnistylesPlugin, notify: boolean = true) => {
@@ -44,7 +44,11 @@ export class UnistylesRuntime {
44
44
  }
45
45
 
46
46
  public setTheme = (name: keyof UnistylesThemes) => {
47
- if (name !== this.themeName && this.unistylesRegistry.hasTheme(name)) {
47
+ if (name === this.themeName) {
48
+ return
49
+ }
50
+
51
+ if (this.unistylesRegistry.hasTheme(name)) {
48
52
  this.unistylesBridge.useTheme(name)
49
53
 
50
54
  return true
@@ -1,23 +1,10 @@
1
1
  import { useRef } from 'react'
2
2
  import type { Optional } from '../types'
3
3
 
4
- const compareVariants = (prevVariants?: Record<string, Optional<string>>, nextVariants?: Record<string, Optional<string>>) => {
5
- const keysA = Object.keys(prevVariants ?? {})
6
- const keysB = Object.keys(nextVariants ?? {})
7
-
8
- if (keysA.length !== keysB.length) {
9
- return false
10
- }
11
-
12
- return keysA.every(key => prevVariants![key] === (nextVariants ?? {})[key])
13
- }
14
-
15
4
  export const useVariants = (variantsMap?: Record<string, Optional<string>>) => {
16
5
  const variantsRef = useRef<Optional<Record<string, Optional<string>>>>(variantsMap)
17
6
 
18
- if (!compareVariants(variantsRef.current, variantsMap)) {
19
- variantsRef.current = variantsMap
20
- }
7
+ variantsRef.current = variantsMap
21
8
 
22
9
  return variantsRef.current
23
10
  }
@@ -1,3 +1,2 @@
1
1
  export { normalizeWebStylesPlugin } from './normalizeWebStylesPlugin'
2
2
  export { preprocessor, normalizeNumericValue, normalizeColor, normalizeStyle } from './normalizer'
3
- export { withPlugins } from './withPlugins'
@@ -45,10 +45,13 @@ type FlattenVariants<T> = T extends object
45
45
 
46
46
  type ParseVariants<T> = T extends object
47
47
  ? T[keyof T] extends object
48
- ? ParseVariants<T[keyof T]>
48
+ ? UnionToIntersection<ParseVariants<T[keyof T]>>
49
49
  : T
50
50
  : T
51
51
 
52
+ type UnionToIntersection<U> =
53
+ (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
54
+
52
55
  type ParseStyleKeys<T> = T extends object
53
56
  ? { [K in keyof T]: ParseNestedObject<T[K]> }
54
57
  : never
package/src/types/core.ts CHANGED
@@ -41,9 +41,8 @@ export type ScreenSize = {
41
41
  height: number
42
42
  }
43
43
 
44
- export type RNStyle = ViewStyle | TextStyle | ImageStyle
45
- export type RNValue = number | string | undefined
44
+ export type RNStyle = ViewStyle & TextStyle & ImageStyle
45
+ export type RNValue = ViewStyle[keyof ViewStyle] | TextStyle[keyof TextStyle] | ImageStyle[keyof ImageStyle]
46
46
  export type NestedStyle = Record<keyof UnistylesBreakpoints | symbol, RNValue>
47
47
  export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | symbol, RNValue]>
48
48
  export type UnistylesTheme = UnistylesThemes[keyof UnistylesThemes]
49
- export type CreateStylesFactory<ST> = (theme: UnistylesTheme) => ST
@@ -4,13 +4,12 @@ export type { Optional, Nullable } from './common'
4
4
  export type {
5
5
  NestedStylePairs,
6
6
  UnistylesTheme,
7
- CreateStylesFactory,
8
7
  ScreenSize,
9
8
  NestedStyle,
10
9
  RNValue,
11
10
  RNStyle
12
11
  } from './core'
13
- export type { StyleSheetWithSuperPowers, StyleSheet } from './stylesheet'
12
+ export type { StyleSheetWithSuperPowers, StyleSheet, AllAvailableKeys } from './stylesheet'
14
13
  export type { ReactNativeStyleSheet } from './breakpoints'
15
14
  export type { ExtractVariantNames } from './variants'
16
15
  export type { UnistylesPlugin } from './plugin'
@@ -3,11 +3,11 @@ import type { ShadowOffset, TransformStyles, UnistylesTheme } from './core'
3
3
  import type { UnistylesBreakpoints } from '../global'
4
4
 
5
5
  // these props are treated differently to nest breakpoints and media queries
6
- type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
6
+ type NestedKeys = 'shadowOffset' | 'transform' | 'textShadowOffset'
7
7
 
8
- type UnistyleView = Omit<ViewStyle, NestedTypes>
9
- type UnistyleText = Omit<TextStyle, NestedTypes>
10
- type UnistyleImage = Omit<ImageStyle, NestedTypes>
8
+ type UnistyleView = Omit<ViewStyle, NestedKeys>
9
+ type UnistyleText = Omit<TextStyle, NestedKeys>
10
+ type UnistyleImage = Omit<ImageStyle, NestedKeys>
11
11
 
12
12
  type UnistyleNestedStyles = {
13
13
  shadowOffset?: ToDeepUnistyles<ShadowOffset>,
@@ -39,7 +39,7 @@ export type UnistylesValues = {
39
39
  [key in BreakpointsOrMediaQueries]?: AllAvailableStyles[propName]
40
40
  }
41
41
  } & Variants & {
42
- [propName in NestedTypes]?: UnistyleNestedStyles[propName]
42
+ [propName in NestedKeys]?: UnistyleNestedStyles[propName]
43
43
  }
44
44
 
45
45
  export type StyleSheet = {
package/src/useStyles.ts CHANGED
@@ -1,15 +1,9 @@
1
1
  import { useMemo } from 'react'
2
2
  import { StyleSheet } from 'react-native'
3
- import { parseStyle, proxifyFunction } from './utils'
3
+ import { parseStyle, proxifyFunction, withPlugins } from './utils'
4
4
  import { useUnistyles, useVariants } from './hooks'
5
5
  import type { UnistylesBreakpoints } from './global'
6
- import { unistyles } from './core'
7
- import type {
8
- ExtractVariantNames,
9
- ReactNativeStyleSheet,
10
- StyleSheetWithSuperPowers,
11
- UnistylesTheme
12
- } from './types'
6
+ import type { ExtractVariantNames, ReactNativeStyleSheet, StyleSheetWithSuperPowers, UnistylesTheme } from './types'
13
7
 
14
8
  type ParsedStylesheet<ST extends StyleSheetWithSuperPowers> = {
15
9
  theme: UnistylesTheme,
@@ -22,47 +16,31 @@ export const useStyles = <ST extends StyleSheetWithSuperPowers>(
22
16
  variantsMap?: ExtractVariantNames<typeof stylesheet>
23
17
  ): ParsedStylesheet<ST> => {
24
18
  const { theme, layout, plugins } = useUnistyles()
25
-
26
- if (!stylesheet) {
27
- return {
28
- theme,
29
- breakpoint: layout.breakpoint,
30
- styles: {} as ReactNativeStyleSheet<ST>
31
- }
32
- }
33
-
34
19
  const variants = useVariants(variantsMap)
35
20
  const parsedStyles = useMemo(() => typeof stylesheet === 'function'
36
21
  ? stylesheet(theme)
37
22
  : stylesheet, [theme, stylesheet, layout])
38
23
 
39
24
  const dynamicStyleSheet = useMemo(() => Object
40
- .entries(parsedStyles)
25
+ .entries(parsedStyles || {})
41
26
  .reduce((acc, [key, value]) => {
42
27
  if (typeof value === 'function') {
43
28
  return {
44
29
  ...acc,
45
- [key]: proxifyFunction(key, value,unistyles.registry.plugins, unistyles.runtime, variants)
30
+ [key]: proxifyFunction(key, value)
46
31
  }
47
32
  }
48
33
 
49
34
  return StyleSheet.create({
50
35
  ...acc,
51
- [key]: parseStyle(
52
- key,
53
- value,
54
- unistyles.registry.plugins,
55
- unistyles.runtime,
56
- variants
57
- )
36
+ [key]: withPlugins(key, parseStyle(value, variants))
58
37
  })
59
- }, {}),
60
- [layout, parsedStyles, variants, plugins]
61
- ) as ReactNativeStyleSheet<ST>
38
+ }, {}), [parsedStyles, variants, plugins]
39
+ )
62
40
 
63
41
  return {
64
42
  theme,
65
43
  breakpoint: layout.breakpoint,
66
- styles: dynamicStyleSheet
44
+ styles: dynamicStyleSheet as ReactNativeStyleSheet<ST>
67
45
  }
68
46
  }
@@ -1,12 +1,12 @@
1
1
  import { unistyles } from '../core'
2
- import type { NestedStyle, NestedStylePairs, Optional, RNValue } from '../types'
2
+ import type { Optional, RNValue } from '../types'
3
3
  import type { UnistylesBreakpoints } from '../global'
4
4
  import { ScreenOrientation, isMobile } from '../common'
5
5
  import { getKeyForUnistylesMediaQuery } from './mqParser'
6
6
 
7
- export const getValueForBreakpoint = (value: NestedStyle): Optional<RNValue> => {
7
+ export const getValueForBreakpoint = (value: Record<string, RNValue>): Optional<RNValue> => {
8
8
  const customMediaQueryKey = getKeyForUnistylesMediaQuery(
9
- Object.entries(value) as NestedStylePairs,
9
+ Object.entries(value),
10
10
  unistyles.runtime.screen
11
11
  ) as keyof typeof value
12
12
 
@@ -2,3 +2,4 @@ export { mq } from './mq'
2
2
  export { getKeyForUnistylesMediaQuery, isWithinTheWidthAndHeight, isValidMq, parseMq } from './mqParser'
3
3
  export { getValueForBreakpoint } from './breakpoints'
4
4
  export { proxifyFunction, parseStyle } from './styles'
5
+ export { withPlugins } from './withPlugins'
@@ -1,4 +1,4 @@
1
- import type { NestedStylePairs, 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))?]/
@@ -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: NestedStylePairs, screenSize: ScreenSize): Optional<string> => {
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,19 +1,14 @@
1
- import type { NestedStyle, UnistylesPlugin, StyleSheet, Optional } from '../types'
1
+ import type { Optional, RNStyle, RNValue } from '../types'
2
2
  import { getValueForBreakpoint } from './breakpoints'
3
- import type { UnistylesRuntime } from '../core'
4
3
  import { isAndroid, isIOS } from '../common'
5
- import { getStyleWithVariants } from './variants'
6
- import { withPlugins } from '../plugins'
4
+ import { withPlugins } from './withPlugins'
7
5
 
8
6
  export const proxifyFunction = (
9
7
  key: string,
10
8
  fn: Function,
11
- plugins: Array<UnistylesPlugin>,
12
- runtime: UnistylesRuntime,
13
9
  variant?: Record<string, Optional<string>>
14
10
  ): Function => new Proxy(fn, {
15
- apply: (target, thisArg, argumentsList) =>
16
- parseStyle(key, target.apply(thisArg, argumentsList), plugins, runtime, variant)
11
+ apply: (target, thisArg, argumentsList) => withPlugins(key, parseStyle(target.apply(thisArg, argumentsList), variant))
17
12
  })
18
13
 
19
14
  export const isPlatformColor = <T extends {}>(value: T): boolean => {
@@ -24,51 +19,47 @@ export const isPlatformColor = <T extends {}>(value: T): boolean => {
24
19
  return isAndroid && 'resource_paths' in value && typeof value.resource_paths === 'object'
25
20
  }
26
21
 
27
- export const parseStyle = <T extends StyleSheet>(
28
- key: string,
22
+ export const parseStyle = <T extends RNStyle>(
29
23
  style: T,
30
- plugins: Array<UnistylesPlugin>,
31
- runtime: UnistylesRuntime,
32
- variant?: Record<string, Optional<string>>
33
- ): T => {
34
- const entries = Object
35
- .entries(getStyleWithVariants(style || {}, variant)) as Array<[keyof T, StyleSheet]>
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)
36
31
 
37
- const parsedStyles = Object
38
- .fromEntries(entries
39
- .map(([key, value]) => {
40
- // dynamic functions
41
- if (typeof value === 'function') {
42
- return [key, value]
43
- }
32
+ return acc
33
+ }
44
34
 
45
- // nested objects
46
- if (key === 'shadowOffset' || key === 'textShadowOffset') {
47
- return [
48
- key,
49
- parseStyle(key, value, plugins, runtime, variant)
50
- ]
51
- }
35
+ // transforms
36
+ if (key === 'transform' && Array.isArray(value)) {
37
+ acc[key] = value.map(value => parseStyle(value, variant))
52
38
 
53
- // transforms
54
- if (key === 'transform' && Array.isArray(value)) {
55
- return [
56
- key,
57
- value.map(value => parseStyle(key, value, plugins, runtime, variant))
58
- ]
59
- }
39
+ return acc
40
+ }
60
41
 
61
- // values or platform colors
62
- if (typeof value !== 'object' || isPlatformColor(value)) {
63
- return [key, value]
64
- }
42
+ // values or platform colors
43
+ if (typeof value !== 'object' || isPlatformColor(value)) {
44
+ acc[key as keyof T] = value
65
45
 
66
- return [
67
- key,
68
- getValueForBreakpoint(value as NestedStyle)
69
- ]
70
- })
71
- ) as T
46
+ return acc
47
+ }
72
48
 
73
- return withPlugins(key, parsedStyles, plugins, runtime) as T
74
- }
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
+
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, style, unistyles.runtime)
10
+ }
11
+
12
+ return acc
13
+ }, style)
@@ -1,14 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.withPlugins = void 0;
7
- const withPlugins = (key, style, plugins, runtime) => plugins.reduce((acc, plugin) => {
8
- if (plugin.onParsedStyle) {
9
- return plugin.onParsedStyle(key, acc, runtime);
10
- }
11
- return acc;
12
- }, style);
13
- exports.withPlugins = withPlugins;
14
- //# sourceMappingURL=withPlugins.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["withPlugins","key","style","plugins","runtime","reduce","acc","plugin","onParsedStyle","exports"],"sourceRoot":"../../../src","sources":["plugins/withPlugins.ts"],"mappings":";;;;;;AAGO,MAAMA,WAAW,GAAGA,CACvBC,GAAW,EACXC,KAAc,EACdC,OAA+B,EAC/BC,OAAyB,KACxBD,OAAO,CAACE,MAAM,CAAC,CAACC,GAAG,EAAEC,MAAM,KAAK;EACjC,IAAIA,MAAM,CAACC,aAAa,EAAE;IACtB,OAAOD,MAAM,CAACC,aAAa,CAACP,GAAG,EAAEK,GAAG,EAAEF,OAAO,CAAC;EAClD;EAEA,OAAOE,GAAG;AACd,CAAC,EAAEJ,KAAK,CAAC;AAAAO,OAAA,CAAAT,WAAA,GAAAA,WAAA"}
@@ -1,52 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getStyleWithVariants = void 0;
7
- const getKeysForVariants = (value, variants) => {
8
- // case for no specified variants by user, we should fallback to 'default'
9
- if (!variants.length) {
10
- return Object.entries(value).map(_ref => {
11
- let [key, value] = _ref;
12
- if ('default' in value) {
13
- return [key, 'default'];
14
- }
15
- return undefined;
16
- }).filter(Boolean);
17
- }
18
- return variants.map(_ref2 => {
19
- let [variantKey, variantValue] = _ref2;
20
- const variantStyle = value[variantKey];
21
- if (variantStyle && variantValue && variantValue in variantStyle) {
22
- return [variantKey, variantValue];
23
- }
24
- if (variantStyle && 'default' in variantStyle) {
25
- return [variantKey, 'default'];
26
- }
27
- return undefined;
28
- }).filter(Boolean);
29
- };
30
- const getStyleWithVariants = (style, variantValues) => {
31
- if (!('variants' in style)) {
32
- return style;
33
- }
34
- const keys = getKeysForVariants(style.variants, Object.entries(variantValues || {}));
35
- const variantsValues = keys.map(_ref3 => {
36
- let [key, nestedKey] = _ref3;
37
- return style.variants[key][nestedKey];
38
- }).reduce((acc, styles) => ({
39
- ...acc,
40
- ...styles
41
- }), {});
42
- const {
43
- variants,
44
- ...otherStyles
45
- } = style;
46
- return {
47
- ...otherStyles,
48
- ...variantsValues
49
- };
50
- };
51
- exports.getStyleWithVariants = getStyleWithVariants;
52
- //# sourceMappingURL=variants.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["getKeysForVariants","value","variants","length","Object","entries","map","_ref","key","undefined","filter","Boolean","_ref2","variantKey","variantValue","variantStyle","getStyleWithVariants","style","variantValues","keys","variantsValues","_ref3","nestedKey","reduce","acc","styles","otherStyles","exports"],"sourceRoot":"../../../src","sources":["utils/variants.ts"],"mappings":";;;;;;AAEA,MAAMA,kBAAkB,GAAGA,CACvBC,KAAkC,EAClCC,QAA2C,KACjB;EAC1B;EACA,IAAI,CAACA,QAAQ,CAACC,MAAM,EAAE;IAClB,OAAOC,MAAM,CACRC,OAAO,CAACJ,KAAK,CAAC,CACdK,GAAG,CAACC,IAAA,IAAkB;MAAA,IAAjB,CAACC,GAAG,EAAEP,KAAK,CAAC,GAAAM,IAAA;MACd,IAAI,SAAS,IAAIN,KAAK,EAAE;QACpB,OAAO,CAACO,GAAG,EAAE,SAAS,CAAC;MAC3B;MAEA,OAAOC,SAAS;IACpB,CAAC,CAAC,CACDC,MAAM,CAACC,OAAO,CAAC;EACxB;EAEA,OAAOT,QAAQ,CACVI,GAAG,CAACM,KAAA,IAAgC;IAAA,IAA/B,CAACC,UAAU,EAAEC,YAAY,CAAC,GAAAF,KAAA;IAC5B,MAAMG,YAAY,GAAGd,KAAK,CAACY,UAAU,CAAC;IAEtC,IAAIE,YAAY,IAAID,YAAY,IAAIA,YAAY,IAAIC,YAAY,EAAE;MAC9D,OAAO,CAACF,UAAU,EAAEC,YAAY,CAAC;IACrC;IAEA,IAAIC,YAAY,IAAI,SAAS,IAAIA,YAAY,EAAE;MAC3C,OAAO,CAACF,UAAU,EAAE,SAAS,CAAC;IAClC;IAEA,OAAOJ,SAAS;EACpB,CAAC,CAAC,CACDC,MAAM,CAACC,OAAO,CAAC;AACxB,CAAC;AAEM,MAAMK,oBAAoB,GAAGA,CAChCC,KAAiB,EACjBC,aAAgD,KAC/C;EACD,IAAI,EAAE,UAAU,IAAID,KAAK,CAAC,EAAE;IACxB,OAAOA,KAAK;EAChB;EAEA,MAAME,IAAI,GAAGnB,kBAAkB,CAC3BiB,KAAK,CAACf,QAAQ,EACdE,MAAM,CAACC,OAAO,CAACa,aAAa,IAAI,CAAC,CAAC,CACtC,CAAC;EAED,MAAME,cAAc,GAAGD,IAAI,CACtBb,GAAG,CAACe,KAAA;IAAA,IAAC,CAACb,GAAG,EAAEc,SAAS,CAAC,GAAAD,KAAA;IAAA,OAAOJ,KAAK,CAACf,QAAQ,CAAiDM,GAAG,CAAC,CAAiCc,SAAS,CAAC;EAAA,EAAC,CAC3IC,MAAM,CAAC,CAACC,GAAG,EAAEC,MAAM,MAAM;IAAE,GAAGD,GAAG;IAAE,GAAGC;EAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;EAEzD,MAAM;IAAEvB,QAAQ;IAAE,GAAGwB;EAAY,CAAC,GAAGT,KAAK;EAE1C,OAAO;IACH,GAAGS,WAAW;IACd,GAAGN;EACP,CAAC;AACL,CAAC;AAAAO,OAAA,CAAAX,oBAAA,GAAAA,oBAAA"}
@@ -1,7 +0,0 @@
1
- export const withPlugins = (key, style, plugins, runtime) => plugins.reduce((acc, plugin) => {
2
- if (plugin.onParsedStyle) {
3
- return plugin.onParsedStyle(key, acc, runtime);
4
- }
5
- return acc;
6
- }, style);
7
- //# sourceMappingURL=withPlugins.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["withPlugins","key","style","plugins","runtime","reduce","acc","plugin","onParsedStyle"],"sourceRoot":"../../../src","sources":["plugins/withPlugins.ts"],"mappings":"AAGA,OAAO,MAAMA,WAAW,GAAGA,CACvBC,GAAW,EACXC,KAAc,EACdC,OAA+B,EAC/BC,OAAyB,KACxBD,OAAO,CAACE,MAAM,CAAC,CAACC,GAAG,EAAEC,MAAM,KAAK;EACjC,IAAIA,MAAM,CAACC,aAAa,EAAE;IACtB,OAAOD,MAAM,CAACC,aAAa,CAACP,GAAG,EAAEK,GAAG,EAAEF,OAAO,CAAC;EAClD;EAEA,OAAOE,GAAG;AACd,CAAC,EAAEJ,KAAK,CAAC"}
@@ -1,45 +0,0 @@
1
- const getKeysForVariants = (value, variants) => {
2
- // case for no specified variants by user, we should fallback to 'default'
3
- if (!variants.length) {
4
- return Object.entries(value).map(_ref => {
5
- let [key, value] = _ref;
6
- if ('default' in value) {
7
- return [key, 'default'];
8
- }
9
- return undefined;
10
- }).filter(Boolean);
11
- }
12
- return variants.map(_ref2 => {
13
- let [variantKey, variantValue] = _ref2;
14
- const variantStyle = value[variantKey];
15
- if (variantStyle && variantValue && variantValue in variantStyle) {
16
- return [variantKey, variantValue];
17
- }
18
- if (variantStyle && 'default' in variantStyle) {
19
- return [variantKey, 'default'];
20
- }
21
- return undefined;
22
- }).filter(Boolean);
23
- };
24
- export const getStyleWithVariants = (style, variantValues) => {
25
- if (!('variants' in style)) {
26
- return style;
27
- }
28
- const keys = getKeysForVariants(style.variants, Object.entries(variantValues || {}));
29
- const variantsValues = keys.map(_ref3 => {
30
- let [key, nestedKey] = _ref3;
31
- return style.variants[key][nestedKey];
32
- }).reduce((acc, styles) => ({
33
- ...acc,
34
- ...styles
35
- }), {});
36
- const {
37
- variants,
38
- ...otherStyles
39
- } = style;
40
- return {
41
- ...otherStyles,
42
- ...variantsValues
43
- };
44
- };
45
- //# sourceMappingURL=variants.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["getKeysForVariants","value","variants","length","Object","entries","map","_ref","key","undefined","filter","Boolean","_ref2","variantKey","variantValue","variantStyle","getStyleWithVariants","style","variantValues","keys","variantsValues","_ref3","nestedKey","reduce","acc","styles","otherStyles"],"sourceRoot":"../../../src","sources":["utils/variants.ts"],"mappings":"AAEA,MAAMA,kBAAkB,GAAGA,CACvBC,KAAkC,EAClCC,QAA2C,KACjB;EAC1B;EACA,IAAI,CAACA,QAAQ,CAACC,MAAM,EAAE;IAClB,OAAOC,MAAM,CACRC,OAAO,CAACJ,KAAK,CAAC,CACdK,GAAG,CAACC,IAAA,IAAkB;MAAA,IAAjB,CAACC,GAAG,EAAEP,KAAK,CAAC,GAAAM,IAAA;MACd,IAAI,SAAS,IAAIN,KAAK,EAAE;QACpB,OAAO,CAACO,GAAG,EAAE,SAAS,CAAC;MAC3B;MAEA,OAAOC,SAAS;IACpB,CAAC,CAAC,CACDC,MAAM,CAACC,OAAO,CAAC;EACxB;EAEA,OAAOT,QAAQ,CACVI,GAAG,CAACM,KAAA,IAAgC;IAAA,IAA/B,CAACC,UAAU,EAAEC,YAAY,CAAC,GAAAF,KAAA;IAC5B,MAAMG,YAAY,GAAGd,KAAK,CAACY,UAAU,CAAC;IAEtC,IAAIE,YAAY,IAAID,YAAY,IAAIA,YAAY,IAAIC,YAAY,EAAE;MAC9D,OAAO,CAACF,UAAU,EAAEC,YAAY,CAAC;IACrC;IAEA,IAAIC,YAAY,IAAI,SAAS,IAAIA,YAAY,EAAE;MAC3C,OAAO,CAACF,UAAU,EAAE,SAAS,CAAC;IAClC;IAEA,OAAOJ,SAAS;EACpB,CAAC,CAAC,CACDC,MAAM,CAACC,OAAO,CAAC;AACxB,CAAC;AAED,OAAO,MAAMK,oBAAoB,GAAGA,CAChCC,KAAiB,EACjBC,aAAgD,KAC/C;EACD,IAAI,EAAE,UAAU,IAAID,KAAK,CAAC,EAAE;IACxB,OAAOA,KAAK;EAChB;EAEA,MAAME,IAAI,GAAGnB,kBAAkB,CAC3BiB,KAAK,CAACf,QAAQ,EACdE,MAAM,CAACC,OAAO,CAACa,aAAa,IAAI,CAAC,CAAC,CACtC,CAAC;EAED,MAAME,cAAc,GAAGD,IAAI,CACtBb,GAAG,CAACe,KAAA;IAAA,IAAC,CAACb,GAAG,EAAEc,SAAS,CAAC,GAAAD,KAAA;IAAA,OAAOJ,KAAK,CAACf,QAAQ,CAAiDM,GAAG,CAAC,CAAiCc,SAAS,CAAC;EAAA,EAAC,CAC3IC,MAAM,CAAC,CAACC,GAAG,EAAEC,MAAM,MAAM;IAAE,GAAGD,GAAG;IAAE,GAAGC;EAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;EAEzD,MAAM;IAAEvB,QAAQ;IAAE,GAAGwB;EAAY,CAAC,GAAGT,KAAK;EAE1C,OAAO;IACH,GAAGS,WAAW;IACd,GAAGN;EACP,CAAC;AACL,CAAC"}
@@ -1,4 +0,0 @@
1
- import type { RNStyle, UnistylesPlugin } from '../types';
2
- import type { UnistylesRuntime } from '../core';
3
- export declare const withPlugins: (key: string, style: RNStyle, plugins: Array<UnistylesPlugin>, runtime: UnistylesRuntime) => RNStyle;
4
- //# sourceMappingURL=withPlugins.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"withPlugins.d.ts","sourceRoot":"","sources":["../../../../src/plugins/withPlugins.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AACxD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE/C,eAAO,MAAM,WAAW,QACf,MAAM,SACJ,OAAO,WACL,MAAM,eAAe,CAAC,WACtB,gBAAgB,YAOpB,CAAA"}
@@ -1,3 +0,0 @@
1
- import type { Optional, StyleSheet } from '../types';
2
- export declare const getStyleWithVariants: (style: StyleSheet, variantValues?: Record<string, Optional<string>>) => StyleSheet;
3
- //# sourceMappingURL=variants.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"variants.d.ts","sourceRoot":"","sources":["../../../../src/utils/variants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAe,MAAM,UAAU,CAAA;AAqCjE,eAAO,MAAM,oBAAoB,UACtB,UAAU,kBACD,OAAO,MAAM,EAAE,SAAS,MAAM,CAAC,CAAC,eAqBnD,CAAA"}