Skip to content

Commit b92d378

Browse files
zeyapmeta-codesync[bot]
authored andcommitted
upstream useAnimatedColor and useAnimatedValueXY (#55539)
Summary: Pull Request resolved: #55539 Move `useAnimatedValueXY` and `useAnimatedColor` hooks into react-native-github so they can be used by OSS alongside the existing `useAnimatedValue` hook which can only take number value. ## Changelog: [General] [Added] - upstream useAnimatedColor and useAnimatedValueXY Reviewed By: fabriziocucci Differential Revision: D93125807 fbshipit-source-id: 74fb3925e8d495cddce7b28bdf852995854f85bb
1 parent 3618224 commit b92d378

9 files changed

Lines changed: 113 additions & 2 deletions

File tree

packages/react-native/Libraries/Animated/Animated.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,21 @@ export namespace Animated {
7777
readonly useNativeDriver: boolean;
7878
};
7979

80+
type AnimatedColorInputValue =
81+
| RgbaValue
82+
| RgbaAnimatedValue
83+
| ColorValue
84+
| null
85+
| undefined;
86+
8087
class AnimatedColor extends AnimatedWithChildren {
8188
r: AnimatedValue;
8289
g: AnimatedValue;
8390
b: AnimatedValue;
8491
a: AnimatedValue;
8592

8693
constructor(
87-
valueIn?: RgbaValue | RgbaAnimatedValue | ColorValue | null,
94+
valueIn?: AnimatedColorInputValue,
8895
config?: AnimatedConfig | null,
8996
);
9097
nativeColor: unknown; // Unsure what to do here
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
*/
9+
10+
import type {Animated} from './Animated';
11+
12+
export function useAnimatedColor(
13+
inputValue?: Animated.AnimatedColorInputValue,
14+
config?: Animated.AnimatedConfig | null | undefined,
15+
): Animated.AnimatedColor;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
*/
10+
11+
import type {AnimatedColorConfig, InputValue} from './nodes/AnimatedColor';
12+
13+
import Animated from './Animated';
14+
import {useRef} from 'react';
15+
16+
export default function useAnimatedColor(
17+
inputValue?: InputValue,
18+
config?: ?AnimatedColorConfig,
19+
): Animated.Color {
20+
const ref = useRef<null | Animated.Color>(null);
21+
if (ref.current == null) {
22+
ref.current = new Animated.Color(inputValue, config);
23+
}
24+
return ref.current;
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
*/
9+
10+
import type {Animated} from './Animated';
11+
12+
export function useAnimatedValueXY(
13+
initialValue: {x: number; y: number},
14+
config?: Animated.AnimatedConfig | null | undefined,
15+
): Animated.ValueXY;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
*/
10+
11+
import Animated from './Animated';
12+
import {useRef} from 'react';
13+
14+
export default function useAnimatedValueXY(
15+
initialValue: {
16+
x: number,
17+
y: number,
18+
},
19+
config?: ?Animated.AnimatedConfig,
20+
): Animated.ValueXY {
21+
const ref = useRef<null | Animated.ValueXY>(null);
22+
if (ref.current == null) {
23+
ref.current = new Animated.ValueXY(initialValue, config);
24+
}
25+
return ref.current;
26+
}

packages/react-native/ReactNativeApi.d.ts

Lines changed: 14 additions & 1 deletion
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<<74ac46b82182a7d97e92c933a361f583>>
7+
* @generated SignedSource<<6463b0a4f3acbeb07e6759c345b927d8>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -5651,10 +5651,21 @@ declare type UnsafeEventObject = Object
56515651
declare type UnsafeMixed = unknown
56525652
declare type UnsafeNativeEventObject = Object
56535653
declare type UnsafeObject = Object
5654+
declare function useAnimatedColor(
5655+
inputValue?: InputValue,
5656+
config?: AnimatedColorConfig | null | undefined,
5657+
): Animated.Color
56545658
declare function useAnimatedValue(
56555659
initialValue: number,
56565660
config?: Animated.AnimatedConfig | null | undefined,
56575661
): Animated.Value
5662+
declare function useAnimatedValueXY(
5663+
initialValue: {
5664+
x: number
5665+
y: number
5666+
},
5667+
config?: Animated.AnimatedConfig | null | undefined,
5668+
): Animated.ValueXY
56585669
declare function useColorScheme(): ColorSchemeName | null | undefined
56595670
declare function usePressability(
56605671
config: null | PressabilityConfig | undefined,
@@ -6253,7 +6264,9 @@ export {
62536264
processColor, // 6e877698
62546265
registerCallableModule, // 839c8cfe
62556266
requireNativeComponent, // 72c09c3d
6267+
useAnimatedColor, // e3511f81
62566268
useAnimatedValue, // b18adb63
6269+
useAnimatedValueXY, // c7ee2332
62576270
useColorScheme, // c216d6f7
62586271
usePressability, // fe1f27d8
62596272
useWindowDimensions, // bb4b683f

packages/react-native/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ module.exports = {
341341
get useAnimatedValue() {
342342
return require('./Libraries/Animated/useAnimatedValue').default;
343343
},
344+
get useAnimatedValueXY() {
345+
return require('./Libraries/Animated/useAnimatedValueXY').default;
346+
},
347+
get useAnimatedColor() {
348+
return require('./Libraries/Animated/useAnimatedColor').default;
349+
},
344350
get useColorScheme() {
345351
return require('./Libraries/Utilities/useColorScheme').default;
346352
},

packages/react-native/index.js.flow

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ export * as TurboModuleRegistry from './Libraries/TurboModule/TurboModuleRegistr
407407
export {default as UIManager} from './Libraries/ReactNative/UIManager';
408408
export {unstable_batchedUpdates} from './Libraries/ReactNative/RendererProxy';
409409
export {default as useAnimatedValue} from './Libraries/Animated/useAnimatedValue';
410+
export {default as useAnimatedValueXY} from './Libraries/Animated/useAnimatedValueXY';
411+
export {default as useAnimatedColor} from './Libraries/Animated/useAnimatedColor';
410412
export type {
411413
PressabilityConfig,
412414
EventHandlers as PressabilityEventHandlers,

packages/react-native/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export * from '../Libraries/Alert/Alert';
7575
export * from '../Libraries/Animated/Animated';
7676
export * from '../Libraries/Animated/Easing';
7777
export * from '../Libraries/Animated/useAnimatedValue';
78+
export * from '../Libraries/Animated/useAnimatedValueXY';
79+
export * from '../Libraries/Animated/useAnimatedColor';
7880
export * from '../Libraries/AppState/AppState';
7981
export * from '../Libraries/BatchedBridge/NativeModules';
8082
export * from '../Libraries/Components/AccessibilityInfo/AccessibilityInfo';

0 commit comments

Comments
 (0)