Skip to content

Commit

Permalink
fix: bring popover overlay to front when used in modeless dialog (#8692)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Feb 20, 2025
1 parent b2d43f5 commit 130ef3b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
34 changes: 34 additions & 0 deletions packages/popover/src/vaadin-popover-overlay-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
*/
import { OverlayMixin } from '@vaadin/overlay/src/vaadin-overlay-mixin.js';
import { PositionMixin } from '@vaadin/overlay/src/vaadin-overlay-position-mixin.js';
import { setNestedOverlay } from '@vaadin/overlay/src/vaadin-overlay-stack-mixin.js';

/**
* Returns the closest parent overlay for given node, if any.
* @param {HTMLElement} node
* @return {HTMLElement}
*/
const getClosestOverlay = (node) => {
let n = node;

while (n && n !== node.ownerDocument) {
n = n.parentNode || n.host;

if (n && n._hasOverlayStackMixin) {
return n;
}
}

return null;
};

/**
* A mixin providing common popover overlay functionality.
Expand All @@ -24,6 +44,10 @@ export const PopoverOverlayMixin = (superClass) =>
};
}

static get observers() {
return ['__openedOrTargetChanged(opened, positionTarget)'];
}

/**
* Tag name prefix used by custom properties.
* @protected
Expand Down Expand Up @@ -96,4 +120,14 @@ export const PopoverOverlayMixin = (superClass) =>
this.style.top = `${overlayRect.top + offset}px`;
}
}

/** @private */
__openedOrTargetChanged(opened, target) {
if (target) {
const parent = getClosestOverlay(target);
if (parent) {
setNestedOverlay(parent, opened ? this : null);
}
}
}
};
19 changes: 19 additions & 0 deletions packages/popover/test/nested.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@vaadin/chai-plugins';
import { esc, fixtureSync, nextRender, nextUpdate, outsideClick } from '@vaadin/testing-helpers';
import sinon from 'sinon';
import './not-animated-styles.js';
import { Popover } from '../vaadin-popover.js';
import { mouseenter, mouseleave } from './helpers.js';
Expand Down Expand Up @@ -167,4 +168,22 @@ describe('nested popover', () => {
expect(popover.opened).to.be.true;
});
});

describe('bring to front', () => {
beforeEach(async () => {
// Open the first popover
target.click();
await nextRender();

// Open the second popover
secondTarget.click();
await nextRender();
});

it('should bring to front nested overlay on parent overlay bringToFront()', () => {
const spy = sinon.spy(secondPopover._overlayElement, 'bringToFront');
popover._overlayElement.bringToFront();
expect(spy).to.be.calledOnce;
});
});
});
29 changes: 28 additions & 1 deletion test/integration/dialog-popover.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from '@vaadin/chai-plugins';
import { resetMouse, sendKeys, sendMouse } from '@vaadin/test-runner-commands';
import { fixtureSync, nextFrame, nextRender, nextUpdate } from '@vaadin/testing-helpers';
import { fixtureSync, mousedown, nextFrame, nextRender, nextUpdate, touchstart } from '@vaadin/testing-helpers';
import './not-animated-styles.js';
import '@vaadin/dialog';
import '@vaadin/popover';
Expand Down Expand Up @@ -84,6 +84,33 @@ describe('popover in dialog', () => {
expect(dialog.opened).to.be.true;
});
});

describe('modeless dialog', () => {
beforeEach(async () => {
dialog.modeless = true;

button.click();
await nextRender();
});

it('should bring popover overlay to front on dialog overlay mousedown', () => {
mousedown(dialog.$.overlay);

const dialogZIndex = parseInt(getComputedStyle(dialog.$.overlay).zIndex);
const popoverZIndex = parseInt(getComputedStyle(overlay).zIndex);

expect(popoverZIndex).to.equal(dialogZIndex + 1);
});

it('should bring popover overlay to front on dialog overlay touchstart', () => {
touchstart(dialog.$.overlay);

const dialogZIndex = parseInt(getComputedStyle(dialog.$.overlay).zIndex);
const popoverZIndex = parseInt(getComputedStyle(overlay).zIndex);

expect(popoverZIndex).to.equal(dialogZIndex + 1);
});
});
});
});

Expand Down

0 comments on commit 130ef3b

Please sign in to comment.