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

Add support for nested AnimatedValues in AnimatedStyle (like shadowOffset) #11030

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions Libraries/Animated/src/AnimatedImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1471,21 +1471,25 @@ class AnimatedStyle extends AnimatedWithChildren {
this._style = style;
}

__getValue(): Object {
var style = {};
for (var key in this._style) {
var value = this._style[key];
// Recursively get animated values for nested styles (like iOS's shadowOffset)
__walkStyleAndGetValues(style) {
return Object.keys(style).reduce((prev, curr) => {
let value = style[curr];
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();
value = value.__getValue();
}
} else {
style[key] = value;
} else if (typeof value === 'object') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will cause issues with Array and null, if I remember correctly both of those will have type object but we don't want to traverse those.

value = this.__walkStyleAndGetValues(value);
}
}
return style;
return { ...prev, [curr]: value };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid creating a new object at each iterations and just mutate the initial one.

}, {});
}

__getValue(): Object {
return this.__walkStyleAndGetValues(this._style);
}

__getAnimatedValue(): Object {
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