Skip to content

Commit

Permalink
fix(overlays): clicking outside iframe not closing the overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
gyulus3 authored Nov 12, 2024
1 parent a19096e commit 2a989f4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-penguins-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': patch
---

[overlays] now closes when iframe loses focus
13 changes: 13 additions & 0 deletions packages/ui/components/overlays/src/OverlayController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,14 @@ export class OverlayController extends EventTarget {
wasMouseUpInside = false;
});
};

/** @type {EventListenerOrEventListenerObject} */
this.__onWindowBlur = () => {
// When the current window loses the focus (clicking outside iframe) the overlay gets hidden
setTimeout(() => {
this.hide();
});
};
}

this.contentWrapperNode[addOrRemoveListener](
Expand Down Expand Up @@ -1321,6 +1329,11 @@ export class OverlayController extends EventTarget {
(this.__onDocumentMouseUp),
true,
);
window[addOrRemoveListener](
'blur',
/** @type {EventListenerOrEventListenerObject} */
(this.__onWindowBlur),
);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions packages/ui/components/overlays/test/OverlayController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,19 @@ describe('OverlayController', () => {

expect(ctrl.isShown).to.be.true;
});

it('hides when window is blurred (useful for iframes)', async () => {
const ctrl = new OverlayController({
...withGlobalTestConfig(),
hidesOnOutsideClick: true,
});
await ctrl.show();

window.dispatchEvent(new Event('blur'));
await aTimeout(0);

expect(ctrl.isShown).to.be.false;
});
});

describe('elementToFocusAfterHide', () => {
Expand Down

0 comments on commit 2a989f4

Please sign in to comment.