react-native-unistyles 1.0.0-beta.2 → 1.0.0-beta.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. package/README.md +342 -35
  2. package/lib/commonjs/createUnistyles.js +17 -3
  3. package/lib/commonjs/createUnistyles.js.map +1 -1
  4. package/lib/commonjs/hooks/index.js +13 -0
  5. package/lib/commonjs/hooks/index.js.map +1 -0
  6. package/lib/commonjs/hooks/useDimensions.js +19 -0
  7. package/lib/commonjs/hooks/useDimensions.js.map +1 -0
  8. package/lib/commonjs/hooks/useDimensions.web.js +31 -0
  9. package/lib/commonjs/hooks/useDimensions.web.js.map +1 -0
  10. package/lib/commonjs/utils/styles.js +20 -10
  11. package/lib/commonjs/utils/styles.js.map +1 -1
  12. package/lib/commonjs/utils/styles.spec.js +66 -0
  13. package/lib/commonjs/utils/styles.spec.js.map +1 -1
  14. package/lib/module/createUnistyles.js +17 -3
  15. package/lib/module/createUnistyles.js.map +1 -1
  16. package/lib/module/hooks/index.js +2 -0
  17. package/lib/module/hooks/index.js.map +1 -0
  18. package/lib/module/hooks/useDimensions.js +12 -0
  19. package/lib/module/hooks/useDimensions.js.map +1 -0
  20. package/lib/module/hooks/useDimensions.web.js +24 -0
  21. package/lib/module/hooks/useDimensions.web.js.map +1 -0
  22. package/lib/module/utils/styles.js +20 -10
  23. package/lib/module/utils/styles.js.map +1 -1
  24. package/lib/module/utils/styles.spec.js +66 -0
  25. package/lib/module/utils/styles.spec.js.map +1 -1
  26. package/lib/typescript/src/createUnistyles.d.ts +5 -1
  27. package/lib/typescript/src/createUnistyles.d.ts.map +1 -1
  28. package/lib/typescript/src/hooks/index.d.ts +2 -0
  29. package/lib/typescript/src/hooks/index.d.ts.map +1 -0
  30. package/lib/typescript/src/hooks/useDimensions.d.ts +3 -0
  31. package/lib/typescript/src/hooks/useDimensions.d.ts.map +1 -0
  32. package/lib/typescript/src/hooks/useDimensions.web.d.ts +3 -0
  33. package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +1 -0
  34. package/lib/typescript/src/types.d.ts +26 -8
  35. package/lib/typescript/src/types.d.ts.map +1 -1
  36. package/lib/typescript/src/utils/styles.d.ts +1 -3
  37. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  38. package/package.json +1 -1
  39. package/src/createUnistyles.ts +20 -4
  40. package/src/hooks/index.ts +1 -0
  41. package/src/hooks/useDimensions.ts +11 -0
  42. package/src/hooks/useDimensions.web.ts +30 -0
  43. package/src/types.ts +64 -20
  44. package/src/utils/styles.ts +43 -13
package/src/types.ts CHANGED
@@ -1,36 +1,80 @@
1
- import type { ImageStyle, TextStyle, TransformsStyle, ViewStyle } from 'react-native'
2
- import type { CSSProperties } from 'react'
1
+ import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
2
+ import type {
3
+ MatrixTransform,
4
+ PerpectiveTransform,
5
+ RotateTransform,
6
+ RotateXTransform,
7
+ RotateYTransform,
8
+ RotateZTransform,
9
+ ScaleTransform,
10
+ ScaleXTransform,
11
+ ScaleYTransform,
12
+ SkewXTransform,
13
+ SkewYTransform,
14
+ TranslateXTransform,
15
+ TranslateYTransform
16
+ } from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
3
17
 
4
18
  export type ScreenSize = {
5
19
  width: number,
6
20
  height: number
7
21
  }
8
22
 
9
- export type CreateStylesFactory<T, Theme> = (theme: Theme) => T
23
+ export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
10
24
 
11
25
  type StyleProperty<T, B extends Record<string, number>> = {
12
- [key in keyof T]?: {
13
- [innerKey in keyof B]?: T[key]
26
+ [K in keyof T]: {
27
+ [innerKey in keyof B]?: T[K]
14
28
  } | {
15
- [innerKey in `:w${string}` | `:h${string}`]?: T[key]
16
- } | T[key]
29
+ [innerKey in string]?: T[K]
30
+ } | T[K]
17
31
  }
18
32
 
19
- export type CustomNamedStyles<T, B extends Record<string, number>> = {
20
- [P in keyof T]:
21
- | ViewStyle
22
- | TextStyle
23
- | ImageStyle
24
- | TransformsStyle
25
- | CSSProperties
26
- | StyleProperty<ViewStyle, B>
27
- | StyleProperty<ImageStyle, B>
28
- | StyleProperty<TextStyle, B>
29
- | (
30
- (...args: Array<never>) => ViewStyle | TextStyle | ImageStyle | TransformsStyle | CSSProperties | StyleProperty<ViewStyle, B> | StyleProperty<ImageStyle, B> | StyleProperty<TextStyle, B>
31
- )
33
+ type ShadowOffsetProps<B extends Record<string, number>> = {
34
+ shadowOffset: {
35
+ width: number | {
36
+ [innerKey in keyof B]?: number
37
+ },
38
+ height: number | {
39
+ [innerKey in keyof B]?: number
40
+ }
41
+ }
42
+ }
43
+
44
+ type TransformStyles<B extends Record<string, number>> =
45
+ PerpectiveTransform | StyleProperty<PerpectiveTransform, B>
46
+ | RotateTransform | StyleProperty<RotateTransform, B>
47
+ | RotateXTransform | StyleProperty<RotateXTransform, B>
48
+ | RotateYTransform | StyleProperty<RotateYTransform, B>
49
+ | RotateZTransform | StyleProperty<RotateZTransform, B>
50
+ | ScaleTransform | StyleProperty<ScaleTransform, B>
51
+ | ScaleXTransform | StyleProperty<ScaleXTransform, B>
52
+ | ScaleYTransform | StyleProperty<ScaleYTransform, B>
53
+ | TranslateXTransform | StyleProperty<TranslateXTransform, B>
54
+ | TranslateYTransform | StyleProperty<TranslateYTransform, B>
55
+ | SkewXTransform | StyleProperty<SkewXTransform, B>
56
+ | SkewYTransform | StyleProperty<SkewYTransform, B>
57
+ | MatrixTransform | StyleProperty<MatrixTransform, B>
58
+
59
+ type TransformProps<B extends Record<string, number>> = {
60
+ transform: Array<TransformStyles<B>>
32
61
  }
33
62
 
63
+ type UnistyleView = Omit<Omit<ViewStyle, 'shadowOffset'>, 'transform'>
64
+ type UnistyleText = Omit<Omit<TextStyle, 'shadowOffset'>, 'transform'>
65
+ type UnistyleImage = Omit<Omit<ImageStyle, 'shadowOffset'>, 'transform'>
66
+
67
+ export type StaticStyles<B extends Record<string, number>> =
68
+ | (UnistyleView | StyleProperty<UnistyleView, B>)
69
+ | (UnistyleText | StyleProperty<UnistyleText, B>)
70
+ | (UnistyleImage | StyleProperty<UnistyleImage, B>)
71
+ & TransformProps<B> & ShadowOffsetProps<B>
72
+
73
+ export type CustomNamedStyles<T, B extends Record<string, number>> = {
74
+ [K in keyof T]: T[K] extends (...args: infer A) => unknown
75
+ ? (...args: A) => StaticStyles<B>
76
+ : StaticStyles<B>
77
+ }
34
78
  export type ExtractBreakpoints<T, B extends Record<string, number>> = T extends Partial<Record<keyof B & string, infer V>>
35
79
  ? V
36
80
  : T extends (...args: infer A) => infer R
@@ -61,19 +61,49 @@ export const parseStyle = <T, B extends Record<string, number>>(
61
61
  breakpoint: keyof B & string,
62
62
  screenSize: ScreenSize,
63
63
  breakpoints: B
64
- ) => Object
65
- .fromEntries(Object
66
- .entries(style)
67
- .map(([key, value]) => {
68
- const isDynamicFunction = typeof value === 'function'
69
- const isValidStyle = typeof value !== 'object' || key === 'transform'
64
+ ): T => {
65
+ const entries = Object.entries(style) as [[
66
+ keyof T,
67
+ CustomNamedStyles<T, B> | Record<keyof B & string, string | number | undefined>]
68
+ ]
70
69
 
71
- if (isDynamicFunction || isValidStyle) {
72
- return [key, value]
73
- }
70
+ return Object
71
+ .fromEntries(entries
72
+ .map(([key, value]) => {
73
+ const isNestedStyle = key === 'shadowOffset'
74
74
 
75
- const valueWithBreakpoint = value as Record<keyof B & string, string | number>
75
+ if (isNestedStyle) {
76
+ return [
77
+ key,
78
+ parseStyle(value as CustomNamedStyles<T, B>, breakpoint, screenSize, breakpoints)
79
+ ]
80
+ }
76
81
 
77
- return [key, getValueForBreakpoint<B>(valueWithBreakpoint, breakpoint, screenSize, breakpoints)]
78
- })
79
- )
82
+ const isTransform = key === 'transform'
83
+
84
+ if (isTransform && Array.isArray(value)) {
85
+ return [
86
+ key,
87
+ value.map(value => parseStyle(value, breakpoint, screenSize, breakpoints))
88
+ ]
89
+ }
90
+
91
+ const isDynamicFunction = typeof value === 'function'
92
+ const isValidStyle = typeof value !== 'object'
93
+
94
+ if (isDynamicFunction || isValidStyle) {
95
+ return [key, value]
96
+ }
97
+
98
+ return [
99
+ key,
100
+ getValueForBreakpoint<B>(
101
+ value as Record<keyof B & string, string | number | undefined>,
102
+ breakpoint,
103
+ screenSize,
104
+ breakpoints
105
+ )
106
+ ]
107
+ })
108
+ )
109
+ }