react-native-unistyles 1.2.0 → 2.0.0-alpha.10
Sign up to get free protection for your applications and to get access to all the features.
- package/cxx/UnistylesRuntime.cpp +255 -0
- package/cxx/UnistylesRuntime.h +60 -0
- package/ios/UnistylesHelpers.h +4 -0
- package/ios/UnistylesHelpers.mm +20 -0
- package/ios/UnistylesModule.h +10 -0
- package/ios/UnistylesModule.mm +158 -0
- package/lib/commonjs/common.js +26 -0
- package/lib/commonjs/common.js.map +1 -0
- package/lib/commonjs/core/UnistyleRegistry.js +38 -0
- package/lib/commonjs/core/UnistyleRegistry.js.map +1 -0
- package/lib/commonjs/core/Unistyles.js +31 -0
- package/lib/commonjs/core/Unistyles.js.map +1 -0
- package/lib/commonjs/core/UnistylesModule.js +9 -0
- package/lib/commonjs/core/UnistylesModule.js.map +1 -0
- package/lib/commonjs/core/UnistylesRuntime.js +66 -0
- package/lib/commonjs/core/UnistylesRuntime.js.map +1 -0
- package/lib/commonjs/core/index.js +13 -0
- package/lib/commonjs/core/index.js.map +1 -0
- package/lib/commonjs/createStyleSheet.js +14 -0
- package/lib/commonjs/createStyleSheet.js.map +1 -0
- package/lib/commonjs/global.js +2 -0
- package/lib/commonjs/global.js.map +1 -0
- package/lib/commonjs/hooks/index.js +10 -3
- package/lib/commonjs/hooks/index.js.map +1 -1
- package/lib/commonjs/hooks/useInitialTheme.js +17 -0
- package/lib/commonjs/hooks/useInitialTheme.js.map +1 -0
- package/lib/commonjs/hooks/useUnistyles.js +54 -0
- package/lib/commonjs/hooks/useUnistyles.js.map +1 -0
- package/lib/commonjs/index.js +35 -6
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/types/common.js +2 -0
- package/lib/commonjs/types/{mediaQueries.js.map → common.js.map} +1 -1
- package/lib/commonjs/types/index.js +11 -0
- package/lib/commonjs/types/index.js.map +1 -1
- package/lib/commonjs/types/mq.js +6 -0
- package/lib/{module/types/mediaQueries.js.map → commonjs/types/mq.js.map} +1 -1
- package/lib/commonjs/types/unistyles.js +6 -0
- package/lib/commonjs/types/unistyles.js.map +1 -0
- package/lib/commonjs/useStyles.js +49 -0
- package/lib/commonjs/useStyles.js.map +1 -0
- package/lib/commonjs/utils/breakpoints.js +44 -82
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/common.js +6 -1
- package/lib/commonjs/utils/common.js.map +1 -1
- package/lib/commonjs/utils/index.js +25 -30
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/mq.js +89 -0
- package/lib/commonjs/utils/mq.js.map +1 -0
- package/lib/commonjs/utils/mqParser.js +86 -0
- package/lib/commonjs/utils/mqParser.js.map +1 -0
- package/lib/commonjs/utils/styles.js +6 -53
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/variants.js +20 -0
- package/lib/commonjs/utils/variants.js.map +1 -0
- package/lib/module/common.js +22 -0
- package/lib/module/common.js.map +1 -0
- package/lib/module/core/UnistyleRegistry.js +31 -0
- package/lib/module/core/UnistyleRegistry.js.map +1 -0
- package/lib/module/core/Unistyles.js +25 -0
- package/lib/module/core/Unistyles.js.map +1 -0
- package/lib/module/core/UnistylesModule.js +3 -0
- package/lib/module/core/UnistylesModule.js.map +1 -0
- package/lib/module/core/UnistylesRuntime.js +59 -0
- package/lib/module/core/UnistylesRuntime.js.map +1 -0
- package/lib/module/core/index.js +2 -0
- package/lib/module/core/index.js.map +1 -0
- package/lib/module/createStyleSheet.js +7 -0
- package/lib/module/createStyleSheet.js.map +1 -0
- package/lib/module/global.js +2 -0
- package/lib/module/global.js.map +1 -0
- package/lib/module/hooks/index.js +2 -1
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/useInitialTheme.js +10 -0
- package/lib/module/hooks/useInitialTheme.js.map +1 -0
- package/lib/module/hooks/useUnistyles.js +47 -0
- package/lib/module/hooks/useUnistyles.js.map +1 -0
- package/lib/module/index.js +13 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/types/common.js +2 -0
- package/lib/module/types/common.js.map +1 -0
- package/lib/module/types/index.js +1 -0
- package/lib/module/types/index.js.map +1 -1
- package/lib/module/types/mq.js +2 -0
- package/lib/module/types/mq.js.map +1 -0
- package/lib/module/types/unistyles.js +2 -0
- package/lib/module/types/unistyles.js.map +1 -0
- package/lib/module/useStyles.js +42 -0
- package/lib/module/useStyles.js.map +1 -0
- package/lib/module/utils/breakpoints.js +43 -82
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/common.js +5 -0
- package/lib/module/utils/common.js.map +1 -1
- package/lib/module/utils/index.js +5 -3
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/mq.js +83 -0
- package/lib/module/utils/mq.js.map +1 -0
- package/lib/module/utils/mqParser.js +79 -0
- package/lib/module/utils/mqParser.js.map +1 -0
- package/lib/module/utils/styles.js +7 -55
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/variants.js +13 -0
- package/lib/module/utils/variants.js.map +1 -0
- package/lib/typescript/src/common.d.ts +17 -0
- package/lib/typescript/src/common.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistyleRegistry.d.ts +15 -0
- package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -0
- package/lib/typescript/src/core/Unistyles.d.ts +13 -0
- package/lib/typescript/src/core/Unistyles.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistylesModule.d.ts +6 -0
- package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts +24 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -0
- package/lib/typescript/src/core/index.d.ts +2 -0
- package/lib/typescript/src/core/index.d.ts.map +1 -0
- package/lib/typescript/src/createStyleSheet.d.ts +3 -0
- package/lib/typescript/src/createStyleSheet.d.ts.map +1 -0
- package/lib/typescript/src/global.d.ts +7 -0
- package/lib/typescript/src/global.d.ts.map +1 -0
- package/lib/typescript/src/hooks/index.d.ts +2 -1
- package/lib/typescript/src/hooks/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useInitialTheme.d.ts +3 -0
- package/lib/typescript/src/hooks/useInitialTheme.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useUnistyles.d.ts +12 -0
- package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +14 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/types/breakpoints.d.ts +12 -13
- package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/types/common.d.ts +3 -0
- package/lib/typescript/src/types/common.d.ts.map +1 -0
- package/lib/typescript/src/types/core.d.ts +34 -18
- package/lib/typescript/src/types/core.d.ts.map +1 -1
- package/lib/typescript/src/types/index.d.ts +5 -2
- package/lib/typescript/src/types/index.d.ts.map +1 -1
- package/lib/typescript/src/types/mq.d.ts +3 -0
- package/lib/typescript/src/types/mq.d.ts.map +1 -0
- package/lib/typescript/src/types/unistyles.d.ts +37 -0
- package/lib/typescript/src/types/unistyles.d.ts.map +1 -0
- package/lib/typescript/src/useStyles.d.ts +10 -0
- package/lib/typescript/src/useStyles.d.ts.map +1 -0
- package/lib/typescript/src/utils/breakpoints.d.ts +5 -62
- package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/utils/common.d.ts +5 -0
- package/lib/typescript/src/utils/common.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +5 -3
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/mq.d.ts +21 -0
- package/lib/typescript/src/utils/mq.d.ts.map +1 -0
- package/lib/typescript/src/utils/mqParser.d.ts +3 -0
- package/lib/typescript/src/utils/mqParser.d.ts.map +1 -0
- package/lib/typescript/src/utils/styles.d.ts +4 -49
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/lib/typescript/src/utils/variants.d.ts +3 -0
- package/lib/typescript/src/utils/variants.d.ts.map +1 -0
- package/package.json +30 -19
- package/react-native-unistyles.podspec +22 -0
- package/src/common.ts +19 -0
- package/src/core/UnistyleRegistry.ts +41 -0
- package/src/core/Unistyles.ts +34 -0
- package/src/core/UnistylesModule.ts +7 -0
- package/src/core/UnistylesRuntime.ts +73 -0
- package/src/core/index.ts +1 -0
- package/src/createStyleSheet.ts +9 -0
- package/src/global.ts +5 -0
- package/src/hooks/index.ts +2 -1
- package/src/hooks/useInitialTheme.ts +11 -0
- package/src/hooks/useUnistyles.ts +55 -0
- package/src/index.ts +30 -2
- package/src/types/breakpoints.ts +16 -19
- package/src/types/common.ts +2 -0
- package/src/types/core.ts +42 -24
- package/src/types/index.ts +5 -9
- package/src/types/mq.ts +3 -0
- package/src/types/unistyles.ts +45 -0
- package/src/useStyles.ts +56 -0
- package/src/utils/breakpoints.ts +57 -88
- package/src/utils/common.ts +6 -0
- package/src/utils/index.ts +5 -10
- package/src/utils/mq.ts +106 -0
- package/src/utils/mqParser.ts +99 -0
- package/src/utils/styles.ts +17 -64
- package/src/utils/variants.ts +17 -0
- package/lib/commonjs/UnistylesTheme.js +0 -21
- package/lib/commonjs/UnistylesTheme.js.map +0 -1
- package/lib/commonjs/createUnistyles.js +0 -57
- package/lib/commonjs/createUnistyles.js.map +0 -1
- package/lib/commonjs/hooks/useDimensions.js +0 -10
- package/lib/commonjs/hooks/useDimensions.js.map +0 -1
- package/lib/commonjs/hooks/useDimensions.web.js +0 -34
- package/lib/commonjs/hooks/useDimensions.web.js.map +0 -1
- package/lib/commonjs/types/mediaQueries.js +0 -2
- package/lib/commonjs/utils/mediaQueries.js +0 -189
- package/lib/commonjs/utils/mediaQueries.js.map +0 -1
- package/lib/module/UnistylesTheme.js +0 -12
- package/lib/module/UnistylesTheme.js.map +0 -1
- package/lib/module/createUnistyles.js +0 -50
- package/lib/module/createUnistyles.js.map +0 -1
- package/lib/module/hooks/useDimensions.js +0 -3
- package/lib/module/hooks/useDimensions.js.map +0 -1
- package/lib/module/hooks/useDimensions.web.js +0 -27
- package/lib/module/hooks/useDimensions.web.js.map +0 -1
- package/lib/module/types/mediaQueries.js +0 -2
- package/lib/module/utils/mediaQueries.js +0 -176
- package/lib/module/utils/mediaQueries.js.map +0 -1
- package/lib/typescript/examples/expo/src/App.d.ts +0 -3
- package/lib/typescript/examples/expo/src/App.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/Extreme.d.ts +0 -7
- package/lib/typescript/examples/expo/src/examples/Extreme.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/Memoization.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/Memoization.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/Minimal.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/Minimal.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/PlatformColors.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/PlatformColors.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/Theme.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/Theme.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/index.d.ts +0 -9
- package/lib/typescript/examples/expo/src/examples/index.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/index.d.ts +0 -2
- package/lib/typescript/examples/expo/src/index.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts +0 -8
- package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/styles/index.d.ts +0 -46
- package/lib/typescript/examples/expo/src/styles/index.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/styles/theme.d.ts +0 -24
- package/lib/typescript/examples/expo/src/styles/theme.d.ts.map +0 -1
- package/lib/typescript/src/UnistylesTheme.d.ts +0 -9
- package/lib/typescript/src/UnistylesTheme.d.ts.map +0 -1
- package/lib/typescript/src/createUnistyles.d.ts +0 -10
- package/lib/typescript/src/createUnistyles.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useDimensions.d.ts +0 -3
- package/lib/typescript/src/hooks/useDimensions.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useDimensions.web.d.ts +0 -3
- package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +0 -1
- package/lib/typescript/src/types/mediaQueries.d.ts +0 -8
- package/lib/typescript/src/types/mediaQueries.d.ts.map +0 -1
- package/lib/typescript/src/utils/mediaQueries.d.ts +0 -130
- package/lib/typescript/src/utils/mediaQueries.d.ts.map +0 -1
- package/src/UnistylesTheme.tsx +0 -17
- package/src/__tests__/createUnistyles.spec.tsx +0 -192
- package/src/createUnistyles.ts +0 -70
- package/src/hooks/useDimensions.ts +0 -4
- package/src/hooks/useDimensions.web.ts +0 -34
- package/src/types/mediaQueries.ts +0 -10
- package/src/utils/mediaQueries.ts +0 -201
package/src/types/breakpoints.ts
CHANGED
@@ -1,36 +1,33 @@
|
|
1
1
|
import type { OpaqueColorValue } from 'react-native'
|
2
|
-
import type {
|
3
|
-
|
4
|
-
|
5
|
-
export type SortedBreakpointEntries<B extends Breakpoints> = [[keyof B & string, number]]
|
6
|
-
|
7
|
-
export type ScreenSize = {
|
8
|
-
width: number,
|
9
|
-
height: number
|
10
|
-
}
|
11
|
-
|
12
|
-
export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
|
2
|
+
import type { UnistylesBreakpoints } from '../global'
|
3
|
+
import type { MediaQuery } from './mq'
|
4
|
+
import type { VariantValue } from './core'
|
13
5
|
|
14
6
|
type WithEmptyObject<V> = keyof V extends never ? {} : V
|
15
7
|
|
16
|
-
|
8
|
+
type ExtractBreakpoints<T> = T extends Partial<Record<keyof UnistylesBreakpoints & string, infer V>>
|
17
9
|
? WithEmptyObject<V>
|
18
10
|
: T extends (...args: infer A) => infer R
|
19
|
-
? (...args: A) => ExtractBreakpoints<R
|
11
|
+
? (...args: A) => ExtractBreakpoints<R>
|
20
12
|
: {
|
21
13
|
[K in keyof T]: T[K] extends (...args: infer A) => infer R
|
22
|
-
? (...args: A) => ExtractBreakpoints<R
|
14
|
+
? (...args: A) => ExtractBreakpoints<R>
|
23
15
|
: T[K] extends object
|
24
|
-
? ExtractBreakpoints<T[K]
|
16
|
+
? ExtractBreakpoints<T[K]>
|
25
17
|
: T[K]
|
26
18
|
}
|
27
19
|
|
28
|
-
|
29
|
-
|
20
|
+
type FlattenObjectTypes<T> = T[keyof T]
|
21
|
+
type RemoveKeysWithPrefix<T> = T extends (...args: Array<any>) => infer R
|
22
|
+
? (...args: Parameters<T>) => RemoveKeysWithPrefix<R>
|
30
23
|
: T extends object
|
31
24
|
? T extends OpaqueColorValue
|
32
25
|
? string
|
33
26
|
: T extends Record<string, infer _V>
|
34
|
-
?
|
35
|
-
|
27
|
+
? T extends { variants: VariantValue }
|
28
|
+
? RemoveKeysWithPrefix<FlattenObjectTypes<T['variants']>>
|
29
|
+
: { [K in keyof T as K extends MediaQuery ? keyof UnistylesBreakpoints & string : K]: RemoveKeysWithPrefix<T[K]> }
|
30
|
+
: { [K in keyof T]: RemoveKeysWithPrefix<T[K]> }
|
36
31
|
: T
|
32
|
+
|
33
|
+
export type ReactNativeStyleSheet<T> = ExtractBreakpoints<RemoveKeysWithPrefix<T>>
|
package/src/types/core.ts
CHANGED
@@ -14,14 +14,19 @@ import type {
|
|
14
14
|
TranslateYTransform
|
15
15
|
} from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
|
16
16
|
import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
|
17
|
-
import type {
|
18
|
-
import type {
|
17
|
+
import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
|
18
|
+
import type { MediaQuery } from './mq'
|
19
19
|
|
20
20
|
type ShadowOffset = {
|
21
21
|
width: number,
|
22
22
|
height: number
|
23
23
|
}
|
24
24
|
|
25
|
+
export type ScreenSize = {
|
26
|
+
width: number,
|
27
|
+
height: number
|
28
|
+
}
|
29
|
+
|
25
30
|
type TransformStyles =
|
26
31
|
& PerpectiveTransform
|
27
32
|
& RotateTransform
|
@@ -37,37 +42,50 @@ type TransformStyles =
|
|
37
42
|
& SkewYTransform
|
38
43
|
& MatrixTransform
|
39
44
|
|
40
|
-
type UnistyleNested
|
41
|
-
shadowOffset?: DeepUniStyle<ShadowOffset
|
42
|
-
textShadowOffset?: DeepUniStyle<ShadowOffset
|
43
|
-
transform?: Array<DeepUniStyle<TransformStyles
|
45
|
+
type UnistyleNested = {
|
46
|
+
shadowOffset?: DeepUniStyle<ShadowOffset>,
|
47
|
+
textShadowOffset?: DeepUniStyle<ShadowOffset>,
|
48
|
+
transform?: Array<DeepUniStyle<TransformStyles>>
|
44
49
|
}
|
45
50
|
|
46
|
-
type UniStyle<V
|
47
|
-
[innerKey in keyof
|
51
|
+
type UniStyle<V> = {
|
52
|
+
[innerKey in keyof UnistylesBreakpoints]?: V
|
48
53
|
} | {
|
49
|
-
[innerKey in
|
50
|
-
} |
|
54
|
+
[innerKey in MediaQuery]: V
|
55
|
+
} | {
|
56
|
+
[innerKey in 'variants']?: {
|
57
|
+
[variant: string]: V
|
58
|
+
}
|
59
|
+
}
|
51
60
|
|
52
|
-
type DeepUniStyle<T
|
53
|
-
[K in keyof T]?: UniStyle<T[K]
|
61
|
+
type DeepUniStyle<T> = {
|
62
|
+
[K in keyof T]?: UniStyle<T[K]> | T[K]
|
54
63
|
}
|
55
64
|
|
56
65
|
// these props are treated differently to nest breakpoints and media queries
|
57
66
|
type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
|
58
67
|
|
59
|
-
type UnistyleView
|
60
|
-
type UnistyleText
|
61
|
-
type UnistyleImage
|
68
|
+
type UnistyleView = DeepUniStyle<Omit<ViewStyle, NestedTypes>>
|
69
|
+
type UnistyleText = DeepUniStyle<Omit<TextStyle, NestedTypes>>
|
70
|
+
type UnistyleImage = DeepUniStyle<Omit<ImageStyle, NestedTypes>>
|
62
71
|
|
63
|
-
export type StaticStyles
|
64
|
-
| UnistyleView
|
65
|
-
| UnistyleText
|
66
|
-
| UnistyleImage
|
67
|
-
& UnistyleNested
|
72
|
+
export type StaticStyles =
|
73
|
+
| UnistyleView
|
74
|
+
| UnistyleText
|
75
|
+
| UnistyleImage
|
76
|
+
& UnistyleNested
|
77
|
+
|
78
|
+
export type CustomNamedStyles<T> = {
|
79
|
+
[K in keyof T]: T[K] extends (...args: infer A) => StaticStyles
|
80
|
+
? (...args: A) => StaticStyles
|
81
|
+
: StaticStyles
|
82
|
+
}
|
68
83
|
|
69
|
-
export type
|
70
|
-
|
71
|
-
|
72
|
-
|
84
|
+
export type RNValue = number | string | undefined
|
85
|
+
export type VariantValue = Record<string, RNValue>
|
86
|
+
export type NestedStyle = Record<keyof UnistylesBreakpoints | MediaQuery | 'variants', RNValue> & {
|
87
|
+
variants: VariantValue
|
73
88
|
}
|
89
|
+
export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | MediaQuery | 'variants', RNValue | VariantValue]>
|
90
|
+
export type UnistylesTheme = UnistylesThemes[keyof UnistylesThemes]
|
91
|
+
export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
|
package/src/types/index.ts
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
export * from './normalizer'
|
2
|
-
export
|
3
|
-
export type {
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
ExtractBreakpoints,
|
8
|
-
RemoveKeysWithPrefix,
|
9
|
-
SortedBreakpointEntries
|
10
|
-
} from './breakpoints'
|
2
|
+
export * from './unistyles'
|
3
|
+
export type { Optional, Nullable } from './common'
|
4
|
+
export type { MediaQuery } from './mq'
|
5
|
+
export type { CustomNamedStyles, NestedStylePairs, UnistylesTheme, CreateStylesFactory, ScreenSize, NestedStyle, RNValue } from './core'
|
6
|
+
export type { ReactNativeStyleSheet } from './breakpoints'
|
package/src/types/mq.ts
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
import { CxxUnistylesEventTypes, ScreenOrientation } from '../common'
|
2
|
+
import type { UnistylesThemes, UnistylesBreakpoints } from '../global'
|
3
|
+
import type { ScreenSize } from './core'
|
4
|
+
import type { Optional } from './common'
|
5
|
+
|
6
|
+
export type ColorSchemeName = Optional<'light' | 'dark'>
|
7
|
+
|
8
|
+
export type UnistylesConfig = {
|
9
|
+
adaptiveThemes?: boolean
|
10
|
+
}
|
11
|
+
|
12
|
+
export type UnistylesBridge = {
|
13
|
+
// getters
|
14
|
+
screenWidth: number,
|
15
|
+
screenHeight: number,
|
16
|
+
hasAdaptiveThemes: boolean,
|
17
|
+
themeName: keyof UnistylesThemes,
|
18
|
+
breakpoint: keyof UnistylesBreakpoints,
|
19
|
+
colorScheme: ColorSchemeName,
|
20
|
+
sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>,
|
21
|
+
|
22
|
+
// setters
|
23
|
+
themes: Array<keyof UnistylesThemes>,
|
24
|
+
useBreakpoints(breakpoints: UnistylesBreakpoints): void,
|
25
|
+
useTheme(name: keyof UnistylesThemes): void,
|
26
|
+
useAdaptiveThemes(enable: boolean): void
|
27
|
+
}
|
28
|
+
|
29
|
+
export type UnistylesThemeEvent = {
|
30
|
+
type: CxxUnistylesEventTypes.Theme,
|
31
|
+
payload: {
|
32
|
+
themeName: keyof UnistylesThemes
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
export type UnistylesMobileLayoutEvent = {
|
37
|
+
type: CxxUnistylesEventTypes.Layout,
|
38
|
+
payload: {
|
39
|
+
screen: ScreenSize,
|
40
|
+
breakpoint: keyof UnistylesBreakpoints,
|
41
|
+
orientation: ScreenOrientation
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
export type UnistylesEvents = UnistylesThemeEvent | UnistylesMobileLayoutEvent
|
package/src/useStyles.ts
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
import { useMemo } from 'react'
|
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'
|
6
|
+
import type { UnistylesBreakpoints } from './global'
|
7
|
+
|
8
|
+
type ParsedStylesheet<ST extends CustomNamedStyles<ST>> = {
|
9
|
+
theme: UnistylesTheme,
|
10
|
+
breakpoint: keyof UnistylesBreakpoints,
|
11
|
+
styles: ReactNativeStyleSheet<ST>
|
12
|
+
}
|
13
|
+
|
14
|
+
export const useStyles = <ST extends CustomNamedStyles<ST>>(
|
15
|
+
stylesheet?: ST | CreateStylesFactory<ST, UnistylesTheme>,
|
16
|
+
variant?: string
|
17
|
+
): ParsedStylesheet<ST> => {
|
18
|
+
const { theme, layout } = useUnistyles()
|
19
|
+
const { screenSize, breakpoint } = layout
|
20
|
+
|
21
|
+
if (!stylesheet) {
|
22
|
+
return {
|
23
|
+
theme,
|
24
|
+
breakpoint,
|
25
|
+
styles: {} as ReactNativeStyleSheet<ST>
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
const parsedStyles = useMemo(() => typeof stylesheet === 'function'
|
30
|
+
? stylesheet(theme)
|
31
|
+
: stylesheet, [theme, stylesheet])
|
32
|
+
|
33
|
+
const dynamicStyleSheet = useMemo(() => Object
|
34
|
+
.entries(parsedStyles)
|
35
|
+
.reduce((acc, [key, value]) => {
|
36
|
+
const style = value as CustomNamedStyles<ST>
|
37
|
+
|
38
|
+
if (typeof value === 'function') {
|
39
|
+
return {
|
40
|
+
...acc,
|
41
|
+
[key]: proxifyFunction(value, breakpoint, screenSize, variant)
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
return StyleSheet.create({
|
46
|
+
...acc,
|
47
|
+
[key]: parseStyle<ST>(style, breakpoint, screenSize, variant)
|
48
|
+
})
|
49
|
+
}, {} as ST), [breakpoint, screenSize, parsedStyles, variant]) as ReactNativeStyleSheet<ST>
|
50
|
+
|
51
|
+
return {
|
52
|
+
theme,
|
53
|
+
breakpoint,
|
54
|
+
styles: dynamicStyleSheet
|
55
|
+
}
|
56
|
+
}
|
package/src/utils/breakpoints.ts
CHANGED
@@ -1,37 +1,22 @@
|
|
1
|
-
import {
|
2
|
-
import
|
3
|
-
import {
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
* Additionally, it validates that:
|
11
|
-
* 1. The first breakpoint starts with a value of 0.
|
12
|
-
* 2. No duplicate breakpoint values exist.
|
13
|
-
*
|
14
|
-
* If the validation fails, appropriate error messages are logged to the console.
|
15
|
-
*
|
16
|
-
* @template B - An object type where keys are strings and values are numbers.
|
17
|
-
* @param {B} breakpoints - The breakpoints object to be sorted and validated.
|
18
|
-
* @returns {B} A new object with sorted and validated breakpoints.
|
19
|
-
*
|
20
|
-
* @example
|
21
|
-
* const input = { md: 768, lg: 1024, sm: 0 }
|
22
|
-
* sortAndValidateBreakpoints(input) // returns { sm: 0, md: 768, lg: 1024 }
|
23
|
-
*/
|
24
|
-
export const sortAndValidateBreakpoints = <B extends Breakpoints>(breakpoints: B): B => {
|
1
|
+
import { unistyles } from '../core'
|
2
|
+
import { isMobile, Orientation, throwError } from './common'
|
3
|
+
import type { NestedStyle, NestedStylePairs, RNValue } from '../types'
|
4
|
+
import type { UnistylesBreakpoints } from '../global'
|
5
|
+
import { ScreenOrientation } from '../common'
|
6
|
+
import { getKeyForUnistylesMediaQuery } from './mqParser'
|
7
|
+
import { getKeyForVariant } from './variants'
|
8
|
+
|
9
|
+
export const sortAndValidateBreakpoints = (breakpoints: UnistylesBreakpoints): UnistylesBreakpoints => {
|
25
10
|
const sortedPairs = Object
|
26
11
|
.entries(breakpoints)
|
27
12
|
.sort((breakpoint1, breakpoint2) => {
|
28
13
|
const [, value1] = breakpoint1
|
29
14
|
const [, value2] = breakpoint2
|
30
15
|
|
31
|
-
return value1 - value2
|
16
|
+
return (value1 as number) - (value2 as number)
|
32
17
|
})
|
33
18
|
|
34
|
-
const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs)) as
|
19
|
+
const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs)) as UnistylesBreakpoints
|
35
20
|
const breakpointValues = Object.values(sortedBreakpoints)
|
36
21
|
const [firstBreakpoint] = breakpointValues
|
37
22
|
|
@@ -46,25 +31,10 @@ export const sortAndValidateBreakpoints = <B extends Breakpoints>(breakpoints: B
|
|
46
31
|
return sortedBreakpoints
|
47
32
|
}
|
48
33
|
|
49
|
-
|
50
|
-
* Determines the appropriate breakpoint key for a given screen width based on provided breakpoints.
|
51
|
-
*
|
52
|
-
* This function takes a screen width and an object of breakpoints. It returns the key of the breakpoint
|
53
|
-
* that the screen width falls into. The breakpoints are assumed to be sorted in ascending order.
|
54
|
-
*
|
55
|
-
* @template B - An object type where keys are strings and values are numbers representing screen widths.
|
56
|
-
* @param {number} width - The screen width to determine the breakpoint for.
|
57
|
-
* @param breakpointEntries - sorted pairs of breakpoints
|
58
|
-
* @returns {keyof B & string} The key of the breakpoint that the screen width falls into.
|
59
|
-
*
|
60
|
-
* @example
|
61
|
-
* const breakpoints = { sm: 0, md: 768, lg: 1024 }
|
62
|
-
* getBreakpointFromScreenWidth(800, breakpoints) // returns 'md'
|
63
|
-
*/
|
64
|
-
export const getBreakpointFromScreenWidth = <B extends Breakpoints>(width: number, breakpointEntries: SortedBreakpointEntries<B>): keyof B & string => {
|
34
|
+
export const getBreakpointFromScreenWidth = (width: number, breakpointEntries: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>): keyof UnistylesBreakpoints & string => {
|
65
35
|
const [key] = breakpointEntries
|
66
36
|
.find(([, value], index, otherBreakpoints) => {
|
67
|
-
const minVal = value
|
37
|
+
const minVal = value as number
|
68
38
|
const maxVal = otherBreakpoints[index + 1]?.[1]
|
69
39
|
|
70
40
|
if (!maxVal) {
|
@@ -72,69 +42,68 @@ export const getBreakpointFromScreenWidth = <B extends Breakpoints>(width: numbe
|
|
72
42
|
}
|
73
43
|
|
74
44
|
return width >= minVal && width < maxVal
|
75
|
-
}) as [keyof
|
45
|
+
}) as [keyof UnistylesBreakpoints & string, number]
|
76
46
|
|
77
47
|
return key
|
78
48
|
}
|
79
49
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
* @example
|
97
|
-
*
|
98
|
-
* const values = { ':w[200]': 'value1', sm: 'value2', md: 'value3' }
|
99
|
-
* const screenSize = { width: 250, height: 400 }
|
100
|
-
* const breakpoints = { sm: 300, md: 600, lg: 900 }
|
101
|
-
*
|
102
|
-
* getValueForBreakpoint(values, 'sm', screenSize, breakpoints); // 'value1'
|
103
|
-
*/
|
104
|
-
export const getValueForBreakpoint = <B extends Breakpoints>(
|
105
|
-
value: Record<keyof B & string, string | number | undefined>,
|
106
|
-
breakpoint: keyof B & string,
|
107
|
-
screenSize: ScreenSize,
|
108
|
-
breakpointPairs: SortedBreakpointEntries<B>
|
109
|
-
): string | number | undefined => {
|
110
|
-
// the highest priority is for custom media queries
|
111
|
-
const customMediaQueries = Object
|
112
|
-
.entries(value)
|
113
|
-
.filter(([key]) => isMediaQuery(key))
|
114
|
-
const customMediaQueryKey = getKeyForCustomMediaQuery(customMediaQueries, screenSize)
|
115
|
-
|
116
|
-
if (customMediaQueryKey && customMediaQueryKey in value) {
|
50
|
+
// todo move it to different file
|
51
|
+
export const getValueForNestedStyle = (value: NestedStyle, variant?: string): RNValue => {
|
52
|
+
// the highest priority is for variants
|
53
|
+
const variantKey = getKeyForVariant(value, variant)
|
54
|
+
|
55
|
+
if (variantKey) {
|
56
|
+
return value.variants[variantKey as string]
|
57
|
+
}
|
58
|
+
|
59
|
+
// then custom media queries
|
60
|
+
const customMediaQueryKey = getKeyForUnistylesMediaQuery(
|
61
|
+
Object.entries(value) as NestedStylePairs,
|
62
|
+
unistyles.runtime.screen
|
63
|
+
) as keyof typeof value
|
64
|
+
|
65
|
+
if (customMediaQueryKey) {
|
117
66
|
return value[customMediaQueryKey]
|
118
67
|
}
|
119
68
|
|
120
|
-
//
|
121
|
-
|
122
|
-
const
|
69
|
+
// at this point user didn't use custom media queries (:w, :h)
|
70
|
+
// check if user defined any breakpoints
|
71
|
+
const hasBreakpoints = unistyles.runtime.sortedBreakpoints.length > 0
|
72
|
+
|
73
|
+
// if not then we can fall back to horizontal and portrait (mobile only)
|
74
|
+
if (!hasBreakpoints && isMobile && (Orientation.Landscape in value || Orientation.Portrait in value)) {
|
75
|
+
return value[
|
76
|
+
unistyles.runtime.orientation === ScreenOrientation.Portrait
|
77
|
+
? Orientation.Portrait
|
78
|
+
: Orientation.Landscape
|
79
|
+
]
|
80
|
+
}
|
81
|
+
|
82
|
+
// let's get the current breakpoint
|
83
|
+
const breakpoint = unistyles.runtime.breakpoint
|
84
|
+
|
85
|
+
if (!breakpoint) {
|
86
|
+
return undefined
|
87
|
+
}
|
88
|
+
|
89
|
+
// if user defined breakpoints, then we look for the valid one
|
90
|
+
const directBreakpoint = value[breakpoint]
|
123
91
|
|
124
92
|
// if there is a direct key like 'sm' or 'md', or value for this key exists but its undefined
|
125
|
-
if (directBreakpoint || (
|
93
|
+
if (directBreakpoint || (breakpoint in value)) {
|
126
94
|
return directBreakpoint
|
127
95
|
}
|
128
96
|
|
129
|
-
// there is no direct hit for breakpoint nor media-query,
|
97
|
+
// there is no direct hit for breakpoint nor media-query, let's simulate CSS cascading
|
98
|
+
const breakpointPairs = unistyles.runtime.sortedBreakpoints
|
130
99
|
const currentBreakpoint = breakpointPairs
|
131
|
-
.findIndex(([key]) => key ===
|
100
|
+
.findIndex(([key]) => key === breakpoint)
|
132
101
|
|
133
102
|
const availableBreakpoints = breakpointPairs
|
134
103
|
.filter(([key], index) => index < currentBreakpoint && key && key in value)
|
135
104
|
.map(([key]) => key)
|
136
105
|
|
137
106
|
return breakpointPairs.length > 0
|
138
|
-
? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof
|
107
|
+
? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof UnistylesBreakpoints & string]
|
139
108
|
: undefined
|
140
109
|
}
|
package/src/utils/common.ts
CHANGED
@@ -8,7 +8,13 @@ export const warn = (message: string) => {
|
|
8
8
|
console.warn(`🦄 [react-native-unistyles]: ${message}`)
|
9
9
|
}
|
10
10
|
|
11
|
+
export const isMobile = Platform.OS === 'android' || Platform.OS === 'ios'
|
11
12
|
export const isWeb = Platform.OS === 'web'
|
12
13
|
export const isIOS = Platform.OS === 'ios'
|
13
14
|
export const isAndroid = Platform.OS === 'android'
|
14
15
|
export const isServer = typeof window === 'undefined'
|
16
|
+
|
17
|
+
export const Orientation = {
|
18
|
+
Landscape: 'landscape',
|
19
|
+
Portrait: 'portrait'
|
20
|
+
} as const
|
package/src/utils/index.ts
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
export { normalizeStyles } from './normalizeStyles'
|
2
2
|
export * from './normalizer'
|
3
|
-
export {
|
3
|
+
export { mq, MQSymbol } from './mq'
|
4
|
+
export { getKeyForVariant } from './variants'
|
5
|
+
export { getKeyForUnistylesMediaQuery } from './mqParser'
|
6
|
+
export { getBreakpointFromScreenWidth, sortAndValidateBreakpoints, getValueForNestedStyle } from './breakpoints'
|
4
7
|
export { proxifyFunction, parseStyle } from './styles'
|
5
|
-
export { isServer } from './common'
|
6
|
-
export {
|
7
|
-
extractValues,
|
8
|
-
getKeyForCustomMediaQuery,
|
9
|
-
isMediaQuery,
|
10
|
-
isWithinTheHeight,
|
11
|
-
isWithinTheWidth,
|
12
|
-
isWithinTheWidthAndHeight
|
13
|
-
} from './mediaQueries'
|
8
|
+
export { isServer, Orientation } from './common'
|
package/src/utils/mq.ts
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
import type { MediaQuery, Nullable } from '../types'
|
2
|
+
import type { UnistylesBreakpoints } from '../global'
|
3
|
+
import { unistyles } from '../core'
|
4
|
+
|
5
|
+
export const MQSymbol = Symbol('unistyles-mq')
|
6
|
+
|
7
|
+
type MQValue = keyof UnistylesBreakpoints | number
|
8
|
+
|
9
|
+
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'
|
36
|
+
}
|
37
|
+
|
38
|
+
const getMQValue = (value: Nullable<MQValue>) => {
|
39
|
+
if (typeof value === 'number') {
|
40
|
+
return value
|
41
|
+
}
|
42
|
+
|
43
|
+
if (value === null) {
|
44
|
+
return 0
|
45
|
+
}
|
46
|
+
|
47
|
+
return unistyles.registry.breakpoints[value] ?? 0
|
48
|
+
}
|
49
|
+
|
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
|
98
|
+
}
|
99
|
+
|
100
|
+
if (prop === MQProp.shortH || prop === MQProp.height) {
|
101
|
+
return widthHandler
|
102
|
+
}
|
103
|
+
|
104
|
+
return Reflect.get(target, prop, receiver)
|
105
|
+
}
|
106
|
+
})
|