Last active
March 28, 2019 17:43
-
-
Save andrei-cacio/c54173c36a422f194a808628a0e38430 to your computer and use it in GitHub Desktop.
Revisions
-
andrei-cacio revised this gist
Mar 19, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ function ClickOutsideH({ children, onClick }) { return function() { document.removeEventListener("click", handleClick); }; }, []); return React.Children.map(children, (element, idx) => React.cloneElement(element, { ref: refs[idx] }) -
andrei-cacio created this gist
Mar 19, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ import React, { Component, useEffect, useCallback } from "react"; function ClickOutsideH({ children, onClick }) { const refs = React.Children.map(children, () => React.createRef()); const handleClick = useCallback(e => { const isOutside = refs.every(ref => { return !ref.current.contains(e.target); }); if (isOutside) { onClick(); } }, []); useEffect(() => { document.addEventListener("click", handleClick); return function() { document.removeEventListener("click", handleClick); }; }); return React.Children.map(children, (element, idx) => React.cloneElement(element, { ref: refs[idx] }) ); }