diff --git a/components/Navbar/navbar.js b/components/Navbar/navbar.js index 59ec489c..b490c498 100644 --- a/components/Navbar/navbar.js +++ b/components/Navbar/navbar.js @@ -1,6 +1,6 @@ import Link from 'next/link'; import Dropdown from '../illustration/dropdown'; -import { useState } from 'react'; +import { useState,useEffect } from 'react'; import links from '../../config/links.json'; import NavDrop from './navDrop'; import Hamburger from '../illustration/hamburger'; @@ -12,6 +12,19 @@ function Navbar() { const isTablet = useMediaQuery({ maxWidth: '1118px' }); const [drop, setDrop] = useState(false); const [show, setShow] = useState(null); + useEffect(() => { + function handleOutsideClick(event) { + console.log(event.target.closest('.subMenu')) + if (show && !event.target.closest('.subMenu')) { + setShow(false); + } + } + + document.addEventListener('mousedown', handleOutsideClick); + return () => { + document.removeEventListener('mousedown', handleOutsideClick); + }; + }, [show]); return (
@@ -43,11 +56,11 @@ function Navbar() { onClick={() => show === link.title ? setShow(null) : setShow(link.title) } - className='text-[#F0F4F5] ml-16 text-[15px] cursor-pointer relative flex flex-col' + className='text-[#F0F4F5] ml-16 text-[15px] group cursor-pointer relative flex flex-col' >
{link.subMenu ? ( -
+
{link.title}{' '} {link.subMenu && ( {show && show === link.title && link.subMenu && ( -
+
{link.subMenu.map((subL) => (