From 18bdbd812ef103fd12a00c544019e0806e8f394c Mon Sep 17 00:00:00 2001 From: PUNEET-EMM <98829227+PUNEET-EMM@users.noreply.github.com> Date: Wed, 13 Dec 2023 00:36:10 +0530 Subject: [PATCH] fix: hamburger-menu (#255) * FixHamburgerMenu * FixHamburger-Menu * Fix-checks * Fix-changes-package.lock * Fixchanges-package.lock --- components/Navbar/navDrop.js | 13 ++++++++----- components/Navbar/navbar.js | 27 ++++++++++++++++++++++----- components/illustration/hamburger.js | 10 ++++++---- package-lock.json | 2 +- 4 files changed, 37 insertions(+), 15 deletions(-) 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