-
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.
Merge pull request #5347 from Sage/FE-5282-popover-container-click-in…
…side-portal fix(popover-container): ensure clicks inside popover do not close it
- Loading branch information
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; | ||
}; |