Hamburger menu logic not working properly in React - reactjs

I have set up the logic for Hamburger menu in Navbar.jsx but when I press the hamburger button it just transitions to X button and doesn't display links menu. How do I display the menu and what would be your approach to animating the transition between hamburger and X icon. Here is my code:
import React from 'react'
import './Navbar.css'
import { useState } from 'react'
import { FaSearch } from 'react-icons/fa'
import { NavLink } from 'react-router-dom'
import { GiHamburgerMenu } from 'react-icons/gi'
import { AiOutlineClose } from 'react-icons/ai'
function Navbar() {
const [toggleMenu, setToggleMenu] = useState(false)
const [toggleSearch, setSearchWindow] = useState(false)
return (
<div className='mka__navbar'>
<div className='mka__navbar-links'>
<div className='mka__navbar-links_container'>
<p><NavLink to='/'>HOME</NavLink></p>
<p><NavLink to='/fahrzeugangebote'>FAHRZEUGANGEBOTE</NavLink></p>
<p><NavLink to='/finanzierung'>FINANZIERUNG</NavLink></p>
<p><NavLink to='/fahrzeugankauf'>FAHRZEUGANKAUF</NavLink></p>
<p><NavLink to='/galerie'>GALERIE</NavLink></p>
<p><NavLink to='/kontakt'>KONTAKT</NavLink></p>
<div className='mka__search_column'>
<p><i><FaSearch onClick={() => setSearchWindow(!toggleSearch)} /></i></p>
{
toggleSearch ?
<div className='mka__search-div'>
< input type="text" value="Suche..."/>
</div>
: null
}
</div>
</div>
{
toggleMenu
? <AiOutlineClose color="#fff" className='mka___hamburger-menu' size={27} onClick={() => setToggleMenu(false)} />
: <GiHamburgerMenu size={27} className='mka___hamburger-menu' color="#fff" onClick={() => setToggleMenu(true)} />
}
{
<div className='mka__navbar-menu-margins'>
<div className='mka__navbar-menu_container'>
<div className='mka__navbar-menu_container-links'>
<p><NavLink to='/'>HOME</NavLink></p>
<p><NavLink to='/fahrzeugangebote'>FAHRZEUGANGEBOTE</NavLink></p>
<p><NavLink to='/finanzierung'>FINANZIERUNG</NavLink></p>
<p><NavLink to='/fahrzeugankauf'>FAHRZEUGANKAUF</NavLink></p>
<p><NavLink to='/galerie'>GALERIE</NavLink></p>
<p><NavLink to='/kontakt'>KONTAKT</NavLink></p>
</div>
</div>
</div> && toggleMenu
}
</div>
</div>
);
}
export default Navbar;
And here is my CSS code for the same component:
/* Navbar links */
.mka__navbar {
display: flex;
padding: 2rem;
}
.mka__navbar-links {
flex: 1;
display: flex;
justify-content: end;
align-items: center;
}
.mka__navbar-links_container {
display: flex;
flex-direction: row;
}
.mka__navbar-links_container p {
color: #fff;
font-size: 14px;
line-height: 66px;
text-transform: uppercase;
font-weight: 400;
font-family: var(--font-family);
transition: padding .25s linear;
padding: 0 0.5rem;
cursor: pointer;
}
.mka__navbar-links_container p > a:link {
color: #fff;
}
.mka__navbar-links_container p > a:hover {
transition: 0.5s;
color: #db2d2e;
}
.mka__navbar-links_container p > i:hover {
transition: 0.5s;
color: #db2d2e;
}
.mka__navbar-links_container p > a.active {
color: #db2d2e;
}
/* Hamburger menu */
.mka__navbar-menu_container-links {
color: #fff;
font-size: 14px;
line-height: 66px;
text-transform: uppercase;
font-weight: 400;
font-family: var(--font-family);
padding: 0 1.5rem;
cursor: pointer;
width: 100%;
}
/* Search */
.mka__search-div {
background: #0e0e0ed1;
display: block;
padding: 15px;
width: 327px;
z-index: 888;
border-radius: 0;
border-top: 5px solid #db2d2e;
position: absolute;
margin-left: auto;
margin-right: auto;
left: 26rem;
right: 0;
top: 9rem;
}
input {
border: 1px solid #fff;
background: #0f1215;
color: gray;
padding: 12px;
width: 290px;
}
.mka__search_column {
display: flex;
flex-direction: column;
}
/* Media Queries */
#media (min-width: 992px){
.mka__navbar-links {
justify-content: center;
}
.mka__navbar-menu_container-links {
display: none;
}
.mka___hamburger-menu {
display: none;
}
}
#media (max-width: 992px){
.mka__navbar-links_container {
display: none;
}
.mka__navbar-menu_container {
background-color: #0f1215;
width: 100%;
margin-top: 0.5rem;
border-radius: 5px;
}
.mka__navbar-menu-margins {
margin-left: auto;
margin-right: auto;
padding-left: 15px;
padding-right: 15px;
width: 100%;
margin-top: 40px;
}
}
#media (min-width: 1200px){
.mka__navbar-links_container p {
padding: 0 1rem;
}
}
#media (max-width: 1200px){
.mka__search-div {
left: 21rem;
}
}

I created a hamburger menu which you can toggle it with X icon. and also implemented the drawer transition. the link is here https://codesandbox.io/s/great-hugle-oy5s8b?file=/src/App.js
hope it can help you.

Related

how can i move image a very little bit up and add text below it { note i am talking about the highlighted profile image with blue }

how can i move image a very little bit up and add text below it { note i am talking about the highlighted profile image with blue } [image here][1] also see my code and tell every step to do that i have tired margin button but it didn't work
`import { Header } from "../../features/theme/Header";
import styles from "./Home.module.css";
import { useAppSelector } from "../../app/hooks";
import { Feed } from "../../features";
export function Home(): JSX.Element {
const { currentUserImage } = useAppSelector((state) => state.currentUser);
return (
<div className={styles.home}>
<Header page="Home" />
<div className={styles.tweetField}>
<div className={styles.userAvatar}>
<img src={currentUserImage} alt="" />
</div>
<div>
<div>
</div>
</div>
</div>
<Feed />
</div>
);
}
my css code
.home{
border-right: solid 0.2px var(--border-color);
}
.homeHeader{
display: flex;
align-items: center;
justify-content: space-between;
padding: 1rem;
border-bottom: solid 0.2px var(--border-color);
font-size: 1.2rem;
font-weight: bold;
}
.homeHeader button{
border: none;
background: none;
font-size: 1.3rem;
color: var(--theme-page-text);
}
.tweetField{
display: flex;
margin-top: 1rem;
border-bottom: solid 0.2px var(--border-color);
}
.userAvatar{
outline: none;
width: 3rem;
height: 3rem;
border-radius: 50%;
margin-left: 1rem;
background: #fff;
}
.userAvatar img{
width: 3rem;
height: 3rem;
border-radius: 50%;
object-fit: cover;
background: #fff;
}
.tweetActions{
width: 100%;
margin: 1rem;
}
.tweetActions span{
font-size: 1.3rem;
color: var(--light-text-color);
}
.tweetActions div{
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 1rem;
}
.tweetActions button:first-child{
border: none;
background: none;
outline: none;
color: var(--primary-color);
font-size: 1.2rem;
}
.tweetActions button:last-child{
border: none;
outline: none;
color: #fff;
background: var(--primary-color);
opacity: 0.5;
font-size: 1rem;
font-weight: bold;
padding: 0.5rem 1.2rem;
border-radius: 2rem;
}
image here =
[1]: https://i.stack.imgur.com/JpZKd.png

Animation react Does not reset

I am working on animation in React app. I need animation starts working after hover off the button, the animation only works one time and after that for next time hover off the button does not make animation works.
I used
setTimeout(()=> {
e.target.classList.add(classes.hide);
}, 1);
to reset time to works again but it does not work out.
#keyframes rotate-before {
0% { right:0px; top:0px;}
25% { right:40px; top:0px;}
50% {bottom:10px; top:178px;right:40px;}
}
.content {
position: absolute;
inset: 30px;
z-index: 3;
display: flex;
flex-direction:column;
align-items: center;
justify-content: center;
}
.content:hover .button{
padding: .8rem .1rem;
background-color: #00092C;
border:none;
border-radius: 50%;
cursor: pointer;
color: #fff;
visibility:visible;
position: relative;
}
.hover{
padding: .8rem .1rem;
background-color: #00092C;
border:none;
border-radius: 50%;
cursor: pointer;
color: #fff;
visibility:visible;
position: relative;
animation: rotate-before 0.5s linear;
animation-iteration-count: 1;
}
.content .button{
padding: .8rem .1rem;
background-color: #00092C;
border:none;
border-radius: 50%;
cursor: pointer;
color: #fff;
position: relative;
visibility:hidden;
}
.hide{
padding: .8rem .1rem;
background-color: #00092C;
border:none;
border-radius: 50%;
cursor: pointer;
color: #fff;
position: relative;
visibility:hidden;
}
.content img {
position: absolute;
width: 100%;
height:100%;
object-fit: cover;
}
import "./App.css";
import { AnimatePresence, motion, useTransform } from "framer-motion/dist/framer-motion";
import classes from "./components/anm.module.css";
function App() {
const [isHovered, setHovered] = useState(false);
const change = (e) => {
e.target.classList.add(classes.hover);
setTimeout(()=> {
e.target.classList.add(classes.hide);
}, 1);
};
return (
<div className={classes.box}>
<div className={classes.content}>
<img
src="https://budgetreno.ca/wp-content/uploads/Pylon-25_compressed-thegem-portfolio-metro.jpg"
alt=""
/>
<motion.div className="square" onHoverEnd={change} whileHover={{}}>
<button id="test" className={classes.button}>
Follow
</button>
</motion.div>
</div>
</div>
);
}
export default App;

Pushing Sign Up & Login buttons to bottom of sidebar

I am trying to recreate this design:
Click to view
However, the below is currently occurring.
Click to view
I am trying to push the SignUp and Login buttons to the bottom of the sidebar to match the first image. Am I missing a CSS class that would allow me to do so, or would this be a JS configuration? Hope I have not overlooked something basic that would allow me to achieve this state.
Sidebar.js
import React from "react";
import "../App.css";
import { SidebarData } from './SidebarData'
import Logo from './Logo.svg'
import { Login } from './Login'
import { SignUp } from './SignUp'
function Sidebar() {
return (
<div className="Sidebar">
<div className="Header">
<div><img src = {Logo} alt='Logo’ className='Logo’ /></div>
</div>
<div class="line-break" />
<ul className="SidebarList">
{SidebarData.map((val, key) => {
return (
<li
key={key}
className="row"
id={window.location.pathname == val.link ? "active" : ""}
onClick={() => {
window.location.pathname = val.link;
}}
>
<div id="icon">{val.icon}</div> <div id="title">{val.title}</div>
</li>
);
})}
</ul>
<div class="line-break" />
<div className="footer">
<ul className= "SidebarList">
{Login.map((val, key) => {
return (
<li
key={key}
className="Login"
id={window.location.pathname == val.link ? "active" : ""}
onClick={() => {
window.location.pathname = val.link;
}}
>
<div id="title">{val.title}</div>
</li>
)}
)}
</ul>
<ul className= "SidebarList">
{SignUp.map((val, key) => {
return (
<li
key={key}
className="SignUp"
id={window.location.pathname == val.link ? "active" : ""}
onClick={() => {
window.location.pathname = val.link;
}}
>
<div id="title">{val.title}</div>
</li>
)}
)}
</ul>
</div>
</div>
Here is CSS
.App {
width: 100vw;
height: 100vh;
}
body {
margin: 0;
padding: 0;
}
.Sidebar {
height: 100%;
width: 300px;
background-color: white;
border-right: 1px solid #F0F4FB;
padding-left: 15px;
box-sizing: border-box;
}
.SidebarList {
height: auto;
width: 100%;
padding-left: 15px;
font-size: 18px;
border: 2px #FD954E;
box-sizing: border-box
}
.SidebarList .row {
width: 100%;
height: 50px;
background-color: white;
list-style-type: none;
margin: 0%;
padding-bottom: 15px;
display: flex;
color: #A7ACB6;
justify-content: center;
align-items: center;
font-family: Arial, Helvetica, sans-serif;
}
.SidebarList .row:hover {
cursor: pointer;
background-color: #E7E7E7 ;
}
.SidebarList #active {
background-color: white;
color: #FD954E
}
.SidebarList .Login {
background-color: white;
color: #FD954E;
width: 90%;
height: 5vh;
right: 1596px;
top: 958px;
border: 1px solid #FD954E;
box-sizing: border-box;
border-radius: 19.5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif
}
.SidebarList .SignUp {
width: 90%;
height: 5vh;
right: 1596px;
top: 1011px;
background: #FD954E;
border-radius: 19.5px;
border: none;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.row #icon {
flex: 30%;
display: grid;
place-items: center;
transform: scale(1.2)
}
.row #title {
flex: 70%;
}
.Logo {
padding-left: 25px;
padding-top: 25px;
padding-bottom: 30px;
width: 55%;
}
.line-break {
height: 1px;
width: 100%;
background-color: #F0F4FB;
}
ul.nav {
list-style-type: none;
padding: 20px 0 0 40px;
list-style-type: none;
}
ul.nav li {
margin-bottom: 14px;
list-style-type: none;
}
ul.nav li:last-child {
margin-bottom: 0;
list-style-type: none;
}
.footer {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px 0;
}
.footer button {
padding: 6px 0;
width: 80%;
margin-bottom: 14px;
}
.footer button:last-child {
margin-bottom: 0;
}
.header .Logo {
height: 40px;
width: 200px;
background-color: grey;
margin: 20px;
}
Without seeing the source for the entire page and not just a couple of the buttons, it is impossible to say for sure, but my guess would be the following is causing your problem:
.Sidebar {
height: 100%;
...
}
Setting height: 100% will only tell the sidebar to take up 100% of the space it needs for its content (or 100% of the height of its bounding container, depending on the display properties of both). I would recommend changing it to this to solve your problem:
.Sidebar {
min-height: 100vh;
...
}

Mobile menu effect expands to desktop view

Question:
How can I have the navigation of the mobile menu not affect desktop? When I click on the hamburger button to hide the menu and resize the browser to desktop the navigation disappears. I want to know what I did wrong to better understand my mistake.
GIF Video
Full Code Below:
class Navigation extends React.Component {
constructor(props) {
super(props);
this.state = {
show: false
}
this.toggleShow = this.toggleShow.bind(this)
this.hide = this.hide.bind(this)
}
toggleShow() {
this.setState({
show: !this.state.show
});
}
hide(e) {
if (e && e.relatedTarget) {
e.relatedTarget.click();
}
}
render() {
return (
<Router>
<div className="FlexContainer NavbarContainer">
<div className="mobilecontainer LeftNav">
<h2 className="BrandName LeftNav mobileboxmenu inline FarRight">Kommonplaces</h2>
<div
className="hamburger inlinev"
onClick={this.toggleShow}
onBlur={this.hide}>
<img alt="menubtn" src={hamburger}></img>
</div>
</div>
{
this.state.show &&
(
<ul className="NavBar">
<Dropdown/>
<li className="RightNav"><Link to="/">Host Your Space</Link></li>
<li className="RightNav"><Link to="/">About Us</Link></li>
<li className="RightNav"><Link to="/">Contact Us</Link></li>
<li className="RightNav"><Link to="/">Sign Up</Link></li>
<li className="RightNav"><Link to="/">Login</Link></li>
</ul>
)
}
</div>
</Router>
);
}
}
export default Navigation;
SCSS Code:
.NavbarContainer {
border-bottom: 1px #E7E7E7 solid;
.mobilecontainer {
margin: 2.1em;
.hamburger {
display: inline;
img {
width: 35px;
cursor: pointer;
float: right;
}
}
}
.NavBar {
// display: none;
.RightNav {
text-align: center;
padding: 27px 0;
border-right: none;
border-left: none;
border-bottom: 1px #E7E7E7 solid;
font-size: large;
a {
color: #0E0E0E;
}
}
}
.Dropdown {
float: none;
overflow: hidden;
.Dropdown-Content {
position: relative;
background-color: white;
z-index: 2;
min-width: 217px;
a {
float: none;
display: block;
text-align: center;
border: 1px #E7E7E7 solid;
padding: 15px 22px;
}
}
}
.dropdown-content {
a {
&:hover {
background-color: #ddd
}
}
}
.dropdown {
&:hover {
.dropdown-content {
display: block;
}
}
}
.LeftNav {
flex-grow: 8.2;
font-weight: bold;
font-size: large;
display: flex;
align-items: center;
}
#media (min-width: 55em) {
display: flex;
flex-direction: row;
align-items: center;
top: 0;
left: 0;
z-index: 9999;
width: 100%;
height: 60px;
background-color: #ffffff;
padding: 3em;
.LeftNav {
flex-grow: 0.2;
font-weight: bold;
font-size: large;
}
.mobilecontainer {
margin: 0;
.hamburger {
img {
display: none
}
}
}
.NavBar {
display: flex;
.RightNav {
padding-left: 15px;
padding-right: 15px;
text-align: initial;
border-bottom: none;
font-size: large;
}
.FarRight {
flex-grow: 1;
}
}
.Dropdown {
float: left;
overflow: hidden;
.Dropdown-Content {
position: absolute;
top: 96px;
background-color: white;
z-index: 2;
min-width: 217px;
a {
text-align: left;
padding: 15px 22px;
}
}
}
}
}
This is SCSS code that I'm using for this project.
this.state.show is responsible just for toggle mobile menu.when show ===false react ignore rendering navBar neither in mobile or desktop.you cant use this way for your goal.My suggestion is to consider using react-bootstrap Navbar or reactstrap Navbar
I hope this will help you.

How to change class of Nav bar when scrolling down in react?

hi this is my code and i want to change my Navigation bar color on scrolling but how can i do it?
i want to remove my class and add my new class to it,but i cant do this and i am newbie in react.
do i need life cycle hooks?
what should i do for my state?
import React from 'react';
import DrawerToggleButton from '../SideDrawer/DrawerToggleButton'
import './Toolbar.css'
class Toolbar extends React.Component {
state = {
};
scroll = () => {
if (this.scrollTop() <= 10){
(".toolbar").addClass("scroll");
}
else{
(".toolbar").removeClass("scroll")
}
};
render() {
return(
<header className="toolbar">
<nav className="toolbar__navigation">
<div className="toolbar__toggle-button">
<DrawerToggleButton click={this.props.drawerClickHandler} />
</div>
<div className="toolbar__logo"><a href={"/"}>THE LOGO</a></div>
<div className="spacer"/>
<div className="toolbar_navigation-items">
<ul>
<li><a href={"/"}>Products</a> </li>
<li><a href={"/"}>Users</a> </li>
</ul>
</div>
</nav>
</header>
);
}
}
export default Toolbar;
and this is my css code:
.toolbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: linear-gradient(
to right,
#e4faff 0%,
#e9f8ff 50%,
#f9f6ff 50%,
#e9f8ff 100%);
height: 56px;
z-index: 200;
}
.toolbar__navigation {
display: flex;
height: 100%;
align-items: center;
padding: 0 1rem;
}
.toolbar__logo {
margin-left: 1rem;
}
.toolbar__logo a {
color: #3500b6;
text-decoration: none;
font-size: 1.5rem;
}
.spacer {
flex: 1;
}
.toolbar_navigation-items ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
}
.toolbar_navigation-items li {
padding: 0 0.5rem;
}
.toolbar_navigation-items a {
color: #370ab6;
text-decoration: none;
font-size: 18px;
font-weight: bold;
}
.toolbar_navigation-items a:hover,
.toolbar_navigation-items a:active {
color: #000000;
border-bottom: 2px solid black;
}
.toolbar.scroll {
background-color: black;
color: white;
}
#media (max-width: 768px) {
.toolbar_navigation-items {
display: none;
}
}
#media (min-width: 769px) {
.toolbar__toggle-button {
display: none;
}
.toolbar__logo {
margin-left: 0;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
i used javascript as i now about this syntax but in react what should i do?

Resources