Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[animation] Add support for animating nested styles #12909

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions Libraries/Animated/src/AnimatedImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1485,32 +1485,48 @@ class AnimatedStyle extends AnimatedWithChildren {
this._style = style;
}

__getValue(): Object {
var style = {};
for (var key in this._style) {
var value = this._style[key];
// Recursively get values for nested styles (like iOS's shadowOffset)
__walkStyleAndGetValues(style) {
let updatedStyle = {};
for (let key in style) {
let value = style[key];
if (value instanceof Animated) {
if (!value.__isNative) {
// We cannot use value of natively driven nodes this way as the value we have access from
// JS may not be up to date.
style[key] = value.__getValue();
updatedStyle[key] = value.__getValue();
}
} else if (value && !Array.isArray(value) && typeof value === 'object') {
// Support animating nested values (for example: shadowOffset.height)
updatedStyle[key] = this.__walkStyleAndGetValues(value);
} else {
style[key] = value;
updatedStyle[key] = value;
}
}
return style;
return updatedStyle;
}

__getAnimatedValue(): Object {
var style = {};
for (var key in this._style) {
var value = this._style[key];
__getValue(): Object {
return this.__walkStyleAndGetValues(this._style);
}

// Recursively get animated values for nested styles (like iOS's shadowOffset)
__walkStyleAndGetAnimatedValues(style) {
let updatedStyle = {};
for (let key in style) {
let value = style[key];
if (value instanceof Animated) {
style[key] = value.__getAnimatedValue();
updatedStyle[key] = value.__getAnimatedValue();
} else if (value && !Array.isArray(value) && typeof value === 'object') {
// Support animating nested values (for example: shadowOffset.height)
updatedStyle[key] = this.__walkStyleAndGetAnimatedValues(value);
}
}
return style;
return updatedStyle;
}

__getAnimatedValue(): Object {
return this.__walkStyleAndGetAnimatedValues(this._style);
}

__attach(): void {
Expand Down
14 changes: 13 additions & 1 deletion Libraries/Animated/src/__tests__/Animated-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ describe('Animated tests', () => {
outputRange: [100, 200],
})},
{scale: anim},
]
],
shadowOffset: {
width: anim,
height: anim,
},
}
}, callback);

Expand All @@ -47,6 +51,10 @@ describe('Animated tests', () => {
{translateX: 100},
{scale: 0},
],
shadowOffset: {
width: 0,
height: 0,
},
},
});

Expand All @@ -62,6 +70,10 @@ describe('Animated tests', () => {
{translateX: 150},
{scale: 0.5},
],
shadowOffset: {
width: 0.5,
height: 0.5,
},
},
});

Expand Down