Skip to content

Commit 562a974

Browse files
zeyapfacebook-github-bot
authored andcommitted
(js part) Support PlatformColor type of toValue and interpolation outputRange (facebook#54457)
Summary: ## Changelog: [General] [Added] (js part) Support PlatformColor type of toValue and interpolation outputRange Reviewed By: javache Differential Revision: D86255713
1 parent 1859245 commit 562a974

File tree

5 files changed

+65
-32
lines changed

5 files changed

+65
-32
lines changed

packages/react-native/Libraries/Animated/createAnimatedComponent.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @format
99
*/
1010

11+
import type {NativeColorValue} from '../StyleSheet/StyleSheetTypes';
1112
import type AnimatedAddition from './nodes/AnimatedAddition';
1213
import type AnimatedDiffClamp from './nodes/AnimatedDiffClamp';
1314
import type AnimatedDivision from './nodes/AnimatedDivision';
@@ -46,6 +47,7 @@ export type WithAnimatedValue<+T> = T extends Builtin | Nullable
4647
| AnimatedInterpolation<number | string>
4748
| AnimatedInterpolation<number>
4849
| AnimatedInterpolation<string>
50+
| AnimatedInterpolation<NativeColorValue>
4951
: T extends $ReadOnlyArray<infer P>
5052
? $ReadOnlyArray<WithAnimatedValue<P>>
5153
: T extends {...}

packages/react-native/Libraries/Animated/nodes/AnimatedInterpolation.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
'use strict';
1414

15+
import type {NativeColorValue} from '../../StyleSheet/StyleSheetTypes';
1516
import type {PlatformConfig} from '../AnimatedPlatformConfig';
1617
import type AnimatedNode from './AnimatedNode';
1718
import type {AnimatedNodeConfig} from './AnimatedNode';
@@ -26,7 +27,9 @@ import invariant from 'invariant';
2627

2728
type ExtrapolateType = 'extend' | 'identity' | 'clamp';
2829

29-
export type InterpolationConfigType<OutputT: number | string> = $ReadOnly<{
30+
export type InterpolationConfigType<
31+
OutputT: number | string | NativeColorValue,
32+
> = $ReadOnly<{
3033
...AnimatedNodeConfig,
3134
inputRange: $ReadOnlyArray<number>,
3235
outputRange: $ReadOnlyArray<OutputT>,
@@ -82,6 +85,23 @@ function createNumericInterpolation(
8285
};
8386
}
8487

88+
function createPlatformColorInterpolation(
89+
config: InterpolationConfigType<NativeColorValue>,
90+
): (input: number) => NativeColorValue {
91+
const outputRange = config.outputRange;
92+
const outputRangeIndices = Array.from(Array(outputRange.length).keys());
93+
const interpolateIndex = createNumericInterpolation({
94+
...config,
95+
inputRange: config.inputRange,
96+
outputRange: outputRangeIndices,
97+
});
98+
99+
return input => {
100+
// PlatformColor interpolation should happen natively, here we just use the closest color
101+
return outputRange[Math.floor(interpolateIndex(input))];
102+
};
103+
}
104+
85105
function interpolate(
86106
input: number,
87107
inputMin: number,
@@ -277,7 +297,7 @@ function findRange(input: number, inputRange: $ReadOnlyArray<number>) {
277297
return i - 1;
278298
}
279299

280-
function checkValidRanges<OutputT: number | string>(
300+
function checkValidRanges<OutputT: number | string | NativeColorValue>(
281301
inputRange: $ReadOnlyArray<number>,
282302
outputRange: $ReadOnlyArray<OutputT>,
283303
) {
@@ -304,7 +324,7 @@ function checkValidInputRange(arr: $ReadOnlyArray<number>) {
304324
}
305325
}
306326

307-
function checkInfiniteRange<OutputT: number | string>(
327+
function checkInfiniteRange<OutputT: number | string | NativeColorValue>(
308328
name: string,
309329
arr: $ReadOnlyArray<OutputT>,
310330
) {
@@ -322,7 +342,7 @@ function checkInfiniteRange<OutputT: number | string>(
322342
}
323343

324344
export default class AnimatedInterpolation<
325-
OutputT: number | string,
345+
OutputT: number | string | NativeColorValue,
326346
> extends AnimatedWithChildren {
327347
_parent: AnimatedNode;
328348
_config: InterpolationConfigType<OutputT>;
@@ -347,6 +367,10 @@ export default class AnimatedInterpolation<
347367
const config = this._config;
348368
if (config.outputRange && typeof config.outputRange[0] === 'string') {
349369
this._interpolation = (createStringInterpolation((config: any)): any);
370+
} else if (typeof config.outputRange[0] === 'object') {
371+
this._interpolation = (createPlatformColorInterpolation(
372+
(config: any),
373+
): any);
350374
} else {
351375
this._interpolation = (createNumericInterpolation((config: any)): any);
352376
}
@@ -403,6 +427,8 @@ export default class AnimatedInterpolation<
403427
return NativeAnimatedHelper.transformDataType(value);
404428
}
405429
}): any);
430+
} else if (typeof outputRange[0] === 'object') {
431+
outputType = 'platform_color';
406432
}
407433

408434
return {

packages/react-native/Libraries/Animated/nodes/AnimatedValue.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @format
99
*/
1010

11+
import type {NativeColorValue} from '../../StyleSheet/StyleSheetTypes';
1112
import type {EventSubscription} from '../../vendor/emitter/EventEmitter';
1213
import type {PlatformConfig} from '../AnimatedPlatformConfig';
1314
import type Animation from '../animations/Animation';
@@ -298,7 +299,7 @@ export default class AnimatedValue extends AnimatedWithChildren {
298299
* Interpolates the value before updating the property, e.g. mapping 0-1 to
299300
* 0-10.
300301
*/
301-
interpolate<OutputT: number | string>(
302+
interpolate<OutputT: number | string | NativeColorValue>(
302303
config: InterpolationConfigType<OutputT>,
303304
): AnimatedInterpolation<OutputT> {
304305
return new AnimatedInterpolation(this, config);

packages/react-native/ReactNativeApi.d.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<1eb51bd683ea578d108218d5a48c42ec>>
7+
* @generated SignedSource<<8907cb13f6d6798b38a019f18b769637>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -1453,7 +1453,7 @@ declare class AnimatedEvent {
14531453
)
14541454
}
14551455
declare class AnimatedInterpolation_default<
1456-
OutputT extends number | string,
1456+
OutputT extends NativeColorValue | number | string,
14571457
> extends AnimatedWithChildren_default {
14581458
constructor(
14591459
parent: AnimatedNode_default,
@@ -1534,7 +1534,7 @@ declare class AnimatedValue_default extends AnimatedWithChildren_default {
15341534
constructor(value: number, config?: AnimatedValueConfig | null | undefined)
15351535
extractOffset(): void
15361536
flattenOffset(): void
1537-
interpolate<OutputT extends number | string>(
1537+
interpolate<OutputT extends NativeColorValue | number | string>(
15381538
config: InterpolationConfigType<OutputT>,
15391539
): AnimatedInterpolation_default<OutputT>
15401540
removeAllListeners(): void
@@ -2766,17 +2766,18 @@ declare type InternalTextInput = (
27662766
ref?: React.Ref<TextInputInstance>
27672767
},
27682768
) => React.ReactNode
2769-
declare type InterpolationConfigType<OutputT extends number | string> =
2770-
Readonly<
2771-
AnimatedNodeConfig & {
2772-
extrapolate?: ExtrapolateType
2773-
extrapolateLeft?: ExtrapolateType
2774-
extrapolateRight?: ExtrapolateType
2775-
inputRange: ReadonlyArray<number>
2776-
outputRange: ReadonlyArray<OutputT>
2777-
easing?: (input: number) => number
2778-
}
2779-
>
2769+
declare type InterpolationConfigType<
2770+
OutputT extends NativeColorValue | number | string,
2771+
> = Readonly<
2772+
AnimatedNodeConfig & {
2773+
extrapolate?: ExtrapolateType
2774+
extrapolateLeft?: ExtrapolateType
2775+
extrapolateRight?: ExtrapolateType
2776+
inputRange: ReadonlyArray<number>
2777+
outputRange: ReadonlyArray<OutputT>
2778+
easing?: (input: number) => number
2779+
}
2780+
>
27802781
declare type IOSKeyboardEvent = Readonly<
27812782
BaseKeyboardEvent & {
27822783
isEventFromThisApp: boolean
@@ -5417,6 +5418,7 @@ declare type TimingAnimationConfig = Readonly<
54175418
| AnimatedInterpolation_default<number>
54185419
| AnimatedValue_default
54195420
| AnimatedValueXY_default
5421+
| NativeColorValue
54205422
| number
54215423
| RgbaValue
54225424
| {
@@ -5899,6 +5901,7 @@ declare type WithAnimatedValue<T> = T extends Builtin | Nullable
58995901
| AnimatedAddition_default
59005902
| AnimatedDiffClamp_default
59015903
| AnimatedDivision_default
5904+
| AnimatedInterpolation_default<NativeColorValue>
59025905
| AnimatedInterpolation_default<number | string>
59035906
| AnimatedInterpolation_default<number>
59045907
| AnimatedInterpolation_default<string>
@@ -5940,7 +5943,7 @@ export {
59405943
AlertOptions, // a0cdac0f
59415944
AlertType, // 5ab91217
59425945
AndroidKeyboardEvent, // e03becc8
5943-
Animated, // 6b6a0b2e
5946+
Animated, // 1b188caf
59445947
AppConfig, // ebddad4b
59455948
AppRegistry, // 6cdee1d6
59465949
AppState, // f7097b1b
@@ -5986,8 +5989,8 @@ export {
59865989
EventSubscription, // b8d084aa
59875990
ExtendedExceptionData, // 5a6ccf5a
59885991
FilterFunction, // bf24c0e3
5989-
FlatList, // cbb48cbe
5990-
FlatListProps, // 451be810
5992+
FlatList, // 977c3d2d
5993+
FlatListProps, // 86e2dcab
59915994
FocusEvent, // 529b43eb
59925995
FontVariant, // 7c7558bb
59935996
GestureResponderEvent, // b466f6d6
@@ -6120,14 +6123,14 @@ export {
61206123
ScrollToLocationParamsType, // d7ecdad1
61216124
ScrollView, // 7fb7c469
61226125
ScrollViewImperativeMethods, // eb20aa46
6123-
ScrollViewProps, // 27986ff5
6126+
ScrollViewProps, // 912ae116
61246127
ScrollViewPropsAndroid, // 84e2134b
61256128
ScrollViewPropsIOS, // d83c9733
61266129
ScrollViewScrollToOptions, // 3313411e
61276130
SectionBase, // b376bddc
6128-
SectionList, // ff1193b2
6131+
SectionList, // 5b815722
61296132
SectionListData, // 119baf83
6130-
SectionListProps, // c9ac8e07
6133+
SectionListProps, // b97e7451
61316134
SectionListRenderItem, // 1fad0435
61326135
SectionListRenderItemInfo, // 745e1992
61336136
Separators, // 6a45f7e3
@@ -6176,7 +6179,7 @@ export {
61766179
TouchableNativeFeedback, // aaa5b42c
61776180
TouchableNativeFeedbackProps, // 372d3213
61786181
TouchableOpacity, // 7e33acfd
6179-
TouchableOpacityProps, // ba6c0ba4
6182+
TouchableOpacityProps, // 80b3a106
61806183
TouchableWithoutFeedback, // 7363a906
61816184
TouchableWithoutFeedbackProps, // 68e3d87f
61826185
TransformsStyle, // 65e70f18
@@ -6192,17 +6195,17 @@ export {
61926195
ViewStyle, // c2db0e6e
61936196
VirtualViewMode, // 85a69ef6
61946197
VirtualizedList, // 4d513939
6195-
VirtualizedListProps, // a99d36db
6198+
VirtualizedListProps, // 1e67a9a5
61966199
VirtualizedSectionList, // 446ba0df
6197-
VirtualizedSectionListProps, // 6cd4b378
6200+
VirtualizedSectionListProps, // b1ad0bbb
61986201
WrapperComponentProvider, // 9cf3844c
61996202
codegenNativeCommands, // e16d62f7
62006203
codegenNativeComponent, // ed4c8103
62016204
findNodeHandle, // c6b08494
62026205
processColor, // 00453092
62036206
registerCallableModule, // 86b65226
62046207
requireNativeComponent, // e659aa32
6205-
useAnimatedValue, // 2c80bbc2
6208+
useAnimatedValue, // 125eb11a
62066209
useColorScheme, // c216d6f7
62076210
useWindowDimensions, // bb4b683f
62086211
}

packages/react-native/src/private/animated/NativeAnimatedValidation.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
*/
1010

1111
import type {InterpolationConfigType} from '../../../Libraries/Animated/nodes/AnimatedInterpolation';
12+
import type {NativeColorValue} from '../../../Libraries/StyleSheet/StyleSheetTypes';
1213

1314
import {
1415
isSupportedInterpolationParam,
1516
isSupportedStyleProp,
1617
isSupportedTransformProp,
1718
} from '../../../Libraries/Animated/NativeAnimatedAllowlist';
1819

19-
export function validateInterpolation<OutputT: number | string>(
20-
config: InterpolationConfigType<OutputT>,
21-
): void {
20+
export function validateInterpolation<
21+
OutputT: number | string | NativeColorValue,
22+
>(config: InterpolationConfigType<OutputT>): void {
2223
for (const key in config) {
2324
if (key !== 'debugID' && !isSupportedInterpolationParam(key)) {
2425
console.error(

0 commit comments

Comments
 (0)