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 and vaadin-bot committed May 28, 2024
1 parent 5396954 commit 1b77856
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 @@ -219,6 +219,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.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,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();

Check failure on line 116 in packages/overlay/test/position-mixin.test.js

View workflow job for this annotation

GitHub Actions / Lint

'nextRender' is not defined
expect(overlay.opened).to.be.false;
});

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

Check failure on line 122 in packages/overlay/test/position-mixin.test.js

View workflow job for this annotation

GitHub Actions / Lint

'nextRender' is not defined
expect(overlay.opened).to.be.false;
});

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

0 comments on commit 1b77856

Please sign in to comment.