Skip to content

Commit 4bf0e13

Browse files
author
Steven Orvell
committed
Defer property application only when a custom-style is first created.
1 parent 27e1dcd commit 4bf0e13

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/lib/custom-style.html

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@
116116
// we may not have any textContent yet due to parser yielding
117117
// if so, wait until we do...
118118
if (e.textContent || this.include) {
119-
this._apply();
119+
this._apply(true);
120120
} else {
121121
var self = this;
122122
var observer = new MutationObserver(function() {
123123
observer.disconnect();
124-
self._apply();
124+
self._apply(true);
125125
});
126126
observer.observe(e, {childList: true});
127127
}
@@ -131,7 +131,7 @@
131131

132132
// polyfill this style with root scoping and
133133
// apply custom properties!
134-
_apply: function() {
134+
_apply: function(deferProperties) {
135135
// used applied element from HTMLImports polyfill or this
136136
var e = this.__appliedElement || this;
137137
if (this.include) {
@@ -157,9 +157,18 @@
157157
// `_apply` after B.
158158
// This case should only occur with native webcomponents.
159159
var self = this;
160-
requestAnimationFrame(function() {
160+
function fn() {
161161
self._applyCustomProperties(e);
162-
});
162+
}
163+
if (this._pendingApplyProperties) {
164+
cancelAnimationFrame(this._pendingApplyProperties);
165+
this._pendingApplyProperties = null;
166+
}
167+
if (deferProperties) {
168+
this._pendingApplyProperties = requestAnimationFrame(fn);
169+
} else {
170+
fn();
171+
}
163172
}
164173
},
165174

test/unit/custom-style.html

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,15 @@
359359
assertComputed(m, '4px');
360360
});
361361

362-
test('dynamic custom-styles apply', function(done) {
362+
test('dynamic custom-styles apply', function() {
363363
var dynamic = document.querySelector('.dynamic');
364364
assertComputed(dynamic, '0px');
365365
var ds = document.createElement('style', 'custom-style');
366366
ds.textContent = ':root { --dynamic: 11px solid orange; }';
367367
document.head.appendChild(ds);
368+
CustomElements.takeRecords();
368369
Polymer.updateStyles();
369-
Polymer.RenderStatus.afterNextRender(null, function() {
370-
assertComputed(dynamic, '11px');
371-
done();
372-
});
370+
assertComputed(dynamic, '11px');
373371
});
374372

375373
test('custom-styles apply normal and property values to elements and cannot be late bound via inheritance', function() {
@@ -409,20 +407,18 @@
409407
document.body.removeChild(style);
410408
});
411409

412-
test('imperative custom style with include', function(done) {
410+
test('imperative custom style with include', function() {
413411
var style = document.createElement('style', 'custom-style');
414412
style.include = 'shared-style2';
415413
var d = document.createElement('div');
416414
d.classList.add('zazz');
417415
document.body.appendChild(d);
418416
document.body.appendChild(style);
419417
CustomElements.takeRecords();
420-
Polymer.RenderStatus.afterNextRender(null, function() {
421-
assertComputed(d, '16px');
422-
document.body.removeChild(d);
423-
document.body.removeChild(style);
424-
done();
425-
});
418+
Polymer.updateStyles();
419+
assertComputed(d, '16px');
420+
document.body.removeChild(d);
421+
document.body.removeChild(style);
426422
});
427423

428424
test('imperative custom style with non-existent include', function() {

0 commit comments

Comments
 (0)