Design comparison
Solution retrospective
Finally i managed to make the hamburger menu working! Happy with my progress.
Community feedback
- @marcoberdianoPosted over 2 years ago
Hello Marge C. Thanks for your the suggestions. I did exatly what you propose but still have some problems. But i'm working on it, i made some progress.
1 - @msunjiPosted over 2 years ago
Heya Marco! I know responsive nav bars aren't always the easiest, so great job on this challenge! 👍
I checked out your code and this is what I've done for the menu.
- Instead of using
checkSize
and theuseEffect
hook to toggle your menu, you can use CSS classes. In your media query inNavBar.css
, setnav-links
todisplay: none
, but also create another class calledactive
withdisplay: flex
as a declaration. Something like this:
.nav-links { // your other CSS declarations display: none; } .active { display: flex; }
The idea here is that when you click on the hamburger menu icon, you want to toggle some piece of state which should then set or not set the
active
class.- Add a class to your hamburger icon, then in
NavBar.css
, set it todisplay: none
on larger screen sizes anddisplay: block
on smaller screen sizes. This way the hamburger icon only shows up on smaller screens. - In
NavBar.jsx
, setshowMenu
's default state to false and then make an event handlerhandleMobileMenu
that toggles theshowMenu
state value. Then add anonClick
event handler to your hamburger icon div and passhandleMobileMenu
to it. It'll look something like this:
const handleMobileMenu = () => { setShowMenu(!showMenu); }; <div className="hamburger" onClick={handleMobileMenu}> // your icon </div>
- By the way, I imported
NavLinks
intoNavBar.jsx
instead ofNormalMenu
, then revised your code, so the end product looks like this (which I'll explain a bit below):
<div> <nav className="nav-container"> <div className="logo"> <img src={logo} alt={logo}></img> </div> <NavLinks showMenu={showMenu} /> <div className="hamburger" onClick={handleMobileMenu}> <img src={iconMenu} alt={iconMenu}></img> </div> </nav> </div>
- Pass the value of
showMenu
to yourNavLinks
component. You'll use this to toggle theactive
class you made earlier. - In your
NavLinks
component, toggle the class name like so:
<div className={`nav-links ${showMenu ? 'active' : ''}`}> // your code </div>
This should sort out your issue for the most part, but for positioning and stuff, I'll leave that to you. Sorry this explanation got super long! If it's not very clear and you have any questions, feel free to reach out to me. Hope this helped!
0 - Instead of using
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord