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.
Files changed (90) hide show
  1. package/README.md +16 -3
  2. package/lib/commonjs/UnistylesTheme.js +2 -2
  3. package/lib/commonjs/UnistylesTheme.js.map +1 -1
  4. package/lib/commonjs/hooks/useDimensions.web.js +3 -2
  5. package/lib/commonjs/hooks/useDimensions.web.js.map +1 -1
  6. package/lib/commonjs/types/index.js +15 -0
  7. package/lib/commonjs/types/index.js.map +1 -1
  8. package/lib/commonjs/types/normalizer.js +6 -0
  9. package/lib/commonjs/types/normalizer.js.map +1 -0
  10. package/lib/commonjs/utils/common.js +3 -3
  11. package/lib/commonjs/utils/common.js.map +1 -1
  12. package/lib/commonjs/utils/index.js +34 -0
  13. package/lib/commonjs/utils/index.js.map +1 -1
  14. package/lib/commonjs/utils/module.d.js +2 -0
  15. package/lib/commonjs/utils/module.d.js.map +1 -0
  16. package/lib/commonjs/utils/normalizeStyles.web.js +13 -13
  17. package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -1
  18. package/lib/commonjs/utils/normalizer.js +89 -0
  19. package/lib/commonjs/utils/normalizer.js.map +1 -0
  20. package/lib/commonjs/utils/styles.js +1 -1
  21. package/lib/commonjs/utils/styles.js.map +1 -1
  22. package/lib/module/hooks/useDimensions.web.js +3 -2
  23. package/lib/module/hooks/useDimensions.web.js.map +1 -1
  24. package/lib/module/types/index.js +1 -1
  25. package/lib/module/types/index.js.map +1 -1
  26. package/lib/module/types/normalizer.js +2 -0
  27. package/lib/module/types/normalizer.js.map +1 -0
  28. package/lib/module/utils/common.js +2 -1
  29. package/lib/module/utils/common.js.map +1 -1
  30. package/lib/module/utils/index.js +2 -0
  31. package/lib/module/utils/index.js.map +1 -1
  32. package/lib/module/utils/module.d.js +2 -0
  33. package/lib/module/utils/module.d.js.map +1 -0
  34. package/lib/module/utils/normalizeStyles.web.js +13 -13
  35. package/lib/module/utils/normalizeStyles.web.js.map +1 -1
  36. package/lib/module/utils/normalizer.js +79 -0
  37. package/lib/module/utils/normalizer.js.map +1 -0
  38. package/lib/module/utils/styles.js +1 -1
  39. package/lib/module/utils/styles.js.map +1 -1
  40. package/lib/typescript/examples/expo/src/App.d.ts +3 -0
  41. package/lib/typescript/examples/expo/src/App.d.ts.map +1 -0
  42. package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts +3 -0
  43. package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts.map +1 -0
  44. package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts +3 -0
  45. package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts.map +1 -0
  46. package/lib/typescript/examples/expo/src/examples/Extreme.d.ts +7 -0
  47. package/lib/typescript/examples/expo/src/examples/Extreme.d.ts.map +1 -0
  48. package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts +3 -0
  49. package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts.map +1 -0
  50. package/lib/typescript/examples/expo/src/examples/Memoization.d.ts +3 -0
  51. package/lib/typescript/examples/expo/src/examples/Memoization.d.ts.map +1 -0
  52. package/lib/typescript/examples/expo/src/examples/Minimal.d.ts +3 -0
  53. package/lib/typescript/examples/expo/src/examples/Minimal.d.ts.map +1 -0
  54. package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts +3 -0
  55. package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts.map +1 -0
  56. package/lib/typescript/examples/expo/src/examples/Theme.d.ts +3 -0
  57. package/lib/typescript/examples/expo/src/examples/Theme.d.ts.map +1 -0
  58. package/lib/typescript/examples/expo/src/examples/index.d.ts +8 -0
  59. package/lib/typescript/examples/expo/src/examples/index.d.ts.map +1 -0
  60. package/lib/typescript/examples/expo/src/index.d.ts +2 -0
  61. package/lib/typescript/examples/expo/src/index.d.ts.map +1 -0
  62. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts +8 -0
  63. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts.map +1 -0
  64. package/lib/typescript/examples/expo/src/styles/index.d.ts +46 -0
  65. package/lib/typescript/examples/expo/src/styles/index.d.ts.map +1 -0
  66. package/lib/typescript/examples/expo/src/styles/theme.d.ts +24 -0
  67. package/lib/typescript/examples/expo/src/styles/theme.d.ts.map +1 -0
  68. package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +1 -1
  69. package/lib/typescript/src/types/index.d.ts +1 -0
  70. package/lib/typescript/src/types/index.d.ts.map +1 -1
  71. package/lib/typescript/src/types/normalizer.d.ts +20 -0
  72. package/lib/typescript/src/types/normalizer.d.ts.map +1 -0
  73. package/lib/typescript/src/utils/common.d.ts +2 -1
  74. package/lib/typescript/src/utils/common.d.ts.map +1 -1
  75. package/lib/typescript/src/utils/index.d.ts +2 -0
  76. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  77. package/lib/typescript/src/utils/normalizeStyles.web.d.ts +4 -1
  78. package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -1
  79. package/lib/typescript/src/utils/normalizer.d.ts +11 -0
  80. package/lib/typescript/src/utils/normalizer.d.ts.map +1 -0
  81. package/package.json +10 -3
  82. package/src/hooks/useDimensions.web.ts +3 -2
  83. package/src/types/index.ts +1 -0
  84. package/src/types/normalizer.ts +29 -0
  85. package/src/utils/common.ts +2 -1
  86. package/src/utils/index.ts +2 -0
  87. package/src/utils/module.d.ts +3 -0
  88. package/src/utils/normalizeStyles.web.ts +21 -42
  89. package/src/utils/normalizer.ts +99 -0
  90. 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 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 => {
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 styles)) {
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(styles),
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 {}>(styles: T): NormalizedTextShadow => {
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 styles)) {
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(styles),
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 {}>(styles: T): T => {
80
- const normalizedTransform = ('transform' in styles && Array.isArray(styles.transform))
81
- ? { transform: preprocessor.createTransformValue(styles.transform) }
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 styles ||
86
- 'shadowOffset' in styles ||
87
- 'shadowOpacity' in styles ||
88
- 'shadowRadius' in styles
89
- ) ? normalizeBoxShadow(styles) : {}
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 styles ||
93
- 'textShadowOffset' in styles ||
94
- 'textShadowRadius' in styles
95
- ) ? normalizeTextShadow(styles) : {}
71
+ 'textShadowColor' in style ||
72
+ 'textShadowOffset' in style ||
73
+ 'textShadowRadius' in style
74
+ ) ? normalizeTextShadow(style as TextShadow) : {}
96
75
 
97
76
  return {
98
- ...styles,
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
+ }
@@ -109,7 +109,7 @@ export const parseStyle = <T, B extends Breakpoints>(
109
109
  })
110
110
  )
111
111
 
112
- return isWeb()
112
+ return isWeb
113
113
  ? normalizeStyles(parsedStyles)
114
114
  : parsedStyles
115
115
  }