Skip to content

Commit 0293380

Browse files
authored
refactor: make overlay bringToFront work with native popovers (#9746)
1 parent 0199847 commit 0293380

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const attachedInstances = new Set();
1212
* @private
1313
*/
1414
const getAttachedInstances = () =>
15-
[...attachedInstances]
16-
.filter((el) => el instanceof HTMLElement && el._hasOverlayStackMixin && !el.hasAttribute('closing'))
17-
.sort((a, b) => a.__zIndex - b.__zIndex || 0);
15+
[...attachedInstances].filter(
16+
(el) => el instanceof HTMLElement && el._hasOverlayStackMixin && !el.hasAttribute('closing'),
17+
);
1818

1919
/**
2020
* Returns all attached overlay instances excluding notification container,
@@ -87,16 +87,27 @@ export const OverlayStackMixin = (superClass) =>
8787
* Brings the overlay as visually the frontmost one.
8888
*/
8989
bringToFront() {
90+
// Update z-index to be the highest among all attached overlays
91+
// TODO: Can be removed after switching all overlays to be based on native popover
9092
let zIndex = '';
9193
const frontmost = getAttachedInstances()
9294
.filter((o) => o !== this)
9395
.pop();
9496
if (frontmost) {
95-
const frontmostZIndex = frontmost.__zIndex;
97+
const frontmostZIndex = parseFloat(getComputedStyle(frontmost).zIndex);
9698
zIndex = frontmostZIndex + 1;
9799
}
98100
this.style.zIndex = zIndex;
99-
this.__zIndex = zIndex || parseFloat(getComputedStyle(this).zIndex);
101+
102+
// Update stacking order of native popover-based overlays
103+
if (this.matches(':popover-open')) {
104+
this.hidePopover();
105+
this.showPopover();
106+
}
107+
108+
// Update order of attached instances
109+
this._removeAttachedInstance();
110+
this._appendAttachedInstance();
100111

101112
// If there is a nested overlay, call `bringToFront()` for it as well.
102113
if (overlayMap.has(this)) {

packages/overlay/test/multiple.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,28 @@ describe('multiple overlays', () => {
317317
escKeyDown(input);
318318
expect(spy.called).to.be.false;
319319
});
320+
321+
describe('native popovers', () => {
322+
beforeEach(() => {
323+
modeless1.popover = 'manual';
324+
modeless2.popover = 'manual';
325+
});
326+
327+
it('should update stacking order when using bringToFront', () => {
328+
modeless1.opened = true;
329+
modeless1.showPopover();
330+
modeless2.opened = true;
331+
modeless2.showPopover();
332+
333+
modeless1.bringToFront();
334+
335+
expect(modeless1._last).to.be.true;
336+
337+
// Check that the overlay is also visually the frontmost
338+
const frontmost = getFrontmostOverlayFromScreenCenter();
339+
expect(frontmost).to.equal(modeless1);
340+
});
341+
});
320342
});
321343

322344
describe('setNestedOverlay', () => {

0 commit comments

Comments
 (0)