Merge pull request #15483 from Budibase/detect-iframe-clicks

Detect iframe clicks via loss of focus
This commit is contained in:
Andrew Kingston 2025-02-05 11:59:49 +00:00 committed by GitHub
commit f9706656d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 1 deletions

View File

@ -34,7 +34,7 @@ let candidateTarget: HTMLElement | undefined
// Processes a "click outside" event and invokes callbacks if our source element // Processes a "click outside" event and invokes callbacks if our source element
// is valid // is valid
const handleClick = (e: MouseEvent) => { const handleClick = (e: MouseEvent) => {
const target = e.target as HTMLElement const target = (e.target || e.relatedTarget) as HTMLElement
// Ignore click if this is an ignored class // Ignore click if this is an ignored class
if (target.closest('[data-ignore-click-outside="true"]')) { if (target.closest('[data-ignore-click-outside="true"]')) {
@ -91,9 +91,19 @@ const handleMouseDown = (e: MouseEvent) => {
document.addEventListener("click", handleMouseUp, true) document.addEventListener("click", handleMouseUp, true)
} }
// Handle iframe clicks by detecting a loss of focus on the main window
const handleBlur = () => {
if (document.activeElement?.tagName === "IFRAME") {
handleClick(
new MouseEvent("click", { relatedTarget: document.activeElement })
)
}
}
// Global singleton listeners for our events // Global singleton listeners for our events
document.addEventListener("mousedown", handleMouseDown) document.addEventListener("mousedown", handleMouseDown)
document.addEventListener("contextmenu", handleClick) document.addEventListener("contextmenu", handleClick)
window.addEventListener("blur", handleBlur)
/** /**
* Adds or updates a click handler * Adds or updates a click handler