React adding transform and transition causes hamburger menu to appear and disappear immediately after

35 Views Asked by At

I have a header where the components will change into a hamburger menu when it is responsive. Everything is working fine, the buttons are working fine. However when I added a transform and transition, whenever I clicked on the hamburger menu, it appears and disappears completely, immediately after.

My jsx file

import { useState } from "react";
import { Link } from "react-scroll";
import { GiHamburgerMenu } from "react-icons/gi";
import { IoCloseCircleOutline } from "react-icons/io5";

export default function HeaderRoute() {

    const [open, setOpen] = useState(false);

    function handleMenu() {
        setOpen(!open);
    }

    return (
        <>
            <div className="nav-link">
                <Link
                    to="about-me"
                    spy={true}
                    smooth={true}
                    offset={-150}
                    duration={500}
                    className="link-about-me"
                >
                        About Me
                </Link>
                <Link
                    to="projects"
                    spy={true}
                    smooth={true}
                    offset={-150}
                    duration={500}
                    className="link-projects"
                >
                        Projects
                </Link>
                <Link
                    to="contact"
                    spy={true}
                    smooth={true}
                    offset={-150}
                    duration={500}
                    className="link-contact"
                >
                        Contact
                </Link>
            </div>
            { open ? 
                <div className="nav-menu">
                    <div className="nav-menu-close"><button className="close-menu-button" onClick={handleMenu}><IoCloseCircleOutline /></button></div>
                    <div className="nav-menu-link">
                        <Link
                            to="about-me"
                            spy={true}
                            smooth={true}
                            offset={-150}
                            duration={500}
                            className="nav-menu-link-about-me"
                            onClick={handleMenu}
                        >
                                About Me
                        </Link>
                        <Link
                            to="projects"
                            spy={true}
                            smooth={true}
                            offset={-150}
                            duration={500}
                            className="nav-menu-link-projects"
                            onClick={handleMenu}
                        >
                                Projects
                        </Link>
                        <Link
                            to="contact"
                            spy={true}
                            smooth={true}
                            offset={-150}
                            duration={500}
                            className="nav-menu-link-contact"
                            onClick={handleMenu}
                        >
                                Contact
                        </Link>
                    </div>
                </div>
                :
                <div className="hamburger-menu">
                    <div className="hamburger-icon"><button className="hamburger-menu-button" onClick={handleMenu}><GiHamburgerMenu /></button></div>
                </div>
            }
        </>
    )
}

My css file:

.link-about-me, .link-projects, .link-contact {
    text-decoration: none;
    color: black;
    cursor: pointer;
    margin-right: 30px;

    &:hover {
        border-bottom: 1px solid #333;
    }
}

.hamburger-menu {
    display: none;
    cursor: pointer;
}

.hamburger-menu-button {
    all: revert;
    padding: 13px;
    font-size: 20px;
    border: none;
    background-color: white;
    cursor: pointer;
}

.close-menu-button {
    all: revert;
    font-size: 30px;
    border: none;
    background-color: white;
    cursor: pointer;
}

.nav-menu {
    cursor: pointer;
    background: #fff;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    min-height: 40rem;
    /* transform isn't working properly */
    transform: translateX(-100%);
    transition: transform 0.5s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.nav-menu-close {
    position: absolute;
    font-size: 30px;
    top: 3.3rem;
    right: 2.5rem;
    width: 3rem;
    height: 3rem;
}

.nav-menu-link {
    font-size: 25px;
    display: flex;
    flex-direction: column;
    row-gap: 50px;
    text-align: center;
}

.nav-menu-link-about-me:hover, .nav-menu-link-projects:hover, .nav-menu-link-contact:hover {
    border-bottom: 1px solid #333;

}

@media (max-width: 600px) {
    .nav-link {
        display: none;
    }

    .hamburger-menu {
        display: block;
    }
}

Anyone knows what's wrong with my code? thank you!

0

There are 0 best solutions below