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.
Files changed (85) 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/normalizeStyles.web.js +13 -13
  15. package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -1
  16. package/lib/commonjs/utils/normalizer.js +83 -0
  17. package/lib/commonjs/utils/normalizer.js.map +1 -0
  18. package/lib/commonjs/utils/styles.js +1 -1
  19. package/lib/commonjs/utils/styles.js.map +1 -1
  20. package/lib/module/hooks/useDimensions.web.js +3 -2
  21. package/lib/module/hooks/useDimensions.web.js.map +1 -1
  22. package/lib/module/types/index.js +1 -1
  23. package/lib/module/types/index.js.map +1 -1
  24. package/lib/module/types/normalizer.js +2 -0
  25. package/lib/module/types/normalizer.js.map +1 -0
  26. package/lib/module/utils/common.js +2 -1
  27. package/lib/module/utils/common.js.map +1 -1
  28. package/lib/module/utils/index.js +2 -0
  29. package/lib/module/utils/index.js.map +1 -1
  30. package/lib/module/utils/normalizeStyles.web.js +13 -13
  31. package/lib/module/utils/normalizeStyles.web.js.map +1 -1
  32. package/lib/module/utils/normalizer.js +75 -0
  33. package/lib/module/utils/normalizer.js.map +1 -0
  34. package/lib/module/utils/styles.js +1 -1
  35. package/lib/module/utils/styles.js.map +1 -1
  36. package/lib/typescript/examples/expo/src/App.d.ts +3 -0
  37. package/lib/typescript/examples/expo/src/App.d.ts.map +1 -0
  38. package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts +3 -0
  39. package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts.map +1 -0
  40. package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts +3 -0
  41. package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts.map +1 -0
  42. package/lib/typescript/examples/expo/src/examples/Extreme.d.ts +7 -0
  43. package/lib/typescript/examples/expo/src/examples/Extreme.d.ts.map +1 -0
  44. package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts +3 -0
  45. package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts.map +1 -0
  46. package/lib/typescript/examples/expo/src/examples/Memoization.d.ts +3 -0
  47. package/lib/typescript/examples/expo/src/examples/Memoization.d.ts.map +1 -0
  48. package/lib/typescript/examples/expo/src/examples/Minimal.d.ts +3 -0
  49. package/lib/typescript/examples/expo/src/examples/Minimal.d.ts.map +1 -0
  50. package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts +3 -0
  51. package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts.map +1 -0
  52. package/lib/typescript/examples/expo/src/examples/Theme.d.ts +3 -0
  53. package/lib/typescript/examples/expo/src/examples/Theme.d.ts.map +1 -0
  54. package/lib/typescript/examples/expo/src/examples/index.d.ts +8 -0
  55. package/lib/typescript/examples/expo/src/examples/index.d.ts.map +1 -0
  56. package/lib/typescript/examples/expo/src/index.d.ts +2 -0
  57. package/lib/typescript/examples/expo/src/index.d.ts.map +1 -0
  58. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts +8 -0
  59. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts.map +1 -0
  60. package/lib/typescript/examples/expo/src/styles/index.d.ts +46 -0
  61. package/lib/typescript/examples/expo/src/styles/index.d.ts.map +1 -0
  62. package/lib/typescript/examples/expo/src/styles/theme.d.ts +24 -0
  63. package/lib/typescript/examples/expo/src/styles/theme.d.ts.map +1 -0
  64. package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +1 -1
  65. package/lib/typescript/src/types/index.d.ts +1 -0
  66. package/lib/typescript/src/types/index.d.ts.map +1 -1
  67. package/lib/typescript/src/types/normalizer.d.ts +20 -0
  68. package/lib/typescript/src/types/normalizer.d.ts.map +1 -0
  69. package/lib/typescript/src/utils/common.d.ts +2 -1
  70. package/lib/typescript/src/utils/common.d.ts.map +1 -1
  71. package/lib/typescript/src/utils/index.d.ts +2 -0
  72. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  73. package/lib/typescript/src/utils/normalizeStyles.web.d.ts +4 -1
  74. package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -1
  75. package/lib/typescript/src/utils/normalizer.d.ts +11 -0
  76. package/lib/typescript/src/utils/normalizer.d.ts.map +1 -0
  77. package/package.json +8 -3
  78. package/src/hooks/useDimensions.web.ts +3 -2
  79. package/src/types/index.ts +1 -0
  80. package/src/types/normalizer.ts +29 -0
  81. package/src/utils/common.ts +2 -1
  82. package/src/utils/index.ts +2 -0
  83. package/src/utils/normalizeStyles.web.ts +21 -42
  84. package/src/utils/normalizer.ts +105 -0
  85. 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,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
+ }
@@ -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
  }