-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: detect click outside on iframe
Clicks on iframe don't bubble, so they are undetected by our `document.documentElement` listeners. Workarounds in the SO threads and dev internet fall into 3 categories: - add pseudo-elements cover ups on iframes; - wrap iframes on an extra <div> and capture the click on it; - window.onblur + document.activeElement combo; The last one seemed the less intrusive: - it doesn't block clicks on iframe content itself; - it doesn't add extra DIVs which could cause layout issues; - it keeps performance impact to a minimum; How it works: The gist is that a click on an iframe will move `focus` to it. Since iframe has its own `window` our main app window will blur: on that event we can then check if document.activeElement is an IFRAME and execute the handler considering it a "faux click outside". Implementation details: - adds a custom `vco:faux-iframe-click` eventName id to the provided eventsNames array, this will be used to perform conditional logic: - bind `blur` event to window instead of document.documentElement; - use `onFauxIframeClick` instead of default `onEvent` as handler wrapper; - `onFauxIframeClick` doens't require the event path logic checks as it only uses `document.activeElement` and simply wraps handler execution into a setTimeout to workaround an issue with Firefox, that only sets `activeElement` correctly after blur, on the next tick (event loop). Caveats: - Click outside will be triggered once on iframe. Subsequent clicks will not execute the handler untill focus has been moved back to main window. This is the "expected" behaviour as by clicking the iframe, focus will move to iframe contents — a different window. There might be way to workaround this, by triggering `window.focus()` at the end of the provided handler but that will break normal tab/focus flow, therefore not included by default. - Moving focus to iframe via tab navigation also triggers `window.blur` consequently the click-outside handler.
- Loading branch information
1 parent
e56b509
commit e2e2bd1
Showing
2 changed files
with
103 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters