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

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. package/cxx/UnistylesRuntime.h +6 -6
  2. package/lib/commonjs/common.js +15 -15
  3. package/lib/commonjs/common.js.map +1 -1
  4. package/lib/commonjs/core/UnistylesRuntime.js +1 -1
  5. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
  6. package/lib/commonjs/createStyleSheet.js +1 -6
  7. package/lib/commonjs/createStyleSheet.js.map +1 -1
  8. package/lib/commonjs/hooks/index.js +7 -0
  9. package/lib/commonjs/hooks/index.js.map +1 -1
  10. package/lib/commonjs/hooks/useUnistyles.js +4 -7
  11. package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
  12. package/lib/commonjs/hooks/useVariants.js +24 -0
  13. package/lib/commonjs/hooks/useVariants.js.map +1 -0
  14. package/lib/commonjs/plugins/normalizer/normalizer.js +1 -1
  15. package/lib/commonjs/plugins/normalizer/normalizer.js.map +1 -1
  16. package/lib/commonjs/types/{mq.js → stylesheet.js} +1 -1
  17. package/lib/commonjs/types/{mq.js.map → stylesheet.js.map} +1 -1
  18. package/lib/commonjs/useStyles.js +5 -4
  19. package/lib/commonjs/useStyles.js.map +1 -1
  20. package/lib/commonjs/utils/index.js +0 -6
  21. package/lib/commonjs/utils/index.js.map +1 -1
  22. package/lib/commonjs/utils/mq.js +36 -68
  23. package/lib/commonjs/utils/mq.js.map +1 -1
  24. package/lib/commonjs/utils/styles.js +14 -8
  25. package/lib/commonjs/utils/styles.js.map +1 -1
  26. package/lib/commonjs/utils/variants.js +33 -13
  27. package/lib/commonjs/utils/variants.js.map +1 -1
  28. package/lib/module/common.js +14 -14
  29. package/lib/module/common.js.map +1 -1
  30. package/lib/module/core/UnistylesRuntime.js +1 -1
  31. package/lib/module/core/UnistylesRuntime.js.map +1 -1
  32. package/lib/module/createStyleSheet.js +1 -6
  33. package/lib/module/createStyleSheet.js.map +1 -1
  34. package/lib/module/hooks/index.js +1 -0
  35. package/lib/module/hooks/index.js.map +1 -1
  36. package/lib/module/hooks/useUnistyles.js +5 -8
  37. package/lib/module/hooks/useUnistyles.js.map +1 -1
  38. package/lib/module/hooks/useVariants.js +17 -0
  39. package/lib/module/hooks/useVariants.js.map +1 -0
  40. package/lib/module/plugins/normalizer/normalizer.js +1 -1
  41. package/lib/module/plugins/normalizer/normalizer.js.map +1 -1
  42. package/lib/module/types/stylesheet.js +2 -0
  43. package/lib/module/types/{mq.js.map → stylesheet.js.map} +1 -1
  44. package/lib/module/useStyles.js +6 -5
  45. package/lib/module/useStyles.js.map +1 -1
  46. package/lib/module/utils/index.js +1 -1
  47. package/lib/module/utils/index.js.map +1 -1
  48. package/lib/module/utils/mq.js +35 -67
  49. package/lib/module/utils/mq.js.map +1 -1
  50. package/lib/module/utils/styles.js +15 -9
  51. package/lib/module/utils/styles.js.map +1 -1
  52. package/lib/module/utils/variants.js +31 -11
  53. package/lib/module/utils/variants.js.map +1 -1
  54. package/lib/typescript/src/common.d.ts +10 -10
  55. package/lib/typescript/src/common.d.ts.map +1 -1
  56. package/lib/typescript/src/createStyleSheet.d.ts +2 -2
  57. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -1
  58. package/lib/typescript/src/global.d.ts.map +1 -1
  59. package/lib/typescript/src/hooks/index.d.ts +1 -0
  60. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  61. package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
  62. package/lib/typescript/src/hooks/useVariants.d.ts +3 -0
  63. package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -0
  64. package/lib/typescript/src/plugins/normalizer/normalizer.d.ts.map +1 -1
  65. package/lib/typescript/src/types/breakpoints.d.ts +24 -15
  66. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  67. package/lib/typescript/src/types/core.d.ts +5 -29
  68. package/lib/typescript/src/types/core.d.ts.map +1 -1
  69. package/lib/typescript/src/types/index.d.ts +2 -2
  70. package/lib/typescript/src/types/index.d.ts.map +1 -1
  71. package/lib/typescript/src/types/stylesheet.d.ts +40 -0
  72. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -0
  73. package/lib/typescript/src/types/unistyles.d.ts +5 -6
  74. package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
  75. package/lib/typescript/src/types/variants.d.ts +13 -7
  76. package/lib/typescript/src/types/variants.d.ts.map +1 -1
  77. package/lib/typescript/src/useStyles.d.ts +3 -3
  78. package/lib/typescript/src/useStyles.d.ts.map +1 -1
  79. package/lib/typescript/src/utils/index.d.ts +1 -1
  80. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  81. package/lib/typescript/src/utils/mq.d.ts +15 -14
  82. package/lib/typescript/src/utils/mq.d.ts.map +1 -1
  83. package/lib/typescript/src/utils/styles.d.ts +3 -3
  84. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  85. package/lib/typescript/src/utils/variants.d.ts +2 -2
  86. package/lib/typescript/src/utils/variants.d.ts.map +1 -1
  87. package/package.json +15 -15
  88. package/src/common.ts +10 -10
  89. package/src/core/UnistylesRuntime.ts +1 -1
  90. package/src/createStyleSheet.ts +2 -8
  91. package/src/global.ts +1 -0
  92. package/src/hooks/index.ts +1 -0
  93. package/src/hooks/useUnistyles.ts +5 -8
  94. package/src/hooks/useVariants.ts +23 -0
  95. package/src/plugins/normalizer/normalizer.ts +3 -3
  96. package/src/types/breakpoints.ts +51 -27
  97. package/src/types/core.ts +8 -45
  98. package/src/types/index.ts +1 -2
  99. package/src/types/stylesheet.ts +49 -0
  100. package/src/types/unistyles.ts +5 -6
  101. package/src/types/variants.ts +17 -9
  102. package/src/useStyles.ts +15 -15
  103. package/src/utils/index.ts +1 -1
  104. package/src/utils/mq.ts +31 -84
  105. package/src/utils/styles.ts +18 -17
  106. package/src/utils/variants.ts +41 -17
  107. package/lib/module/types/mq.js +0 -2
  108. package/lib/typescript/src/types/mq.d.ts +0 -3
  109. package/lib/typescript/src/types/mq.d.ts.map +0 -1
  110. package/src/types/mq.ts +0 -3
@@ -0,0 +1,23 @@
1
+ import { useRef } from 'react'
2
+ import type { Optional } from '../types'
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
+ export const useVariants = (variantsMap?: Record<string, Optional<string>>) => {
16
+ const variantsRef = useRef<Optional<Record<string, Optional<string>>>>(variantsMap)
17
+
18
+ if (!compareVariants(variantsRef.current, variantsMap)) {
19
+ variantsRef.current = variantsMap
20
+ }
21
+
22
+ return variantsRef.current
23
+ }
@@ -1,7 +1,7 @@
1
1
  // based on react-native-web normalizer
2
2
  // https://github.com/necolas/react-native-web
3
3
  import normalizeColors from '@react-native/normalize-colors'
4
- import type { TextShadow, Transforms, BoxShadow } from '../../types'
4
+ import type { TextShadow, Transforms, BoxShadow, Nullable } from '../../types'
5
5
 
6
6
  type Preprocessor = {
7
7
  createTextShadowValue(style: TextShadow): string,
@@ -15,9 +15,9 @@ export const normalizeColor = (color: string, opacity: number = 1) => {
15
15
  return color
16
16
  }
17
17
 
18
- const integer = normalizeColors(color) as number | null
18
+ const integer = normalizeColors(color) as Nullable<number>
19
19
 
20
- // If the colour is an unknown format, the return value is null
20
+ // If the color is an unknown format, the return value is null
21
21
  if (integer === null) {
22
22
  return color
23
23
  }
@@ -1,34 +1,58 @@
1
- import type { OpaqueColorValue } from 'react-native'
2
- import type { UnistylesBreakpoints } from '../global'
3
- import type { MediaQuery } from './mq'
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'
4
5
 
5
- type WithEmptyObject<V> = keyof V extends never ? {} : V
6
+ type ExtractTransformArray<T> = T extends object
7
+ ? { [K in keyof T]: ExtractBreakpoints<T[K]> }
8
+ : never
6
9
 
7
- type ExtractBreakpoints<T> = T extends Partial<Record<keyof UnistylesBreakpoints & string, infer V>>
8
- ? WithEmptyObject<V>
9
- : T extends (...args: infer A) => infer R
10
- ? (...args: A) => ExtractBreakpoints<R>
11
- : {
12
- [K in keyof T]: T[K] extends (...args: infer A) => infer R
13
- ? (...args: A) => ExtractBreakpoints<R>
14
- : T[K] extends object
15
- ? ExtractBreakpoints<T[K]>
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
19
+
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]>
16
30
  : T[K]
17
- }
31
+ }
32
+ : T
18
33
 
19
- type UnionToIntersection<U> =
20
- (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
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
21
45
 
22
- type RemoveKeysWithPrefix<T> = T extends (...args: Array<any>) => infer R
23
- ? (...args: Parameters<T>) => RemoveKeysWithPrefix<R>
24
- : T extends object
25
- ? T extends OpaqueColorValue
26
- ? string
27
- : T extends Record<string, infer _V>
28
- ? T extends { variants: infer _V }
29
- ? Omit<T, 'variants'> & UnionToIntersection<_V[keyof _V]>
30
- : { [K in keyof T as K extends MediaQuery ? keyof UnistylesBreakpoints & string : K]: RemoveKeysWithPrefix<T[K]> }
31
- : { [K in keyof T]: RemoveKeysWithPrefix<T[K]> }
46
+ type ParseVariants<T> = T extends object
47
+ ? T[keyof T] extends object
48
+ ? ParseVariants<T[keyof T]>
32
49
  : T
50
+ : T
51
+
52
+ type ParseStyleKeys<T> = T extends object
53
+ ? { [K in keyof T]: ParseNestedObject<T[K]> }
54
+ : never
33
55
 
34
- export type ReactNativeStyleSheet<T> = ExtractBreakpoints<RemoveKeysWithPrefix<T>>
56
+ export type ReactNativeStyleSheet<T> = T extends (theme: UnistylesTheme) => infer R
57
+ ? ParseStyleKeys<R>
58
+ : ParseStyleKeys<T>
package/src/types/core.ts CHANGED
@@ -15,19 +15,13 @@ import type {
15
15
  } from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
16
16
  import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
17
17
  import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
18
- import type { MediaQuery } from './mq'
19
18
 
20
- type ShadowOffset = {
19
+ export type ShadowOffset = {
21
20
  width: number,
22
21
  height: number
23
22
  }
24
23
 
25
- export type ScreenSize = {
26
- width: number,
27
- height: number
28
- }
29
-
30
- type TransformStyles =
24
+ export type TransformStyles =
31
25
  & PerpectiveTransform
32
26
  & RotateTransform
33
27
  & RotateXTransform
@@ -42,45 +36,14 @@ type TransformStyles =
42
36
  & SkewYTransform
43
37
  & MatrixTransform
44
38
 
45
- type UnistyleNested = {
46
- shadowOffset?: DeepUniStyle<ShadowOffset>,
47
- textShadowOffset?: DeepUniStyle<ShadowOffset>,
48
- transform?: Array<DeepUniStyle<TransformStyles>>,
49
- variants?: Record<string, UnistyleView | UnistyleText | UnistyleImage & Omit<UnistyleNested, 'variants'>>
50
- }
51
-
52
- type UniStyle<V> = {
53
- [innerKey in keyof UnistylesBreakpoints]?: V
54
- } | {
55
- [innerKey in MediaQuery]: V
56
- }
57
-
58
- type DeepUniStyle<T> = {
59
- [K in keyof T]?: UniStyle<T[K]> | T[K]
60
- }
61
-
62
- // these props are treated differently to nest breakpoints and media queries
63
- type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
64
-
65
- type UnistyleView = DeepUniStyle<Omit<ViewStyle, NestedTypes>>
66
- type UnistyleText = DeepUniStyle<Omit<TextStyle, NestedTypes>>
67
- type UnistyleImage = DeepUniStyle<Omit<ImageStyle, NestedTypes>>
68
-
69
- export type StaticStyles =
70
- | UnistyleView
71
- | UnistyleText
72
- | UnistyleImage
73
- & UnistyleNested
74
-
75
- export type CustomNamedStyles<T> = {
76
- [K in keyof T]: T[K] extends (...args: infer A) => StaticStyles
77
- ? (...args: A) => StaticStyles
78
- : StaticStyles
39
+ export type ScreenSize = {
40
+ width: number,
41
+ height: number
79
42
  }
80
43
 
81
44
  export type RNStyle = ViewStyle | TextStyle | ImageStyle
82
45
  export type RNValue = number | string | undefined
83
- export type NestedStyle = Record<keyof UnistylesBreakpoints | MediaQuery, RNValue>
84
- export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | MediaQuery, RNValue]>
46
+ export type NestedStyle = Record<keyof UnistylesBreakpoints | symbol, RNValue>
47
+ export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | symbol, RNValue]>
85
48
  export type UnistylesTheme = UnistylesThemes[keyof UnistylesThemes]
86
- export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
49
+ export type CreateStylesFactory<ST> = (theme: UnistylesTheme) => ST
@@ -1,9 +1,7 @@
1
1
  export * from './normalizer'
2
2
  export * from './unistyles'
3
3
  export type { Optional, Nullable } from './common'
4
- export type { MediaQuery } from './mq'
5
4
  export type {
6
- CustomNamedStyles,
7
5
  NestedStylePairs,
8
6
  UnistylesTheme,
9
7
  CreateStylesFactory,
@@ -12,6 +10,7 @@ export type {
12
10
  RNValue,
13
11
  RNStyle
14
12
  } from './core'
13
+ export type { StyleSheetWithSuperPowers, StyleSheet } from './stylesheet'
15
14
  export type { ReactNativeStyleSheet } from './breakpoints'
16
15
  export type { ExtractVariantNames } from './variants'
17
16
  export type { UnistylesPlugin } from './plugin'
@@ -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 NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
7
+
8
+ type UnistyleView = Omit<ViewStyle, NestedTypes>
9
+ type UnistyleText = Omit<TextStyle, NestedTypes>
10
+ type UnistyleImage = Omit<ImageStyle, NestedTypes>
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 NestedTypes]?: 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,10 +1,9 @@
1
- import { CxxUnistylesEventTypes, ScreenOrientation } from '../common'
1
+ import { UnistylesEventType, ScreenOrientation } from '../common'
2
2
  import type { UnistylesThemes, UnistylesBreakpoints } from '../global'
3
3
  import type { ScreenSize } from './core'
4
- import type { Optional } from './common'
5
4
  import type { UnistylesPlugin } from './plugin'
6
5
 
7
- export type ColorSchemeName = Optional<'light' | 'dark'>
6
+ export type ColorSchemeName = 'light' | 'dark' | 'unspecified'
8
7
 
9
8
  export type UnistylesConfig = {
10
9
  adaptiveThemes?: boolean,
@@ -33,14 +32,14 @@ export type UnistylesBridge = {
33
32
  }
34
33
 
35
34
  export type UnistylesThemeEvent = {
36
- type: CxxUnistylesEventTypes.Theme,
35
+ type: UnistylesEventType.Theme,
37
36
  payload: {
38
37
  themeName: keyof UnistylesThemes
39
38
  }
40
39
  }
41
40
 
42
41
  export type UnistylesMobileLayoutEvent = {
43
- type: CxxUnistylesEventTypes.Layout,
42
+ type: UnistylesEventType.Layout,
44
43
  payload: {
45
44
  screen: ScreenSize,
46
45
  breakpoint: keyof UnistylesBreakpoints,
@@ -49,7 +48,7 @@ export type UnistylesMobileLayoutEvent = {
49
48
  }
50
49
 
51
50
  export type UnistylesPluginEvent = {
52
- type: CxxUnistylesEventTypes.Plugin
51
+ type: UnistylesEventType.Plugin
53
52
  }
54
53
 
55
54
  export type UnistylesEvents = UnistylesThemeEvent | UnistylesMobileLayoutEvent | UnistylesPluginEvent
@@ -1,11 +1,19 @@
1
- export type ExtractVariantNames<T> = T extends object
2
- ? {
3
- [K in keyof T]: T[K] extends { variants: infer V }
4
- ? V extends object
5
- ? { [VK in keyof V]: VK extends 'default'
6
- ? never : VK }[keyof V] | ExtractVariantNames<V>
7
- : never
8
- : ExtractVariantNames<T[K]>
9
- }[keyof T]
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
10
11
  : never
11
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,26 +1,25 @@
1
1
  import { useMemo } from 'react'
2
2
  import { StyleSheet } from 'react-native'
3
3
  import { parseStyle, proxifyFunction } from './utils'
4
+ import { useUnistyles, useVariants } from './hooks'
5
+ import type { UnistylesBreakpoints } from './global'
6
+ import { unistyles } from './core'
4
7
  import type {
5
- CreateStylesFactory,
6
- CustomNamedStyles,
7
8
  ExtractVariantNames,
8
9
  ReactNativeStyleSheet,
10
+ StyleSheetWithSuperPowers,
9
11
  UnistylesTheme
10
12
  } from './types'
11
- import { useUnistyles } from './hooks'
12
- import type { UnistylesBreakpoints } from './global'
13
- import { unistyles } from './core'
14
13
 
15
- type ParsedStylesheet<ST extends CustomNamedStyles<ST>> = {
14
+ type ParsedStylesheet<ST extends StyleSheetWithSuperPowers> = {
16
15
  theme: UnistylesTheme,
17
16
  breakpoint: keyof UnistylesBreakpoints,
18
17
  styles: ReactNativeStyleSheet<ST>
19
18
  }
20
19
 
21
- export const useStyles = <ST extends CustomNamedStyles<ST>>(
22
- stylesheet?: ST | CreateStylesFactory<ST, UnistylesTheme>,
23
- variant?: ExtractVariantNames<typeof stylesheet> & string
20
+ export const useStyles = <ST extends StyleSheetWithSuperPowers>(
21
+ stylesheet?: ST,
22
+ variantsMap?: ExtractVariantNames<typeof stylesheet>
24
23
  ): ParsedStylesheet<ST> => {
25
24
  const { theme, layout, plugins } = useUnistyles()
26
25
 
@@ -32,6 +31,7 @@ export const useStyles = <ST extends CustomNamedStyles<ST>>(
32
31
  }
33
32
  }
34
33
 
34
+ const variants = useVariants(variantsMap)
35
35
  const parsedStyles = useMemo(() => typeof stylesheet === 'function'
36
36
  ? stylesheet(theme)
37
37
  : stylesheet, [theme, stylesheet, layout])
@@ -42,22 +42,22 @@ export const useStyles = <ST extends CustomNamedStyles<ST>>(
42
42
  if (typeof value === 'function') {
43
43
  return {
44
44
  ...acc,
45
- [key]: proxifyFunction(key, value,unistyles.registry.plugins, unistyles.runtime, variant)
45
+ [key]: proxifyFunction(key, value,unistyles.registry.plugins, unistyles.runtime, variants)
46
46
  }
47
47
  }
48
48
 
49
49
  return StyleSheet.create({
50
50
  ...acc,
51
- [key]: parseStyle<ST>(
51
+ [key]: parseStyle(
52
52
  key,
53
- value as CustomNamedStyles<ST>,
53
+ value,
54
54
  unistyles.registry.plugins,
55
55
  unistyles.runtime,
56
- variant
56
+ variants
57
57
  )
58
58
  })
59
- }, {} as ST),
60
- [layout, parsedStyles, variant, plugins]
59
+ }, {}),
60
+ [layout, parsedStyles, variants, plugins]
61
61
  ) as ReactNativeStyleSheet<ST>
62
62
 
63
63
  return {
@@ -1,4 +1,4 @@
1
- export { mq, MQSymbol } from './mq'
1
+ 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'
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 || prop === 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,8 +1,8 @@
1
- import type { CustomNamedStyles, NestedStyle, RNStyle, UnistylesPlugin } from '../types'
1
+ import type { NestedStyle, UnistylesPlugin, StyleSheet, Optional } from '../types'
2
2
  import { getValueForBreakpoint } from './breakpoints'
3
3
  import type { UnistylesRuntime } from '../core'
4
4
  import { isAndroid, isIOS } from '../common'
5
- import { getStyleWithVariant } from './variants'
5
+ import { getStyleWithVariants } from './variants'
6
6
  import { withPlugins } from '../plugins'
7
7
 
8
8
  export const proxifyFunction = (
@@ -10,7 +10,7 @@ export const proxifyFunction = (
10
10
  fn: Function,
11
11
  plugins: Array<UnistylesPlugin>,
12
12
  runtime: UnistylesRuntime,
13
- variant?: string
13
+ variant?: Record<string, Optional<string>>
14
14
  ): Function => new Proxy(fn, {
15
15
  apply: (target, thisArg, argumentsList) =>
16
16
  parseStyle(key, target.apply(thisArg, argumentsList), plugins, runtime, variant)
@@ -24,41 +24,42 @@ export const isPlatformColor = <T extends {}>(value: T): boolean => {
24
24
  return isAndroid && 'resource_paths' in value && typeof value.resource_paths === 'object'
25
25
  }
26
26
 
27
- export const parseStyle = <T extends RNStyle>(
27
+ export const parseStyle = <T extends StyleSheet>(
28
28
  key: string,
29
- style: CustomNamedStyles<T>,
29
+ style: T,
30
30
  plugins: Array<UnistylesPlugin>,
31
31
  runtime: UnistylesRuntime,
32
- variant?: string
32
+ variant?: Record<string, Optional<string>>
33
33
  ): T => {
34
34
  const entries = Object
35
- .entries(getStyleWithVariant(style || {}, variant)) as Array<[keyof T, CustomNamedStyles<T> | NestedStyle]>
35
+ .entries(getStyleWithVariants(style || {}, variant)) as Array<[keyof T, StyleSheet]>
36
36
 
37
37
  const parsedStyles = Object
38
38
  .fromEntries(entries
39
39
  .map(([key, value]) => {
40
- const hasNestedProperties = key === 'shadowOffset' || key === 'textShadowOffset'
40
+ // dynamic functions
41
+ if (typeof value === 'function') {
42
+ return [key, value]
43
+ }
41
44
 
42
- if (hasNestedProperties) {
45
+ // nested objects
46
+ if (key === 'shadowOffset' || key === 'textShadowOffset') {
43
47
  return [
44
48
  key,
45
- parseStyle(key, value as CustomNamedStyles<T>, plugins, runtime, variant)
49
+ parseStyle(key, value, plugins, runtime, variant)
46
50
  ]
47
51
  }
48
52
 
49
- const isTransform = key === 'transform'
50
-
51
- if (isTransform && Array.isArray(value)) {
53
+ // transforms
54
+ if (key === 'transform' && Array.isArray(value)) {
52
55
  return [
53
56
  key,
54
57
  value.map(value => parseStyle(key, value, plugins, runtime, variant))
55
58
  ]
56
59
  }
57
60
 
58
- const isDynamicFunction = typeof value === 'function'
59
- const isValidStyle = typeof value !== 'object' || isPlatformColor(value)
60
-
61
- if (isDynamicFunction || isValidStyle) {
61
+ // values or platform colors
62
+ if (typeof value !== 'object' || isPlatformColor(value)) {
62
63
  return [key, value]
63
64
  }
64
65