react-native-unistyles 1.0.0 → 1.1.1
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/module.d.js +2 -0
- package/lib/commonjs/utils/module.d.js.map +1 -0
- 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 +89 -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/module.d.js +2 -0
- package/lib/module/utils/module.d.js.map +1 -0
- 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 +79 -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 +10 -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/module.d.ts +3 -0
- package/src/utils/normalizeStyles.web.ts +21 -42
- package/src/utils/normalizer.ts +99 -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,99 @@
|
|
1
|
+
// based on react-native-web normalizer
|
2
|
+
// https://github.com/necolas/react-native-web
|
3
|
+
import normalizeColors from '@react-native/normalize-colors'
|
4
|
+
import type { TextShadow, Transforms, BoxShadow } from '../types'
|
5
|
+
|
6
|
+
type Preprocessor = {
|
7
|
+
createTextShadowValue(style: TextShadow): string,
|
8
|
+
createBoxShadowValue(style: Required<BoxShadow>): string,
|
9
|
+
createTransformValue(transforms: Required<Transforms>): string,
|
10
|
+
}
|
11
|
+
|
12
|
+
export const normalizeColor = (color: string, opacity: number = 1) => {
|
13
|
+
// If the opacity is 1 there's no need to normalize the color
|
14
|
+
if (opacity === 1) {
|
15
|
+
return color
|
16
|
+
}
|
17
|
+
|
18
|
+
const integer = normalizeColors(color) as number | null
|
19
|
+
|
20
|
+
// If the colour is an unknown format, the return value is null
|
21
|
+
if (integer === null) {
|
22
|
+
return color
|
23
|
+
}
|
24
|
+
|
25
|
+
const hex = integer.toString(16).padStart(8, '0')
|
26
|
+
|
27
|
+
if (hex.length === 8) {
|
28
|
+
const [r = 0, g = 0, b = 0, a = 1] = hex
|
29
|
+
.split(/(?=(?:..)*$)/)
|
30
|
+
.map(x => parseInt(x, 16))
|
31
|
+
.filter(num => !isNaN(num))
|
32
|
+
|
33
|
+
return `rgba(${r},${g},${b},${((a as number) / 255) * opacity})`
|
34
|
+
}
|
35
|
+
|
36
|
+
return color
|
37
|
+
}
|
38
|
+
|
39
|
+
export const normalizeNumericValue = (value: number) => value ? `${value}px` : value
|
40
|
+
const normalizeTransform = (key: string, value: number | string) => {
|
41
|
+
if (key.includes('scale')) {
|
42
|
+
return value
|
43
|
+
}
|
44
|
+
|
45
|
+
if (typeof value === 'number') {
|
46
|
+
return normalizeNumericValue(value)
|
47
|
+
}
|
48
|
+
|
49
|
+
return value
|
50
|
+
}
|
51
|
+
|
52
|
+
const createTextShadowValue = (style: TextShadow) => {
|
53
|
+
// at this point every prop is present
|
54
|
+
const { textShadowColor, textShadowOffset, textShadowRadius } = style
|
55
|
+
const offsetX = normalizeNumericValue(textShadowOffset.width)
|
56
|
+
const offsetY = normalizeNumericValue(textShadowOffset.height)
|
57
|
+
const radius = normalizeNumericValue(textShadowRadius)
|
58
|
+
const color = normalizeColor(textShadowColor as string)
|
59
|
+
|
60
|
+
return `${offsetX} ${offsetY} ${radius} ${color}`
|
61
|
+
}
|
62
|
+
|
63
|
+
const createBoxShadowValue = (style: BoxShadow) => {
|
64
|
+
// at this point every prop is present
|
65
|
+
const { shadowColor, shadowOffset, shadowOpacity, shadowRadius } = style
|
66
|
+
const offsetX = normalizeNumericValue(shadowOffset.width)
|
67
|
+
const offsetY = normalizeNumericValue(shadowOffset.height)
|
68
|
+
const radius = normalizeNumericValue(shadowRadius)
|
69
|
+
const color = normalizeColor(shadowColor as string, shadowOpacity as number)
|
70
|
+
|
71
|
+
return `${offsetX} ${offsetY} ${radius} ${color}`
|
72
|
+
}
|
73
|
+
|
74
|
+
const createTransformValue = (transforms: Transforms) => transforms
|
75
|
+
.map(transform => {
|
76
|
+
const [key] = Object.keys(transform)
|
77
|
+
|
78
|
+
if (!key) {
|
79
|
+
return undefined
|
80
|
+
}
|
81
|
+
|
82
|
+
const value = transform[key as keyof typeof transform]
|
83
|
+
|
84
|
+
switch(key) {
|
85
|
+
case 'matrix':
|
86
|
+
case 'matrix3d':
|
87
|
+
return `${key}(${(value as Array<number>).join(',')})`
|
88
|
+
default:
|
89
|
+
return `${key}(${normalizeTransform(key, value)})`
|
90
|
+
}
|
91
|
+
})
|
92
|
+
.filter(Boolean)
|
93
|
+
.join(' ')
|
94
|
+
|
95
|
+
export const preprocessor: Preprocessor = {
|
96
|
+
createTextShadowValue,
|
97
|
+
createBoxShadowValue,
|
98
|
+
createTransformValue
|
99
|
+
}
|