Skip to content

Commit

Permalink
FixHamburgerMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
PUNEET-EMM committed Dec 7, 2023
1 parent 8e25866 commit 3e694d2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 47 deletions.
10 changes: 6 additions & 4 deletions components/Navbar/navDrop.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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 (
<div className='absolute ml-[-16px] top-16 w-full bg-[#1B1130]'>
Expand Down Expand Up @@ -54,6 +56,6 @@ function NavDrop({setDrop}) {
</div>
</div>
);
}
})

export default NavDrop
export default NavDrop;
27 changes: 22 additions & 5 deletions components/Navbar/navbar.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand All @@ -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 (
<div className='container flex justify-center items-center sticky top-0 backdrop-blur z-[99]'>
<div className='w-[1131px]'>
Expand All @@ -35,12 +52,12 @@ function Navbar() {
{isTablet ? (
<div>
{drop ? (
<button onClick={() => setDrop(false)}>
<button>
<Cancel />
</button>
) : (
<button onClick={() => setDrop(true)}>
<Hamburger />
<button>
<Hamburger ref={svg}/>
</button>
)}
</div>
Expand Down Expand Up @@ -85,7 +102,7 @@ function Navbar() {
))}
</div>
)}
{isTablet && drop && <NavDrop setDrop={setDrop} />}
{isTablet && drop && <NavDrop setDrop={setDrop} ref={menuRef} />}
</div>
</div>
</div>
Expand Down
9 changes: 5 additions & 4 deletions components/illustration/hamburger.js
Original file line number Diff line number Diff line change
@@ -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 (
<svg
<svg ref ={ref}
width='30px'
height='30px'
viewBox='0 0 24 24'
Expand Down Expand Up @@ -40,6 +41,6 @@ function Hamburger({className}) {
</g>
</svg>
);
}
})

export default Hamburger
34 changes: 0 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e694d2

Please sign in to comment.