ItServices
ItServices

Reputation: 63

How to close the floating menu if you click outside it with React

I created a Header component containing the site navbar which through useState and aria-attribute in the css shows or hides the menu in the mobile version of the site. I used onFocus to manage the automatic closing of the side menu but it doesn't work if you click outside the menu ... it only works if you click on a menu item. I also tried with onBlur but still it doesn't work.

How do I make the menu close if I click outside its panel?

Header component:

import { useRef, useState } from 'react';
import { Link } from 'react-router-dom';
import logo from '../imgs/logo.webp';

function Header() {
    const refNavBar = useRef(),
            [isOpen, setIsOpen] = useState(false),
            handleToggle = () => setIsOpen(!isOpen);

    return (
        <header>
            <div className="container g-2 pbk-1">
                <Link to="/">
                    <img src={logo} alt="" />
                </Link>

                <button
                    className="nav-toggle"
                    aria-controls={refNavBar}
                    aria-expanded={isOpen}
                    onClick={handleToggle}
                >
                    <div className="bar1" />
                    <div className="bar2" />
                    <div className="bar3" />
                </button>

                <nav
                    id="navbar"
                    ref={refNavBar}
                    data-visible={isOpen}
                    onFocus={handleToggle}
                >
                    <ul className="flex g-2">
                        <li><Link to="/">Home</Link></li>
                        <li><Link to="/about">About</Link></li>
                        <li><Link to="/gallery">Gallery</Link></li>
                        <li><Link to="/contact">Contact</Link></li>
                    </ul>
                </nav>
            </div>
        </header>
    );
}

export default Header;

style:

.flex {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
}
.g-2 { gap: 2rem; }
.pbk-1 { padding-block: 1rem; }

header {
    position: fixed;
    width: 100%;
    background-color: var(--bg);
}

header .container {
    display: grid;
    justify-items: end;
    align-items: center;
    grid-template-columns: 10rem 1fr;
}
button.nav-toggle {
    display: none;
}
header nav ul li > a {
    color: var(--white);
    text-decoration: none;
}
header nav ul li > a:hover {
    opacity: .8;
}

@media (max-width: 44em) {
    .flex {
        flex-direction: column;
    }

    button.nav-toggle {
        display: block;
        position: absolute;
        right: 1rem;
        width: 2.5rem;
        border-radius: 0;
        padding: 0;
        background: transparent;
        z-index: 9999;
    }
    button.nav-toggle .bar1,
    button.nav-toggle .bar2,
    button.nav-toggle .bar3 {
        width: 100%;
        height: .12rem;
        margin-block: .65rem;
        background-color: var(--white);
        transition: var(--ease-in-out);
    }
    button.nav-toggle[aria-expanded="true"] .bar1 {
        transform: rotate(-45deg) translate(-50%, -50%);
    }
    button.nav-toggle[aria-expanded="true"] .bar2 {
        opacity: 0;
    }
    button.nav-toggle[aria-expanded="true"] .bar3 {
        transform: rotate(45deg) translate(-50%, -50%);
    }

    nav#navbar {
        position: fixed;
        inset: 0 0 0 28%;
        padding: min(20rem, 15vh) 2rem;
        background: var(--bg);
        z-index: 9998;
        transform: translateX(100%);
        transition: transform var(--ease-in-out);
    }
    nav#navbar[data-visible="true"] {
        transform: translateX(0);
    }

What am I doing wrong? A thousand thanks

Upvotes: 1

Views: 2505

Answers (1)

Alija Fajic
Alija Fajic

Reputation: 598

Detect click outside component example, can this work at your example?

    import React, { useRef, useEffect } from "react";

/**
 * Hook that alerts clicks outside of the passed ref
 */
function useOutsideAlerter(ref) {
  useEffect(() => {
    /**
     * Alert if clicked on outside of element
     */
    function handleClickOutside(event) {
      if (ref.current && !ref.current.contains(event.target)) {
        alert("You clicked outside of me!");
      }
    }
    // Bind the event listener
    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      // Unbind the event listener on clean up
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [ref]);
}

/**
 * Component that alerts if you click outside of it
 */
export default function OutsideAlerter(props) {
  const wrapperRef = useRef(null);
  useOutsideAlerter(wrapperRef);

  return <div ref={wrapperRef}>{props.children}</div>;
}

Upvotes: 0

Related Questions