Skip to content

Commit

Permalink
fix(useOnClickOutside): use conditional handler window event
Browse files Browse the repository at this point in the history
Workaround already used by v0 to handle facebook/react#20074
  • Loading branch information
ling1726 committed May 25, 2021
1 parent 1cfbb7a commit c0764c6
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions packages/react-utilities/src/hooks/useOnClickOutside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,43 @@ export const useOnClickOutside = (options: UseOnClickOutsideOptions) => {
});

React.useEffect(() => {
// Store the current event to avoid triggering handlers immediately
// Note this depends on a deprecated but extremely well supported quirk of the web platform
// https://github.com/facebook/react/issues/20074
let currentEvent = getWindowEvent(window);

const conditionalHandler = (event: MouseEvent | TouchEvent) => {
// Skip if this event is the same as the one running when we added the handlers
if (event === currentEvent) {
currentEvent = undefined;
return;
}

listener(event);
};

if (!disabled) {
element?.addEventListener('click', listener);
element?.addEventListener('touchstart', listener);
element?.addEventListener('click', conditionalHandler);
element?.addEventListener('touchstart', conditionalHandler);
}

return () => {
element?.removeEventListener('click', listener);
element?.removeEventListener('touchstart', listener);
element?.removeEventListener('click', conditionalHandler);
element?.removeEventListener('touchstart', conditionalHandler);
};
}, [listener, element, disabled]);
};

const getWindowEvent = (target: Node | Window): Event | undefined => {
if (target) {
if (typeof (target as Window).window === 'object' && (target as Window).window === target) {
// eslint-disable-next-line deprecation/deprecation
return target.event;
}

// eslint-disable-next-line deprecation/deprecation
return (target as Node).ownerDocument?.defaultView?.event ?? undefined;
}

return undefined;
};

0 comments on commit c0764c6

Please sign in to comment.