Skip to content

Commit 0677a13

Browse files
authored
fix(useOutsideClick): fix a case where click outside fired twice in a row in an iframe (#86)
1 parent 6731f29 commit 0677a13

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/useClickOutside.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,13 @@ function useClickOutside(
9595
if (disabled || ref == null) return undefined;
9696

9797
const doc = ownerDocument(getRefTarget(ref)!);
98+
const ownerWindow = doc.defaultView || window;
9899

99100
// Store the current event to avoid triggering handlers immediately
101+
// For things rendered in an iframe, the event might originate on the parent window
102+
// so we should fall back to that global event if the local one doesn't exist
100103
// https://github.com/facebook/react/issues/20074
101-
let currentEvent = (doc.defaultView || window).event;
104+
let currentEvent = ownerWindow.event ?? ownerWindow.parent?.event;
102105

103106
let removeInitialTriggerListener: (() => void) | null = null;
104107
if (InitialTriggerEvents[clickTrigger]) {

0 commit comments

Comments
 (0)