@builder.io/sdk-react-native 2.0.5 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/lib/browser/commonjs/components/content/content.js +6 -1
  2. package/lib/browser/commonjs/components/content/content.js.map +1 -1
  3. package/lib/browser/commonjs/components/content-variants/content-variants.js +3 -1
  4. package/lib/browser/commonjs/components/content-variants/content-variants.js.map +1 -1
  5. package/lib/browser/commonjs/components/content-variants/extra-framework-props-types.js +2 -0
  6. package/lib/browser/commonjs/components/content-variants/extra-framework-props-types.js.map +1 -0
  7. package/lib/browser/commonjs/constants/sdk-version.js +1 -1
  8. package/lib/browser/commonjs/context/extra-context-types.js +2 -0
  9. package/lib/browser/commonjs/context/extra-context-types.js.map +1 -0
  10. package/lib/browser/commonjs/functions/transform-style-property.js +168 -6
  11. package/lib/browser/commonjs/functions/transform-style-property.js.map +1 -1
  12. package/lib/browser/module/components/content/content.js +6 -1
  13. package/lib/browser/module/components/content/content.js.map +1 -1
  14. package/lib/browser/module/components/content-variants/content-variants.js +3 -1
  15. package/lib/browser/module/components/content-variants/content-variants.js.map +1 -1
  16. package/lib/browser/module/components/content-variants/extra-framework-props-types.js +2 -0
  17. package/lib/browser/module/components/content-variants/extra-framework-props-types.js.map +1 -0
  18. package/lib/browser/module/constants/sdk-version.js +1 -1
  19. package/lib/browser/module/context/extra-context-types.js +2 -0
  20. package/lib/browser/module/context/extra-context-types.js.map +1 -0
  21. package/lib/browser/module/functions/transform-style-property.js +168 -5
  22. package/lib/browser/module/functions/transform-style-property.js.map +1 -1
  23. package/lib/browser/typescript/components/content/content.d.ts.map +1 -1
  24. package/lib/browser/typescript/components/content-variants/content-variants.d.ts.map +1 -1
  25. package/lib/browser/typescript/components/content-variants/content-variants.types.d.ts +2 -1
  26. package/lib/browser/typescript/components/content-variants/content-variants.types.d.ts.map +1 -1
  27. package/lib/browser/typescript/components/content-variants/extra-framework-props-types.d.ts +7 -0
  28. package/lib/browser/typescript/components/content-variants/extra-framework-props-types.d.ts.map +1 -0
  29. package/lib/browser/typescript/constants/sdk-version.d.ts +1 -1
  30. package/lib/browser/typescript/context/extra-context-types.d.ts +4 -0
  31. package/lib/browser/typescript/context/extra-context-types.d.ts.map +1 -0
  32. package/lib/browser/typescript/context/types.d.ts +2 -1
  33. package/lib/browser/typescript/context/types.d.ts.map +1 -1
  34. package/lib/browser/typescript/functions/transform-style-property.d.ts.map +1 -1
  35. package/lib/edge/commonjs/components/content/content.js +6 -1
  36. package/lib/edge/commonjs/components/content/content.js.map +1 -1
  37. package/lib/edge/commonjs/components/content-variants/content-variants.js +3 -1
  38. package/lib/edge/commonjs/components/content-variants/content-variants.js.map +1 -1
  39. package/lib/edge/commonjs/components/content-variants/extra-framework-props-types.js +2 -0
  40. package/lib/edge/commonjs/components/content-variants/extra-framework-props-types.js.map +1 -0
  41. package/lib/edge/commonjs/constants/sdk-version.js +1 -1
  42. package/lib/edge/commonjs/context/extra-context-types.js +2 -0
  43. package/lib/edge/commonjs/context/extra-context-types.js.map +1 -0
  44. package/lib/edge/commonjs/functions/transform-style-property.js +168 -6
  45. package/lib/edge/commonjs/functions/transform-style-property.js.map +1 -1
  46. package/lib/edge/module/components/content/content.js +6 -1
  47. package/lib/edge/module/components/content/content.js.map +1 -1
  48. package/lib/edge/module/components/content-variants/content-variants.js +3 -1
  49. package/lib/edge/module/components/content-variants/content-variants.js.map +1 -1
  50. package/lib/edge/module/components/content-variants/extra-framework-props-types.js +2 -0
  51. package/lib/edge/module/components/content-variants/extra-framework-props-types.js.map +1 -0
  52. package/lib/edge/module/constants/sdk-version.js +1 -1
  53. package/lib/edge/module/context/extra-context-types.js +2 -0
  54. package/lib/edge/module/context/extra-context-types.js.map +1 -0
  55. package/lib/edge/module/functions/transform-style-property.js +168 -5
  56. package/lib/edge/module/functions/transform-style-property.js.map +1 -1
  57. package/lib/edge/typescript/components/content/content.d.ts.map +1 -1
  58. package/lib/edge/typescript/components/content-variants/content-variants.d.ts.map +1 -1
  59. package/lib/edge/typescript/components/content-variants/content-variants.types.d.ts +2 -1
  60. package/lib/edge/typescript/components/content-variants/content-variants.types.d.ts.map +1 -1
  61. package/lib/edge/typescript/components/content-variants/extra-framework-props-types.d.ts +7 -0
  62. package/lib/edge/typescript/components/content-variants/extra-framework-props-types.d.ts.map +1 -0
  63. package/lib/edge/typescript/constants/sdk-version.d.ts +1 -1
  64. package/lib/edge/typescript/context/extra-context-types.d.ts +4 -0
  65. package/lib/edge/typescript/context/extra-context-types.d.ts.map +1 -0
  66. package/lib/edge/typescript/context/types.d.ts +2 -1
  67. package/lib/edge/typescript/context/types.d.ts.map +1 -1
  68. package/lib/edge/typescript/functions/transform-style-property.d.ts.map +1 -1
  69. package/lib/node/commonjs/components/content/content.js +6 -1
  70. package/lib/node/commonjs/components/content/content.js.map +1 -1
  71. package/lib/node/commonjs/components/content-variants/content-variants.js +3 -1
  72. package/lib/node/commonjs/components/content-variants/content-variants.js.map +1 -1
  73. package/lib/node/commonjs/components/content-variants/extra-framework-props-types.js +2 -0
  74. package/lib/node/commonjs/components/content-variants/extra-framework-props-types.js.map +1 -0
  75. package/lib/node/commonjs/constants/sdk-version.js +1 -1
  76. package/lib/node/commonjs/context/extra-context-types.js +2 -0
  77. package/lib/node/commonjs/context/extra-context-types.js.map +1 -0
  78. package/lib/node/commonjs/functions/transform-style-property.js +168 -6
  79. package/lib/node/commonjs/functions/transform-style-property.js.map +1 -1
  80. package/lib/node/module/components/content/content.js +6 -1
  81. package/lib/node/module/components/content/content.js.map +1 -1
  82. package/lib/node/module/components/content-variants/content-variants.js +3 -1
  83. package/lib/node/module/components/content-variants/content-variants.js.map +1 -1
  84. package/lib/node/module/components/content-variants/extra-framework-props-types.js +2 -0
  85. package/lib/node/module/components/content-variants/extra-framework-props-types.js.map +1 -0
  86. package/lib/node/module/constants/sdk-version.js +1 -1
  87. package/lib/node/module/context/extra-context-types.js +2 -0
  88. package/lib/node/module/context/extra-context-types.js.map +1 -0
  89. package/lib/node/module/functions/transform-style-property.js +168 -5
  90. package/lib/node/module/functions/transform-style-property.js.map +1 -1
  91. package/lib/node/typescript/components/content/content.d.ts.map +1 -1
  92. package/lib/node/typescript/components/content-variants/content-variants.d.ts.map +1 -1
  93. package/lib/node/typescript/components/content-variants/content-variants.types.d.ts +2 -1
  94. package/lib/node/typescript/components/content-variants/content-variants.types.d.ts.map +1 -1
  95. package/lib/node/typescript/components/content-variants/extra-framework-props-types.d.ts +7 -0
  96. package/lib/node/typescript/components/content-variants/extra-framework-props-types.d.ts.map +1 -0
  97. package/lib/node/typescript/constants/sdk-version.d.ts +1 -1
  98. package/lib/node/typescript/context/extra-context-types.d.ts +4 -0
  99. package/lib/node/typescript/context/extra-context-types.d.ts.map +1 -0
  100. package/lib/node/typescript/context/types.d.ts +2 -1
  101. package/lib/node/typescript/context/types.d.ts.map +1 -1
  102. package/lib/node/typescript/functions/transform-style-property.d.ts.map +1 -1
  103. package/package.json +1 -1
  104. package/src/components/content/content.tsx +5 -0
  105. package/src/components/content-variants/content-variants.tsx +6 -1
  106. package/src/components/content-variants/content-variants.types.ts +2 -1
  107. package/src/components/content-variants/extra-framework-props-types.ts +6 -0
  108. package/src/constants/sdk-version.ts +1 -1
  109. package/src/context/extra-context-types.ts +3 -0
  110. package/src/context/types.ts +2 -1
  111. package/src/functions/transform-style-property.ts +252 -14
@@ -1,14 +1,225 @@
1
- import cssToStyleSheet from 'css-to-react-native';
1
+ import { getPropertyName, getStylesForProperty } from 'css-to-react-native';
2
2
  import type { ImageStyle, TextStyle, ViewStyle } from 'react-native';
3
3
  import type { BuilderContextInterface } from '../context/types';
4
4
  import type { BuilderBlock } from '../types/builder-block';
5
5
  import type { Dictionary } from '../types/typescript';
6
6
  import { extractCssVarDefaultValue } from './extract-css-var-default-value';
7
7
 
8
- const cssToReactNative: typeof cssToStyleSheet = (cssToStyleSheet as any)
9
- .default
10
- ? (cssToStyleSheet as any).default
11
- : cssToStyleSheet;
8
+ function extractVarValue(value: string): string {
9
+ // Regular expression to find var() expressions
10
+ const varRegex = /var\(--[^,]+?,\s*([^)]+)\)/;
11
+
12
+ // Function to replace var() with its fallback
13
+ let newValue = value;
14
+ let match;
15
+ while ((match = newValue.match(varRegex))) {
16
+ newValue = newValue.replace(match[0], match[1].trim());
17
+ }
18
+
19
+ return newValue;
20
+ }
21
+ // Common regex patterns
22
+ const numberPattern = /^-?\d+(\.\d+)?$/;
23
+ const lengthPattern = /^-?(\d+)(px|%)?$/;
24
+ const pixelPattern = /^-?(\d+)(px)?$/;
25
+ const colorPattern =
26
+ /^(#[0-9A-Fa-f]{6}|#[0-9A-Fa-f]{3}|rgb\(\d{1,3},\s?\d{1,3},\s?\d{1,3}\)|rgba\(\d{1,3},\s?\d{1,3},\s?\d{1,3},\s?([01]|0?\.\d+)\))$/;
27
+ const offsetPattern = /^\{width:\s?-?\d+(px)?,\s?height:\s?-?\d+(px)?\}$/;
28
+
29
+ type CSSPropertyValidator = (value: string) => boolean;
30
+
31
+ const cssProperties: Record<string, CSSPropertyValidator> = {
32
+ // Ignore this, we add it to elements but this is the behavior already in
33
+ // React Native and ignored by it
34
+ boxSizing: () => true,
35
+ // Layout Properties
36
+ width: (value: string) =>
37
+ lengthPattern.test(value) || ['auto', 'fit-content'].includes(value),
38
+ height: (value: string) =>
39
+ lengthPattern.test(value) || ['auto', 'fit-content'].includes(value),
40
+ minWidth: (value: string) => lengthPattern.test(value) || value === 'auto',
41
+ maxWidth: (value: string) => lengthPattern.test(value) || value === 'auto',
42
+ minHeight: (value: string) => lengthPattern.test(value) || value === 'auto',
43
+ maxHeight: (value: string) => lengthPattern.test(value) || value === 'auto',
44
+ aspectRatio: (value: string) =>
45
+ numberPattern.test(value) || /^\d+\/\d+$/.test(value),
46
+
47
+ // Flexbox Properties
48
+ flex: (value: string) => numberPattern.test(value),
49
+ flexBasis: (value: string) => lengthPattern.test(value) || value === 'auto',
50
+ flexDirection: (value: string) =>
51
+ ['row', 'row-reverse', 'column', 'column-reverse'].includes(value),
52
+ flexGrow: (value: string) => numberPattern.test(value),
53
+ flexShrink: (value: string) => numberPattern.test(value),
54
+ flexWrap: (value: string) =>
55
+ ['wrap', 'nowrap', 'wrap-reverse'].includes(value),
56
+
57
+ // Alignment Properties
58
+ alignContent: (value: string) =>
59
+ [
60
+ 'flex-start',
61
+ 'flex-end',
62
+ 'center',
63
+ 'stretch',
64
+ 'space-between',
65
+ 'space-around',
66
+ ].includes(value),
67
+ alignItems: (value: string) =>
68
+ ['flex-start', 'flex-end', 'center', 'stretch', 'baseline'].includes(value),
69
+ alignSelf: (value: string) =>
70
+ [
71
+ 'auto',
72
+ 'flex-start',
73
+ 'flex-end',
74
+ 'center',
75
+ 'stretch',
76
+ 'baseline',
77
+ ].includes(value),
78
+ justifyContent: (value: string) =>
79
+ [
80
+ 'flex-start',
81
+ 'flex-end',
82
+ 'center',
83
+ 'space-between',
84
+ 'space-around',
85
+ 'space-evenly',
86
+ ].includes(value),
87
+
88
+ // Positioning Properties
89
+ position: (value: string) => ['absolute', 'relative'].includes(value),
90
+ top: (value: string) => lengthPattern.test(value) || value === 'auto',
91
+ right: (value: string) => lengthPattern.test(value) || value === 'auto',
92
+ bottom: (value: string) => lengthPattern.test(value) || value === 'auto',
93
+ left: (value: string) => lengthPattern.test(value) || value === 'auto',
94
+ zIndex: (value: string) => /^-?\d+$/.test(value),
95
+
96
+ // Margin and Padding Properties
97
+ margin: (value: string) => lengthPattern.test(value) || value === 'auto',
98
+ marginTop: (value: string) => lengthPattern.test(value) || value === 'auto',
99
+ marginRight: (value: string) => lengthPattern.test(value) || value === 'auto',
100
+ marginBottom: (value: string) =>
101
+ lengthPattern.test(value) || value === 'auto',
102
+ marginLeft: (value: string) => lengthPattern.test(value) || value === 'auto',
103
+ marginHorizontal: (value: string) =>
104
+ lengthPattern.test(value) || value === 'auto',
105
+ marginVertical: (value: string) =>
106
+ lengthPattern.test(value) || value === 'auto',
107
+ padding: (value: string) => lengthPattern.test(value),
108
+ paddingTop: (value: string) => lengthPattern.test(value),
109
+ paddingRight: (value: string) => lengthPattern.test(value),
110
+ paddingBottom: (value: string) => lengthPattern.test(value),
111
+ paddingLeft: (value: string) => lengthPattern.test(value),
112
+ paddingHorizontal: (value: string) => lengthPattern.test(value),
113
+ paddingVertical: (value: string) => lengthPattern.test(value),
114
+
115
+ // Border Properties
116
+ borderStyle: (value: string) =>
117
+ ['solid', 'dotted', 'dashed', 'none'].includes(value),
118
+ borderWidth: (value: string) => pixelPattern.test(value),
119
+ borderTopWidth: (value: string) => pixelPattern.test(value),
120
+ borderRightWidth: (value: string) => pixelPattern.test(value),
121
+ borderBottomWidth: (value: string) => pixelPattern.test(value),
122
+ borderLeftWidth: (value: string) => pixelPattern.test(value),
123
+ borderColor: (value: string) => colorPattern.test(value),
124
+ borderTopColor: (value: string) => colorPattern.test(value),
125
+ borderRightColor: (value: string) => colorPattern.test(value),
126
+ borderBottomColor: (value: string) => colorPattern.test(value),
127
+ borderLeftColor: (value: string) => colorPattern.test(value),
128
+ borderRadius: (value: string) => pixelPattern.test(value),
129
+ borderTopLeftRadius: (value: string) => pixelPattern.test(value),
130
+ borderTopRightRadius: (value: string) => pixelPattern.test(value),
131
+ borderBottomLeftRadius: (value: string) => pixelPattern.test(value),
132
+ borderBottomRightRadius: (value: string) => pixelPattern.test(value),
133
+
134
+ // Background Properties
135
+ backgroundColor: (value: string) => colorPattern.test(value),
136
+ opacity: (value: string) => /^([01]|0?\.\d+)$/.test(value),
137
+
138
+ // Text Properties
139
+ color: (value: string) => colorPattern.test(value),
140
+ fontFamily: () => true, // Any string is valid
141
+ fontSize: (value: string) => pixelPattern.test(value),
142
+ fontStyle: (value: string) => ['normal', 'italic'].includes(value),
143
+ fontWeight: (value: string) =>
144
+ [
145
+ 'normal',
146
+ 'bold',
147
+ '100',
148
+ '200',
149
+ '300',
150
+ '400',
151
+ '500',
152
+ '600',
153
+ '700',
154
+ '800',
155
+ '900',
156
+ ].includes(value),
157
+ letterSpacing: (value: string) => pixelPattern.test(value),
158
+ lineHeight: (value: string) =>
159
+ numberPattern.test(value) || pixelPattern.test(value),
160
+ textAlign: (value: string) =>
161
+ ['auto', 'left', 'right', 'center', 'justify'].includes(value),
162
+ textDecorationLine: (value: string) =>
163
+ ['none', 'underline', 'line-through', 'underline line-through'].includes(
164
+ value
165
+ ),
166
+ textDecorationStyle: (value: string) =>
167
+ ['solid', 'double', 'dotted', 'dashed'].includes(value),
168
+ textDecorationColor: (value: string) => colorPattern.test(value),
169
+ textShadowColor: (value: string) => colorPattern.test(value),
170
+ textShadowOffset: (value: string) => offsetPattern.test(value),
171
+ textShadowRadius: (value: string) => pixelPattern.test(value),
172
+ textTransform: (value: string) =>
173
+ ['none', 'uppercase', 'lowercase', 'capitalize'].includes(value),
174
+
175
+ // Other Properties
176
+ elevation: (value: string) => /^\d+$/.test(value), // Android-specific
177
+ resizeMode: (value: string) =>
178
+ ['cover', 'contain', 'stretch', 'repeat', 'center'].includes(value),
179
+ overflow: (value: string) => ['visible', 'hidden', 'scroll'].includes(value),
180
+ display: (value: string) => ['none', 'flex'].includes(value),
181
+
182
+ // iOS-specific Shadow Properties
183
+ shadowColor: (value: string) => colorPattern.test(value),
184
+ shadowOffset: (value: string) => offsetPattern.test(value),
185
+ shadowOpacity: (value: string) => /^([01]|0?\.\d+)$/.test(value),
186
+ shadowRadius: (value: string) => pixelPattern.test(value),
187
+
188
+ // Android-specific Properties
189
+ includeFontPadding: (value: string) => ['true', 'false'].includes(value),
190
+
191
+ // Transform Properties
192
+ transform: (value: string) => {
193
+ const validTransforms = [
194
+ /^translate\(\s*-?\d+(\.\d+)?(px|%)?\s*,\s*-?\d+(\.\d+)?(px|%)?\s*\)$/,
195
+ /^scale\(\s*-?\d+(\.\d+)?\s*(,\s*-?\d+(\.\d+)?)?\s*\)$/,
196
+ /^rotate\(\s*-?\d+(\.\d+)?deg\s*\)$/,
197
+ /^skew\(\s*-?\d+(\.\d+)?deg\s*(,\s*-?\d+(\.\d+)?deg)?\s*\)$/,
198
+ ];
199
+ const transforms = value.split(/\s(?=[a-z])/).filter(Boolean);
200
+ return transforms.every((t) =>
201
+ validTransforms.some((regex) => regex.test(t))
202
+ );
203
+ },
204
+ };
205
+
206
+ function validateReactNativeCssProperty(key: string, value: string): boolean {
207
+ const validator = cssProperties[key];
208
+ if (!validator) {
209
+ // Property not supported in React Native
210
+ return false;
211
+ }
212
+ if (typeof value !== 'string') {
213
+ return false;
214
+ }
215
+ return validator(extractVarValue(value.trim()));
216
+ }
217
+
218
+ // type CssToReactNative = typeof import('css-to-react-native')
219
+ // const cssToReactNative: typeof cssToStyleSheet = (cssToStyleSheet as any)
220
+ // .default
221
+ // ? (cssToStyleSheet as any).default
222
+ // : cssToStyleSheet;
12
223
 
13
224
  type StyleSheetProperties = ImageStyle & TextStyle & ViewStyle;
14
225
 
@@ -167,8 +378,10 @@ function omit<T extends object>(obj: T, ...values: (keyof T)[]): Partial<T> {
167
378
  const inRange = (value: number, min: number, max: number) =>
168
379
  value >= min && value <= max;
169
380
 
381
+ type Styles = { [key: string]: string };
382
+
170
383
  const processValue = (
171
- styles: { [key: string]: string },
384
+ styles: Styles,
172
385
  [key, value]: [string, unknown]
173
386
  ): string | undefined => {
174
387
  if (typeof value !== 'string' || value === '') return undefined;
@@ -228,12 +441,18 @@ const processValue = (
228
441
  /**
229
442
  * @description Cleans styles that RN can't handle and css-to-react-native doesn't fix
230
443
  */
231
- const cleanCssStyleProps = (styles: {
232
- [key: string]: string;
233
- }): { [key: string]: string } =>
444
+ const cleanCssStyleProps = ({
445
+ styles,
446
+ strictStyleMode,
447
+ }: {
448
+ styles: Styles;
449
+ strictStyleMode: boolean;
450
+ }): Styles =>
234
451
  Object.entries(styles).reduce((acc, [key, value]) => {
235
452
  const processedValue = processValue(styles, [key, value]);
236
453
  if (processedValue === undefined) return acc;
454
+ if (strictStyleMode && !validateReactNativeCssProperty(key, processedValue))
455
+ return acc;
237
456
  return { ...acc, [key]: processedValue };
238
457
  }, {});
239
458
 
@@ -269,13 +488,32 @@ function getReactNativeBlockStyles({
269
488
  ...largeAndMediumStyles,
270
489
  ...(responsiveStyles.small || {}),
271
490
  ...blockStyles,
272
- } as Record<string, string | number>;
491
+ };
273
492
 
274
- const cleanedCSS = cleanCssStyleProps(styles as any);
493
+ const cleanedCSS = cleanCssStyleProps({
494
+ styles,
495
+ strictStyleMode: context.strictStyleMode,
496
+ });
275
497
 
276
- const newStyles = cssToReactNative(
277
- Object.entries(cleanedCSS)
278
- ) as any as CSSStyleDeclaration;
498
+ /**
499
+ * Rewrite of library's default export with per-rule error-swallowing:
500
+ * https://github.com/styled-components/css-to-react-native/blob/837637bb134a88e8cd734b51634338fd6555068d/src/index.js#L70-L79
501
+ */
502
+ const newStyles = Object.entries(cleanedCSS).reduce((accum, rule) => {
503
+ try {
504
+ const propertyName = getPropertyName(rule[0]);
505
+ const stylesForProp = getStylesForProperty(propertyName, rule[1], true);
506
+ return Object.assign(accum, stylesForProp);
507
+ } catch (error) {
508
+ /**
509
+ * Silently ignore any values that cause a crash in `css-to-react-native`
510
+ * Example: this is most common when actively editing. In the process of
511
+ * typing `15px`, the visual editor sends `15p` which throws an error.
512
+ */
513
+ }
514
+
515
+ return accum;
516
+ }, {});
279
517
 
280
518
  return newStyles;
281
519
  }