react-native-unistyles 1.0.0-beta.4 → 1.0.0-beta.5
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/commonjs/createUnistyles.js +10 -12
- package/lib/commonjs/createUnistyles.js.map +1 -1
- package/lib/commonjs/mediaQueries.js +2 -0
- package/lib/commonjs/mediaQueries.js.map +1 -0
- package/lib/commonjs/utils/breakpoints.js +12 -16
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/breakpoints.spec.js +20 -15
- package/lib/commonjs/utils/breakpoints.spec.js.map +1 -1
- package/lib/commonjs/utils/common.js +8 -1
- package/lib/commonjs/utils/common.js.map +1 -1
- package/lib/commonjs/utils/index.js +7 -0
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/mediaQueries.spec.js +1 -1
- package/lib/commonjs/utils/mediaQueries.spec.js.map +1 -1
- package/lib/commonjs/utils/normalizeStyles.js +10 -0
- package/lib/commonjs/utils/normalizeStyles.js.map +1 -0
- package/lib/commonjs/utils/normalizeStyles.web.js +59 -0
- package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -0
- package/lib/commonjs/utils/styles.js +14 -11
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/styles.spec.js +17 -7
- package/lib/commonjs/utils/styles.spec.js.map +1 -1
- package/lib/module/createUnistyles.js +10 -12
- package/lib/module/createUnistyles.js.map +1 -1
- package/lib/module/mediaQueries.js +2 -0
- package/lib/module/mediaQueries.js.map +1 -0
- package/lib/module/utils/breakpoints.js +12 -16
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/breakpoints.spec.js +20 -15
- package/lib/module/utils/breakpoints.spec.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 +1 -0
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/mediaQueries.spec.js +1 -1
- package/lib/module/utils/mediaQueries.spec.js.map +1 -1
- package/lib/module/utils/normalizeStyles.js +3 -0
- package/lib/module/utils/normalizeStyles.js.map +1 -0
- package/lib/module/utils/normalizeStyles.web.js +52 -0
- package/lib/module/utils/normalizeStyles.web.js.map +1 -0
- package/lib/module/utils/styles.js +14 -11
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/styles.spec.js +17 -7
- package/lib/module/utils/styles.spec.js.map +1 -1
- package/lib/typescript/src/createUnistyles.d.ts +2 -2
- package/lib/typescript/src/createUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/mediaQueries.d.ts +2 -0
- package/lib/typescript/src/mediaQueries.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +35 -13
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/utils/breakpoints.d.ts +6 -6
- package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/utils/common.d.ts +2 -0
- package/lib/typescript/src/utils/common.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +1 -0
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/normalizeStyles.d.ts +2 -0
- package/lib/typescript/src/utils/normalizeStyles.d.ts.map +1 -0
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts +2 -0
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -0
- package/lib/typescript/src/utils/styles.d.ts +5 -5
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/package.json +9 -2
- package/src/createUnistyles.ts +20 -14
- package/src/mediaQueries.ts +33 -0
- package/src/types.ts +42 -15
- package/src/utils/breakpoints.ts +15 -15
- package/src/utils/common.ts +8 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/normalizeStyles.ts +2 -0
- package/src/utils/normalizeStyles.web.ts +103 -0
- package/src/utils/styles.ts +20 -14
package/src/types.ts
CHANGED
@@ -14,6 +14,11 @@ import type {
|
|
14
14
|
TranslateXTransform,
|
15
15
|
TranslateYTransform
|
16
16
|
} from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
|
17
|
+
import type { MediaQueries } from './mediaQueries'
|
18
|
+
|
19
|
+
export type Breakpoints = Record<string, number>
|
20
|
+
|
21
|
+
export type SortedBreakpointEntries<B extends Breakpoints> = [[keyof B & string, number]]
|
17
22
|
|
18
23
|
export type ScreenSize = {
|
19
24
|
width: number,
|
@@ -22,26 +27,45 @@ export type ScreenSize = {
|
|
22
27
|
|
23
28
|
export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
|
24
29
|
|
25
|
-
type StyleProperty<T, B extends
|
30
|
+
type StyleProperty<T, B extends Breakpoints> = {
|
26
31
|
[K in keyof T]: {
|
27
32
|
[innerKey in keyof B]?: T[K]
|
28
33
|
} | {
|
29
|
-
[innerKey in
|
34
|
+
[innerKey in MediaQueries]?: T[K]
|
30
35
|
} | T[K]
|
31
36
|
}
|
32
37
|
|
33
|
-
type ShadowOffsetProps<B
|
38
|
+
type ShadowOffsetProps<B> = {
|
34
39
|
shadowOffset: {
|
35
40
|
width: number | {
|
36
41
|
[innerKey in keyof B]?: number
|
42
|
+
} | {
|
43
|
+
[innerKey in MediaQueries]: number
|
37
44
|
},
|
38
45
|
height: number | {
|
39
46
|
[innerKey in keyof B]?: number
|
47
|
+
} | {
|
48
|
+
[innerKey in MediaQueries]: number
|
40
49
|
}
|
41
50
|
}
|
42
51
|
}
|
43
52
|
|
44
|
-
type
|
53
|
+
type TextShadowOffsetProps<B> = {
|
54
|
+
textShadowOffset: {
|
55
|
+
width: number | {
|
56
|
+
[innerKey in keyof B]?: number
|
57
|
+
} | {
|
58
|
+
[innerKey in MediaQueries]: number
|
59
|
+
},
|
60
|
+
height: number | {
|
61
|
+
[innerKey in keyof B]?: number
|
62
|
+
} | {
|
63
|
+
[innerKey in MediaQueries]: number
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
type TransformStyles<B extends Breakpoints> =
|
45
69
|
PerpectiveTransform | StyleProperty<PerpectiveTransform, B>
|
46
70
|
| RotateTransform | StyleProperty<RotateTransform, B>
|
47
71
|
| RotateXTransform | StyleProperty<RotateXTransform, B>
|
@@ -56,27 +80,30 @@ type TransformStyles<B extends Record<string, number>> =
|
|
56
80
|
| SkewYTransform | StyleProperty<SkewYTransform, B>
|
57
81
|
| MatrixTransform | StyleProperty<MatrixTransform, B>
|
58
82
|
|
59
|
-
type TransformProps<B extends
|
83
|
+
type TransformProps<B extends Breakpoints> = {
|
60
84
|
transform: Array<TransformStyles<B>>
|
61
85
|
}
|
62
86
|
|
63
|
-
type UnistyleView = Omit<Omit<ViewStyle, 'shadowOffset'>, 'transform'>
|
64
|
-
type UnistyleText = Omit<Omit<TextStyle, 'shadowOffset'>, 'transform'>
|
65
|
-
type UnistyleImage = Omit<Omit<ImageStyle, 'shadowOffset'>, 'transform'>
|
87
|
+
type UnistyleView = Omit<Omit<Omit<ViewStyle, 'shadowOffset'>, 'transform'>, 'textShadowOffset'>
|
88
|
+
type UnistyleText = Omit<Omit<Omit<TextStyle, 'shadowOffset'>, 'transform'>, 'textShadowOffset'>
|
89
|
+
type UnistyleImage = Omit<Omit<Omit<ImageStyle, 'shadowOffset'>, 'transform'>, 'textShadowOffset'>
|
66
90
|
|
67
|
-
export type StaticStyles<B extends
|
91
|
+
export type StaticStyles<B extends Breakpoints> =
|
68
92
|
| (UnistyleView | StyleProperty<UnistyleView, B>)
|
69
93
|
| (UnistyleText | StyleProperty<UnistyleText, B>)
|
70
94
|
| (UnistyleImage | StyleProperty<UnistyleImage, B>)
|
71
|
-
& TransformProps<B> & ShadowOffsetProps<B>
|
95
|
+
& TransformProps<B> & ShadowOffsetProps<B> & TextShadowOffsetProps<B>
|
72
96
|
|
73
|
-
export type CustomNamedStyles<T, B extends
|
97
|
+
export type CustomNamedStyles<T, B extends Breakpoints> = {
|
74
98
|
[K in keyof T]: T[K] extends (...args: infer A) => unknown
|
75
99
|
? (...args: A) => StaticStyles<B>
|
76
100
|
: StaticStyles<B>
|
77
101
|
}
|
78
|
-
|
79
|
-
|
102
|
+
|
103
|
+
type WithEmptyObject<V> = keyof V extends never ? {} : V
|
104
|
+
|
105
|
+
export type ExtractBreakpoints<T, B extends Breakpoints> = T extends Partial<Record<keyof B & string, infer V>>
|
106
|
+
? WithEmptyObject<V>
|
80
107
|
: T extends (...args: infer A) => infer R
|
81
108
|
? (...args: A) => ExtractBreakpoints<R, B>
|
82
109
|
: {
|
@@ -87,10 +114,10 @@ export type ExtractBreakpoints<T, B extends Record<string, number>> = T extends
|
|
87
114
|
: T[K]
|
88
115
|
}
|
89
116
|
|
90
|
-
export type RemoveKeysWithPrefix<T, B extends
|
117
|
+
export type RemoveKeysWithPrefix<T, B extends Breakpoints> = T extends (...args: Array<any>) => infer R
|
91
118
|
? (...args: Parameters<T>) => RemoveKeysWithPrefix<R, B>
|
92
119
|
: T extends object
|
93
120
|
? T extends Record<string, infer _V>
|
94
|
-
? { [K in keyof T as K extends
|
121
|
+
? { [K in keyof T as K extends MediaQueries ? keyof B & string : K]: RemoveKeysWithPrefix<T[K], B> }
|
95
122
|
: { [K in keyof T]: RemoveKeysWithPrefix<T[K], B> }
|
96
123
|
: T
|
package/src/utils/breakpoints.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { throwError } from './common'
|
2
|
-
import type { ScreenSize } from '../types'
|
2
|
+
import type { Breakpoints, ScreenSize, SortedBreakpointEntries } from '../types'
|
3
3
|
import { getKeyForCustomMediaQuery, isMediaQuery } from './mediaQueries'
|
4
4
|
|
5
5
|
/**
|
@@ -21,7 +21,7 @@ import { getKeyForCustomMediaQuery, isMediaQuery } from './mediaQueries'
|
|
21
21
|
* const input = { md: 768, lg: 1024, sm: 0 }
|
22
22
|
* sortAndValidateBreakpoints(input) // returns { sm: 0, md: 768, lg: 1024 }
|
23
23
|
*/
|
24
|
-
export const sortAndValidateBreakpoints = <B extends
|
24
|
+
export const sortAndValidateBreakpoints = <B extends Breakpoints>(breakpoints: B): B => {
|
25
25
|
const sortedPairs = Object
|
26
26
|
.entries(breakpoints)
|
27
27
|
.sort((breakpoint1, breakpoint2) => {
|
@@ -54,16 +54,15 @@ export const sortAndValidateBreakpoints = <B extends Record<string, number>>(bre
|
|
54
54
|
*
|
55
55
|
* @template B - An object type where keys are strings and values are numbers representing screen widths.
|
56
56
|
* @param {number} width - The screen width to determine the breakpoint for.
|
57
|
-
* @param
|
57
|
+
* @param breakpointEntries - sorted pairs of breakpoints
|
58
58
|
* @returns {keyof B & string} The key of the breakpoint that the screen width falls into.
|
59
59
|
*
|
60
60
|
* @example
|
61
61
|
* const breakpoints = { sm: 0, md: 768, lg: 1024 }
|
62
62
|
* getBreakpointFromScreenWidth(800, breakpoints) // returns 'md'
|
63
63
|
*/
|
64
|
-
export const getBreakpointFromScreenWidth = <B extends
|
65
|
-
const [key] =
|
66
|
-
.entries(breakpoints)
|
64
|
+
export const getBreakpointFromScreenWidth = <B extends Breakpoints>(width: number, breakpointEntries: SortedBreakpointEntries<B>): keyof B & string => {
|
65
|
+
const [key] = breakpointEntries
|
67
66
|
.find(([, value], index, otherBreakpoints) => {
|
68
67
|
const minVal = value
|
69
68
|
const maxVal = otherBreakpoints[index + 1]?.[1]
|
@@ -90,7 +89,7 @@ export const getBreakpointFromScreenWidth = <B extends Record<string, number>>(w
|
|
90
89
|
* @param {Record<keyof B & string, string | number>} value - An object containing values associated with breakpoints or custom media queries.
|
91
90
|
* @param {keyof B & string} breakpoint - The breakpoint name to check against.
|
92
91
|
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
93
|
-
* @param
|
92
|
+
* @param breakpointPairs - sorted pairs of breakpoints
|
94
93
|
*
|
95
94
|
* @returns {string | number | undefined} Returns the value associated with the matching breakpoint or custom media query, or `undefined` if no match is found.
|
96
95
|
*
|
@@ -102,7 +101,12 @@ export const getBreakpointFromScreenWidth = <B extends Record<string, number>>(w
|
|
102
101
|
*
|
103
102
|
* getValueForBreakpoint(values, 'sm', screenSize, breakpoints); // 'value1'
|
104
103
|
*/
|
105
|
-
export const getValueForBreakpoint = <B extends
|
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 => {
|
106
110
|
// the highest priority is for custom media queries
|
107
111
|
const customMediaQueries = Object
|
108
112
|
.entries(value)
|
@@ -123,18 +127,14 @@ export const getValueForBreakpoint = <B extends Record<string, number>>(value: R
|
|
123
127
|
}
|
124
128
|
|
125
129
|
// there is no direct hit for breakpoint nor media-query, so let's simulate CSS cascading
|
126
|
-
const
|
127
|
-
.entries(breakpoints)
|
128
|
-
.map(([key, bpValue]) => [key.toLowerCase(), bpValue])
|
129
|
-
|
130
|
-
const currentBreakpoint = allBreakpoints
|
130
|
+
const currentBreakpoint = breakpointPairs
|
131
131
|
.findIndex(([key]) => key === unifiedKey)
|
132
132
|
|
133
|
-
const availableBreakpoints =
|
133
|
+
const availableBreakpoints = breakpointPairs
|
134
134
|
.filter(([key], index) => index < currentBreakpoint && key && key in value)
|
135
135
|
.map(([key]) => key)
|
136
136
|
|
137
|
-
return
|
137
|
+
return breakpointPairs.length > 0
|
138
138
|
? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof B & string]
|
139
139
|
: undefined
|
140
140
|
}
|
package/src/utils/common.ts
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
import { Platform } from 'react-native'
|
2
|
+
|
1
3
|
export const throwError = (message: string) => {
|
2
4
|
throw new Error(`🦄 [react-native-unistyles]: ${message}`)
|
3
5
|
}
|
6
|
+
|
7
|
+
export const warn = (message: string) => {
|
8
|
+
console.warn(`🦄 [react-native-unistyles]: ${message}`)
|
9
|
+
}
|
10
|
+
|
11
|
+
export const isWeb = () => Platform.OS === 'web'
|
package/src/utils/index.ts
CHANGED
@@ -0,0 +1,103 @@
|
|
1
|
+
import { warn } from './common'
|
2
|
+
|
3
|
+
const preprocessor: Preprocessor = require('react-native-web/src/exports/StyleSheet/preprocess.js')
|
4
|
+
|
5
|
+
type Preprocessor = {
|
6
|
+
createTextShadowValue<T>(styles: any): T,
|
7
|
+
createBoxShadowValue<T>(styles: any): T,
|
8
|
+
createTransformValue<T>(transforms: any): T,
|
9
|
+
}
|
10
|
+
|
11
|
+
type NormalizedBoxShadow = {
|
12
|
+
shadowColor: undefined,
|
13
|
+
shadowOffset: undefined,
|
14
|
+
shadowOpacity: undefined,
|
15
|
+
shadowRadius: undefined,
|
16
|
+
boxShadow?: string
|
17
|
+
}
|
18
|
+
|
19
|
+
type NormalizedTextShadow = {
|
20
|
+
textShadowColor: undefined
|
21
|
+
textShadowOffset: undefined
|
22
|
+
textShadowRadius: undefined,
|
23
|
+
textShadow?: string
|
24
|
+
}
|
25
|
+
|
26
|
+
const normalizeBoxShadow = <T extends {}>(styles: T): NormalizedBoxShadow => {
|
27
|
+
const requiredBoxShadowProperties = [
|
28
|
+
'shadowColor',
|
29
|
+
'shadowOffset',
|
30
|
+
'shadowOpacity',
|
31
|
+
'shadowRadius'
|
32
|
+
]
|
33
|
+
|
34
|
+
if (!requiredBoxShadowProperties.every(prop => prop in styles)) {
|
35
|
+
warn(`can't apply box shadow as you miss at least one of these properties: ${requiredBoxShadowProperties.join(', ')}`)
|
36
|
+
|
37
|
+
return {
|
38
|
+
shadowColor: undefined,
|
39
|
+
shadowOffset: undefined,
|
40
|
+
shadowOpacity: undefined,
|
41
|
+
shadowRadius: undefined
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
return {
|
46
|
+
boxShadow: preprocessor.createBoxShadowValue(styles),
|
47
|
+
shadowColor: undefined,
|
48
|
+
shadowOffset: undefined,
|
49
|
+
shadowOpacity: undefined,
|
50
|
+
shadowRadius: undefined
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
const normalizeTextShadow = <T extends {}>(styles: T): NormalizedTextShadow => {
|
55
|
+
const requiredTextShadowProperties = [
|
56
|
+
'textShadowColor',
|
57
|
+
'textShadowOffset',
|
58
|
+
'textShadowRadius'
|
59
|
+
]
|
60
|
+
|
61
|
+
if (!requiredTextShadowProperties.every(prop => prop in styles)) {
|
62
|
+
warn(`can't apply text shadow as you miss at least one of these properties: ${requiredTextShadowProperties.join(', ')}`)
|
63
|
+
|
64
|
+
return {
|
65
|
+
textShadowColor: undefined,
|
66
|
+
textShadowOffset: undefined,
|
67
|
+
textShadowRadius: undefined
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
return {
|
72
|
+
textShadow: preprocessor.createTextShadowValue(styles),
|
73
|
+
textShadowColor: undefined,
|
74
|
+
textShadowOffset: undefined,
|
75
|
+
textShadowRadius: undefined
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
export const normalizeStyles = <T extends {}>(styles: T): T => {
|
80
|
+
const normalizedTransform = ('transform' in styles && Array.isArray(styles.transform))
|
81
|
+
? { transform: preprocessor.createTransformValue(styles.transform) }
|
82
|
+
: {}
|
83
|
+
|
84
|
+
const normalizedBoxShadow = (
|
85
|
+
'shadowColor' in styles ||
|
86
|
+
'shadowOffset' in styles ||
|
87
|
+
'shadowOpacity' in styles ||
|
88
|
+
'shadowRadius' in styles
|
89
|
+
) ? normalizeBoxShadow(styles) : {}
|
90
|
+
|
91
|
+
const normalizedTextShadow = (
|
92
|
+
'textShadowColor' in styles ||
|
93
|
+
'textShadowOffset' in styles ||
|
94
|
+
'textShadowRadius' in styles
|
95
|
+
) ? normalizeTextShadow(styles) : {}
|
96
|
+
|
97
|
+
return {
|
98
|
+
...styles,
|
99
|
+
...normalizedTransform,
|
100
|
+
...normalizedBoxShadow,
|
101
|
+
...normalizedTextShadow
|
102
|
+
}
|
103
|
+
}
|
package/src/utils/styles.ts
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
import type { CustomNamedStyles, ScreenSize } from '../types'
|
1
|
+
import type { Breakpoints, CustomNamedStyles, ScreenSize, SortedBreakpointEntries } from '../types'
|
2
2
|
import { getValueForBreakpoint } from './breakpoints'
|
3
|
+
import { normalizeStyles } from './normalizeStyles'
|
4
|
+
import { isWeb } from './common'
|
3
5
|
|
4
6
|
/**
|
5
7
|
* Proxies a function to parse its return value for custom media queries or breakpoints.
|
@@ -9,7 +11,7 @@ import { getValueForBreakpoint } from './breakpoints'
|
|
9
11
|
* @param {Function} fn - The function to be proxified.
|
10
12
|
* @param {keyof B & string} breakpoint - The breakpoint name to check against.
|
11
13
|
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
12
|
-
* @param
|
14
|
+
* @param breakpointPairs - sorted pairs of breakpoints
|
13
15
|
*
|
14
16
|
* @returns {Function} Returns the proxified function
|
15
17
|
*
|
@@ -22,13 +24,13 @@ import { getValueForBreakpoint } from './breakpoints'
|
|
22
24
|
* const proxifiedFunction = proxifyFunction(myFunction, 'sm', screenSize, breakpoints)
|
23
25
|
* proxifiedFunction() // parsed style based on screenSize and breakpoints
|
24
26
|
*/
|
25
|
-
export const proxifyFunction = <B extends
|
27
|
+
export const proxifyFunction = <B extends Breakpoints>(
|
26
28
|
fn: Function, breakpoint: keyof B & string,
|
27
29
|
screenSize: ScreenSize,
|
28
|
-
|
30
|
+
breakpointPairs: SortedBreakpointEntries<B>
|
29
31
|
): Function => new Proxy(fn, {
|
30
32
|
apply: (target, thisArg, argumentsList) =>
|
31
|
-
parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize,
|
33
|
+
parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize, breakpointPairs)
|
32
34
|
})
|
33
35
|
|
34
36
|
/**
|
@@ -43,7 +45,7 @@ export const proxifyFunction = <B extends Record<string, number>>(
|
|
43
45
|
* @param {CustomNamedStyles<T, B>} style - The style object to be parsed.
|
44
46
|
* @param {keyof B & string} breakpoint - The breakpoint name to check against.
|
45
47
|
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
46
|
-
* @param
|
48
|
+
* @param breakpointPairs - sorted pairs of breakpoints
|
47
49
|
*
|
48
50
|
* @returns {Record<string, string | number | Function>} Returns the parsed style object with resolved custom media queries or breakpoints.
|
49
51
|
*
|
@@ -56,26 +58,26 @@ export const proxifyFunction = <B extends Record<string, number>>(
|
|
56
58
|
* const parsedStyle = parseStyle(style, 'sm', screenSize, breakpoints)
|
57
59
|
* // { fontSize: '12px' }
|
58
60
|
*/
|
59
|
-
export const parseStyle = <T, B extends
|
61
|
+
export const parseStyle = <T, B extends Breakpoints>(
|
60
62
|
style: CustomNamedStyles<T, B>,
|
61
63
|
breakpoint: keyof B & string,
|
62
64
|
screenSize: ScreenSize,
|
63
|
-
|
65
|
+
breakpointPairs: SortedBreakpointEntries<B>
|
64
66
|
): T => {
|
65
67
|
const entries = Object.entries(style) as [[
|
66
68
|
keyof T,
|
67
69
|
CustomNamedStyles<T, B> | Record<keyof B & string, string | number | undefined>]
|
68
70
|
]
|
69
71
|
|
70
|
-
|
72
|
+
const parsedStyles = Object
|
71
73
|
.fromEntries(entries
|
72
74
|
.map(([key, value]) => {
|
73
|
-
const
|
75
|
+
const hasNestedProperties = key === 'shadowOffset' || key === 'textShadowOffset'
|
74
76
|
|
75
|
-
if (
|
77
|
+
if (hasNestedProperties) {
|
76
78
|
return [
|
77
79
|
key,
|
78
|
-
parseStyle(value as CustomNamedStyles<T, B>, breakpoint, screenSize,
|
80
|
+
parseStyle(value as CustomNamedStyles<T, B>, breakpoint, screenSize, breakpointPairs)
|
79
81
|
]
|
80
82
|
}
|
81
83
|
|
@@ -84,7 +86,7 @@ export const parseStyle = <T, B extends Record<string, number>>(
|
|
84
86
|
if (isTransform && Array.isArray(value)) {
|
85
87
|
return [
|
86
88
|
key,
|
87
|
-
value.map(value => parseStyle(value, breakpoint, screenSize,
|
89
|
+
value.map(value => parseStyle(value, breakpoint, screenSize, breakpointPairs))
|
88
90
|
]
|
89
91
|
}
|
90
92
|
|
@@ -101,9 +103,13 @@ export const parseStyle = <T, B extends Record<string, number>>(
|
|
101
103
|
value as Record<keyof B & string, string | number | undefined>,
|
102
104
|
breakpoint,
|
103
105
|
screenSize,
|
104
|
-
|
106
|
+
breakpointPairs
|
105
107
|
)
|
106
108
|
]
|
107
109
|
})
|
108
110
|
)
|
111
|
+
|
112
|
+
return isWeb()
|
113
|
+
? normalizeStyles(parsedStyles)
|
114
|
+
: parsedStyles
|
109
115
|
}
|