Skip to content

Commit

Permalink
fixed mobile menu close after click at nav link
Browse files Browse the repository at this point in the history
  • Loading branch information
vinib96 committed Jun 3, 2024
1 parent 9d6faa2 commit fbd437d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
14 changes: 9 additions & 5 deletions Frontend/src/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function Header() {
};

const toggleCart = () => {
console.log(calculateTotalItems(cartItems))
console.log(calculateTotalItems(cartItems));
setIsCartOpen((prevState) => !prevState);
};

Expand Down Expand Up @@ -91,13 +91,17 @@ function Header() {
{!isLoginPage && !isRegisterPage && isLoggedIn && (
<>
{' '}
<button onClick={toggleCart} style={{position: 'relative'}}>
<button onClick={toggleCart} style={{ position: 'relative' }}>
<img
src='/assets/shopping_bag.svg'
alt='account icon'
className='account-icon'
/>
{calculateTotalItems(cartItems) !==0 && <div className='header__cart-quantity'><p>{calculateTotalItems(cartItems)}</p></div>}
{calculateTotalItems(cartItems) !== 0 && (
<div className='header__cart-quantity'>
<p>{calculateTotalItems(cartItems)}</p>
</div>
)}
</button>
<button
className='header__user-button'
Expand Down Expand Up @@ -162,8 +166,8 @@ function Header() {
</div>
<Nav />
<div className={menuOpen ? 'header__menu_open' : 'header__menu'}>
<Search menuOpen={menuOpen} />
<Nav menuOpen={menuOpen} />
<Search menuOpen={menuOpen} setMenuOpen={setMenuOpen} />
<Nav menuOpen={menuOpen} setMenuOpen={setMenuOpen} />
</div>{' '}
{isCartOpen && (
<Cart
Expand Down
34 changes: 22 additions & 12 deletions Frontend/src/components/Nav.jsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import './Nav.css';
import { Link } from 'react-router-dom';
function Nav({ menuOpen }) {
const handleClick = () => {
import { Link, useNavigate } from 'react-router-dom';
function Nav({ menuOpen, setMenuOpen }) {
const navigate = useNavigate();

const handleClick = (path) => {
setMenuOpen(false);
navigate(path);
};

const handleScrollToFooter = () => {
setMenuOpen(false);
const footer = document.getElementById('footer');
footer.scrollIntoView({ behavior: 'smooth' });
if (footer) {
footer.scrollIntoView({ behavior: 'smooth' });
}
};
return (
<nav>
<ul className={menuOpen ? 'header__hamburger' : 'nav'}>
<li className='nav__li'>
<Link className='nav__link' to={'/store'}>
<button className='nav__link' onClick={() => handleClick('/store')}>
LOJA
</Link>
</button>
</li>
<li className='nav__li'>
<Link className='nav__link' to={'/sobre'}>
<button className='nav__link' onClick={() => handleClick('/sobre')}>
SOBRE
</Link>
</button>
</li>
<li className='nav__li'>
<Link className='nav__link' to={'/maps'}>
<button className='nav__link' onClick={() => handleClick('/maps')}>
PONTOS DE COLETA
</Link>
</button>
</li>
<li className='nav__li'>
<a onClick={handleClick} className='nav__link'>
<button className='nav__link' onClick={handleScrollToFooter}>
CONTATO
</a>
</button>
</li>
</ul>
</nav>
Expand Down
3 changes: 2 additions & 1 deletion Frontend/src/components/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './Search.css';
import { useState } from 'react';
import { useNavigate, useLocation, useSearchParams } from 'react-router-dom';

function Search({ menuOpen }) {
function Search({ menuOpen, setMenuOpen }) {
const [searchParams, setSearchParams] = useSearchParams({ q: '' });
const navigate = useNavigate();
const [searchFile, setSearchFile] = useState('');
Expand All @@ -17,6 +17,7 @@ function Search({ menuOpen }) {
setSearchParams('q', searchFile);
navigate(`/store?q=${searchFile}`);
setSearchFile('');
setMenuOpen(false);
};
return (
<div className={menuOpen ? 'header__hamburger' : 'search-container'}>
Expand Down

0 comments on commit fbd437d

Please sign in to comment.