-
-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: close navbar on clicking outside (#788)
Co-authored-by: Tamal Das <[email protected]>
- Loading branch information
1 parent
2ad7bf7
commit 9a3f2ab
Showing
4 changed files
with
129 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React, { useEffect } from "react"; | ||
|
||
export default function ClickAwayListener(props) { | ||
const { children, onClickAway } = props; | ||
|
||
useEffect(() => { | ||
function handleClick(event) { | ||
// Check if the clicked element is inside the component | ||
if (!event.target.closest(".click-away-listener")) { | ||
// If it's not, call the onClickAway function | ||
onClickAway(); | ||
} | ||
} | ||
|
||
document.addEventListener("mousedown", handleClick); | ||
|
||
return () => { | ||
document.removeEventListener("mousedown", handleClick); | ||
}; | ||
}, [onClickAway]); | ||
|
||
return <div className="click-away-listener">{children}</div>; | ||
} |