@@ -24,14 +24,17 @@ import type {ProcessedColorValue} from '../../StyleSheet/processColor';
24
24
export type AnimatedColorConfig = $ReadOnly < {
25
25
useNativeDriver : boolean ,
26
26
} > ;
27
- type ColorListenerCallback = ( value : string ) => mixed ;
27
+
28
+ type ColorListenerCallback = ( value : ColorValue ) => mixed ;
29
+
28
30
export type RgbaValue = {
29
31
+ r : number ,
30
32
+ g : number ,
31
33
+ b : number ,
32
34
+ a : number ,
33
35
...
34
36
} ;
37
+
35
38
type RgbaAnimatedValue = {
36
39
+ r : AnimatedValue ,
37
40
+ g : AnimatedValue ,
@@ -40,6 +43,8 @@ type RgbaAnimatedValue = {
40
43
...
41
44
} ;
42
45
46
+ const NativeAnimatedAPI = NativeAnimatedHelper . API ;
47
+
43
48
const defaultColor : RgbaValue = { r : 0 , g : 0 , b : 0 , a : 1.0 } ;
44
49
let _uniqueId = 1 ;
45
50
@@ -161,34 +166,43 @@ export default class AnimatedColor extends AnimatedWithChildren {
161
166
* and update all the bound properties.
162
167
*/
163
168
setValue ( value : RgbaValue | ColorValue ) : void {
164
- this . nativeColor = null ;
169
+ let shouldUpdateNodeConfig = false ;
170
+ if ( this . __isNative ) {
171
+ const nativeTag = this . __getNativeTag ( ) ;
172
+ NativeAnimatedAPI . setWaitingForIdentifier ( nativeTag . toString ( ) ) ;
173
+ }
165
174
166
175
const processedColor : RgbaValue | NativeColorValue =
167
176
processColor ( value ) ?? defaultColor ;
168
177
if ( isRgbaValue ( processedColor ) ) {
169
- // $FlowIgnore[incompatible-cast ] - Type is verified above
170
- const rgbaValue : RgbaValue = ( processedColor : RgbaValue ) ;
178
+ // $FlowIgnore[incompatible-type ] - Type is verified above
179
+ const rgbaValue : RgbaValue = processedColor ;
171
180
this . r . setValue ( rgbaValue . r ) ;
172
181
this . g . setValue ( rgbaValue . g ) ;
173
182
this . b . setValue ( rgbaValue . b ) ;
174
183
this . a . setValue ( rgbaValue . a ) ;
184
+ if ( this . nativeColor != null ) {
185
+ this . nativeColor = null ;
186
+ shouldUpdateNodeConfig = true ;
187
+ }
175
188
} else {
176
- // $FlowIgnore[incompatible-cast] - Type is verified above
177
- this . nativeColor = ( processedColor : NativeColorValue ) ;
178
- }
179
-
180
- if ( this . nativeColor ) {
181
- if ( ! this . __isNative ) {
182
- this . __makeNative ( ) ;
189
+ // $FlowIgnore[incompatible-type] - Type is verified above
190
+ const nativeColor : NativeColorValue = processedColor ;
191
+ if ( this . nativeColor !== nativeColor ) {
192
+ this . nativeColor = nativeColor ;
193
+ shouldUpdateNodeConfig = true ;
183
194
}
195
+ }
184
196
197
+ if ( this . __isNative ) {
185
198
const nativeTag = this . __getNativeTag ( ) ;
186
- NativeAnimatedHelper . API . setWaitingForIdentifier ( nativeTag . toString ( ) ) ;
187
- NativeAnimatedHelper . API . updateAnimatedNodeConfig (
188
- nativeTag ,
189
- this . __getNativeConfig ( ) ,
190
- ) ;
191
- NativeAnimatedHelper . API . unsetWaitingForIdentifier ( nativeTag . toString ( ) ) ;
199
+ if ( shouldUpdateNodeConfig ) {
200
+ NativeAnimatedAPI . updateAnimatedNodeConfig (
201
+ nativeTag ,
202
+ this . __getNativeConfig ( ) ,
203
+ ) ;
204
+ }
205
+ NativeAnimatedAPI . unsetWaitingForIdentifier ( nativeTag . toString ( ) ) ;
192
206
}
193
207
}
194
208
@@ -275,7 +289,7 @@ export default class AnimatedColor extends AnimatedWithChildren {
275
289
* final value after stopping the animation, which is useful for updating
276
290
* state to match the animation position with layout.
277
291
*/
278
- stopAnimation ( callback ?: ( value : string ) => void ) : void {
292
+ stopAnimation ( callback ? : ColorListenerCallback ) : void {
279
293
this . r . stopAnimation ( ) ;
280
294
this . g . stopAnimation ( ) ;
281
295
this . b . stopAnimation ( ) ;
@@ -286,16 +300,20 @@ export default class AnimatedColor extends AnimatedWithChildren {
286
300
/**
287
301
* Stops any animation and resets the value to its original.
288
302
*/
289
- resetAnimation ( callback ?: ( value : string ) => void ) : void {
303
+ resetAnimation ( callback ? : ColorListenerCallback ) : void {
290
304
this . r . resetAnimation ( ) ;
291
305
this . g . resetAnimation ( ) ;
292
306
this . b . resetAnimation ( ) ;
293
307
this . a . resetAnimation ( ) ;
294
308
callback && callback ( this . __getValue ( ) ) ;
295
309
}
296
310
297
- __getValue ( ) : string {
298
- return `rgba(${ this . r . __getValue ( ) } , ${ this . g . __getValue ( ) } , ${ this . b . __getValue ( ) } , ${ this . a . __getValue ( ) } )` ;
311
+ __getValue ( ) : ColorValue {
312
+ if ( this . nativeColor != null ) {
313
+ return this . nativeColor ;
314
+ } else {
315
+ return `rgba(${ this . r . __getValue ( ) } , ${ this . g . __getValue ( ) } , ${ this . b . __getValue ( ) } , ${ this . a . __getValue ( ) } )` ;
316
+ }
299
317
}
300
318
301
319
__attach ( ) : void {
0 commit comments