react-native-unistyles 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +16 -3
- package/lib/commonjs/UnistylesTheme.js +2 -2
- package/lib/commonjs/UnistylesTheme.js.map +1 -1
- package/lib/commonjs/hooks/useDimensions.web.js +3 -2
- package/lib/commonjs/hooks/useDimensions.web.js.map +1 -1
- package/lib/commonjs/types/index.js +15 -0
- package/lib/commonjs/types/index.js.map +1 -1
- package/lib/commonjs/types/normalizer.js +6 -0
- package/lib/commonjs/types/normalizer.js.map +1 -0
- package/lib/commonjs/utils/common.js +3 -3
- package/lib/commonjs/utils/common.js.map +1 -1
- package/lib/commonjs/utils/index.js +34 -0
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/normalizeStyles.web.js +13 -13
- package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -1
- package/lib/commonjs/utils/normalizer.js +83 -0
- package/lib/commonjs/utils/normalizer.js.map +1 -0
- package/lib/commonjs/utils/styles.js +1 -1
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/module/hooks/useDimensions.web.js +3 -2
- package/lib/module/hooks/useDimensions.web.js.map +1 -1
- package/lib/module/types/index.js +1 -1
- package/lib/module/types/index.js.map +1 -1
- package/lib/module/types/normalizer.js +2 -0
- package/lib/module/types/normalizer.js.map +1 -0
- package/lib/module/utils/common.js +2 -1
- package/lib/module/utils/common.js.map +1 -1
- package/lib/module/utils/index.js +2 -0
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/normalizeStyles.web.js +13 -13
- package/lib/module/utils/normalizeStyles.web.js.map +1 -1
- package/lib/module/utils/normalizer.js +75 -0
- package/lib/module/utils/normalizer.js.map +1 -0
- package/lib/module/utils/styles.js +1 -1
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/typescript/examples/expo/src/App.d.ts +3 -0
- package/lib/typescript/examples/expo/src/App.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts +3 -0
- package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts +3 -0
- package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/examples/Extreme.d.ts +7 -0
- package/lib/typescript/examples/expo/src/examples/Extreme.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts +3 -0
- package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/examples/Memoization.d.ts +3 -0
- package/lib/typescript/examples/expo/src/examples/Memoization.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/examples/Minimal.d.ts +3 -0
- package/lib/typescript/examples/expo/src/examples/Minimal.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts +3 -0
- package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/examples/Theme.d.ts +3 -0
- package/lib/typescript/examples/expo/src/examples/Theme.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/examples/index.d.ts +8 -0
- package/lib/typescript/examples/expo/src/examples/index.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/index.d.ts +2 -0
- package/lib/typescript/examples/expo/src/index.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts +8 -0
- package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/styles/index.d.ts +46 -0
- package/lib/typescript/examples/expo/src/styles/index.d.ts.map +1 -0
- package/lib/typescript/examples/expo/src/styles/theme.d.ts +24 -0
- package/lib/typescript/examples/expo/src/styles/theme.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +1 -1
- package/lib/typescript/src/types/index.d.ts +1 -0
- package/lib/typescript/src/types/index.d.ts.map +1 -1
- package/lib/typescript/src/types/normalizer.d.ts +20 -0
- package/lib/typescript/src/types/normalizer.d.ts.map +1 -0
- package/lib/typescript/src/utils/common.d.ts +2 -1
- package/lib/typescript/src/utils/common.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +2 -0
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts +4 -1
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -1
- package/lib/typescript/src/utils/normalizer.d.ts +11 -0
- package/lib/typescript/src/utils/normalizer.d.ts.map +1 -0
- package/package.json +8 -3
- package/src/hooks/useDimensions.web.ts +3 -2
- package/src/types/index.ts +1 -0
- package/src/types/normalizer.ts +29 -0
- package/src/utils/common.ts +2 -1
- package/src/utils/index.ts +2 -0
- package/src/utils/normalizeStyles.web.ts +21 -42
- package/src/utils/normalizer.ts +105 -0
- package/src/utils/styles.ts +1 -1
@@ -1,29 +1,8 @@
|
|
1
1
|
import { warn } from './common'
|
2
|
+
import { preprocessor } from './normalizer'
|
3
|
+
import type { NormalizedBoxShadow, NormalizedTextShadow, BoxShadow, TextShadow, Transforms } from '../types'
|
2
4
|
|
3
|
-
const
|
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 => {
|
5
|
+
const normalizeBoxShadow = <T extends BoxShadow>(style: T): NormalizedBoxShadow => {
|
27
6
|
const requiredBoxShadowProperties = [
|
28
7
|
'shadowColor',
|
29
8
|
'shadowOffset',
|
@@ -31,7 +10,7 @@ const normalizeBoxShadow = <T extends {}>(styles: T): NormalizedBoxShadow => {
|
|
31
10
|
'shadowRadius'
|
32
11
|
]
|
33
12
|
|
34
|
-
if (!requiredBoxShadowProperties.every(prop => prop in
|
13
|
+
if (!requiredBoxShadowProperties.every(prop => prop in style)) {
|
35
14
|
warn(`can't apply box shadow as you miss at least one of these properties: ${requiredBoxShadowProperties.join(', ')}`)
|
36
15
|
|
37
16
|
return {
|
@@ -43,7 +22,7 @@ const normalizeBoxShadow = <T extends {}>(styles: T): NormalizedBoxShadow => {
|
|
43
22
|
}
|
44
23
|
|
45
24
|
return {
|
46
|
-
boxShadow: preprocessor.createBoxShadowValue(
|
25
|
+
boxShadow: preprocessor.createBoxShadowValue(style),
|
47
26
|
shadowColor: undefined,
|
48
27
|
shadowOffset: undefined,
|
49
28
|
shadowOpacity: undefined,
|
@@ -51,14 +30,14 @@ const normalizeBoxShadow = <T extends {}>(styles: T): NormalizedBoxShadow => {
|
|
51
30
|
}
|
52
31
|
}
|
53
32
|
|
54
|
-
const normalizeTextShadow = <T extends
|
33
|
+
const normalizeTextShadow = <T extends TextShadow>(style: T): NormalizedTextShadow => {
|
55
34
|
const requiredTextShadowProperties = [
|
56
35
|
'textShadowColor',
|
57
36
|
'textShadowOffset',
|
58
37
|
'textShadowRadius'
|
59
38
|
]
|
60
39
|
|
61
|
-
if (!requiredTextShadowProperties.every(prop => prop in
|
40
|
+
if (!requiredTextShadowProperties.every(prop => prop in style)) {
|
62
41
|
warn(`can't apply text shadow as you miss at least one of these properties: ${requiredTextShadowProperties.join(', ')}`)
|
63
42
|
|
64
43
|
return {
|
@@ -69,33 +48,33 @@ const normalizeTextShadow = <T extends {}>(styles: T): NormalizedTextShadow => {
|
|
69
48
|
}
|
70
49
|
|
71
50
|
return {
|
72
|
-
textShadow: preprocessor.createTextShadowValue(
|
51
|
+
textShadow: preprocessor.createTextShadowValue(style),
|
73
52
|
textShadowColor: undefined,
|
74
53
|
textShadowOffset: undefined,
|
75
54
|
textShadowRadius: undefined
|
76
55
|
}
|
77
56
|
}
|
78
57
|
|
79
|
-
export const normalizeStyles = <T extends {}>(
|
80
|
-
const normalizedTransform = ('transform' in
|
81
|
-
? { transform: preprocessor.createTransformValue(
|
58
|
+
export const normalizeStyles = <T extends BoxShadow | TextShadow | { transform: Transforms }>(style: T): T => {
|
59
|
+
const normalizedTransform = ('transform' in style && Array.isArray(style.transform))
|
60
|
+
? { transform: preprocessor.createTransformValue(style.transform) }
|
82
61
|
: {}
|
83
62
|
|
84
63
|
const normalizedBoxShadow = (
|
85
|
-
'shadowColor' in
|
86
|
-
'shadowOffset' in
|
87
|
-
'shadowOpacity' in
|
88
|
-
'shadowRadius' in
|
89
|
-
) ? normalizeBoxShadow(
|
64
|
+
'shadowColor' in style ||
|
65
|
+
'shadowOffset' in style ||
|
66
|
+
'shadowOpacity' in style ||
|
67
|
+
'shadowRadius' in style
|
68
|
+
) ? normalizeBoxShadow(style as BoxShadow) : {}
|
90
69
|
|
91
70
|
const normalizedTextShadow = (
|
92
|
-
'textShadowColor' in
|
93
|
-
'textShadowOffset' in
|
94
|
-
'textShadowRadius' in
|
95
|
-
) ? normalizeTextShadow(
|
71
|
+
'textShadowColor' in style ||
|
72
|
+
'textShadowOffset' in style ||
|
73
|
+
'textShadowRadius' in style
|
74
|
+
) ? normalizeTextShadow(style as TextShadow) : {}
|
96
75
|
|
97
76
|
return {
|
98
|
-
...
|
77
|
+
...style,
|
99
78
|
...normalizedTransform,
|
100
79
|
...normalizedBoxShadow,
|
101
80
|
...normalizedTextShadow
|
@@ -0,0 +1,105 @@
|
|
1
|
+
// based on react-native-web normalizer
|
2
|
+
// https://github.com/necolas/react-native-web
|
3
|
+
import type { TextShadow, Transforms, BoxShadow } from '../types'
|
4
|
+
|
5
|
+
type Preprocessor = {
|
6
|
+
createTextShadowValue(style: TextShadow): string,
|
7
|
+
createBoxShadowValue(style: Required<BoxShadow>): string,
|
8
|
+
createTransformValue(transforms: Required<Transforms>): string,
|
9
|
+
}
|
10
|
+
|
11
|
+
// for now supports
|
12
|
+
// hex colors (3, 6, 8) chars
|
13
|
+
// colors like orange red etc.
|
14
|
+
export const normalizeColor = (color: string, opacity: number = 1) => {
|
15
|
+
if (!color.startsWith('#')) {
|
16
|
+
return color
|
17
|
+
}
|
18
|
+
|
19
|
+
if (color.length === 9) {
|
20
|
+
const [r, g, b, a] = color
|
21
|
+
.slice(1)
|
22
|
+
.split(/(?=(?:..)*$)/)
|
23
|
+
.map(x => parseInt(x, 16))
|
24
|
+
.filter(num => !isNaN(num))
|
25
|
+
|
26
|
+
return `rgba(${r},${g},${b},${(a as number) / 255})`
|
27
|
+
}
|
28
|
+
|
29
|
+
const sanitizedHex = color.length === 4
|
30
|
+
? color
|
31
|
+
.slice(1)
|
32
|
+
.split('')
|
33
|
+
.map(char => `${char}${char}`)
|
34
|
+
.join('')
|
35
|
+
: color.slice(1)
|
36
|
+
|
37
|
+
return sanitizedHex
|
38
|
+
.split(/(?=(?:..)*$)/)
|
39
|
+
.map(x => parseInt(x, 16))
|
40
|
+
.filter(num => !isNaN(num))
|
41
|
+
.reduce((acc, color) => `${acc}${color},`, 'rgba(')
|
42
|
+
.concat(`${opacity})`)
|
43
|
+
}
|
44
|
+
|
45
|
+
export const normalizeNumericValue = (value: number) => value ? `${value}px` : value
|
46
|
+
const normalizeTransform = (key: string, value: number | string) => {
|
47
|
+
if (key.includes('scale')) {
|
48
|
+
return value
|
49
|
+
}
|
50
|
+
|
51
|
+
if (typeof value === 'number') {
|
52
|
+
return normalizeNumericValue(value)
|
53
|
+
}
|
54
|
+
|
55
|
+
return value
|
56
|
+
}
|
57
|
+
|
58
|
+
const createTextShadowValue = (style: TextShadow) => {
|
59
|
+
// at this point every prop is present
|
60
|
+
const { textShadowColor, textShadowOffset, textShadowRadius } = style
|
61
|
+
const offsetX = normalizeNumericValue(textShadowOffset.width)
|
62
|
+
const offsetY = normalizeNumericValue(textShadowOffset.height)
|
63
|
+
const radius = normalizeNumericValue(textShadowRadius)
|
64
|
+
const color = normalizeColor(textShadowColor as string)
|
65
|
+
|
66
|
+
return `${offsetX} ${offsetY} ${radius} ${color}`
|
67
|
+
}
|
68
|
+
|
69
|
+
const createBoxShadowValue = (style: BoxShadow) => {
|
70
|
+
// at this point every prop is present
|
71
|
+
const { shadowColor, shadowOffset, shadowOpacity, shadowRadius } = style
|
72
|
+
const offsetX = normalizeNumericValue(shadowOffset.width)
|
73
|
+
const offsetY = normalizeNumericValue(shadowOffset.height)
|
74
|
+
const radius = normalizeNumericValue(shadowRadius)
|
75
|
+
const color = normalizeColor(shadowColor as string, shadowOpacity as number)
|
76
|
+
|
77
|
+
return `${offsetX} ${offsetY} ${radius} ${color}`
|
78
|
+
}
|
79
|
+
|
80
|
+
const createTransformValue = (transforms: Transforms) => transforms
|
81
|
+
.map(transform => {
|
82
|
+
const [key] = Object.keys(transform)
|
83
|
+
|
84
|
+
if (!key) {
|
85
|
+
return undefined
|
86
|
+
}
|
87
|
+
|
88
|
+
const value = transform[key as keyof typeof transform]
|
89
|
+
|
90
|
+
switch(key) {
|
91
|
+
case 'matrix':
|
92
|
+
case 'matrix3d':
|
93
|
+
return `${key}(${(value as Array<number>).join(',')})`
|
94
|
+
default:
|
95
|
+
return `${key}(${normalizeTransform(key, value)})`
|
96
|
+
}
|
97
|
+
})
|
98
|
+
.filter(Boolean)
|
99
|
+
.join(' ')
|
100
|
+
|
101
|
+
export const preprocessor: Preprocessor = {
|
102
|
+
createTextShadowValue,
|
103
|
+
createBoxShadowValue,
|
104
|
+
createTransformValue
|
105
|
+
}
|