Skip to content

Commit 616cb84

Browse files
authored
refactor: update overlay stack logic to not rely on body children (#9738)
1 parent 4074e7e commit 616cb84

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

packages/notification/src/vaadin-notification-mixin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ export const NotificationContainerMixin = (superClass) =>
4646
_openedChanged(opened) {
4747
if (opened) {
4848
document.body.appendChild(this);
49+
this._appendAttachedInstance();
4950
document.addEventListener('vaadin-overlay-close', this._boundVaadinOverlayClose);
5051
if (this._boundIosResizeListener) {
5152
this._detectIosNavbar();
5253
window.addEventListener('resize', this._boundIosResizeListener);
5354
}
5455
} else {
5556
document.body.removeChild(this);
57+
this._removeAttachedInstance();
5658
document.removeEventListener('vaadin-overlay-close', this._boundVaadinOverlayClose);
5759
if (this._boundIosResizeListener) {
5860
window.removeEventListener('resize', this._boundIosResizeListener);

packages/overlay/src/vaadin-overlay-mixin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,12 @@ export const OverlayMixin = (superClass) =>
369369

370370
/** @private */
371371
_animatedOpening() {
372-
if (this.parentNode === document.body && this.hasAttribute('closing')) {
372+
if (this._isAttached && this.hasAttribute('closing')) {
373373
this._flushAnimation('closing');
374374
}
375375
this._attachOverlay();
376+
this._appendAttachedInstance();
377+
this.bringToFront();
376378
if (!this.modeless) {
377379
this._enterModalState();
378380
}
@@ -392,7 +394,6 @@ export const OverlayMixin = (superClass) =>
392394
this._placeholder = document.createComment('vaadin-overlay-placeholder');
393395
this.parentNode.insertBefore(this._placeholder, this);
394396
document.body.appendChild(this);
395-
this.bringToFront();
396397
}
397398

398399
/** @private */
@@ -403,6 +404,7 @@ export const OverlayMixin = (superClass) =>
403404
/** @private */
404405
_finishClosing() {
405406
this._detachOverlay();
407+
this._removeAttachedInstance();
406408
this.$.overlay.style.removeProperty('pointer-events');
407409
this.removeAttribute('closing');
408410
this.dispatchEvent(new CustomEvent('vaadin-overlay-closed'));
@@ -413,7 +415,7 @@ export const OverlayMixin = (superClass) =>
413415
if (this.hasAttribute('opening')) {
414416
this._flushAnimation('opening');
415417
}
416-
if (this._placeholder) {
418+
if (this._isAttached) {
417419
this._exitModalState();
418420
this.setAttribute('closing', '');
419421
this.dispatchEvent(new CustomEvent('vaadin-overlay-closing'));

packages/overlay/src/vaadin-overlay-stack-mixin.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66

7+
/** @type {Set<HTMLElement>} */
8+
const attachedInstances = new Set();
9+
710
/**
811
* Returns all attached overlays in visual stacking order.
912
* @private
1013
*/
1114
const getAttachedInstances = () =>
12-
Array.from(document.body.children)
15+
[...attachedInstances]
1316
.filter((el) => el instanceof HTMLElement && el._hasOverlayStackMixin && !el.hasAttribute('closing'))
1417
.sort((a, b) => a.__zIndex - b.__zIndex || 0);
1518

@@ -70,6 +73,16 @@ export const OverlayStackMixin = (superClass) =>
7073
return isLastOverlay(this);
7174
}
7275

76+
/**
77+
* Returns true if this is overlay is attached.
78+
*
79+
* @return {boolean}
80+
* @protected
81+
*/
82+
get _isAttached() {
83+
return attachedInstances.has(this);
84+
}
85+
7386
/**
7487
* Brings the overlay as visually the frontmost one.
7588
*/
@@ -133,4 +146,16 @@ export const OverlayStackMixin = (superClass) =>
133146
}
134147
}
135148
}
149+
150+
/** @protected */
151+
_appendAttachedInstance() {
152+
attachedInstances.add(this);
153+
}
154+
155+
/** @protected */
156+
_removeAttachedInstance() {
157+
if (this._isAttached) {
158+
attachedInstances.delete(this);
159+
}
160+
}
136161
};

0 commit comments

Comments
 (0)