click outside handled

This commit is contained in:
VividTruthKeeper 2022-09-05 14:35:37 +03:00
parent ab2e4e3e5f
commit aecd8dcef5
1 changed files with 9 additions and 6 deletions

View File

@ -1,5 +1,5 @@
// Modules
import { useRef, useEffect } from 'react';
import { useRef, useEffect } from "react";
import { useLocation, Link } from "react-router-dom";
// Icons
@ -20,14 +20,17 @@ const Nav = ({
const refTrigger = useRef<any>(null);
useEffect(() => {
if(ref.current && refTrigger.current){
window.addEventListener('click', (e:any) => {
if(!e.target.contains(ref.current) && !e.target.contains(refTrigger.current)){
if (ref.current && refTrigger.current) {
window.addEventListener("click", (e: any) => {
if (
!e.target.contains(ref.current) &&
!e.target.contains(refTrigger.current)
) {
setDropdown(false);
}
})
});
}
}, [setDropdown, ref])
}, [setDropdown, ref]);
return (
<nav className="nav">