Created
February 23, 2022 18:34
-
-
Save gsambrotta/4e6af036e7be5de13d0259b297c8a89a to your computer and use it in GitHub Desktop.
Tailwind React Hamburger Menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// full code link: https://codesandbox.io/s/tailwind-react-hamburger-menu-tjhfyx | |
import { useState } from "react"; // import state | |
export default function Header() { | |
const [isNavOpen, setIsNavOpen] = useState(false); // initiate isNavOpen state with false | |
return ( | |
<div className="flex items-center justify-between border-b border-gray-400 py-8"> | |
<a href="/"> | |
<img src="https://designbygio.it/images/logo.png" alt="logo" /> | |
</a> | |
<nav> | |
<section className="MOBILE-MENU flex lg:hidden"> | |
<div | |
className="HAMBURGER-ICON space-y-2" | |
onClick={() => setIsNavOpen((prev) => !prev)} // toggle isNavOpen state on click | |
> | |
<span className="block h-0.5 w-8 animate-pulse bg-gray-600"></span> | |
<span className="block h-0.5 w-8 animate-pulse bg-gray-600"></span> | |
<span className="block h-0.5 w-8 animate-pulse bg-gray-600"></span> | |
</div> | |
<div className={isNavOpen ? "showMenuNav" : "hideMenuNav"}> // toggle class based on isNavOpen state | |
<div | |
className="CROSS-ICON absolute top-0 right-0 px-8 py-8" | |
onClick={() => setIsNavOpen(false)} // change isNavOpen state to false to close the menu | |
> | |
<svg | |
className="h-8 w-8 text-gray-600" | |
viewBox="0 0 24 24" | |
fill="none" | |
stroke="currentColor" | |
strokeWidth="2" | |
strokeLinecap="round" | |
strokeLinejoin="round" | |
> | |
<line x1="18" y1="6" x2="6" y2="18" /> | |
<line x1="6" y1="6" x2="18" y2="18" /> | |
</svg> | |
</div> | |
<ul className="MENU-LINK-MOBILE-OPEN flex flex-col items-center justify-between min-h-[250px]"> | |
<li className="border-b border-gray-400 my-8 uppercase"> | |
<a href="/about">About</a> | |
</li> | |
<li className="border-b border-gray-400 my-8 uppercase"> | |
<a href="/portfolio">Portfolio</a> | |
</li> | |
<li className="border-b border-gray-400 my-8 uppercase"> | |
<a href="/contact">Contact</a> | |
</li> | |
</ul> | |
</div> | |
</section> | |
<ul className="DESKTOP-MENU hidden space-x-8 lg:flex"> | |
<li> | |
<a href="/about">About</a> | |
</li> | |
<li> | |
<a href="/portfolio">Portfolio</a> | |
</li> | |
<li> | |
<a href="/contact">Contact</a> | |
</li> | |
</ul> | |
</nav> | |
<style>{` | |
.hideMenuNav { | |
display: none; | |
} | |
.showMenuNav { | |
display: block; | |
position: absolute; | |
width: 100%; | |
height: 100vh; | |
top: 0; | |
left: 0; | |
background: white; | |
z-index: 10; | |
display: flex; | |
flex-direction: column; | |
justify-content: space-evenly; | |
align-items: center; | |
} | |
`}</style> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment