-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close the new USWDS page header dropdown when clicking outside of it [#1296](#1296) ### Validation / Testing Open one of the two drop down options, click on another link on the page. When the browser navigates to the new page that original dropdown menu should be closed.
- Loading branch information
Showing
2 changed files
with
29 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useEffect, useRef } from 'react'; | ||
export const useClickOutside = (onClose: () => void) => { | ||
const ref = useRef<HTMLDivElement>(null); | ||
useEffect(() => { | ||
const handleClickOutside = (event: MouseEvent) => { | ||
if (ref.current && !ref.current.contains(event.target as Node)) { | ||
onClose(); | ||
} | ||
}; | ||
document.addEventListener('click', handleClickOutside, true); | ||
return () => | ||
document.removeEventListener('click', handleClickOutside, true); | ||
}, [onClose]); | ||
return ref; | ||
}; |