From aecd8dcef5fb0c1981f75b57e8285f529e56c74c Mon Sep 17 00:00:00 2001 From: VividTruthKeeper Date: Mon, 5 Sep 2022 14:35:37 +0300 Subject: [PATCH] click outside handled --- src/components/global/Nav.tsx | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/components/global/Nav.tsx b/src/components/global/Nav.tsx index 055586d..60c55d1 100644 --- a/src/components/global/Nav.tsx +++ b/src/components/global/Nav.tsx @@ -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(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 (