Skip to content

Commit fb8af28

Browse files
authored
Only force Safari reflow once (#186)
1 parent 710ecea commit fb8af28

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

src/vaadin-dialog-resizable-mixin.html

+4-27
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152

153153
if (e.button === 0 || e.touches) {
154154
e.preventDefault();
155-
this.__saveScrollPosition();
155+
156156
this._originalBounds = this._getOverlayBounds();
157157
const event = this.__getMouseOrFirstTouchEvent(e);
158158
this._originalMouseCoords = {top: event.pageY, left: event.pageX};
@@ -209,7 +209,7 @@
209209
}
210210
}
211211
});
212-
this.__forceSafariReflow();
212+
213213
this.$.overlay.notifyResize();
214214
}
215215
}
@@ -224,44 +224,21 @@
224224
window.removeEventListener('mouseup', this._resizeListeners.stop[direction]);
225225
window.removeEventListener('touchend', this._resizeListeners.stop[direction]);
226226
this.dispatchEvent(new CustomEvent('resize', {detail: this._getResizeDimensions()}));
227-
this.__restoreScrollPosition();
228227
}
229228

230229
/**
231230
* @return {!DialogResizeDimensions}
232231
* @protected
233232
*/
234233
_getResizeDimensions() {
234+
const scrollPosition = this.$.overlay.$.resizerContainer.scrollTop;
235235
const {width, height} = getComputedStyle(this.$.overlay.$.overlay);
236236
const content = this.$.overlay.$.content;
237237
content.setAttribute('style', 'position: absolute; top: 0; right: 0; bottom: 0; left: 0; box-sizing: content-box; height: auto;');
238238
const {width: contentWidth, height: contentHeight} = getComputedStyle(content);
239239
content.removeAttribute('style');
240+
this.$.overlay.$.resizerContainer.scrollTop = scrollPosition;
240241
return {width, height, contentWidth, contentHeight};
241242
}
242-
243-
/**
244-
* Safari 13 renders overflowing elements incorrectly.
245-
* This forces it to recalculate height.
246-
* @private
247-
*/
248-
__forceSafariReflow() {
249-
const overlay = this.$.overlay.$.overlay;
250-
overlay.style.display = 'block';
251-
window.requestAnimationFrame(() => {
252-
overlay.style.display = '';
253-
this.__restoreScrollPosition();
254-
});
255-
}
256-
257-
/** @private */
258-
__saveScrollPosition() {
259-
this.__scrollTop = this.$.overlay.$.resizerContainer.scrollTop;
260-
}
261-
262-
/** @private */
263-
__restoreScrollPosition() {
264-
this.$.overlay.$.resizerContainer.scrollTop = this.__scrollTop;
265-
}
266243
};
267244
</script>

src/vaadin-dialog.html

+16
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@
392392
if (overlay.style.position !== 'absolute') {
393393
overlay.style.position = 'absolute';
394394
overlay.style.maxWidth = 'none';
395+
this.__forceSafariReflow();
395396
}
396397

397398
for (const arg in parsedBounds) {
@@ -442,6 +443,21 @@
442443
__getMouseOrFirstTouchEvent(e) {
443444
return e.touches ? e.touches[0] : e;
444445
}
446+
447+
/**
448+
* Safari 13 renders overflowing elements incorrectly.
449+
* This forces it to recalculate height.
450+
* @private
451+
*/
452+
__forceSafariReflow() {
453+
const scrollPosition = this.$.overlay.$.resizerContainer.scrollTop;
454+
const overlay = this.$.overlay.$.overlay;
455+
overlay.style.display = 'block';
456+
window.requestAnimationFrame(() => {
457+
overlay.style.display = '';
458+
this.$.overlay.$.resizerContainer.scrollTop = scrollPosition;
459+
});
460+
}
445461
}
446462

447463
customElements.define(DialogElement.is, DialogElement);

test/vaadin-dialog_draggable-resizable-test.html

+15-10
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,17 @@
545545
expect(dialog._setBounds).to.be.calledOnce;
546546
});
547547

548-
it('should not reset scroll position on dragstart', () => {
548+
it('should not reset scroll position on dragstart', (done) => {
549549
dialog.modeless = true;
550550
button.style.marginBottom = '200px';
551551
dialog._setBounds({height: '100px'});
552-
container.scrollTop = 100;
553-
expect(container.scrollTop).to.equal(100);
554-
drag(container);
555-
expect(container.scrollTop).to.equal(100);
552+
requestAnimationFrame(() => {
553+
container.scrollTop = 100;
554+
expect(container.scrollTop).to.equal(100);
555+
drag(container);
556+
expect(container.scrollTop).to.equal(100);
557+
done();
558+
});
556559
});
557560
});
558561

@@ -783,12 +786,14 @@
783786
overlay.content.appendChild(div);
784787
window.ShadyDOM && window.ShadyDOM.flush();
785788
dialog._setBounds({height: 200});
786-
overlay.$.content.style.padding = '20px';
787-
container.scrollTop = 100;
788-
resize(overlayPart.querySelector('.s'), 0, -50);
789789
window.requestAnimationFrame(() => {
790-
expect(container.scrollTop).to.equal(100);
791-
done();
790+
overlay.$.content.style.padding = '20px';
791+
container.scrollTop = 100;
792+
resize(overlayPart.querySelector('.s'), 0, -50);
793+
window.requestAnimationFrame(() => {
794+
expect(container.scrollTop).to.equal(100);
795+
done();
796+
});
792797
});
793798
});
794799
}

0 commit comments

Comments
 (0)