Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngStyle): skip setting empty value when new style has the property #16709

Merged
merged 7 commits into from
Oct 4, 2018
Merged

fix(ngStyle): skip setting empty value when new style has the property #16709

merged 7 commits into from
Oct 4, 2018

Conversation

itchyny
Copy link
Contributor

@itchyny itchyny commented Oct 2, 2018

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix.

What is the current behavior? (You can also link to an open issue here)
Using ng-style with CSS transition with jQuery 3.3.1 (currently the latest tag), the animation is disabled in certain circumstances.
https://jsfiddle.net/qz5a46yc/1/

Without jQuery (using jqLite.css) or using jQuery 3.2.1, the div animates. I reported to the jQuery dev on this behavior difference: jquery/jquery#4185. I think this is a bug in jQuery.

This pull request suggests a workaround on the disabled animation issue. I'm not sure how the report to the jQuery will be handled, but we can fix the behavior with reasonable changes in AngularJS. Skipping the common properties with newStyles will fix the animation issue with jQuery 3.3.1, which can be confirmed by https://jsfiddle.net/g3st70fh/1/. I think the diff is reasonable and also improves the performance because the properties in oldStyles and newStyles are same in most cases.

What is the new behavior (if this is a feature change)?
I believe there's no changes except for fixing the animation issue.

Does this PR introduce a breaking change?
No, I believe.

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Fix/Feature: Docs have been added/updated
  • Fix/Feature: Tests have been added; existing tests pass

Other information:

Sorry, something went wrong.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

1 similar comment
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

@itchyny
Copy link
Contributor Author

itchyny commented Oct 2, 2018

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

1 similar comment
@googlebot
Copy link

CLAs look good, thanks!

@googlebot googlebot added cla: yes and removed cla: no labels Oct 2, 2018

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
Previously, all the properties in oldStyles are set to empty value once.
Using AngularJS with jQuery 3.3.1, this disables the CSS transition as
reported in jquery/jquery#4185.
forEach(oldStyles, function(val, style) { element.css(style, '');});
forEach(oldStyles, function(val, style) {
if (!(newStyles && newStyles.hasOwnProperty(style))) {
element.css(style, '');
Copy link
Member

Choose a reason for hiding this comment

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

Incorrect indentation.

@@ -54,7 +54,11 @@
var ngStyleDirective = ngDirective(function(scope, element, attr) {
scope.$watchCollection(attr.ngStyle, function ngStyleWatchAction(newStyles, oldStyles) {
if (oldStyles && (newStyles !== oldStyles)) {
forEach(oldStyles, function(val, style) { element.css(style, '');});
forEach(oldStyles, function(val, style) {
if (!(newStyles && newStyles.hasOwnProperty(style))) {
Copy link
Member

Choose a reason for hiding this comment

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

What if newStyles does have the property, but the value is undefined or null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, in that case the style should be cleared. Fixed in 5d74f0f.

@gkalpak
Copy link
Member

gkalpak commented Oct 2, 2018

Looks reasonable. I'd still wait for a response from the jQuery team on whether this change was intentional or not.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
… falsy

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
forEach(oldStyles, function(val, style) { element.css(style, '');});
forEach(oldStyles, function(val, style) {
if (!(newStyles && newStyles[style])) {
element.css(style, '');
Copy link
Collaborator

@jbedard jbedard Oct 3, 2018

Choose a reason for hiding this comment

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

Would it be possible to do newStyles[style] = '' instead (maybe also needing a newStyles null check + assign). This way we will only make a single element.css(newStyles) call at the end instead of multiple .css calls.

Also should this be an undefined/null check instead of just falsey check? What if newStyles[style] is 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For sure. Fixed in 95d2458.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

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

LGTM 👍
It would be nice to have a tests that verifies that falsy style values (e.g. 0) are still updated correctly.

@mgol mgol merged commit bb5a7e3 into angular:master Oct 4, 2018
@mgol
Copy link
Member

mgol commented Oct 4, 2018

Landed, thanks!

@itchyny itchyny deleted the improve-ng-style-skip-old-styles branch October 4, 2018 16:28
@itchyny
Copy link
Contributor Author

itchyny commented Oct 4, 2018

Thank you!

mgol pushed a commit that referenced this pull request Oct 4, 2018
Previously, all the properties in oldStyles are set to empty value once.
Using AngularJS with jQuery 3.3.1, this disables the CSS transition as
reported in jquery/jquery#4185.

Closes #16709
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants