diff --git a/components/Navbar/navDrop.js b/components/Navbar/navDrop.js index 9b0cdbe3..d0d3984d 100644 --- a/components/Navbar/navDrop.js +++ b/components/Navbar/navDrop.js @@ -1,12 +1,14 @@ -import React,{useState} from 'react'; +import React,{useState,forwardRef} from 'react'; import links from '../../config/links.json'; import Link from 'next/link'; import Dropdown from '../illustration/dropdown'; -function NavDrop({setDrop}) { + +const NavDrop = forwardRef((props, ref)=> { + const {setDrop}=props; const [show, setShow] = useState(null); return ( -
+
{links.map((link) => { return ( @@ -54,6 +56,7 @@ function NavDrop({setDrop}) {
); -} +}) +NavDrop.displayName = 'NavDrop'; -export default NavDrop \ No newline at end of file +export default NavDrop; \ No newline at end of file diff --git a/components/Navbar/navbar.js b/components/Navbar/navbar.js index 148d1da7..5d475240 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,useEffect } from 'react'; +import { useState,useEffect,useRef} from 'react'; import links from '../../config/links.json'; import NavDrop from './navDrop'; import Hamburger from '../illustration/hamburger'; @@ -13,6 +13,8 @@ function Navbar() { const isTablet = useMediaQuery({ maxWidth: '1118px' }); const [drop, setDrop] = useState(false); const [show, setShow] = useState(null); + const menuRef = useRef(null); + const svg = useRef(null); function handleClosing(event) { if (show && !event.target.closest('.subMenu')) { setShow(false); @@ -21,6 +23,21 @@ function Navbar() { useEffect(() => { document.addEventListener('mousedown', handleClosing); }, [show]); + + const handleCloseMenu = (event) => { + if (menuRef .current && !menuRef .current.contains(event.target)) { + setDrop(false); + }if(svg.current&&event.target==svg.current){ + setDrop(true); + } + }; + + useEffect(() => { + document.addEventListener('click', handleCloseMenu); + return () => { + document.removeEventListener('click', handleCloseMenu); + }; + }, [menuRef]); return (
@@ -35,12 +52,12 @@ function Navbar() { {isTablet ? (
{drop ? ( - ) : ( - )}
@@ -85,7 +102,7 @@ function Navbar() { ))}
)} - {isTablet && drop && } + {isTablet && drop && }
diff --git a/components/illustration/hamburger.js b/components/illustration/hamburger.js index 8fd82128..1bb1e4af 100644 --- a/components/illustration/hamburger.js +++ b/components/illustration/hamburger.js @@ -1,8 +1,9 @@ -import React from 'react' +import React,{forwardRef} from 'react' -function Hamburger({className}) { +const Hamburger = forwardRef((props, ref)=> { + const {className} = props; return ( - ); -} +}) +Hamburger.displayName = 'Hamburger'; export default Hamburger \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d6b5e6c1..a43cf06d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5909,4 +5909,4 @@ } } } -} +} \ No newline at end of file