react-native-unistyles 2.0.0-alpha.13 → 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/UnistyleRegistry.js +3 -2
- package/lib/commonjs/core/UnistyleRegistry.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/plugins/withPlugins.js +2 -6
- package/lib/commonjs/plugins/withPlugins.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 +8 -11
- package/lib/commonjs/useStyles.js.map +1 -1
- package/lib/commonjs/utils/index.js +22 -5
- 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 +19 -23
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/variants.js +43 -8
- 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/UnistyleRegistry.js +4 -3
- package/lib/module/core/UnistyleRegistry.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/plugins/withPlugins.js +2 -6
- package/lib/module/plugins/withPlugins.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 +9 -12
- package/lib/module/useStyles.js.map +1 -1
- package/lib/module/utils/index.js +1 -2
- 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 +20 -24
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/variants.js +41 -6
- 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/core/UnistyleRegistry.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/plugins/withPlugins.d.ts +3 -2
- package/lib/typescript/src/plugins/withPlugins.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 -2
- 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 +4 -4
- 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/UnistyleRegistry.ts +6 -3
- 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/plugins/withPlugins.ts +13 -15
- 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 +22 -16
- package/src/utils/index.ts +1 -2
- package/src/utils/mq.ts +56 -41
- package/src/utils/styles.ts +28 -45
- package/src/utils/variants.ts +55 -7
package/src/types/index.ts
CHANGED
@@ -3,7 +3,6 @@ export * from './unistyles'
|
|
3
3
|
export type { Optional, Nullable } from './common'
|
4
4
|
export type { MediaQuery } from './mq'
|
5
5
|
export type {
|
6
|
-
CustomNamedStyles,
|
7
6
|
NestedStylePairs,
|
8
7
|
UnistylesTheme,
|
9
8
|
CreateStylesFactory,
|
@@ -12,6 +11,7 @@ export type {
|
|
12
11
|
RNValue,
|
13
12
|
RNStyle
|
14
13
|
} from './core'
|
14
|
+
export type { StyleSheetWithSuperPowers, StyleSheet } from './stylesheet'
|
15
15
|
export type { ReactNativeStyleSheet } from './breakpoints'
|
16
16
|
export type { ExtractVariantNames } from './variants'
|
17
17
|
export type { UnistylesPlugin } from './plugin'
|
package/src/types/mq.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import { MQSymbol } from '../utils'
|
1
|
+
import { MQSymbol, MQHeight, MQHeightAndWidth, MQWidth, MQWidthAndHeight } from '../utils'
|
2
2
|
|
3
|
-
export type MediaQuery = typeof MQSymbol
|
3
|
+
export type MediaQuery = typeof MQSymbol | typeof MQHeight | typeof MQWidth | typeof MQHeightAndWidth | typeof MQWidthAndHeight
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
|
2
|
+
import type { ShadowOffset, TransformStyles, UnistylesTheme } from './core'
|
3
|
+
import type { UnistylesBreakpoints } from '../global'
|
4
|
+
import type { MediaQuery } from './mq'
|
5
|
+
|
6
|
+
// these props are treated differently to nest breakpoints and media queries
|
7
|
+
type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
|
8
|
+
|
9
|
+
type UnistyleView = Omit<ViewStyle, NestedTypes>
|
10
|
+
type UnistyleText = Omit<TextStyle, NestedTypes>
|
11
|
+
type UnistyleImage = Omit<ImageStyle, NestedTypes>
|
12
|
+
|
13
|
+
type UnistyleNestedStyles = {
|
14
|
+
shadowOffset?: ToDeepUnistyles<ShadowOffset>,
|
15
|
+
textShadowOffset?: ToDeepUnistyles<ShadowOffset>,
|
16
|
+
transform?: Array<ToDeepUnistyles<TransformStyles>>
|
17
|
+
}
|
18
|
+
|
19
|
+
type Variants = {
|
20
|
+
variants?: {
|
21
|
+
[variantName: string]: {
|
22
|
+
[variant: string]: {
|
23
|
+
[propName in AllAvailableKeys]?: AllAvailableStyles[propName] | {
|
24
|
+
[key in BreakpointsOrMediaQueries]?: AllAvailableStyles[propName]
|
25
|
+
} & {
|
26
|
+
[propName in NestedTypes]?: UnistyleNestedStyles[propName]
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
type ToDeepUnistyles<T> = {
|
34
|
+
[K in keyof T]?: T[K] | {
|
35
|
+
[key in BreakpointsOrMediaQueries]?: T[K]
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
type AllAvailableStyles = UnistyleView & UnistyleText & UnistyleImage & UnistyleNestedStyles
|
40
|
+
|
41
|
+
export type AllAvailableKeys = keyof (UnistyleView & UnistyleText & UnistyleImage)
|
42
|
+
export type BreakpointsOrMediaQueries = keyof UnistylesBreakpoints | MediaQuery
|
43
|
+
|
44
|
+
export type UnistylesValues = {
|
45
|
+
[propName in AllAvailableKeys]?: AllAvailableStyles[propName] | {
|
46
|
+
[key in BreakpointsOrMediaQueries]?: AllAvailableStyles[propName]
|
47
|
+
}
|
48
|
+
} & Variants & {
|
49
|
+
[propName in NestedTypes]?: UnistyleNestedStyles[propName]
|
50
|
+
}
|
51
|
+
|
52
|
+
export type StyleSheet = {
|
53
|
+
[styleName: string]: UnistylesValues | ((...args: any) => UnistylesValues)
|
54
|
+
}
|
55
|
+
|
56
|
+
export type StyleSheetWithSuperPowers = ((theme: UnistylesTheme) => StyleSheet) | StyleSheet
|
package/src/types/unistyles.ts
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
import {
|
1
|
+
import { UnistylesEventType, ScreenOrientation } from '../common'
|
2
2
|
import type { UnistylesThemes, UnistylesBreakpoints } from '../global'
|
3
3
|
import type { ScreenSize } from './core'
|
4
|
-
import type { Optional } from './common'
|
5
4
|
import type { UnistylesPlugin } from './plugin'
|
6
5
|
|
7
|
-
export type ColorSchemeName =
|
6
|
+
export type ColorSchemeName = 'light' | 'dark' | 'unspecified'
|
8
7
|
|
9
8
|
export type UnistylesConfig = {
|
10
9
|
adaptiveThemes?: boolean,
|
@@ -33,14 +32,14 @@ export type UnistylesBridge = {
|
|
33
32
|
}
|
34
33
|
|
35
34
|
export type UnistylesThemeEvent = {
|
36
|
-
type:
|
35
|
+
type: UnistylesEventType.Theme,
|
37
36
|
payload: {
|
38
37
|
themeName: keyof UnistylesThemes
|
39
38
|
}
|
40
39
|
}
|
41
40
|
|
42
41
|
export type UnistylesMobileLayoutEvent = {
|
43
|
-
type:
|
42
|
+
type: UnistylesEventType.Layout,
|
44
43
|
payload: {
|
45
44
|
screen: ScreenSize,
|
46
45
|
breakpoint: keyof UnistylesBreakpoints,
|
@@ -49,7 +48,7 @@ export type UnistylesMobileLayoutEvent = {
|
|
49
48
|
}
|
50
49
|
|
51
50
|
export type UnistylesPluginEvent = {
|
52
|
-
type:
|
51
|
+
type: UnistylesEventType.Plugin
|
53
52
|
}
|
54
53
|
|
55
54
|
export type UnistylesEvents = UnistylesThemeEvent | UnistylesMobileLayoutEvent | UnistylesPluginEvent
|
package/src/types/variants.ts
CHANGED
@@ -1,11 +1,19 @@
|
|
1
|
-
export type ExtractVariantNames<T> = T extends
|
2
|
-
?
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
export type ExtractVariantNames<T> = T extends (...args: any) => infer R
|
2
|
+
? ExtractVariantKeys<R>
|
3
|
+
: ExtractVariantKeys<T>
|
4
|
+
|
5
|
+
type ExtractVariantKeys<T> = T extends object
|
6
|
+
? ExtractVariant<T[keyof T]>
|
7
|
+
: never
|
8
|
+
|
9
|
+
type ExtractSubVariantKeys<T> = T extends object
|
10
|
+
? keyof Omit<T, 'default'> | undefined
|
10
11
|
: never
|
11
12
|
|
13
|
+
type ExtractVariant<T> = T extends (...args: any) => infer R
|
14
|
+
? R extends { variants: infer V }
|
15
|
+
? { [key in keyof V]?: ExtractSubVariantKeys<V[key]> }
|
16
|
+
: never
|
17
|
+
: T extends { variants: infer V }
|
18
|
+
? { [key in keyof V]?: ExtractSubVariantKeys<V[key]> }
|
19
|
+
: never
|
package/src/useStyles.ts
CHANGED
@@ -1,37 +1,37 @@
|
|
1
1
|
import { useMemo } from 'react'
|
2
2
|
import { StyleSheet } from 'react-native'
|
3
3
|
import { parseStyle, proxifyFunction } from './utils'
|
4
|
+
import { useUnistyles, useVariants } from './hooks'
|
5
|
+
import type { UnistylesBreakpoints } from './global'
|
6
|
+
import { unistyles } from './core'
|
4
7
|
import type {
|
5
|
-
CreateStylesFactory,
|
6
|
-
CustomNamedStyles,
|
7
8
|
ExtractVariantNames,
|
8
9
|
ReactNativeStyleSheet,
|
10
|
+
StyleSheetWithSuperPowers,
|
9
11
|
UnistylesTheme
|
10
12
|
} from './types'
|
11
|
-
import { useUnistyles } from './hooks'
|
12
|
-
import type { UnistylesBreakpoints } from './global'
|
13
13
|
|
14
|
-
type ParsedStylesheet<ST extends
|
14
|
+
type ParsedStylesheet<ST extends StyleSheetWithSuperPowers> = {
|
15
15
|
theme: UnistylesTheme,
|
16
16
|
breakpoint: keyof UnistylesBreakpoints,
|
17
17
|
styles: ReactNativeStyleSheet<ST>
|
18
18
|
}
|
19
19
|
|
20
|
-
export const useStyles = <ST extends
|
21
|
-
stylesheet?: ST
|
22
|
-
|
20
|
+
export const useStyles = <ST extends StyleSheetWithSuperPowers>(
|
21
|
+
stylesheet?: ST,
|
22
|
+
variantsMap?: ExtractVariantNames<typeof stylesheet>
|
23
23
|
): ParsedStylesheet<ST> => {
|
24
24
|
const { theme, layout, plugins } = useUnistyles()
|
25
|
-
const { screenSize, breakpoint } = layout
|
26
25
|
|
27
26
|
if (!stylesheet) {
|
28
27
|
return {
|
29
28
|
theme,
|
30
|
-
breakpoint,
|
29
|
+
breakpoint: layout.breakpoint,
|
31
30
|
styles: {} as ReactNativeStyleSheet<ST>
|
32
31
|
}
|
33
32
|
}
|
34
33
|
|
34
|
+
const variants = useVariants(variantsMap)
|
35
35
|
const parsedStyles = useMemo(() => typeof stylesheet === 'function'
|
36
36
|
? stylesheet(theme)
|
37
37
|
: stylesheet, [theme, stylesheet, layout])
|
@@ -39,24 +39,30 @@ export const useStyles = <ST extends CustomNamedStyles<ST>>(
|
|
39
39
|
const dynamicStyleSheet = useMemo(() => Object
|
40
40
|
.entries(parsedStyles)
|
41
41
|
.reduce((acc, [key, value]) => {
|
42
|
-
const style = value as CustomNamedStyles<ST>
|
43
|
-
|
44
42
|
if (typeof value === 'function') {
|
45
43
|
return {
|
46
44
|
...acc,
|
47
|
-
[key]: proxifyFunction(key, value,
|
45
|
+
[key]: proxifyFunction(key, value,unistyles.registry.plugins, unistyles.runtime, variants)
|
48
46
|
}
|
49
47
|
}
|
50
48
|
|
51
49
|
return StyleSheet.create({
|
52
50
|
...acc,
|
53
|
-
[key]: parseStyle
|
51
|
+
[key]: parseStyle(
|
52
|
+
key,
|
53
|
+
value,
|
54
|
+
unistyles.registry.plugins,
|
55
|
+
unistyles.runtime,
|
56
|
+
variants
|
57
|
+
)
|
54
58
|
})
|
55
|
-
}, {}
|
59
|
+
}, {}),
|
60
|
+
[layout, parsedStyles, variants, plugins]
|
61
|
+
) as ReactNativeStyleSheet<ST>
|
56
62
|
|
57
63
|
return {
|
58
64
|
theme,
|
59
|
-
breakpoint,
|
65
|
+
breakpoint: layout.breakpoint,
|
60
66
|
styles: dynamicStyleSheet
|
61
67
|
}
|
62
68
|
}
|
package/src/utils/index.ts
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
export { mq, MQSymbol } from './mq'
|
2
|
-
export { getKeyForVariant } from './variants'
|
1
|
+
export { mq, MQSymbol, MQHeight, MQWidth, MQHeightAndWidth, MQWidthAndHeight } from './mq'
|
3
2
|
export { getKeyForUnistylesMediaQuery, isWithinTheWidthAndHeight, isValidMq, parseMq } from './mqParser'
|
4
3
|
export { getValueForBreakpoint } from './breakpoints'
|
5
4
|
export { proxifyFunction, parseStyle } from './styles'
|
package/src/utils/mq.ts
CHANGED
@@ -1,28 +1,32 @@
|
|
1
|
-
import type {
|
1
|
+
import type { Nullable } from '../types'
|
2
2
|
import type { UnistylesBreakpoints } from '../global'
|
3
3
|
import { unistyles } from '../core'
|
4
4
|
|
5
5
|
export const MQSymbol = Symbol('unistyles-mq')
|
6
|
+
export const MQWidth = Symbol('unistyles-mq-width')
|
7
|
+
export const MQHeight = Symbol('unistyles-mq-height')
|
8
|
+
export const MQWidthAndHeight = Symbol('unistyles-mq-width-and-height')
|
9
|
+
export const MQHeightAndWidth = Symbol('unistyles-mq-height-and-width')
|
6
10
|
|
7
11
|
type MQValue = keyof UnistylesBreakpoints | number
|
8
12
|
|
9
13
|
type MQHandler = {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
+
only: {
|
15
|
+
width(wMin?: Nullable<MQValue>, wMax?: MQValue): typeof MQWidth,
|
16
|
+
height(hMin?: Nullable<MQValue>, hMax?: MQValue): typeof MQHeight,
|
17
|
+
},
|
18
|
+
width(wMin?: Nullable<MQValue>, wMax?: MQValue): {
|
19
|
+
and: {
|
20
|
+
height(hMin?: Nullable<MQValue>, hMax?: MQValue): typeof MQWidthAndHeight
|
21
|
+
}
|
22
|
+
},
|
23
|
+
height(hMin?: Nullable<MQValue>, hMax?: MQValue): {
|
24
|
+
and: {
|
25
|
+
width(wMin?: Nullable<MQValue>, wMax?: MQValue): typeof MQHeightAndWidth
|
26
|
+
}
|
27
|
+
}
|
14
28
|
}
|
15
29
|
|
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
30
|
type FinalHandler = {
|
27
31
|
[MQSymbol]: true
|
28
32
|
}
|
@@ -31,8 +35,8 @@ enum MQProp {
|
|
31
35
|
toString = 'toString',
|
32
36
|
width = 'width',
|
33
37
|
height = 'height',
|
34
|
-
|
35
|
-
|
38
|
+
only = 'only',
|
39
|
+
and = 'and'
|
36
40
|
}
|
37
41
|
|
38
42
|
const getMQValue = (value: Nullable<MQValue>) => {
|
@@ -47,60 +51,71 @@ const getMQValue = (value: Nullable<MQValue>) => {
|
|
47
51
|
return unistyles.registry.breakpoints[value] ?? 0
|
48
52
|
}
|
49
53
|
|
50
|
-
const widthHandler = (hMin: Nullable<MQValue> = 0, hMax: MQValue = Infinity) => new Proxy
|
54
|
+
const widthHandler = (hMin: Nullable<MQValue> = 0, hMax: MQValue = Infinity) => new Proxy({} as MQHandler, {
|
51
55
|
get: (target, prop, receiver) => {
|
52
56
|
if (prop === Symbol.toPrimitive || prop === MQProp.toString) {
|
53
57
|
return () => `:h[${getMQValue(hMin)}, ${getMQValue(hMax)}]`
|
54
58
|
}
|
55
59
|
|
56
|
-
if (prop === MQProp.
|
57
|
-
return
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
if (prop === MQProp.and) {
|
61
|
+
return {
|
62
|
+
width: (wMin: MQValue = 0, wMax: MQValue = Infinity) => new Proxy<FinalHandler>({} as FinalHandler, {
|
63
|
+
get: (target, prop, receiver) => {
|
64
|
+
if (prop === Symbol.toPrimitive || prop === MQProp.toString) {
|
65
|
+
return () => `:w[${getMQValue(wMin)}, ${getMQValue(wMax)}]:h[${getMQValue(hMin)}, ${getMQValue(hMax)}]`
|
66
|
+
}
|
62
67
|
|
63
|
-
|
64
|
-
|
65
|
-
|
68
|
+
return Reflect.get(target, prop, receiver)
|
69
|
+
}
|
70
|
+
})
|
71
|
+
}
|
66
72
|
}
|
67
73
|
|
68
74
|
return Reflect.get(target, prop, receiver)
|
69
75
|
}
|
70
76
|
})
|
71
77
|
|
72
|
-
const heightHandler = (wMin: Nullable<MQValue> = 0, wMax: MQValue = Infinity) => new Proxy({} as
|
78
|
+
const heightHandler = (wMin: Nullable<MQValue> = 0, wMax: MQValue = Infinity) => new Proxy({} as MQHandler, {
|
73
79
|
get: (target, prop, receiver) => {
|
74
80
|
if (prop === Symbol.toPrimitive || prop === MQProp.toString) {
|
75
81
|
return () => `:w[${getMQValue(wMin)}, ${getMQValue(wMax)}]`
|
76
82
|
}
|
77
83
|
|
78
|
-
if (prop === MQProp.
|
79
|
-
return
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
+
if (prop === MQProp.and) {
|
85
|
+
return {
|
86
|
+
height: (hMin: MQValue = 0, hMax: MQValue = Infinity) => new Proxy<FinalHandler>({} as FinalHandler, {
|
87
|
+
get: (target, prop, receiver) => {
|
88
|
+
if (prop === Symbol.toPrimitive || prop === MQProp.toString) {
|
89
|
+
return () => `:w[${getMQValue(wMin)}, ${getMQValue(wMax)}]:h[${getMQValue(hMin)}, ${getMQValue(hMax)}]`
|
90
|
+
}
|
84
91
|
|
85
|
-
|
86
|
-
|
87
|
-
|
92
|
+
return Reflect.get(target, prop, receiver)
|
93
|
+
}
|
94
|
+
})
|
95
|
+
}
|
88
96
|
}
|
89
97
|
|
90
98
|
return Reflect.get(target, prop, receiver)
|
91
99
|
}
|
92
100
|
})
|
93
101
|
|
94
|
-
export const mq = new Proxy({} as MQHandler, {
|
102
|
+
export const mq = new Proxy<MQHandler>({} as MQHandler, {
|
95
103
|
get: (target, prop, receiver) => {
|
96
|
-
if (prop === MQProp.
|
97
|
-
return
|
104
|
+
if (prop === MQProp.only) {
|
105
|
+
return {
|
106
|
+
width: heightHandler,
|
107
|
+
height: widthHandler
|
108
|
+
}
|
98
109
|
}
|
99
110
|
|
100
|
-
if (prop === MQProp.
|
111
|
+
if (prop === MQProp.height) {
|
101
112
|
return widthHandler
|
102
113
|
}
|
103
114
|
|
115
|
+
if (prop === MQProp.width) {
|
116
|
+
return heightHandler
|
117
|
+
}
|
118
|
+
|
104
119
|
return Reflect.get(target, prop, receiver)
|
105
120
|
}
|
106
121
|
})
|
package/src/utils/styles.ts
CHANGED
@@ -1,18 +1,19 @@
|
|
1
|
-
import type {
|
1
|
+
import type { NestedStyle, UnistylesPlugin, StyleSheet, Optional } from '../types'
|
2
2
|
import { getValueForBreakpoint } from './breakpoints'
|
3
|
-
import type {
|
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 = (
|
9
9
|
key: string,
|
10
|
-
fn: Function,
|
11
|
-
|
12
|
-
|
10
|
+
fn: Function,
|
11
|
+
plugins: Array<UnistylesPlugin>,
|
12
|
+
runtime: UnistylesRuntime,
|
13
|
+
variant?: Record<string, Optional<string>>
|
13
14
|
): Function => new Proxy(fn, {
|
14
15
|
apply: (target, thisArg, argumentsList) =>
|
15
|
-
parseStyle(key, target.apply(thisArg, argumentsList),
|
16
|
+
parseStyle(key, target.apply(thisArg, argumentsList), plugins, runtime, variant)
|
16
17
|
})
|
17
18
|
|
18
19
|
export const isPlatformColor = <T extends {}>(value: T): boolean => {
|
@@ -23,60 +24,42 @@ export const isPlatformColor = <T extends {}>(value: T): boolean => {
|
|
23
24
|
return isAndroid && 'resource_paths' in value && typeof value.resource_paths === 'object'
|
24
25
|
}
|
25
26
|
|
26
|
-
export const parseStyle = <T extends
|
27
|
+
export const parseStyle = <T extends StyleSheet>(
|
27
28
|
key: string,
|
28
|
-
style:
|
29
|
-
|
30
|
-
|
31
|
-
variant?: string
|
29
|
+
style: T,
|
30
|
+
plugins: Array<UnistylesPlugin>,
|
31
|
+
runtime: UnistylesRuntime,
|
32
|
+
variant?: Record<string, Optional<string>>
|
32
33
|
): T => {
|
33
|
-
const entries =
|
34
|
-
.entries(style || {}) as Array<[keyof T,
|
35
|
-
.map(([key, value]) => {
|
36
|
-
if (key !== 'variants') {
|
37
|
-
return [key, value]
|
38
|
-
}
|
39
|
-
|
40
|
-
const variantKey = getKeyForVariant(
|
41
|
-
value as NestedStyle,
|
42
|
-
variant
|
43
|
-
)
|
44
|
-
|
45
|
-
if (!variantKey) {
|
46
|
-
return undefined
|
47
|
-
}
|
48
|
-
|
49
|
-
return Object
|
50
|
-
.entries(value[variantKey as keyof typeof value] as NestedStyle)
|
51
|
-
.flat()
|
52
|
-
})
|
53
|
-
.filter(Boolean) as Array<[keyof T, CustomNamedStyles<T> | NestedStyle]>
|
34
|
+
const entries = Object
|
35
|
+
.entries(getStyleWithVariants(style || {}, variant)) as Array<[keyof T, StyleSheet]>
|
54
36
|
|
55
37
|
const parsedStyles = Object
|
56
38
|
.fromEntries(entries
|
57
39
|
.map(([key, value]) => {
|
58
|
-
|
40
|
+
// dynamic functions
|
41
|
+
if (typeof value === 'function') {
|
42
|
+
return [key, value]
|
43
|
+
}
|
59
44
|
|
60
|
-
|
45
|
+
// nested objects
|
46
|
+
if (key === 'shadowOffset' || key === 'textShadowOffset') {
|
61
47
|
return [
|
62
48
|
key,
|
63
|
-
parseStyle(key, value
|
49
|
+
parseStyle(key, value, plugins, runtime, variant)
|
64
50
|
]
|
65
51
|
}
|
66
52
|
|
67
|
-
|
68
|
-
|
69
|
-
if (isTransform && Array.isArray(value)) {
|
53
|
+
// transforms
|
54
|
+
if (key === 'transform' && Array.isArray(value)) {
|
70
55
|
return [
|
71
56
|
key,
|
72
|
-
value.map(value => parseStyle(key, value,
|
57
|
+
value.map(value => parseStyle(key, value, plugins, runtime, variant))
|
73
58
|
]
|
74
59
|
}
|
75
60
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
if (isDynamicFunction || isValidStyle) {
|
61
|
+
// values or platform colors
|
62
|
+
if (typeof value !== 'object' || isPlatformColor(value)) {
|
80
63
|
return [key, value]
|
81
64
|
}
|
82
65
|
|
@@ -87,5 +70,5 @@ export const parseStyle = <T extends RNStyle>(
|
|
87
70
|
})
|
88
71
|
) as T
|
89
72
|
|
90
|
-
return withPlugins(key, parsedStyles) as T
|
73
|
+
return withPlugins(key, parsedStyles, plugins, runtime) as T
|
91
74
|
}
|
package/src/utils/variants.ts
CHANGED
@@ -1,13 +1,61 @@
|
|
1
|
-
import type { Optional, NestedStyle } from '../types'
|
1
|
+
import type { Optional, StyleSheet, NestedStyle } from '../types'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
+
}
|
15
|
+
|
16
|
+
return undefined
|
17
|
+
})
|
18
|
+
.filter(Boolean) as Array<[string, string]>
|
6
19
|
}
|
7
20
|
|
8
|
-
|
9
|
-
|
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]>
|
36
|
+
}
|
37
|
+
|
38
|
+
export const getStyleWithVariants = (
|
39
|
+
style: StyleSheet,
|
40
|
+
variantValues?: Record<string, Optional<string>>
|
41
|
+
) => {
|
42
|
+
if (!('variants' in style)) {
|
43
|
+
return style
|
10
44
|
}
|
11
45
|
|
12
|
-
|
46
|
+
const keys = getKeysForVariants(
|
47
|
+
style.variants as Record<string, NestedStyle>,
|
48
|
+
Object.entries(variantValues || {})
|
49
|
+
)
|
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 }), {})
|
54
|
+
|
55
|
+
const { variants, ...otherStyles } = style
|
56
|
+
|
57
|
+
return {
|
58
|
+
...otherStyles,
|
59
|
+
...variantsValues
|
60
|
+
}
|
13
61
|
}
|