@office-iss/react-native-win32 0.75.1 → 0.76.0-preview.1

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 (174) hide show
  1. package/.eslintrc.js +11 -0
  2. package/.flowconfig +5 -4
  3. package/CHANGELOG.json +172 -40
  4. package/CHANGELOG.md +57 -24
  5. package/Libraries/Alert/Alert.js +3 -0
  6. package/Libraries/Animated/AnimatedEvent.js +1 -1
  7. package/Libraries/Animated/AnimatedImplementation.js +7 -7
  8. package/Libraries/Animated/NativeAnimatedAllowlist.js +111 -0
  9. package/Libraries/Animated/animations/Animation.js +11 -1
  10. package/Libraries/Animated/animations/DecayAnimation.js +1 -1
  11. package/Libraries/Animated/animations/SpringAnimation.js +1 -1
  12. package/Libraries/Animated/animations/TimingAnimation.js +2 -1
  13. package/Libraries/Animated/components/AnimatedScrollView.js +3 -2
  14. package/Libraries/Animated/createAnimatedComponent.js +10 -9
  15. package/Libraries/Animated/nodes/AnimatedColor.js +1 -1
  16. package/Libraries/Animated/nodes/AnimatedInterpolation.js +3 -2
  17. package/Libraries/Animated/nodes/AnimatedNode.js +42 -33
  18. package/Libraries/Animated/nodes/AnimatedObject.js +56 -50
  19. package/Libraries/Animated/nodes/AnimatedProps.js +77 -40
  20. package/Libraries/Animated/nodes/AnimatedStyle.js +103 -59
  21. package/Libraries/Animated/nodes/AnimatedTracking.js +1 -1
  22. package/Libraries/Animated/nodes/AnimatedTransform.js +102 -67
  23. package/Libraries/Animated/nodes/AnimatedValue.js +2 -1
  24. package/Libraries/Animated/nodes/AnimatedWithChildren.js +21 -22
  25. package/Libraries/Animated/useAnimatedProps.js +142 -7
  26. package/Libraries/BatchedBridge/NativeModules.js +2 -0
  27. package/Libraries/Blob/FileReader.js +1 -1
  28. package/Libraries/Blob/URL.js +2 -62
  29. package/Libraries/Blob/URLSearchParams.js +71 -0
  30. package/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js +1 -1
  31. package/Libraries/Components/RefreshControl/__mocks__/RefreshControlMock.js +1 -1
  32. package/Libraries/Components/ScrollView/ScrollView.js +131 -169
  33. package/Libraries/Components/ScrollView/ScrollViewStickyHeader.js +1 -1
  34. package/Libraries/Components/StatusBar/StatusBar.js +3 -1
  35. package/Libraries/Components/TextInput/TextInput.d.ts +32 -2
  36. package/Libraries/Components/TextInput/TextInput.js +230 -94
  37. package/Libraries/Components/TextInput/TextInput.win32.js +230 -100
  38. package/Libraries/Components/View/ReactNativeStyleAttributes.js +22 -0
  39. package/Libraries/Components/View/ReactNativeViewAttributes.js +2 -0
  40. package/Libraries/Components/View/ReactNativeViewAttributes.win32.js +2 -0
  41. package/Libraries/Components/View/ViewAccessibility.d.ts +15 -0
  42. package/Libraries/Components/View/ViewNativeComponent.js +0 -1
  43. package/Libraries/Components/View/ViewPropTypes.js +14 -0
  44. package/Libraries/Components/View/ViewPropTypes.win32.js +14 -0
  45. package/Libraries/Core/ExceptionsManager.js +2 -0
  46. package/Libraries/Core/InitializeCore.js +3 -1
  47. package/Libraries/Core/ReactFiberErrorDialog.js +3 -0
  48. package/Libraries/Core/ReactNativeVersion.js +4 -4
  49. package/Libraries/Core/ReactNativeVersionCheck.win32.js +1 -1
  50. package/Libraries/Core/setUpErrorHandling.js +7 -1
  51. package/Libraries/Core/setUpGlobals.js +1 -0
  52. package/Libraries/Core/setUpReactRefresh.js +0 -4
  53. package/Libraries/Image/AssetSourceResolver.js +28 -1
  54. package/Libraries/Image/Image.android.js +9 -14
  55. package/Libraries/Image/Image.ios.js +11 -22
  56. package/Libraries/Image/Image.win32.js +11 -24
  57. package/Libraries/Image/ImageBackground.js +1 -8
  58. package/Libraries/Image/ImageUtils.js +9 -9
  59. package/Libraries/Image/ImageViewNativeComponent.js +1 -0
  60. package/Libraries/Inspector/Inspector.js +3 -2
  61. package/Libraries/Inspector/Inspector.win32.js +3 -2
  62. package/Libraries/Inspector/InspectorPanel.js +16 -10
  63. package/Libraries/Inspector/NetworkOverlay.js +1 -1
  64. package/Libraries/Interaction/TaskQueue.js +1 -0
  65. package/Libraries/Lists/FlatList.js +1 -1
  66. package/Libraries/Lists/SectionList.js +2 -2
  67. package/Libraries/Lists/SectionListModern.js +3 -3
  68. package/Libraries/LogBox/Data/LogBoxData.js +24 -3
  69. package/Libraries/LogBox/LogBoxNotificationContainer.js +3 -2
  70. package/Libraries/LogBox/UI/LogBoxInspectorHeader.js +9 -8
  71. package/Libraries/LogBox/UI/LogBoxInspectorHeader.win32.js +9 -29
  72. package/Libraries/Modal/Modal.js +0 -1
  73. package/Libraries/NativeComponent/BaseViewConfig.android.js +8 -0
  74. package/Libraries/NativeComponent/BaseViewConfig.ios.js +7 -0
  75. package/Libraries/NativeComponent/BaseViewConfig.win32.js +7 -0
  76. package/Libraries/NativeComponent/NativeComponentRegistry.js +22 -22
  77. package/Libraries/NativeComponent/StaticViewConfigValidator.js +0 -21
  78. package/Libraries/Network/XMLHttpRequest.js +4 -2
  79. package/Libraries/ReactNative/AppContainer-dev.js +1 -5
  80. package/Libraries/ReactNative/AppContainer-prod.js +1 -5
  81. package/Libraries/ReactNative/AppContainer.js +0 -1
  82. package/Libraries/ReactNative/AppRegistry.js +0 -6
  83. package/Libraries/ReactNative/BridgelessUIManager.js +1 -0
  84. package/Libraries/ReactNative/ReactFabricPublicInstance/ReactFabricHostComponent.js +1 -1
  85. package/Libraries/ReactNative/ReactFabricPublicInstance/ReactFabricPublicInstance.js +5 -5
  86. package/Libraries/ReactNative/RendererImplementation.js +26 -4
  87. package/Libraries/ReactNative/getNativeComponentAttributes.js +8 -0
  88. package/Libraries/ReactNative/renderApplication.js +0 -2
  89. package/Libraries/Renderer/shims/ReactNativeTypes.js +11 -4
  90. package/Libraries/StyleSheet/StyleSheet.js +1 -1
  91. package/Libraries/StyleSheet/StyleSheet.win32.js +1 -1
  92. package/Libraries/StyleSheet/StyleSheetTypes.d.ts +74 -15
  93. package/Libraries/StyleSheet/StyleSheetTypes.js +60 -5
  94. package/Libraries/StyleSheet/processBackgroundImage.js +384 -0
  95. package/Libraries/StyleSheet/processBoxShadow.js +211 -0
  96. package/Libraries/StyleSheet/processFilter.js +231 -42
  97. package/Libraries/Text/Text.js +394 -196
  98. package/Libraries/Text/Text.win32.js +442 -229
  99. package/Libraries/Text/TextNativeComponent.js +2 -1
  100. package/Libraries/Text/TextNativeComponent.win32.js +1 -1
  101. package/Libraries/TurboModule/TurboModuleRegistry.js +13 -50
  102. package/Libraries/Types/CodegenTypes.js +3 -1
  103. package/Libraries/Utilities/Appearance.js +108 -84
  104. package/Libraries/Utilities/DevLoadingView.js +2 -4
  105. package/Libraries/Utilities/HMRClient.js +2 -1
  106. package/Libraries/Utilities/ReactNativeTestTools.js +1 -1
  107. package/Libraries/Utilities/createPerformanceLogger.js +0 -9
  108. package/Libraries/Utilities/stringifyViewConfig.js +22 -0
  109. package/Libraries/Utilities/useColorScheme.js +3 -3
  110. package/Libraries/WebSocket/WebSocket.js +1 -1
  111. package/Libraries/promiseRejectionTrackingOptions.js +1 -1
  112. package/Libraries/vendor/emitter/EventEmitter.js +6 -5
  113. package/flow/jest.js +2 -2
  114. package/index.js +3 -1
  115. package/index.win32.js +3 -1
  116. package/jest/mockComponent.js +4 -1
  117. package/jest/mockModal.js +1 -3
  118. package/jest/mockScrollView.js +1 -1
  119. package/jest/renderer.js +2 -2
  120. package/jest/setup.js +16 -13
  121. package/jest.config.js +1 -2
  122. package/overrides.json +22 -22
  123. package/package.json +27 -26
  124. package/src/private/animated/NativeAnimatedHelper.js +438 -0
  125. package/src/private/animated/NativeAnimatedHelper.win32.js +440 -0
  126. package/src/private/animated/NativeAnimatedValidation.js +64 -0
  127. package/src/private/components/HScrollViewNativeComponents.js +56 -0
  128. package/src/private/components/SafeAreaView_INTERNAL_DO_NOT_USE.js +27 -0
  129. package/src/private/components/VScrollViewNativeComponents.js +48 -0
  130. package/src/private/components/useSyncOnScroll.js +48 -0
  131. package/src/private/featureflags/ReactNativeFeatureFlags.js +166 -16
  132. package/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js +29 -5
  133. package/src/private/fusebox/FuseboxSessionObserver.js +42 -0
  134. package/{Libraries/Core → src/private/renderer/errorhandling}/ErrorHandlers.js +14 -4
  135. package/src/private/setup/setUpDOM.js +28 -0
  136. package/src/private/setup/setUpIntersectionObserver.js +27 -0
  137. package/src/private/setup/setUpMutationObserver.js +26 -0
  138. package/src/private/setup/setUpPerformanceObserver.js +64 -0
  139. package/src/private/specs/modules/NativeAppearance.js +3 -3
  140. package/src/private/specs/modules/NativeLinkingManager.js +1 -1
  141. package/src/private/specs/modules/NativePlatformConstantsWin.js +7 -0
  142. package/src/private/specs/modules/NativeSampleTurboModule.js +14 -1
  143. package/src/private/webapis/dom/nodes/ReadOnlyNode.js +6 -4
  144. package/{Libraries/IntersectionObserver → src/private/webapis/intersectionobserver}/IntersectionObserver.js +5 -3
  145. package/{Libraries/IntersectionObserver → src/private/webapis/intersectionobserver}/IntersectionObserverEntry.js +3 -3
  146. package/{Libraries/IntersectionObserver → src/private/webapis/intersectionobserver}/IntersectionObserverManager.js +14 -17
  147. package/src/private/{specs/modules → webapis/intersectionobserver/specs}/NativeIntersectionObserver.js +2 -2
  148. package/{Libraries/IntersectionObserver → src/private/webapis/intersectionobserver/specs}/__mocks__/NativeIntersectionObserver.js +4 -4
  149. package/{Libraries/MutationObserver → src/private/webapis/mutationobserver}/MutationObserver.js +5 -3
  150. package/{Libraries/MutationObserver → src/private/webapis/mutationobserver}/MutationObserverManager.js +24 -15
  151. package/{Libraries/MutationObserver → src/private/webapis/mutationobserver}/MutationRecord.js +4 -6
  152. package/src/private/{specs/modules → webapis/mutationobserver/specs}/NativeMutationObserver.js +2 -2
  153. package/{Libraries/MutationObserver → src/private/webapis/mutationobserver/specs}/__mocks__/NativeMutationObserver.js +5 -5
  154. package/src/private/webapis/performance/{EventCounts.js → EventTiming.js} +65 -3
  155. package/src/private/webapis/performance/LongTasks.js +39 -0
  156. package/src/private/webapis/performance/Performance.js +22 -9
  157. package/src/private/webapis/performance/PerformanceEntry.js +36 -18
  158. package/src/private/webapis/performance/PerformanceObserver.js +29 -43
  159. package/src/private/webapis/performance/RawPerformanceEntry.js +24 -1
  160. package/src/private/webapis/performance/UserTiming.js +17 -12
  161. package/src/private/webapis/performance/specs/NativePerformanceObserver.js +1 -1
  162. package/src-win/Libraries/Components/View/ViewAccessibility.d.ts +15 -0
  163. package/types/experimental.d.ts +20 -1
  164. package/Libraries/Animated/NativeAnimatedHelper.js +0 -615
  165. package/Libraries/Animated/NativeAnimatedHelper.win32.js +0 -617
  166. package/Libraries/Core/setUpIntersectionObserver.js +0 -16
  167. package/Libraries/Core/setUpMutationObserver.js +0 -16
  168. package/Libraries/Core/setUpPerformanceObserver.js +0 -18
  169. package/Libraries/IntersectionObserver/NativeIntersectionObserver.js +0 -13
  170. package/Libraries/MutationObserver/NativeMutationObserver.js +0 -13
  171. package/Libraries/Utilities/verifyComponentAttributeEquivalence.js +0 -135
  172. package/src/private/core/setUpDOM.js +0 -18
  173. package/src/private/webapis/performance/PerformanceEventTiming.js +0 -55
  174. /package/src/private/{core → styles}/composeStyles.js +0 -0
@@ -11,7 +11,6 @@
11
11
  'use strict';
12
12
 
13
13
  import type AnimatedNode from '../Animated/nodes/AnimatedNode';
14
- import type {FilterPrimitive} from '../StyleSheet/processFilter';
15
14
  import type {
16
15
  ____DangerouslyImpreciseStyle_InternalOverrides,
17
16
  ____ImageStyle_InternalOverrides,
@@ -691,15 +690,66 @@ export type ____ShadowStyle_Internal = $ReadOnly<{
691
690
  ...____ShadowStyle_InternalOverrides,
692
691
  }>;
693
692
 
694
- type ____FilterStyle_Internal = $ReadOnly<{
695
- experimental_filter?: $ReadOnlyArray<FilterPrimitive>,
696
- }>;
693
+ export type FilterFunction =
694
+ | {brightness: number | string}
695
+ | {blur: number | string}
696
+ | {contrast: number | string}
697
+ | {grayscale: number | string}
698
+ | {hueRotate: number | string}
699
+ | {invert: number | string}
700
+ | {opacity: number | string}
701
+ | {saturate: number | string}
702
+ | {sepia: number | string}
703
+ | {dropShadow: DropShadowPrimitive | string};
704
+
705
+ export type DropShadowPrimitive = {
706
+ offsetX: number | string,
707
+ offsetY: number | string,
708
+ standardDeviation?: number | string,
709
+ color?: ____ColorValue_Internal,
710
+ };
711
+
712
+ export type GradientValue = {
713
+ type: 'linearGradient',
714
+ // Angle or direction enums
715
+ direction?: string,
716
+ colorStops: $ReadOnlyArray<{
717
+ color: ____ColorValue_Internal,
718
+ positions?: $ReadOnlyArray<string>,
719
+ }>,
720
+ };
721
+
722
+ export type BoxShadowPrimitive = {
723
+ offsetX: number | string,
724
+ offsetY: number | string,
725
+ color?: ____ColorValue_Internal,
726
+ blurRadius?: number | string,
727
+ spreadDistance?: number | string,
728
+ inset?: boolean,
729
+ };
730
+
731
+ type ____BlendMode_Internal =
732
+ | 'normal'
733
+ | 'multiply'
734
+ | 'screen'
735
+ | 'overlay'
736
+ | 'darken'
737
+ | 'lighten'
738
+ | 'color-dodge'
739
+ | 'color-burn'
740
+ | 'hard-light'
741
+ | 'soft-light'
742
+ | 'difference'
743
+ | 'exclusion'
744
+ | 'hue'
745
+ | 'saturation'
746
+ | 'color'
747
+ | 'luminosity';
697
748
 
698
749
  export type ____ViewStyle_InternalCore = $ReadOnly<{
699
750
  ...$Exact<____LayoutStyle_Internal>,
700
751
  ...$Exact<____ShadowStyle_Internal>,
701
752
  ...$Exact<____TransformStyle_Internal>,
702
- ...____FilterStyle_Internal,
703
753
  backfaceVisibility?: 'visible' | 'hidden',
704
754
  backgroundColor?: ____ColorValue_Internal,
705
755
  borderColor?: ____ColorValue_Internal,
@@ -738,6 +788,11 @@ export type ____ViewStyle_InternalCore = $ReadOnly<{
738
788
  elevation?: number,
739
789
  pointerEvents?: 'auto' | 'none' | 'box-none' | 'box-only',
740
790
  cursor?: CursorValue,
791
+ experimental_boxShadow?: $ReadOnlyArray<BoxShadowPrimitive> | string,
792
+ experimental_filter?: $ReadOnlyArray<FilterFunction> | string,
793
+ experimental_mixBlendMode?: ____BlendMode_Internal,
794
+ experimental_backgroundImage?: $ReadOnlyArray<GradientValue> | string,
795
+ isolation?: 'auto' | 'isolate',
741
796
  }>;
742
797
 
743
798
  export type ____ViewStyle_Internal = $ReadOnly<{
@@ -0,0 +1,384 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @format
8
+ * @flow strict-local
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {ProcessedColorValue} from './processColor';
14
+ import type {GradientValue} from './StyleSheetTypes';
15
+
16
+ const processColor = require('./processColor').default;
17
+ const DIRECTION_REGEX =
18
+ /^to\s+(?:top|bottom|left|right)(?:\s+(?:top|bottom|left|right))?/;
19
+ const ANGLE_UNIT_REGEX = /^([+-]?\d*\.?\d+)(deg|grad|rad|turn)$/i;
20
+
21
+ const TO_BOTTOM_START_END_POINTS = {
22
+ start: {x: 0.5, y: 0},
23
+ end: {x: 0.5, y: 1},
24
+ };
25
+
26
+ type ParsedGradientValue = {
27
+ type: 'linearGradient',
28
+ start: {x: number, y: number},
29
+ end: {x: number, y: number},
30
+ colorStops: $ReadOnlyArray<{
31
+ color: ProcessedColorValue,
32
+ position: number,
33
+ }>,
34
+ };
35
+
36
+ export default function processBackgroundImage(
37
+ backgroundImage: ?($ReadOnlyArray<GradientValue> | string),
38
+ ): $ReadOnlyArray<ParsedGradientValue> {
39
+ let result: $ReadOnlyArray<ParsedGradientValue> = [];
40
+ if (backgroundImage == null) {
41
+ return result;
42
+ }
43
+
44
+ if (typeof backgroundImage === 'string') {
45
+ result = parseCSSLinearGradient(backgroundImage.replace(/\n/g, ' '));
46
+ } else if (Array.isArray(backgroundImage)) {
47
+ for (const bgImage of backgroundImage) {
48
+ const processedColorStops: Array<{
49
+ color: ProcessedColorValue,
50
+ position: number | null,
51
+ }> = [];
52
+ for (let index = 0; index < bgImage.colorStops.length; index++) {
53
+ const colorStop = bgImage.colorStops[index];
54
+ const processedColor = processColor(colorStop.color);
55
+ if (processedColor == null) {
56
+ // If a color is invalid, return an empty array and do not apply gradient. Same as web.
57
+ return [];
58
+ }
59
+ if (colorStop.positions != null && colorStop.positions.length > 0) {
60
+ for (const position of colorStop.positions) {
61
+ if (position.endsWith('%')) {
62
+ processedColorStops.push({
63
+ color: processedColor,
64
+ position: parseFloat(position) / 100,
65
+ });
66
+ } else {
67
+ // If a position is invalid, return an empty array and do not apply gradient. Same as web.
68
+ return [];
69
+ }
70
+ }
71
+ } else {
72
+ processedColorStops.push({
73
+ color: processedColor,
74
+ position: null,
75
+ });
76
+ }
77
+ }
78
+
79
+ let points: {
80
+ start: ParsedGradientValue['start'],
81
+ end: ParsedGradientValue['end'],
82
+ } | null = null;
83
+
84
+ if (typeof bgImage.direction === 'undefined') {
85
+ points = TO_BOTTOM_START_END_POINTS;
86
+ } else if (ANGLE_UNIT_REGEX.test(bgImage.direction)) {
87
+ const angle = parseAngle(bgImage.direction);
88
+ if (angle != null) {
89
+ points = calculateStartEndPointsFromAngle(angle);
90
+ }
91
+ } else if (DIRECTION_REGEX.test(bgImage.direction)) {
92
+ const processedPoints = calculateStartEndPointsFromDirection(
93
+ bgImage.direction,
94
+ );
95
+ if (processedPoints != null) {
96
+ points = processedPoints;
97
+ }
98
+ }
99
+
100
+ const fixedColorStops = getFixedColorStops(processedColorStops);
101
+
102
+ if (points != null) {
103
+ result = result.concat({
104
+ type: 'linearGradient',
105
+ start: points.start,
106
+ end: points.end,
107
+ colorStops: fixedColorStops,
108
+ });
109
+ }
110
+ }
111
+ }
112
+
113
+ return result;
114
+ }
115
+
116
+ function parseCSSLinearGradient(
117
+ cssString: string,
118
+ ): $ReadOnlyArray<ParsedGradientValue> {
119
+ const gradients = [];
120
+ let match;
121
+ const linearGradientRegex = /linear-gradient\s*\(((?:\([^)]*\)|[^())])*)\)/gi;
122
+
123
+ while ((match = linearGradientRegex.exec(cssString))) {
124
+ const gradientContent = match[1];
125
+ const parts = gradientContent.split(',');
126
+ let points = TO_BOTTOM_START_END_POINTS;
127
+ const trimmedDirection = parts[0].trim().toLowerCase();
128
+ const colorStopRegex =
129
+ /\s*((?:(?:rgba?|hsla?)\s*\([^)]+\))|#[0-9a-fA-F]+|[a-zA-Z]+)(?:\s+(-?[0-9.]+%?)(?:\s+(-?[0-9.]+%?))?)?\s*/gi;
130
+
131
+ if (ANGLE_UNIT_REGEX.test(trimmedDirection)) {
132
+ const angle = parseAngle(trimmedDirection);
133
+ if (angle != null) {
134
+ points = calculateStartEndPointsFromAngle(angle);
135
+ parts.shift();
136
+ } else {
137
+ // If an angle is invalid, return an empty array and do not apply any gradient. Same as web.
138
+ return [];
139
+ }
140
+ } else if (DIRECTION_REGEX.test(trimmedDirection)) {
141
+ const parsedPoints =
142
+ calculateStartEndPointsFromDirection(trimmedDirection);
143
+ if (parsedPoints != null) {
144
+ points = parsedPoints;
145
+ parts.shift();
146
+ } else {
147
+ // If a direction is invalid, return an empty array and do not apply any gradient. Same as web.
148
+ return [];
149
+ }
150
+ } else if (!colorStopRegex.test(trimmedDirection)) {
151
+ // If first part is not an angle/direction or a color stop, return an empty array and do not apply any gradient. Same as web.
152
+ return [];
153
+ }
154
+ colorStopRegex.lastIndex = 0;
155
+
156
+ const colorStops = [];
157
+ const fullColorStopsStr = parts.join(',');
158
+ let colorStopMatch;
159
+ while ((colorStopMatch = colorStopRegex.exec(fullColorStopsStr))) {
160
+ const [, color, position1, position2] = colorStopMatch;
161
+ const processedColor = processColor(color.trim().toLowerCase());
162
+ if (processedColor == null) {
163
+ // If a color is invalid, return an empty array and do not apply any gradient. Same as web.
164
+ return [];
165
+ }
166
+
167
+ if (typeof position1 !== 'undefined') {
168
+ if (position1.endsWith('%')) {
169
+ colorStops.push({
170
+ color: processedColor,
171
+ position: parseFloat(position1) / 100,
172
+ });
173
+ } else {
174
+ // If a position is invalid, return an empty array and do not apply any gradient. Same as web.
175
+ return [];
176
+ }
177
+ } else {
178
+ colorStops.push({
179
+ color: processedColor,
180
+ position: null,
181
+ });
182
+ }
183
+
184
+ if (typeof position2 !== 'undefined') {
185
+ if (position2.endsWith('%')) {
186
+ colorStops.push({
187
+ color: processedColor,
188
+ position: parseFloat(position2) / 100,
189
+ });
190
+ } else {
191
+ // If a position is invalid, return an empty array and do not apply any gradient. Same as web.
192
+ return [];
193
+ }
194
+ }
195
+ }
196
+
197
+ const fixedColorStops = getFixedColorStops(colorStops);
198
+
199
+ gradients.push({
200
+ type: 'linearGradient',
201
+ start: points.start,
202
+ end: points.end,
203
+ colorStops: fixedColorStops,
204
+ });
205
+ }
206
+
207
+ return gradients;
208
+ }
209
+
210
+ function calculateStartEndPointsFromDirection(direction: string): ?{
211
+ start: {x: number, y: number},
212
+ end: {x: number, y: number},
213
+ } {
214
+ // Remove extra whitespace
215
+ const normalizedDirection = direction.replace(/\s+/g, ' ');
216
+
217
+ switch (normalizedDirection) {
218
+ case 'to right':
219
+ return {
220
+ start: {x: 0, y: 0.5},
221
+ end: {x: 1, y: 0.5},
222
+ };
223
+ case 'to left':
224
+ return {
225
+ start: {x: 1, y: 0.5},
226
+ end: {x: 0, y: 0.5},
227
+ };
228
+ case 'to bottom':
229
+ return TO_BOTTOM_START_END_POINTS;
230
+ case 'to top':
231
+ return {
232
+ start: {x: 0.5, y: 1},
233
+ end: {x: 0.5, y: 0},
234
+ };
235
+ case 'to bottom right':
236
+ case 'to right bottom':
237
+ return {
238
+ start: {x: 0, y: 0},
239
+ end: {x: 1, y: 1},
240
+ };
241
+ case 'to top left':
242
+ case 'to left top':
243
+ return {
244
+ start: {x: 1, y: 1},
245
+ end: {x: 0, y: 0},
246
+ };
247
+ case 'to bottom left':
248
+ case 'to left bottom':
249
+ return {
250
+ start: {x: 1, y: 0},
251
+ end: {x: 0, y: 1},
252
+ };
253
+ case 'to top right':
254
+ case 'to right top':
255
+ return {
256
+ start: {x: 0, y: 1},
257
+ end: {x: 1, y: 0},
258
+ };
259
+ default:
260
+ return null;
261
+ }
262
+ }
263
+
264
+ function calculateStartEndPointsFromAngle(angleRadians: number): {
265
+ start: {x: number, y: number},
266
+ end: {x: number, y: number},
267
+ } {
268
+ // Normalize angle to be between 0 and 2π
269
+ let angleRadiansNormalized = angleRadians % (2 * Math.PI);
270
+ if (angleRadiansNormalized < 0) {
271
+ angleRadiansNormalized += 2 * Math.PI;
272
+ }
273
+
274
+ const endX = 0.5 + 0.5 * Math.sin(angleRadiansNormalized);
275
+ const endY = 0.5 - 0.5 * Math.cos(angleRadiansNormalized);
276
+
277
+ const startX = 1 - endX;
278
+ const startY = 1 - endY;
279
+
280
+ return {
281
+ start: {x: startX, y: startY},
282
+ end: {x: endX, y: endY},
283
+ };
284
+ }
285
+
286
+ function parseAngle(angle: string): ?number {
287
+ const match = angle.match(ANGLE_UNIT_REGEX);
288
+ if (!match) {
289
+ return null;
290
+ }
291
+
292
+ const [, value, unit] = match;
293
+
294
+ const numericValue = parseFloat(value);
295
+ switch (unit) {
296
+ case 'deg':
297
+ return (numericValue * Math.PI) / 180;
298
+ case 'grad':
299
+ return (numericValue * Math.PI) / 200;
300
+ case 'rad':
301
+ return numericValue;
302
+ case 'turn':
303
+ return numericValue * 2 * Math.PI;
304
+ default:
305
+ return null;
306
+ }
307
+ }
308
+
309
+ // https://drafts.csswg.org/css-images-4/#color-stop-fixup
310
+ function getFixedColorStops(
311
+ colorStops: $ReadOnlyArray<{
312
+ color: ProcessedColorValue,
313
+ position: number | null,
314
+ }>,
315
+ ): Array<{
316
+ color: ProcessedColorValue,
317
+ position: number,
318
+ }> {
319
+ let fixedColorStops: Array<{
320
+ color: ProcessedColorValue,
321
+ position: number,
322
+ }> = [];
323
+ let hasNullPositions = false;
324
+ let maxPositionSoFar = colorStops[0].position ?? 0;
325
+ for (let i = 0; i < colorStops.length; i++) {
326
+ const colorStop = colorStops[i];
327
+ let newPosition = colorStop.position;
328
+ if (newPosition === null) {
329
+ // Step 1:
330
+ // If the first color stop does not have a position,
331
+ // set its position to 0%. If the last color stop does not have a position,
332
+ // set its position to 100%.
333
+ if (i === 0) {
334
+ newPosition = 0;
335
+ } else if (i === colorStops.length - 1) {
336
+ newPosition = 1;
337
+ }
338
+ }
339
+ // Step 2:
340
+ // If a color stop or transition hint has a position
341
+ // that is less than the specified position of any color stop or transition hint
342
+ // before it in the list, set its position to be equal to the
343
+ // largest specified position of any color stop or transition hint before it.
344
+ if (newPosition !== null) {
345
+ newPosition = Math.max(newPosition, maxPositionSoFar);
346
+ fixedColorStops[i] = {
347
+ color: colorStop.color,
348
+ position: newPosition,
349
+ };
350
+ maxPositionSoFar = newPosition;
351
+ } else {
352
+ hasNullPositions = true;
353
+ }
354
+ }
355
+
356
+ // Step 3:
357
+ // If any color stop still does not have a position,
358
+ // then, for each run of adjacent color stops without positions,
359
+ // set their positions so that they are evenly spaced between the preceding and
360
+ // following color stops with positions.
361
+ if (hasNullPositions) {
362
+ let lastDefinedIndex = 0;
363
+ for (let i = 1; i < fixedColorStops.length; i++) {
364
+ if (fixedColorStops[i] !== undefined) {
365
+ const unpositionedStops = i - lastDefinedIndex - 1;
366
+ if (unpositionedStops > 0) {
367
+ const startPosition = fixedColorStops[lastDefinedIndex].position;
368
+ const endPosition = fixedColorStops[i].position;
369
+ const increment =
370
+ (endPosition - startPosition) / (unpositionedStops + 1);
371
+ for (let j = 1; j <= unpositionedStops; j++) {
372
+ fixedColorStops[lastDefinedIndex + j] = {
373
+ color: colorStops[lastDefinedIndex + j].color,
374
+ position: startPosition + increment * j,
375
+ };
376
+ }
377
+ }
378
+ lastDefinedIndex = i;
379
+ }
380
+ }
381
+ }
382
+
383
+ return fixedColorStops;
384
+ }
@@ -0,0 +1,211 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict-local
8
+ * @format
9
+ * @oncall react-native
10
+ */
11
+
12
+ import type {ProcessedColorValue} from './processColor';
13
+ import type {BoxShadowPrimitive} from './StyleSheetTypes';
14
+
15
+ import processColor from './processColor';
16
+
17
+ export type ParsedBoxShadow = {
18
+ offsetX: number,
19
+ offsetY: number,
20
+ color?: ProcessedColorValue,
21
+ blurRadius?: number,
22
+ spreadDistance?: number,
23
+ inset?: boolean,
24
+ };
25
+
26
+ export default function processBoxShadow(
27
+ rawBoxShadows: ?($ReadOnlyArray<BoxShadowPrimitive> | string),
28
+ ): Array<ParsedBoxShadow> {
29
+ const result: Array<ParsedBoxShadow> = [];
30
+ if (rawBoxShadows == null) {
31
+ return result;
32
+ }
33
+
34
+ const boxShadowList =
35
+ typeof rawBoxShadows === 'string'
36
+ ? parseBoxShadowString(rawBoxShadows.replace(/\n/g, ' '))
37
+ : rawBoxShadows;
38
+
39
+ for (const rawBoxShadow of boxShadowList) {
40
+ const parsedBoxShadow: ParsedBoxShadow = {
41
+ offsetX: 0,
42
+ offsetY: 0,
43
+ };
44
+
45
+ let value;
46
+ for (const arg in rawBoxShadow) {
47
+ switch (arg) {
48
+ case 'offsetX':
49
+ value =
50
+ typeof rawBoxShadow.offsetX === 'string'
51
+ ? parseLength(rawBoxShadow.offsetX)
52
+ : rawBoxShadow.offsetX;
53
+ if (value == null) {
54
+ return [];
55
+ }
56
+
57
+ parsedBoxShadow.offsetX = value;
58
+ break;
59
+ case 'offsetY':
60
+ value =
61
+ typeof rawBoxShadow.offsetY === 'string'
62
+ ? parseLength(rawBoxShadow.offsetY)
63
+ : rawBoxShadow.offsetY;
64
+ if (value == null) {
65
+ return [];
66
+ }
67
+
68
+ parsedBoxShadow.offsetY = value;
69
+ break;
70
+ case 'spreadDistance':
71
+ value =
72
+ typeof rawBoxShadow.spreadDistance === 'string'
73
+ ? parseLength(rawBoxShadow.spreadDistance)
74
+ : rawBoxShadow.spreadDistance;
75
+ if (value == null) {
76
+ return [];
77
+ }
78
+
79
+ parsedBoxShadow.spreadDistance = value;
80
+ break;
81
+ case 'blurRadius':
82
+ value =
83
+ typeof rawBoxShadow.blurRadius === 'string'
84
+ ? parseLength(rawBoxShadow.blurRadius)
85
+ : rawBoxShadow.blurRadius;
86
+ if (value == null || value < 0) {
87
+ return [];
88
+ }
89
+
90
+ parsedBoxShadow.blurRadius = value;
91
+ break;
92
+ case 'color':
93
+ const color = processColor(rawBoxShadow.color);
94
+ if (color == null) {
95
+ return [];
96
+ }
97
+
98
+ parsedBoxShadow.color = color;
99
+ break;
100
+ case 'inset':
101
+ parsedBoxShadow.inset = rawBoxShadow.inset;
102
+ }
103
+ }
104
+ result.push(parsedBoxShadow);
105
+ }
106
+ return result;
107
+ }
108
+
109
+ function parseBoxShadowString(
110
+ rawBoxShadows: string,
111
+ ): Array<BoxShadowPrimitive> {
112
+ let result: Array<BoxShadowPrimitive> = [];
113
+
114
+ for (const rawBoxShadow of rawBoxShadows
115
+ .split(/,(?![^()]*\))/) // split by comma that is not in parenthesis
116
+ .map(bS => bS.trim())
117
+ .filter(bS => bS !== '')) {
118
+ const boxShadow: BoxShadowPrimitive = {
119
+ offsetX: 0,
120
+ offsetY: 0,
121
+ };
122
+ let offsetX: number | string;
123
+ let offsetY: number | string;
124
+ let keywordDetectedAfterLength = false;
125
+
126
+ let lengthCount = 0;
127
+
128
+ // split rawBoxShadow string by all whitespaces that are not in parenthesis
129
+ const args = rawBoxShadow.split(/\s+(?![^(]*\))/);
130
+ for (const arg of args) {
131
+ const processedColor = processColor(arg);
132
+ if (processedColor != null) {
133
+ if (boxShadow.color != null) {
134
+ return [];
135
+ }
136
+ if (offsetX != null) {
137
+ keywordDetectedAfterLength = true;
138
+ }
139
+ boxShadow.color = arg;
140
+ continue;
141
+ }
142
+
143
+ if (arg === 'inset') {
144
+ if (boxShadow.inset != null) {
145
+ return [];
146
+ }
147
+ if (offsetX != null) {
148
+ keywordDetectedAfterLength = true;
149
+ }
150
+ boxShadow.inset = true;
151
+ continue;
152
+ }
153
+
154
+ switch (lengthCount) {
155
+ case 0:
156
+ offsetX = arg;
157
+ lengthCount++;
158
+ break;
159
+ case 1:
160
+ if (keywordDetectedAfterLength) {
161
+ return [];
162
+ }
163
+ offsetY = arg;
164
+ lengthCount++;
165
+ break;
166
+ case 2:
167
+ if (keywordDetectedAfterLength) {
168
+ return [];
169
+ }
170
+ boxShadow.blurRadius = arg;
171
+ lengthCount++;
172
+ break;
173
+ case 3:
174
+ if (keywordDetectedAfterLength) {
175
+ return [];
176
+ }
177
+ boxShadow.spreadDistance = arg;
178
+ lengthCount++;
179
+ break;
180
+ default:
181
+ return [];
182
+ }
183
+ }
184
+
185
+ if (offsetX == null || offsetY == null) {
186
+ return [];
187
+ }
188
+
189
+ boxShadow.offsetX = offsetX;
190
+ boxShadow.offsetY = offsetY;
191
+
192
+ result.push(boxShadow);
193
+ }
194
+ return result;
195
+ }
196
+
197
+ function parseLength(length: string): ?number {
198
+ // matches on args with units like "1.5 5% -80deg"
199
+ const argsWithUnitsRegex = /([+-]?\d*(\.\d+)?)([\w\W]+)?/g;
200
+ const match = argsWithUnitsRegex.exec(length);
201
+
202
+ if (!match || Number.isNaN(match[1])) {
203
+ return null;
204
+ }
205
+
206
+ if (match[3] != null && match[3] !== 'px') {
207
+ return null;
208
+ }
209
+
210
+ return Number(match[1]);
211
+ }