-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(popover-container): ensure clicks inside popover do not close it
Fix bug where portals inside a component did not automatically work with useClickAwayListener. Refactor useClickAwayListener to no longer take an array of refs and return a handler which can be attached to the parent component and stops the clickAway handler firing on the document - this automatically handlers any React children no matter whether they are rendered as DOM children or not. Remove any refs in parent components which are now unneeded. fix 5322
- Loading branch information
1 parent
32aa0ef
commit 48808bc
Showing
11 changed files
with
101 additions
and
49 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
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
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
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
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
43 changes: 26 additions & 17 deletions
43
src/hooks/__internal__/useClickAwayListener/useClickAwayListener.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,38 @@ | ||
import React, { useEffect, useRef } from "react"; | ||
import { useEffect, useRef, useCallback } from "react"; | ||
|
||
// Needs to also take Portals into account (so can't just check DOM containment), but ideally without using | ||
// event.stopPropagation() which could have unexpected and frustrating consequences for consumers. | ||
// Simple approach taken from https://github.com/facebook/react/issues/10962#issuecomment-444622208 | ||
|
||
export default ( | ||
targets: React.RefObject<HTMLElement>[], | ||
handleClickAway: (ev: Event) => void, | ||
eventTypeId: "mousedown" | "click" = "click" | ||
) => { | ||
const targetsRef = useRef(targets); | ||
targetsRef.current = targets; | ||
const clickIsInside = useRef(false); | ||
|
||
useEffect(() => { | ||
const fnClickAway = (ev: Event) => { | ||
const clickedElements = targetsRef.current.filter( | ||
(targetRef: React.RefObject<HTMLElement>) => | ||
targetRef.current?.contains(ev?.target as Node) | ||
); | ||
|
||
if (!clickedElements?.length) { | ||
handleClickAway(ev); | ||
const onDocumentClick = useCallback( | ||
(ev: Event) => { | ||
if (clickIsInside.current) { | ||
clickIsInside.current = false; | ||
return; | ||
} | ||
}; | ||
|
||
document.addEventListener(eventTypeId, fnClickAway as EventListener); | ||
handleClickAway(ev); | ||
}, | ||
[handleClickAway] | ||
); | ||
|
||
const onInsideClick = useCallback(() => { | ||
clickIsInside.current = true; | ||
}, []); | ||
|
||
useEffect(() => { | ||
document.addEventListener(eventTypeId, onDocumentClick); | ||
|
||
return function cleanup() { | ||
document.removeEventListener(eventTypeId, fnClickAway as EventListener); | ||
document.removeEventListener(eventTypeId, onDocumentClick); | ||
}; | ||
}, [handleClickAway, eventTypeId]); | ||
}, [onDocumentClick, eventTypeId]); | ||
|
||
return onInsideClick; | ||
}; |