Skip to content

Commit

Permalink
fixes header interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
manishsharma004 committed Feb 13, 2024
1 parent 994d536 commit 74bc4d9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
8 changes: 4 additions & 4 deletions assets/styles/mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@
@mixin animFadeInTop($duration: 0.5s, $delay: 0s, $ease: ease-in-out) {
@keyframes fadeInTop {
0% {
opacity: 0;
transform: translateY(-20px);
height: 0%;
// transform: translateY(-20px);
}
100% {
opacity: 1;
transform: translateY(0);
height: 100%;
// transform: translateY(0);
}
}

Expand Down
18 changes: 16 additions & 2 deletions components/Header/components/HeaderMenu/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useEffect, useRef, useState } from "react";
import cx from "classnames";

import Icon from "components/Icon";
Expand Down Expand Up @@ -48,13 +48,27 @@ const MenuItem = ({ icon, title, description, href, target, comingSoon }) => {
};

const HeaderMenu = ({ children, menuItems }) => {
const containerRef = useRef();
const [isOpen, setIsOpen] = useState();

useEffect(() => {
if (isOpen) {
const handleClick = (e) => {
if (containerRef.current && !containerRef.current.contains(e.target)) {
setIsOpen(false);
}
};
document.addEventListener("click", handleClick);
return () => document.removeEventListener("click", handleClick);
}
}, [isOpen]);

return (
<>
<span
className={styles.headerLink}
className={cx(styles.headerLink, { [styles.headerLinkOpen]: isOpen })}
onClick={() => setIsOpen((isOpen) => !isOpen)}
ref={containerRef}
>
<span>
{children}{" "}
Expand Down
6 changes: 4 additions & 2 deletions components/Header/components/HeaderMenu/index.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $header-out-animation-delay: 0.3s;
// position: relative;
cursor: pointer;

&:hover {
&.headerLinkOpen {
.headerMenu {
@include header-menu-hover;

Expand Down Expand Up @@ -85,7 +85,9 @@ $header-out-animation-delay: 0.3s;
@include header-menu;

&:hover {
@include header-menu-hover;
@include min-width(lg) {
@include header-menu-hover;
}

@include mobile {
display: block;
Expand Down
14 changes: 13 additions & 1 deletion components/Header/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useEffect, useState } from "react";
import cx from "classnames";
import Image from "next/image";
import BarsOutlinedIcon from "@ant-design/icons/BarsOutlined";
Expand Down Expand Up @@ -83,6 +83,18 @@ const Header = ({ className }) => {
setIsHeaderOpen((isHeaderOpen) => !isHeaderOpen);
};

useEffect(() => {
if (isHeaderOpen) {
const handleClick = (e) => {
if (!e.target.closest(`.${styles.appHeader}`)) {
setIsHeaderOpen(false);
}
};
document.addEventListener("click", handleClick);
return () => document.removeEventListener("click", handleClick);
}
}, [isHeaderOpen]);

return (
<header className={cx(styles.appHeader, className)}>
<span className={styles.headerWrapper}>
Expand Down

0 comments on commit 74bc4d9

Please sign in to comment.