Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hamburger-menu #255

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions components/Navbar/navDrop.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className='absolute ml-[-16px] top-16 w-full bg-[#1B1130]'>
<div ref ={ref} className='absolute ml-[-16px] top-16 w-full bg-[#1B1130]'>
<div className='flex flex-col p-4 pb-8 w-full'>
{links.map((link) => {
return (
Expand Down Expand Up @@ -54,6 +56,7 @@ function NavDrop({setDrop}) {
</div>
</div>
);
}
})
NavDrop.displayName = 'NavDrop';

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
10 changes: 6 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,7 @@ function Hamburger({className}) {
</g>
</svg>
);
}
})
Hamburger.displayName = 'Hamburger';

export default Hamburger
2 changes: 1 addition & 1 deletion package-lock.json

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