From 9f1ae34f2b96db58d6c1087ed2b5ee7ab708cfe3 Mon Sep 17 00:00:00 2001 From: Shashwat Singh Date: Wed, 15 Nov 2023 18:13:53 +0530 Subject: [PATCH] Fixed the closing issue --- components/Navbar/navbar.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/components/Navbar/navbar.js b/components/Navbar/navbar.js index 59ec489c..7343563f 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,17 @@ function Navbar() { const isTablet = useMediaQuery({ maxWidth: '1118px' }); const [drop, setDrop] = useState(false); const [show, setShow] = useState(null); + useEffect(() => { + function handleClosing(event) { + if (show && !event.target.closest('.subMenu')) { + setShow(false); + } + } + document.addEventListener('mousedown', handleClosing); + return () => { + document.removeEventListener('mousedown', handleClosing); + }; + }, [show]); return (
@@ -43,16 +54,16 @@ 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 && ( )}
@@ -61,7 +72,7 @@ function Navbar() { )}
{show && show === link.title && link.subMenu && ( -
+
{link.subMenu.map((subL) => (
@@ -83,4 +94,4 @@ function Navbar() { ); } -export default Navbar; +export default Navbar; \ No newline at end of file