react-native-unistyles 1.0.0-beta.2 → 1.0.0-beta.4
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.
- package/README.md +342 -35
- package/lib/commonjs/createUnistyles.js +17 -3
- package/lib/commonjs/createUnistyles.js.map +1 -1
- package/lib/commonjs/hooks/index.js +13 -0
- package/lib/commonjs/hooks/index.js.map +1 -0
- package/lib/commonjs/hooks/useDimensions.js +19 -0
- package/lib/commonjs/hooks/useDimensions.js.map +1 -0
- package/lib/commonjs/hooks/useDimensions.web.js +31 -0
- package/lib/commonjs/hooks/useDimensions.web.js.map +1 -0
- package/lib/commonjs/utils/styles.js +20 -10
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/styles.spec.js +66 -0
- package/lib/commonjs/utils/styles.spec.js.map +1 -1
- package/lib/module/createUnistyles.js +17 -3
- package/lib/module/createUnistyles.js.map +1 -1
- package/lib/module/hooks/index.js +2 -0
- package/lib/module/hooks/index.js.map +1 -0
- package/lib/module/hooks/useDimensions.js +12 -0
- package/lib/module/hooks/useDimensions.js.map +1 -0
- package/lib/module/hooks/useDimensions.web.js +24 -0
- package/lib/module/hooks/useDimensions.web.js.map +1 -0
- package/lib/module/utils/styles.js +20 -10
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/styles.spec.js +66 -0
- package/lib/module/utils/styles.spec.js.map +1 -1
- package/lib/typescript/src/createUnistyles.d.ts +5 -1
- package/lib/typescript/src/createUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/hooks/index.d.ts +2 -0
- package/lib/typescript/src/hooks/index.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useDimensions.d.ts +3 -0
- package/lib/typescript/src/hooks/useDimensions.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useDimensions.web.d.ts +3 -0
- package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +26 -8
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/utils/styles.d.ts +1 -3
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/createUnistyles.ts +20 -4
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useDimensions.ts +11 -0
- package/src/hooks/useDimensions.web.ts +30 -0
- package/src/types.ts +64 -20
- package/src/utils/styles.ts +43 -13
package/src/types.ts
CHANGED
@@ -1,36 +1,80 @@
|
|
1
|
-
import type { ImageStyle, TextStyle,
|
2
|
-
import type {
|
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<
|
23
|
+
export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
|
10
24
|
|
11
25
|
type StyleProperty<T, B extends Record<string, number>> = {
|
12
|
-
[
|
13
|
-
[innerKey in keyof B]?: T[
|
26
|
+
[K in keyof T]: {
|
27
|
+
[innerKey in keyof B]?: T[K]
|
14
28
|
} | {
|
15
|
-
[innerKey in
|
16
|
-
} | T[
|
29
|
+
[innerKey in string]?: T[K]
|
30
|
+
} | T[K]
|
17
31
|
}
|
18
32
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
package/src/utils/styles.ts
CHANGED
@@ -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
|
-
) =>
|
65
|
-
.
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
70
|
+
return Object
|
71
|
+
.fromEntries(entries
|
72
|
+
.map(([key, value]) => {
|
73
|
+
const isNestedStyle = key === 'shadowOffset'
|
74
74
|
|
75
|
-
|
75
|
+
if (isNestedStyle) {
|
76
|
+
return [
|
77
|
+
key,
|
78
|
+
parseStyle(value as CustomNamedStyles<T, B>, breakpoint, screenSize, breakpoints)
|
79
|
+
]
|
80
|
+
}
|
76
81
|
|
77
|
-
|
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
|
+
}
|