react-native-css 0.0.0-nightly.2c68396 → 0.0.0-nightly.2cfbf16

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 (70) hide show
  1. package/README.md +5 -5
  2. package/dist/commonjs/compiler/compiler.js +63 -4
  3. package/dist/commonjs/compiler/compiler.js.map +1 -1
  4. package/dist/commonjs/compiler/stylesheet.js +5 -0
  5. package/dist/commonjs/compiler/stylesheet.js.map +1 -1
  6. package/dist/commonjs/compiler/supports.js +1 -3
  7. package/dist/commonjs/compiler/supports.js.map +1 -1
  8. package/dist/commonjs/components/FlatList.js +1 -0
  9. package/dist/commonjs/components/FlatList.js.map +1 -1
  10. package/dist/commonjs/components/SafeAreaProvider.js +13 -0
  11. package/dist/commonjs/components/SafeAreaProvider.js.map +1 -0
  12. package/dist/commonjs/native/styles/functions/filters.js +4 -4
  13. package/dist/commonjs/native/styles/functions/filters.js.map +1 -1
  14. package/dist/commonjs/native/styles/index.js +225 -30
  15. package/dist/commonjs/native/styles/index.js.map +1 -1
  16. package/dist/commonjs/native/styles/shorthands/_handler.js +10 -0
  17. package/dist/commonjs/native/styles/shorthands/_handler.js.map +1 -1
  18. package/dist/commonjs/native/styles/shorthands/box-shadow.js +45 -12
  19. package/dist/commonjs/native/styles/shorthands/box-shadow.js.map +1 -1
  20. package/dist/commonjs/web/api.js +1 -1
  21. package/dist/commonjs/web/api.js.map +1 -1
  22. package/dist/module/compiler/compiler.js +64 -5
  23. package/dist/module/compiler/compiler.js.map +1 -1
  24. package/dist/module/compiler/stylesheet.js +5 -0
  25. package/dist/module/compiler/stylesheet.js.map +1 -1
  26. package/dist/module/compiler/supports.js +1 -3
  27. package/dist/module/compiler/supports.js.map +1 -1
  28. package/dist/module/components/FlatList.js +1 -0
  29. package/dist/module/components/FlatList.js.map +1 -1
  30. package/dist/module/components/SafeAreaProvider.js +4 -0
  31. package/dist/module/components/SafeAreaProvider.js.map +1 -0
  32. package/dist/module/native/styles/functions/filters.js +4 -4
  33. package/dist/module/native/styles/functions/filters.js.map +1 -1
  34. package/dist/module/native/styles/index.js +226 -30
  35. package/dist/module/native/styles/index.js.map +1 -1
  36. package/dist/module/native/styles/shorthands/_handler.js +11 -1
  37. package/dist/module/native/styles/shorthands/_handler.js.map +1 -1
  38. package/dist/module/native/styles/shorthands/box-shadow.js +46 -12
  39. package/dist/module/native/styles/shorthands/box-shadow.js.map +1 -1
  40. package/dist/module/web/api.js +1 -1
  41. package/dist/module/web/api.js.map +1 -1
  42. package/dist/typescript/commonjs/src/compiler/compiler.d.ts.map +1 -1
  43. package/dist/typescript/commonjs/src/compiler/stylesheet.d.ts +1 -0
  44. package/dist/typescript/commonjs/src/compiler/stylesheet.d.ts.map +1 -1
  45. package/dist/typescript/commonjs/src/components/FlatList.d.ts.map +1 -1
  46. package/dist/typescript/commonjs/src/components/SafeAreaProvider.d.ts +2 -0
  47. package/dist/typescript/commonjs/src/components/SafeAreaProvider.d.ts.map +1 -0
  48. package/dist/typescript/commonjs/src/native/styles/index.d.ts.map +1 -1
  49. package/dist/typescript/commonjs/src/native/styles/shorthands/_handler.d.ts.map +1 -1
  50. package/dist/typescript/commonjs/src/native/styles/shorthands/box-shadow.d.ts.map +1 -1
  51. package/dist/typescript/module/src/compiler/compiler.d.ts.map +1 -1
  52. package/dist/typescript/module/src/compiler/stylesheet.d.ts +1 -0
  53. package/dist/typescript/module/src/compiler/stylesheet.d.ts.map +1 -1
  54. package/dist/typescript/module/src/components/FlatList.d.ts.map +1 -1
  55. package/dist/typescript/module/src/components/SafeAreaProvider.d.ts +2 -0
  56. package/dist/typescript/module/src/components/SafeAreaProvider.d.ts.map +1 -0
  57. package/dist/typescript/module/src/native/styles/index.d.ts.map +1 -1
  58. package/dist/typescript/module/src/native/styles/shorthands/_handler.d.ts.map +1 -1
  59. package/dist/typescript/module/src/native/styles/shorthands/box-shadow.d.ts.map +1 -1
  60. package/package.json +19 -6
  61. package/src/compiler/compiler.ts +90 -5
  62. package/src/compiler/stylesheet.ts +6 -0
  63. package/src/compiler/supports.ts +1 -3
  64. package/src/components/FlatList.tsx +1 -0
  65. package/src/components/SafeAreaProvider.tsx +1 -0
  66. package/src/native/styles/functions/filters.ts +6 -6
  67. package/src/native/styles/index.ts +257 -34
  68. package/src/native/styles/shorthands/_handler.ts +14 -1
  69. package/src/native/styles/shorthands/box-shadow.ts +50 -15
  70. package/src/web/api.tsx +1 -1
@@ -17,6 +17,149 @@ import {
17
17
  } from "../reactivity";
18
18
  import { calculateProps } from "./calculate-props";
19
19
 
20
+ /**
21
+ * Checks if the left style object has any properties that are not present in the right style object.
22
+ * This is used to determine if styles need to be preserved in an array or if right can completely override left.
23
+ *
24
+ * Note: This intentionally only checks one direction (left → right). We don't need to check if right has
25
+ * properties that left doesn't have, because right will always be applied/merged. The question we're
26
+ * answering is: "Does left have any properties that would be lost if we just used right?" If yes, we
27
+ * create a style array [left, right] to preserve both. If no, right can safely replace left entirely.
28
+ *
29
+ * @param left - The left style object to check
30
+ * @param right - The right style object to compare against
31
+ * @returns true if left has at least one property key that doesn't exist in right, false otherwise
32
+ *
33
+ * @example
34
+ * hasNonOverlappingProperties({color: 'red', fontSize: 12}, {color: 'blue'}) // true - fontSize is not in right
35
+ * hasNonOverlappingProperties({color: 'red'}, {color: 'blue', fontSize: 12}) // false - all left keys exist in right
36
+ */
37
+ function hasNonOverlappingProperties(
38
+ left: Record<string, any>,
39
+ right: Record<string, any>,
40
+ ): boolean {
41
+ // Null safety check
42
+ if (!left || !right) {
43
+ return false;
44
+ }
45
+
46
+ // Only check own properties to avoid prototype pollution
47
+ for (const key in left) {
48
+ if (Object.prototype.hasOwnProperty.call(left, key)) {
49
+ if (!Object.prototype.hasOwnProperty.call(right, key)) {
50
+ return true;
51
+ }
52
+ }
53
+ }
54
+ return false;
55
+ }
56
+
57
+ /**
58
+ * Flattens a style array into a single object when possible, with rightmost values taking precedence.
59
+ * Returns the original array if it contains any non-plain objects, arrays, or CSS variable objects.
60
+ *
61
+ * Note: This function assumes the input array has already been filtered (e.g., by filterCssVariables),
62
+ * so empty arrays should not reach this function. If they do, they will be treated as non-flattenable.
63
+ *
64
+ * @param styleArray - The style array to potentially flatten
65
+ * @returns A single merged object if all items are plain objects, otherwise the original array
66
+ */
67
+ function flattenStyleArray(styleArray: any[]): any {
68
+ // Check if we can flatten to a single object (all items are plain objects)
69
+ for (const item of styleArray) {
70
+ // Use explicit null check instead of !item to allow falsy values like 0 or false
71
+ if (
72
+ item == null ||
73
+ typeof item !== "object" ||
74
+ Array.isArray(item) ||
75
+ Object.prototype.hasOwnProperty.call(item, VAR_SYMBOL)
76
+ ) {
77
+ return styleArray;
78
+ }
79
+ }
80
+
81
+ // Use reduce to avoid spread operator performance issues with large arrays
82
+ return styleArray.reduce((acc, item) => Object.assign(acc, item), {});
83
+ }
84
+
85
+ /**
86
+ * Recursively filters out CSS variable objects (with VAR_SYMBOL) from style values.
87
+ * This prevents CSS variable runtime objects from leaking into React Native component props.
88
+ *
89
+ * @param value - The value to filter (can be any type: object, array, primitive, etc.)
90
+ * @param depth - Internal recursion depth counter to prevent stack overflow (max 100)
91
+ * @returns The filtered value with CSS variables removed, or `undefined` if the entire value
92
+ * should be filtered out (e.g., empty arrays, objects with only VAR_SYMBOL properties)
93
+ *
94
+ * Filtering behavior:
95
+ * - Objects with VAR_SYMBOL property: returns `undefined` (completely filtered)
96
+ * - Arrays: filters out VAR_SYMBOL objects, returns `undefined` if empty after filtering
97
+ * - Objects: recursively filters properties, returns `undefined` if no properties remain
98
+ * - Primitives (null, undefined, numbers, strings, booleans): returned as-is
99
+ * - Symbol properties: intentionally filtered out for React Native compatibility
100
+ *
101
+ * @example
102
+ * filterCssVariables({fontSize: 16, color: {[VAR_SYMBOL]: true}}) // {fontSize: 16}
103
+ * filterCssVariables([{margin: 10}, {[VAR_SYMBOL]: true}]) // [{margin: 10}]
104
+ * filterCssVariables({color: {[VAR_SYMBOL]: true}}) // undefined (all props filtered)
105
+ */
106
+ function filterCssVariables(value: any, depth = 0): any | undefined {
107
+ // Prevent stack overflow on deeply nested structures
108
+ if (depth > 100) {
109
+ return value;
110
+ }
111
+
112
+ if (value === null || value === undefined) {
113
+ return value;
114
+ }
115
+
116
+ if (Array.isArray(value)) {
117
+ // Single-pass filter with map operation
118
+ const filtered: any[] = [];
119
+
120
+ for (const item of value) {
121
+ const filteredItem = filterCssVariables(item, depth + 1);
122
+ if (
123
+ filteredItem !== undefined &&
124
+ !(
125
+ typeof filteredItem === "object" &&
126
+ filteredItem !== null &&
127
+ Object.prototype.hasOwnProperty.call(filteredItem, VAR_SYMBOL)
128
+ )
129
+ ) {
130
+ filtered.push(filteredItem);
131
+ }
132
+ }
133
+
134
+ return filtered.length > 0 ? filtered : undefined;
135
+ }
136
+
137
+ if (typeof value === "object") {
138
+ // If the object itself has VAR_SYMBOL, filter it out (check own property only)
139
+ if (Object.prototype.hasOwnProperty.call(value, VAR_SYMBOL)) {
140
+ return undefined;
141
+ }
142
+
143
+ // Otherwise, filter VAR_SYMBOL properties from nested objects
144
+ const filtered: Record<string, any> = {};
145
+ let hasProperties = false;
146
+
147
+ // Use Object.keys to only iterate own string properties (not inherited, not Symbols)
148
+ // This intentionally filters out Symbol properties for React Native compatibility
149
+ for (const key of Object.keys(value)) {
150
+ const filteredValue = filterCssVariables(value[key], depth + 1);
151
+ if (filteredValue !== undefined) {
152
+ filtered[key] = filteredValue;
153
+ hasProperties = true;
154
+ }
155
+ }
156
+
157
+ return hasProperties ? filtered : undefined;
158
+ }
159
+
160
+ return value;
161
+ }
162
+
20
163
  export const stylesFamily = family(
21
164
  (
22
165
  hash: string,
@@ -125,6 +268,50 @@ export function getStyledProps(
125
268
  return result;
126
269
  }
127
270
 
271
+ /**
272
+ * Merges two prop objects, skipping keys from `right` whose value is `undefined`
273
+ * or an empty plain object.
274
+ *
275
+ * `Object.assign({}, left, right)` copies all enumerable own properties from `right`,
276
+ * including those with value `undefined` or `{}`. When a component passes
277
+ * `style={undefined}` or `contentContainerStyle={{}}`, the computed NativeWind
278
+ * style from `left` gets overwritten. This function prevents that by only copying
279
+ * values that carry meaningful style information.
280
+ *
281
+ * Empty object detection uses inline `for...in` instead of a separate function or
282
+ * `Object.keys().length` — avoids both function call overhead and intermediate array
283
+ * allocation on a hot path (called per-prop per-render). Consistent with how the
284
+ * `["style"]` target path handles this via `filterCssVariables({})` returning `undefined`.
285
+ *
286
+ * @param left - The computed NativeWind props (className-derived styles)
287
+ * @param right - The inline props from the component (guaranteed non-null by caller; may contain undefined values)
288
+ * @returns A new object with all properties from `left`, overridden only by meaningful values from `right`
289
+ */
290
+ function mergeDefinedProps(
291
+ left: Record<string, any> | undefined,
292
+ right: Record<string, any>,
293
+ ) {
294
+ const result = left ? { ...left } : {};
295
+ for (const key in right) {
296
+ const value = right[key];
297
+ if (value === undefined) continue;
298
+ // Non-object values (strings, numbers, booleans, null, arrays): always assign
299
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
300
+ result[key] = value;
301
+ continue;
302
+ }
303
+ // Plain object: assign only if non-empty. An empty style object (e.g.,
304
+ // contentContainerStyle={{}}) has no properties to apply and should not
305
+ // overwrite computed className styles. The for...in loop only executes
306
+ // if the object has enumerable keys — empty objects are skipped.
307
+ for (const _ in value) {
308
+ result[key] = value;
309
+ break;
310
+ }
311
+ }
312
+ return result;
313
+ }
314
+
128
315
  function deepMergeConfig(
129
316
  config: Config,
130
317
  left: Record<string, any> | undefined,
@@ -145,30 +332,81 @@ function deepMergeConfig(
145
332
  ) {
146
333
  // Special handling for style target when we have inline styles
147
334
  result = { ...left, ...right };
148
- // More performant approach - check for non-overlapping properties without Sets
149
- if (left?.style && right?.style && rightIsInline) {
150
- const leftStyle = left.style;
151
- const rightStyle = right.style;
152
-
153
- // Quick check: do any left properties NOT exist in right?
154
- let hasNonOverlappingProperties = false;
155
- for (const key in leftStyle) {
156
- if (!(key in rightStyle)) {
157
- hasNonOverlappingProperties = true;
158
- break; // Early exit for performance
335
+
336
+ // Handle null/undefined inline styles
337
+ if (right?.style === null || right?.style === undefined) {
338
+ if (left?.style) {
339
+ result.style = left.style;
340
+ }
341
+ } else if (rightIsInline && right?.style) {
342
+ // Filter inline styles if rightIsInline is true
343
+ const filteredRightStyle = filterCssVariables(right.style);
344
+
345
+ if (left?.style) {
346
+ if (!filteredRightStyle) {
347
+ // All inline styles were CSS variables, only use left
348
+ result.style = left.style;
349
+ } else {
350
+ const leftStyle = left.style;
351
+
352
+ // For arrays or objects, check if we need to create a style array
353
+ const leftIsObject =
354
+ typeof leftStyle === "object" && !Array.isArray(leftStyle);
355
+ const rightIsObject =
356
+ typeof filteredRightStyle === "object" &&
357
+ !Array.isArray(filteredRightStyle);
358
+
359
+ if (leftIsObject && rightIsObject) {
360
+ if (hasNonOverlappingProperties(leftStyle, filteredRightStyle)) {
361
+ result.style = [leftStyle, filteredRightStyle];
362
+ } else {
363
+ // All left properties are in right, right overrides
364
+ result.style = filteredRightStyle;
365
+ }
366
+ } else {
367
+ // One or both are arrays, merge them
368
+ result.style = [leftStyle, filteredRightStyle];
369
+ }
370
+ }
371
+ } else {
372
+ // No left style, just use filtered right
373
+ if (filteredRightStyle) {
374
+ result.style = filteredRightStyle;
375
+ } else {
376
+ // All filtered out, remove style prop
377
+ delete result.style;
159
378
  }
160
379
  }
161
-
162
- if (hasNonOverlappingProperties) {
163
- result.style = [leftStyle, rightStyle];
380
+ } else if (!rightIsInline && right?.style) {
381
+ // Merging non-inline styles (e.g., important styles)
382
+ if (left?.style) {
383
+ // If left.style is an array, append right.style
384
+ if (Array.isArray(left.style)) {
385
+ const combined = [...left.style, right.style];
386
+ result.style = flattenStyleArray(combined);
387
+ } else if (
388
+ typeof left.style === "object" &&
389
+ typeof right.style === "object"
390
+ ) {
391
+ // Both are objects, check for overlaps
392
+ if (hasNonOverlappingProperties(left.style, right.style)) {
393
+ result.style = flattenStyleArray([left.style, right.style]);
394
+ } else {
395
+ // All left properties are overridden by right
396
+ result.style = right.style;
397
+ }
398
+ } else {
399
+ // One or both are arrays/mixed types
400
+ const combined = [left.style, right.style];
401
+ result.style = flattenStyleArray(combined);
402
+ }
164
403
  }
165
- // Otherwise, Object.assign above will handle the override correctly
166
404
  }
167
405
  } else {
168
- result = Object.assign({}, left, right);
406
+ result = mergeDefinedProps(left, right);
169
407
  }
170
408
  } else {
171
- result = { ...left };
409
+ result = mergeDefinedProps(left, right);
172
410
  }
173
411
 
174
412
  if (
@@ -211,23 +449,8 @@ function deepMergeConfig(
211
449
  let rightValue = right?.[target];
212
450
 
213
451
  // Strip any inline variables from the target
214
- if (rightIsInline && rightValue) {
215
- if (Array.isArray(rightValue)) {
216
- rightValue = rightValue.filter((v) => {
217
- return typeof v !== "object" || !(v && VAR_SYMBOL in v);
218
- });
219
-
220
- if (rightValue.length === 0) {
221
- rightValue = undefined;
222
- }
223
- } else if (
224
- typeof rightValue === "object" &&
225
- rightValue &&
226
- VAR_SYMBOL in rightValue
227
- ) {
228
- rightValue = undefined;
229
- delete result[target][VAR_SYMBOL];
230
- }
452
+ if (rightIsInline && rightValue !== undefined) {
453
+ rightValue = filterCssVariables(rightValue);
231
454
  }
232
455
 
233
456
  if (rightValue !== undefined) {
@@ -1,5 +1,8 @@
1
1
  /* eslint-disable */
2
- import { isStyleDescriptorArray } from "react-native-css/utilities";
2
+ import {
3
+ isStyleDescriptorArray,
4
+ isStyleFunction,
5
+ } from "react-native-css/utilities";
3
6
 
4
7
  import { setDeepPath } from "../../objects";
5
8
  import { ShortHandSymbol } from "../constants";
@@ -56,6 +59,16 @@ export function shorthandHandler(
56
59
  return type.includes(value) || type.includes(typeof value);
57
60
  }
58
61
 
62
+ // Style functions (var, calc, env, etc.) are unresolved at pattern-match
63
+ // time — their actual values won't be known until runtime variable
64
+ // resolution. Accepting them in any type slot makes pattern matching
65
+ // less strict when variables are involved, but rejecting them would
66
+ // break variable-based shadows entirely (e.g. box-shadow: var(--shadow)
67
+ // where --shadow resolves to "0 4px 6px -1px #000").
68
+ if (isStyleFunction(value)) {
69
+ return true;
70
+ }
71
+
59
72
  switch (type) {
60
73
  case "string":
61
74
  case "number":
@@ -4,21 +4,30 @@ import { isStyleDescriptorArray } from "react-native-css/utilities";
4
4
  import type { StyleFunctionResolver } from "../resolve";
5
5
  import { shorthandHandler } from "./_handler";
6
6
 
7
- const color = ["color", "string"] as const;
7
+ // "color" type accepts both strings ("#fff") and objects (PlatformColor from currentcolor).
8
+ // Using "string" here would reject platform color objects and silently drop the shadow.
9
+ const color = ["color", "color"] as const;
8
10
  const offsetX = ["offsetX", "number"] as const;
9
11
  const offsetY = ["offsetY", "number"] as const;
10
12
  const blurRadius = ["blurRadius", "number"] as const;
11
13
  const spreadDistance = ["spreadDistance", "number"] as const;
12
- // const inset = ["inset", "string"] as const;
14
+ // Match the literal string "inset" - the array type checks if value is in array
15
+ const inset = ["inset", ["inset"]] as const;
13
16
 
14
17
  const handler = shorthandHandler(
15
18
  [
19
+ // Standard patterns (without inset)
16
20
  [offsetX, offsetY, blurRadius, spreadDistance],
17
21
  [offsetX, offsetY, blurRadius, spreadDistance, color],
18
22
  [color, offsetX, offsetY],
19
23
  [color, offsetX, offsetY, blurRadius, spreadDistance],
20
24
  [offsetX, offsetY, color],
21
25
  [offsetX, offsetY, blurRadius, color],
26
+ // Inset patterns - "inset" keyword at the beginning
27
+ [inset, offsetX, offsetY, blurRadius, spreadDistance],
28
+ [inset, offsetX, offsetY, blurRadius, spreadDistance, color],
29
+ [inset, offsetX, offsetY, blurRadius, color],
30
+ [inset, color, offsetX, offsetY, blurRadius, spreadDistance],
22
31
  ],
23
32
  [],
24
33
  "object",
@@ -34,20 +43,32 @@ export const boxShadow: StyleFunctionResolver = (
34
43
 
35
44
  if (!isStyleDescriptorArray(args)) {
36
45
  return args;
37
- } else {
38
- return args
39
- .flatMap(flattenShadowDescriptor)
40
- .map((shadows) => {
41
- if (shadows === undefined) {
42
- return;
43
- } else {
44
- return omitTransparentShadows(
45
- handler(resolveValue, shadows, get, options),
46
- );
47
- }
48
- })
49
- .filter((v) => v !== undefined);
50
46
  }
47
+
48
+ // A flat array of primitives (e.g. [0, 4, 6, -1, "#000"]) is a single shadow
49
+ // resolved from a runtime variable. Pass it directly to the pattern handler.
50
+ // A nested array (e.g. [[0, 4, 6, -1, "#000"], [...]]) is multiple shadows.
51
+ if (args.length > 0 && !Array.isArray(args[0])) {
52
+ const result = handler(resolveValue, args, get, options);
53
+ if (result === undefined) {
54
+ return [];
55
+ }
56
+ const filtered = omitTransparentShadows(result);
57
+ return filtered !== undefined ? [normalizeInsetValue(filtered)] : [];
58
+ }
59
+
60
+ return args
61
+ .flatMap(flattenShadowDescriptor)
62
+ .map((shadows) => {
63
+ if (shadows === undefined) {
64
+ return;
65
+ } else {
66
+ return normalizeInsetValue(
67
+ omitTransparentShadows(handler(resolveValue, shadows, get, options)),
68
+ );
69
+ }
70
+ })
71
+ .filter((v) => v !== undefined);
51
72
  };
52
73
 
53
74
  function flattenShadowDescriptor(arg: StyleDescriptor): StyleDescriptor[] {
@@ -69,3 +90,17 @@ function omitTransparentShadows(style: unknown) {
69
90
 
70
91
  return style;
71
92
  }
93
+
94
+ /**
95
+ * Convert inset: "inset" to inset: true for React Native boxShadow.
96
+ *
97
+ * The shorthand handler matches the literal "inset" string and assigns it as the value.
98
+ * React Native's boxShadow expects inset to be a boolean.
99
+ */
100
+ function normalizeInsetValue(style: unknown) {
101
+ if (typeof style === "object" && style && "inset" in style) {
102
+ return { ...style, inset: true };
103
+ }
104
+
105
+ return style;
106
+ }
package/src/web/api.tsx CHANGED
@@ -31,7 +31,7 @@ export const styled = <
31
31
  _options?: StyledOptions,
32
32
  ) => {
33
33
  return (props: StyledProps<ComponentPropsWithRef<C>, M>) => {
34
- return useCssElement(baseComponent, mapping, props);
34
+ return useCssElement(baseComponent, props, mapping);
35
35
  };
36
36
  };
37
37