From 8b6b47bd57f5b3d47d729c335c304b4c744576d7 Mon Sep 17 00:00:00 2001 From: Zeya Peng Date: Mon, 17 Nov 2025 07:18:05 -0800 Subject: [PATCH] (js part) Support PlatformColor type of toValue and interpolation outputRange (#54457) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54457 ## Changelog: [General] [Added] (js part) Support PlatformColor type of toValue and interpolation outputRange Reviewed By: javache Differential Revision: D86255713 fbshipit-source-id: e6b20924ababcfb128537f532315b751e50d1bc6 --- .../Animated/createAnimatedComponent.js | 2 + .../Animated/nodes/AnimatedInterpolation.js | 44 +++++++++++++-- .../Libraries/Animated/nodes/AnimatedValue.js | 7 ++- packages/react-native/ReactNativeApi.d.ts | 54 ++++++++++--------- .../animated/NativeAnimatedValidation.js | 11 ++-- 5 files changed, 84 insertions(+), 34 deletions(-) diff --git a/packages/react-native/Libraries/Animated/createAnimatedComponent.js b/packages/react-native/Libraries/Animated/createAnimatedComponent.js index 78b01f90ba3..3238bbd862b 100644 --- a/packages/react-native/Libraries/Animated/createAnimatedComponent.js +++ b/packages/react-native/Libraries/Animated/createAnimatedComponent.js @@ -8,6 +8,7 @@ * @format */ +import type {NativeColorValue} from '../StyleSheet/StyleSheetTypes'; import type AnimatedAddition from './nodes/AnimatedAddition'; import type AnimatedDiffClamp from './nodes/AnimatedDiffClamp'; import type AnimatedDivision from './nodes/AnimatedDivision'; @@ -46,6 +47,7 @@ export type WithAnimatedValue<+T> = T extends Builtin | Nullable | AnimatedInterpolation | AnimatedInterpolation | AnimatedInterpolation + | AnimatedInterpolation : T extends $ReadOnlyArray ? $ReadOnlyArray> : T extends {...} diff --git a/packages/react-native/Libraries/Animated/nodes/AnimatedInterpolation.js b/packages/react-native/Libraries/Animated/nodes/AnimatedInterpolation.js index e49369d7597..0372262d11d 100644 --- a/packages/react-native/Libraries/Animated/nodes/AnimatedInterpolation.js +++ b/packages/react-native/Libraries/Animated/nodes/AnimatedInterpolation.js @@ -12,6 +12,7 @@ 'use strict'; +import type {NativeColorValue} from '../../StyleSheet/StyleSheetTypes'; import type {PlatformConfig} from '../AnimatedPlatformConfig'; import type AnimatedNode from './AnimatedNode'; import type {AnimatedNodeConfig} from './AnimatedNode'; @@ -26,7 +27,14 @@ import invariant from 'invariant'; type ExtrapolateType = 'extend' | 'identity' | 'clamp'; -export type InterpolationConfigType = $ReadOnly<{ +export type InterpolationConfigSupportedOutputType = + | number + | string + | NativeColorValue; + +export type InterpolationConfigType< + OutputT: InterpolationConfigSupportedOutputType, +> = $ReadOnly<{ ...AnimatedNodeConfig, inputRange: $ReadOnlyArray, outputRange: $ReadOnlyArray, @@ -82,6 +90,28 @@ function createNumericInterpolation( }; } +function createPlatformColorInterpolation( + config: InterpolationConfigType, +): (input: number) => NativeColorValue { + const outputRange = config.outputRange; + const outputRangeIndices = Array.from(Array(outputRange.length).keys()); + const interpolateIndex = createNumericInterpolation({ + ...config, + inputRange: config.inputRange, + outputRange: outputRangeIndices, + }); + + return input => { + const interpolateResult = interpolateIndex(input); + if (!Number.isInteger(interpolateResult)) { + console.warn( + 'PlatformColor interpolation should happen natively, here we fallback to the closest color', + ); + } + return outputRange[Math.floor(interpolateResult)]; + }; +} + function interpolate( input: number, inputMin: number, @@ -277,7 +307,7 @@ function findRange(input: number, inputRange: $ReadOnlyArray) { return i - 1; } -function checkValidRanges( +function checkValidRanges( inputRange: $ReadOnlyArray, outputRange: $ReadOnlyArray, ) { @@ -304,7 +334,7 @@ function checkValidInputRange(arr: $ReadOnlyArray) { } } -function checkInfiniteRange( +function checkInfiniteRange( name: string, arr: $ReadOnlyArray, ) { @@ -322,7 +352,7 @@ function checkInfiniteRange( } export default class AnimatedInterpolation< - OutputT: number | string, + OutputT: InterpolationConfigSupportedOutputType, > extends AnimatedWithChildren { _parent: AnimatedNode; _config: InterpolationConfigType; @@ -347,6 +377,10 @@ export default class AnimatedInterpolation< const config = this._config; if (config.outputRange && typeof config.outputRange[0] === 'string') { this._interpolation = (createStringInterpolation((config: any)): any); + } else if (typeof config.outputRange[0] === 'object') { + this._interpolation = (createPlatformColorInterpolation( + (config: any), + ): any); } else { this._interpolation = (createNumericInterpolation((config: any)): any); } @@ -403,6 +437,8 @@ export default class AnimatedInterpolation< return NativeAnimatedHelper.transformDataType(value); } }): any); + } else if (typeof outputRange[0] === 'object') { + outputType = 'platform_color'; } return { diff --git a/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js b/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js index 6699021fe79..99f038fbce7 100644 --- a/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js +++ b/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js @@ -12,7 +12,10 @@ import type {EventSubscription} from '../../vendor/emitter/EventEmitter'; import type {PlatformConfig} from '../AnimatedPlatformConfig'; import type Animation from '../animations/Animation'; import type {EndCallback} from '../animations/Animation'; -import type {InterpolationConfigType} from './AnimatedInterpolation'; +import type { + InterpolationConfigSupportedOutputType, + InterpolationConfigType, +} from './AnimatedInterpolation'; import type AnimatedNode from './AnimatedNode'; import type {AnimatedNodeConfig} from './AnimatedNode'; import type AnimatedTracking from './AnimatedTracking'; @@ -298,7 +301,7 @@ export default class AnimatedValue extends AnimatedWithChildren { * Interpolates the value before updating the property, e.g. mapping 0-1 to * 0-10. */ - interpolate( + interpolate( config: InterpolationConfigType, ): AnimatedInterpolation { return new AnimatedInterpolation(this, config); diff --git a/packages/react-native/ReactNativeApi.d.ts b/packages/react-native/ReactNativeApi.d.ts index a8bb1794703..3dff962fc17 100644 --- a/packages/react-native/ReactNativeApi.d.ts +++ b/packages/react-native/ReactNativeApi.d.ts @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<1eb51bd683ea578d108218d5a48c42ec>> + * @generated SignedSource<> * * This file was generated by scripts/js-api/build-types/index.js. */ @@ -1453,7 +1453,7 @@ declare class AnimatedEvent { ) } declare class AnimatedInterpolation_default< - OutputT extends number | string, + OutputT extends InterpolationConfigSupportedOutputType, > extends AnimatedWithChildren_default { constructor( parent: AnimatedNode_default, @@ -1534,7 +1534,7 @@ declare class AnimatedValue_default extends AnimatedWithChildren_default { constructor(value: number, config?: AnimatedValueConfig | null | undefined) extractOffset(): void flattenOffset(): void - interpolate( + interpolate( config: InterpolationConfigType, ): AnimatedInterpolation_default removeAllListeners(): void @@ -2766,17 +2766,22 @@ declare type InternalTextInput = ( ref?: React.Ref }, ) => React.ReactNode -declare type InterpolationConfigType = - Readonly< - AnimatedNodeConfig & { - extrapolate?: ExtrapolateType - extrapolateLeft?: ExtrapolateType - extrapolateRight?: ExtrapolateType - inputRange: ReadonlyArray - outputRange: ReadonlyArray - easing?: (input: number) => number - } - > +declare type InterpolationConfigSupportedOutputType = + | NativeColorValue + | number + | string +declare type InterpolationConfigType< + OutputT extends InterpolationConfigSupportedOutputType, +> = Readonly< + AnimatedNodeConfig & { + extrapolate?: ExtrapolateType + extrapolateLeft?: ExtrapolateType + extrapolateRight?: ExtrapolateType + inputRange: ReadonlyArray + outputRange: ReadonlyArray + easing?: (input: number) => number + } +> declare type IOSKeyboardEvent = Readonly< BaseKeyboardEvent & { isEventFromThisApp: boolean @@ -5899,6 +5904,7 @@ declare type WithAnimatedValue = T extends Builtin | Nullable | AnimatedAddition_default | AnimatedDiffClamp_default | AnimatedDivision_default + | AnimatedInterpolation_default | AnimatedInterpolation_default | AnimatedInterpolation_default | AnimatedInterpolation_default @@ -5940,7 +5946,7 @@ export { AlertOptions, // a0cdac0f AlertType, // 5ab91217 AndroidKeyboardEvent, // e03becc8 - Animated, // 6b6a0b2e + Animated, // e48a2408 AppConfig, // ebddad4b AppRegistry, // 6cdee1d6 AppState, // f7097b1b @@ -5986,8 +5992,8 @@ export { EventSubscription, // b8d084aa ExtendedExceptionData, // 5a6ccf5a FilterFunction, // bf24c0e3 - FlatList, // cbb48cbe - FlatListProps, // 451be810 + FlatList, // ee0215d3 + FlatListProps, // 53c344dc FocusEvent, // 529b43eb FontVariant, // 7c7558bb GestureResponderEvent, // b466f6d6 @@ -6120,14 +6126,14 @@ export { ScrollToLocationParamsType, // d7ecdad1 ScrollView, // 7fb7c469 ScrollViewImperativeMethods, // eb20aa46 - ScrollViewProps, // 27986ff5 + ScrollViewProps, // bb4d0dd0 ScrollViewPropsAndroid, // 84e2134b ScrollViewPropsIOS, // d83c9733 ScrollViewScrollToOptions, // 3313411e SectionBase, // b376bddc - SectionList, // ff1193b2 + SectionList, // 84642036 SectionListData, // 119baf83 - SectionListProps, // c9ac8e07 + SectionListProps, // 4fe82009 SectionListRenderItem, // 1fad0435 SectionListRenderItemInfo, // 745e1992 Separators, // 6a45f7e3 @@ -6176,7 +6182,7 @@ export { TouchableNativeFeedback, // aaa5b42c TouchableNativeFeedbackProps, // 372d3213 TouchableOpacity, // 7e33acfd - TouchableOpacityProps, // ba6c0ba4 + TouchableOpacityProps, // 73c84562 TouchableWithoutFeedback, // 7363a906 TouchableWithoutFeedbackProps, // 68e3d87f TransformsStyle, // 65e70f18 @@ -6192,9 +6198,9 @@ export { ViewStyle, // c2db0e6e VirtualViewMode, // 85a69ef6 VirtualizedList, // 4d513939 - VirtualizedListProps, // a99d36db + VirtualizedListProps, // 06d07d2b VirtualizedSectionList, // 446ba0df - VirtualizedSectionListProps, // 6cd4b378 + VirtualizedSectionListProps, // 05937609 WrapperComponentProvider, // 9cf3844c codegenNativeCommands, // e16d62f7 codegenNativeComponent, // ed4c8103 @@ -6202,7 +6208,7 @@ export { processColor, // 00453092 registerCallableModule, // 86b65226 requireNativeComponent, // e659aa32 - useAnimatedValue, // 2c80bbc2 + useAnimatedValue, // b18adb63 useColorScheme, // c216d6f7 useWindowDimensions, // bb4b683f } diff --git a/packages/react-native/src/private/animated/NativeAnimatedValidation.js b/packages/react-native/src/private/animated/NativeAnimatedValidation.js index 7bff92a42ed..5638267a278 100644 --- a/packages/react-native/src/private/animated/NativeAnimatedValidation.js +++ b/packages/react-native/src/private/animated/NativeAnimatedValidation.js @@ -8,7 +8,10 @@ * @format */ -import type {InterpolationConfigType} from '../../../Libraries/Animated/nodes/AnimatedInterpolation'; +import type { + InterpolationConfigSupportedOutputType, + InterpolationConfigType, +} from '../../../Libraries/Animated/nodes/AnimatedInterpolation'; import { isSupportedInterpolationParam, @@ -16,9 +19,9 @@ import { isSupportedTransformProp, } from '../../../Libraries/Animated/NativeAnimatedAllowlist'; -export function validateInterpolation( - config: InterpolationConfigType, -): void { +export function validateInterpolation< + OutputT: InterpolationConfigSupportedOutputType, +>(config: InterpolationConfigType): void { for (const key in config) { if (key !== 'debugID' && !isSupportedInterpolationParam(key)) { console.error(