Skip to content

Commit

Permalink
fix: hide overlay if position target element is hidden (#7454)
Browse files Browse the repository at this point in the history
* fix: hide overlay if position target element is hidden

If the target element is hidden, either by having `display: none` set to
itself of any of its parents, a resize observer for that element is
triggered.

This change adds a check for the target element size on the resize
observer callback and closes the overlay in case the element is hidden.

Fixes #7437
  • Loading branch information
DiegoCardoso authored May 28, 2024
1 parent 64f6954 commit d8e14c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/overlay/src/vaadin-overlay-position-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ export const PositionMixin = (superClass) =>

const targetRect = this.positionTarget.getBoundingClientRect();

if (targetRect.width === 0 && targetRect.height === 0 && this.opened) {
this.opened = false;
return;
}

// Detect the desired alignment and update the layout accordingly
const shouldAlignStartVertically = this.__shouldAlignStartVertically(targetRect);
this.style.justifyContent = shouldAlignStartVertically ? 'flex-start' : 'flex-end';
Expand Down
12 changes: 12 additions & 0 deletions packages/overlay/test/position-mixin.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ describe('position mixin', () => {
expect(overlay.$.overlay.scrollTop).to.equal(100);
});

it('should close overlay if element is hidden', async () => {
target.style.display = 'none';
await nextRender();
expect(overlay.opened).to.be.false;
});

it('should close overlay if parent element is hidden', async () => {
target.parentElement.style.display = 'none';
await nextRender();
expect(overlay.opened).to.be.false;
});

describe('vertical align top', () => {
beforeEach(() => {
overlay.verticalAlign = TOP;
Expand Down

0 comments on commit d8e14c4

Please sign in to comment.