I hope the title properly described the situation I have.
I am a self-taught learner. Forgive me if the title is incorrect. I would appreciate hearing all your comments.
Symptom:
When I click the hamburger menu on the mobile page’s Navigation bar, it disappears and the menu popup function does not work either.
Here is the code or github
Navbar.js
import React, { Component } from "react";
import { MenuItems } from "./MenuItems";
import "./Navbar.css";
class Navbar extends Component {
state = { clicked: false };
handleClick = () => {
this.setState({ clicked: !this.state.clicked });
};
render() {
return(
<nav className="NavbarItems">
<h1 className="navbar__logo">
<a href="/" style={{ textDecoration: "none"}}>JW</a>
</h1>
<div className="menu__icon" onClick={this.handleClick}>
{/* for the hamburger (bar) menu animation */}
<i className={this.state.clicked ? "fas fa-times" : "fas fa-bars"}></i>
</div>
<ul className={this.state.clicked ? "nav__menu active" : "nav__menu"}>
{MenuItems.map((item, index) => {
return (
<li key={index}>
<a
className={item.cName}
href={item.url}>
{item.title}
</a>
</li>
)
})}
</ul>
</nav>
)
}
}
export default Navbar;
MenuItems.js
export const MenuItems = [
{
title: 'Skills',
url: '#skills',
cName: 'nav__links'
},
{
title: 'Projects',
url: '#work',
cName: 'nav__links'
},
{
title: 'Contact',
url: '#contact',
cName: 'nav__links'
},
]
Navbar.css
.NavbarItems {
/* background: linear-gradient(90deg, rgb(110, 94, 254) 0%, rgba(73, 63, 252, 1) 100%); */
height: 80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.2rem;
overflow: hidden;
background-color: white;
position: fixed;
top: 0;
width: 100%;
}
.navbar__logo {
color: #fff;
justify-self: start;
margin-left: 20px;
cursor: pointer;
}
.fa__react {
margin-left: 0.5rem;
font-size: 1.6rem;
}
.nav__menu {
display: grid;
grid-template-columns: repeat(5, auto);
grid-gap: 10px;
list-style: none;
text-align: center;
width: 70vw;
justify-content: end;
margin-right: 2rem;
}
.nav__links {
color: black;
text-decoration: none;
padding: 0.5rem 1rem;
font-weight: bold;
}
.nav__links:hover {
background-color: #6D76F7;
border-radius: 4px;
transition: all 0.2s ease-out;
}
.fa__bars {
color: #fff;
}
.nav__links__mobile {
display: none;
}
.menu__icon {
display: none;
}
/* mobile start */
@media screen and (max-width: 960px) {
.NavbarItems {
position: relative;
}
.nav__menu {
display: flex;
flex-direction: column;
width: 100%;
height: 500px;
position: absolute;
top: 80px;
left: -100%;
opacity: 1;
transition: all 0.5s ease;
}
.nav__menu.active {
background: #6668f4;
left: 0;
opacity: 1;
transition: all 0.5s ease;
z-index: 1;
}
.nav__links {
text-align: center;
padding: 2rem;
width: 100%;
display: table;
}
.nav__links:hover {
background-color: #7577fa;
border-radius: 0;
}
.navbar__logo {
position: absolute;
top: 0;
left: 0;
transform: translate(25%, 50%)
}
.menu__icon {
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 60%);
font-size: 1.8rem;
cursor: pointer;
}
.fa-times {
color: #fff;
font-size: 2rem;
}
.nav__links__mobile {
display: block;
text-align: center;
padding: 1.5rem;
margin: 2rem auto;
border-radius: 4px;
width: 80%;
background: #4ad9e4;
text-decoration: none;
color: #fff;
font-size: 1.5rem;
}
.nav__links__mobile:hover {
background: #fff;
color: #6568F4;
transition: 250ms;
}
Button {
display: none;
}
}