Skip to content

Commit 5133f7a

Browse files
committed
fix(header): fix bug preventing header menu from closing
1 parent ffd017d commit 5133f7a

6 files changed

Lines changed: 24 additions & 22 deletions

File tree

app/src/hooks/use-mobile.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useEffect, useState } from "react"
22

3-
const defaultMediaQuery = "(min-width: 900px)"
3+
const defaultMediaQuery = "(max-width: 900px)"
44

5-
export const useMobile = (mediaQuery: string = defaultMediaQuery) => {
6-
const [isMobile, setIsMobile] = useState(() => window.matchMedia(mediaQuery).matches)
5+
export const useIsMobile = (mediaQuery: string = defaultMediaQuery) => {
6+
const [isMobile, setIsMobile] = useState(() => false)
77

88
useEffect(() => {
99
const matchMedia = window.matchMedia(mediaQuery)

app/src/lib/@types/props.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { StaticImageData } from "next/image"
2-
import type { ButtonHTMLAttributes, PropsWithChildren, JSX, CSSProperties } from "react"
2+
import type { ButtonHTMLAttributes, PropsWithChildren, JSX, CSSProperties, MouseEventHandler } from "react"
33
import type { VariantProps } from "class-variance-authority"
44
import type { ArgsFunction, PerType, PresetType } from "./types"
55
import type { Transition, Variants } from "motion"
@@ -92,3 +92,7 @@ export interface CallToActionProps {
9292
export interface SectionLayoutProps extends PropsWithChildren {
9393
className?: string
9494
}
95+
96+
export interface HeaderMenuProps {
97+
onLinkClick: MouseEventHandler
98+
}

app/src/ui/community/hero.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const HeroSection = () => {
1313
Join Our Community
1414
</TextEffect>
1515
<TextEffect
16-
className="px-4 text-muted text-lg sm:px-6 md:px-8 base:px-10"
16+
className="px-4 text-muted text-lg sm:px-6 md:px-8 base:px-10 span:text-ellipsis span:[text-wrap:auto]"
1717
preset="fade-in-blur"
1818
speedReveal={1.1}
1919
speedSegment={0.5}

app/src/ui/globals.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ It is generating errors to load animations styles
6969

7070
body:has(> header.open) {
7171
overflow-y: hidden;
72+
padding-right: 0.2rem;
7273
}
7374

7475
main {

app/src/ui/header/header-menu.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import Link from "next/link"
22
import { motion } from "motion/react"
33
import { itemVariants, menuVariants } from "@/ui/motion-config/menu.motion"
44
import { TextScramble } from "@/ui/motion/text-scramble"
5+
import { HeaderMenuProps } from "@/lib/@types/props"
56

6-
export const HeaderMenu = ({ onLinkClick }: { onLinkClick?: () => void } = {}) => {
7+
export const HeaderMenu = ({ onLinkClick }: HeaderMenuProps) => {
78
return (
89
<motion.div
910
className="w-10/12 max-w-md absolute inset-y-0 right-0 z-20 border border-solid border-border bg-black [--nav-menu:100%] base:w-auto base:max-w-none base:inset-0 base:relative base:bg-transparent base:border-0 base:[--nav-menu:0%]"

app/src/ui/header/header.tsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import Link from "next/link"
33
import Image from "next/image"
44
import { useCallback, useEffect, useRef, useState } from "react"
5+
import { useIsMobile } from "@/hooks/use-mobile"
56
import { HeaderMenu } from "./header-menu"
67
import { AnimatePresence } from "motion/react"
78
import { merge } from "@halvaradop/ui-core"
@@ -10,36 +11,31 @@ import menu from "@/assets/menu.svg"
1011
import arrow from "@/assets/arrow.svg"
1112

1213
export const Header = () => {
14+
const isMobile = useIsMobile()
1315
const [isOpen, setIsOpen] = useState(false)
1416
const menuRef = useRef<HTMLDivElement>(null)
1517

1618
const handleToggleMenu = () => {
17-
setIsOpen(!isOpen)
19+
setIsOpen(previous => !previous)
1820
}
1921

2022
const handleMenuLinkClick = useCallback(() => {
21-
if (window.innerWidth < 900) {
23+
if (!isMobile) {
2224
setIsOpen(false)
2325
}
2426
}, [])
2527

2628
useEffect(() => {
27-
const matchScreen = matchMedia("(min-width: 900px)")
28-
29-
const handleSetMatchMedia = () => {
30-
setIsOpen(matchScreen.matches)
31-
}
32-
33-
setIsOpen(matchScreen.matches)
34-
matchScreen.addEventListener("change", handleSetMatchMedia)
35-
return () => matchScreen.removeEventListener("change", handleSetMatchMedia)
36-
}, [])
29+
setIsOpen(!isMobile)
30+
}, [isMobile])
3731

3832
useEffect(() => {
3933
if (!isOpen) return
4034
const handleClickOutside = (event: MouseEvent) => {
41-
if (window.innerWidth < 900 && menuRef.current && !menuRef.current.contains(event.target as Node)) {
42-
setIsOpen(false)
35+
const node = event.target as HTMLElement
36+
if (node.hasAttribute("aria-description") && node.getAttribute("aria-description") === "menu icon") return
37+
if (isMobile && menuRef.current && !menuRef.current.contains(event.target as Node)) {
38+
setIsOpen(() => false)
4339
}
4440
}
4541
document.addEventListener("mousedown", handleClickOutside)
@@ -57,7 +53,7 @@ export const Header = () => {
5753
<Link href="/">@halvaradop/tailwindcss</Link>
5854
</h2>
5955
</div>
60-
<div ref={menuRef} style={{ display: "contents" }}>
56+
<div ref={menuRef}>
6157
<AnimatePresence mode="wait">{isOpen && <HeaderMenu onLinkClick={handleMenuLinkClick} />}</AnimatePresence>
6258
</div>
6359
</nav>
@@ -66,7 +62,7 @@ export const Header = () => {
6662
variant="ghost"
6763
onClick={handleToggleMenu}
6864
>
69-
<Image className="hover:cursor-pointer" src={menu} alt="menu icon" />
65+
<Image className="hover:cursor-pointer" src={menu} alt="menu icon" aria-description="menu icon" />
7066
</Button>
7167
<figure className="group w-10 h-16 hidden img:transition-transform img:ease-linear img:duration-300 hover:cursor-pointer base:w-16 base:flex base:items-center base:justify-center base:relative base:overflow-hidden">
7268
<Image

0 commit comments

Comments
 (0)