Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
feat(useOnClickOutside): Allow passing multiple refs
Browse files Browse the repository at this point in the history
  • Loading branch information
diondiondion committed Nov 12, 2020
1 parent 7bfd92a commit bf3acac
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/useOnClickOutside/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ import useEventListener from '../useEventListener';

export function getOutsideTargetCallback(excludedElementRef, callback) {
return event => {
const excludedElement =
excludedElementRef && excludedElementRef.current;

// Bail out if the clicked element or the currently focused element
// is inside of excludedElement. We need to check the focused element
// to prevent an issue in Chrome where initiating a drag inside of an
// input (to select the text inside of it) and ending that drag outside
// of the input fires a click event, breaking our excludedElement rule.
if (
excludedElement &&
(excludedElement === event.target ||
excludedElement.contains(event.target) ||
excludedElement === document.activeElement ||
excludedElement.contains(document.activeElement))
) {
return null;
const excludedRefs = Array.isArray(excludedElementRef)
? excludedElementRef
: [excludedElementRef];

for (const ref of excludedRefs) {
const excludedElement = ref && ref.current;

// Bail out if the clicked element or the currently focused element
// is inside of excludedElement. We need to check the focused element
// to prevent an issue in Chrome where initiating a drag inside of an
// input (to select the text inside of it) and ending that drag outside
// of the input fires a click event, breaking our excludedElement rule.
if (
excludedElement &&
(excludedElement === event.target ||
excludedElement.contains(event.target) ||
excludedElement === document.activeElement ||
excludedElement.contains(document.activeElement))
) {
return null;
}
}

callback(event);
Expand Down

0 comments on commit bf3acac

Please sign in to comment.