react-native-unistyles 2.0.0-alpha.14 → 2.0.0-alpha.15
Sign up to get free protection for your applications and to get access to all the features.
- package/cxx/UnistylesRuntime.h +6 -6
- package/lib/commonjs/common.js +15 -15
- package/lib/commonjs/common.js.map +1 -1
- package/lib/commonjs/core/UnistylesRuntime.js +1 -1
- package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
- package/lib/commonjs/createStyleSheet.js +1 -6
- package/lib/commonjs/createStyleSheet.js.map +1 -1
- package/lib/commonjs/hooks/index.js +7 -0
- package/lib/commonjs/hooks/index.js.map +1 -1
- package/lib/commonjs/hooks/useUnistyles.js +4 -7
- package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
- package/lib/commonjs/hooks/useVariants.js +24 -0
- package/lib/commonjs/hooks/useVariants.js.map +1 -0
- package/lib/commonjs/plugins/normalizer/normalizer.js +1 -1
- package/lib/commonjs/plugins/normalizer/normalizer.js.map +1 -1
- package/lib/commonjs/types/stylesheet.js +6 -0
- package/lib/commonjs/types/stylesheet.js.map +1 -0
- package/lib/commonjs/useStyles.js +5 -4
- package/lib/commonjs/useStyles.js.map +1 -1
- package/lib/commonjs/utils/index.js +24 -0
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/mq.js +42 -28
- package/lib/commonjs/utils/mq.js.map +1 -1
- package/lib/commonjs/utils/styles.js +14 -8
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/variants.js +33 -13
- package/lib/commonjs/utils/variants.js.map +1 -1
- package/lib/module/common.js +14 -14
- package/lib/module/common.js.map +1 -1
- package/lib/module/core/UnistylesRuntime.js +1 -1
- package/lib/module/core/UnistylesRuntime.js.map +1 -1
- package/lib/module/createStyleSheet.js +1 -6
- package/lib/module/createStyleSheet.js.map +1 -1
- package/lib/module/hooks/index.js +1 -0
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/useUnistyles.js +5 -8
- package/lib/module/hooks/useUnistyles.js.map +1 -1
- package/lib/module/hooks/useVariants.js +17 -0
- package/lib/module/hooks/useVariants.js.map +1 -0
- package/lib/module/plugins/normalizer/normalizer.js +1 -1
- package/lib/module/plugins/normalizer/normalizer.js.map +1 -1
- package/lib/module/types/stylesheet.js +2 -0
- package/lib/module/types/stylesheet.js.map +1 -0
- package/lib/module/useStyles.js +6 -5
- package/lib/module/useStyles.js.map +1 -1
- package/lib/module/utils/index.js +1 -1
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/mq.js +41 -27
- package/lib/module/utils/mq.js.map +1 -1
- package/lib/module/utils/styles.js +15 -9
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/variants.js +31 -11
- package/lib/module/utils/variants.js.map +1 -1
- package/lib/typescript/src/common.d.ts +10 -10
- package/lib/typescript/src/common.d.ts.map +1 -1
- package/lib/typescript/src/createStyleSheet.d.ts +2 -2
- package/lib/typescript/src/createStyleSheet.d.ts.map +1 -1
- package/lib/typescript/src/global.d.ts.map +1 -1
- package/lib/typescript/src/hooks/index.d.ts +1 -0
- package/lib/typescript/src/hooks/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useVariants.d.ts +3 -0
- package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -0
- package/lib/typescript/src/plugins/normalizer/normalizer.d.ts.map +1 -1
- package/lib/typescript/src/types/breakpoints.d.ts +20 -14
- package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/types/core.d.ts +3 -26
- package/lib/typescript/src/types/core.d.ts.map +1 -1
- package/lib/typescript/src/types/index.d.ts +2 -1
- package/lib/typescript/src/types/index.d.ts.map +1 -1
- package/lib/typescript/src/types/mq.d.ts +2 -2
- package/lib/typescript/src/types/mq.d.ts.map +1 -1
- package/lib/typescript/src/types/stylesheet.d.ts +47 -0
- package/lib/typescript/src/types/stylesheet.d.ts.map +1 -0
- package/lib/typescript/src/types/unistyles.d.ts +5 -6
- package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
- package/lib/typescript/src/types/variants.d.ts +13 -7
- package/lib/typescript/src/types/variants.d.ts.map +1 -1
- package/lib/typescript/src/useStyles.d.ts +3 -3
- package/lib/typescript/src/useStyles.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +1 -1
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/mq.d.ts +19 -13
- package/lib/typescript/src/utils/mq.d.ts.map +1 -1
- package/lib/typescript/src/utils/styles.d.ts +3 -3
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/lib/typescript/src/utils/variants.d.ts +2 -2
- package/lib/typescript/src/utils/variants.d.ts.map +1 -1
- package/package.json +14 -14
- package/src/common.ts +10 -10
- package/src/core/UnistylesRuntime.ts +1 -1
- package/src/createStyleSheet.ts +2 -8
- package/src/global.ts +1 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useUnistyles.ts +5 -8
- package/src/hooks/useVariants.ts +23 -0
- package/src/plugins/normalizer/normalizer.ts +3 -3
- package/src/types/breakpoints.ts +47 -26
- package/src/types/core.ts +6 -42
- package/src/types/index.ts +1 -1
- package/src/types/mq.ts +2 -2
- package/src/types/stylesheet.ts +56 -0
- package/src/types/unistyles.ts +5 -6
- package/src/types/variants.ts +17 -9
- package/src/useStyles.ts +15 -15
- package/src/utils/index.ts +1 -1
- package/src/utils/mq.ts +56 -41
- package/src/utils/styles.ts +18 -17
- package/src/utils/variants.ts +41 -17
package/src/utils/styles.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
import type {
|
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 {
|
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
|
27
|
+
export const parseStyle = <T extends StyleSheet>(
|
28
28
|
key: string,
|
29
|
-
style:
|
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(
|
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
|
-
|
40
|
+
// dynamic functions
|
41
|
+
if (typeof value === 'function') {
|
42
|
+
return [key, value]
|
43
|
+
}
|
41
44
|
|
42
|
-
|
45
|
+
// nested objects
|
46
|
+
if (key === 'shadowOffset' || key === 'textShadowOffset') {
|
43
47
|
return [
|
44
48
|
key,
|
45
|
-
parseStyle(key, value
|
49
|
+
parseStyle(key, value, plugins, runtime, variant)
|
46
50
|
]
|
47
51
|
}
|
48
52
|
|
49
|
-
|
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
|
-
|
59
|
-
|
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
|
|
package/src/utils/variants.ts
CHANGED
@@ -1,37 +1,61 @@
|
|
1
|
-
import type { Optional,
|
1
|
+
import type { Optional, StyleSheet, NestedStyle } from '../types'
|
2
2
|
|
3
|
-
const
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
const getKeysForVariants = (
|
4
|
+
value: Record<string, NestedStyle>,
|
5
|
+
variants: Array<[string, Optional<string>]>
|
6
|
+
): Array<[string, string]> => {
|
7
|
+
// case for no specified variants by user, we should fallback to 'default'
|
8
|
+
if (!variants.length) {
|
9
|
+
return Object
|
10
|
+
.entries(value)
|
11
|
+
.map(([key, value]) => {
|
12
|
+
if ('default' in value) {
|
13
|
+
return [key, 'default']
|
14
|
+
}
|
7
15
|
|
8
|
-
|
9
|
-
|
16
|
+
return undefined
|
17
|
+
})
|
18
|
+
.filter(Boolean) as Array<[string, string]>
|
10
19
|
}
|
11
20
|
|
12
|
-
return
|
21
|
+
return variants
|
22
|
+
.map(([variantKey, variantValue]) => {
|
23
|
+
const variantStyle = value[variantKey]
|
24
|
+
|
25
|
+
if (variantStyle && variantValue && variantValue in variantStyle) {
|
26
|
+
return [variantKey, variantValue]
|
27
|
+
}
|
28
|
+
|
29
|
+
if (variantStyle && 'default' in variantStyle) {
|
30
|
+
return [variantKey, 'default']
|
31
|
+
}
|
32
|
+
|
33
|
+
return undefined
|
34
|
+
})
|
35
|
+
.filter(Boolean) as Array<[string, string]>
|
13
36
|
}
|
14
37
|
|
15
|
-
export const
|
16
|
-
style:
|
17
|
-
|
38
|
+
export const getStyleWithVariants = (
|
39
|
+
style: StyleSheet,
|
40
|
+
variantValues?: Record<string, Optional<string>>
|
18
41
|
) => {
|
19
42
|
if (!('variants' in style)) {
|
20
43
|
return style
|
21
44
|
}
|
22
45
|
|
23
|
-
const
|
46
|
+
const keys = getKeysForVariants(
|
24
47
|
style.variants as Record<string, NestedStyle>,
|
25
|
-
|
48
|
+
Object.entries(variantValues || {})
|
26
49
|
)
|
27
|
-
|
28
|
-
|
29
|
-
|
50
|
+
|
51
|
+
const variantsValues = keys
|
52
|
+
.map(([key, nestedKey]) => ((style.variants as Record<string, Record<string, NestedStyle>>)[key] as Record<string, NestedStyle>)[nestedKey])
|
53
|
+
.reduce((acc, styles) => ({ ...acc, ...styles }), {})
|
30
54
|
|
31
55
|
const { variants, ...otherStyles } = style
|
32
56
|
|
33
57
|
return {
|
34
58
|
...otherStyles,
|
35
|
-
...
|
59
|
+
...variantsValues
|
36
60
|
}
|
37
61
|
}
|