Skip to content

Commit b60adf0

Browse files
committed
[refactor] 닫힐때 애니메이션 나오게 수정
1 parent 292f19f commit b60adf0

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

src/shared/components/header/DropdownMenu.tsx

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,54 @@ import Close from '@/shared/assets/icons/close_32.svg';
44
import User from '@/shared/assets/icons/user_24.svg';
55
import Link from 'next/link';
66
import { usePathname } from 'next/navigation';
7-
import { useEffect, useRef } from 'react';
7+
import { useEffect, useRef, useState } from 'react';
88
import gsap from 'gsap';
99
import { useAuthStore } from '@/shared/@store/auth';
1010

1111
interface Props {
1212
isClicked: boolean;
1313
setIsClicked: (state: boolean) => void;
14+
visible: boolean;
15+
setVisible: (state: boolean) => void;
1416
}
1517

16-
function DropdownMenu({ isClicked, setIsClicked }: Props) {
18+
function DropdownMenu({ isClicked, setIsClicked, visible, setVisible }: Props) {
1719
const pathname = usePathname();
1820
const menuRef = useRef<HTMLDivElement>(null);
1921
const textRef = useRef<(HTMLSpanElement | null)[]>([]);
22+
const tlRef = useRef<GSAPTimeline | null>(null);
2023

2124
const { isLoggedIn, logout } = useAuthStore();
2225

2326
useEffect(() => {
2427
if (!menuRef.current) return;
28+
if (!tlRef.current) {
29+
const tl = gsap.timeline({
30+
paused: true,
31+
onReverseComplete: () => {
32+
setVisible(false);
33+
},
34+
});
2535

26-
if (isClicked) {
27-
gsap.fromTo(
36+
tl.fromTo(
2837
menuRef.current,
29-
{
30-
x: -200,
31-
opacity: 0,
32-
},
33-
{
34-
x: 0,
35-
opacity: 1,
36-
duration: 0.8,
37-
ease: 'expo.inOut',
38-
}
38+
{ x: -200, opacity: 0 },
39+
{ x: 0, opacity: 1, duration: 0.5, ease: 'expo.inOut' }
3940
);
41+
42+
tlRef.current = tl;
4043
}
44+
45+
if (isClicked) {
46+
setVisible(true);
47+
tlRef.current.play();
48+
} else {
49+
tlRef.current.reverse();
50+
}
51+
52+
return () => {
53+
// tl.kill();
54+
};
4155
}, [isClicked]);
4256

4357
const handleMouseEnter = (index: number) => {
@@ -62,7 +76,7 @@ function DropdownMenu({ isClicked, setIsClicked }: Props) {
6276

6377
return (
6478
<nav
65-
className="w-full h-screen bg-secondary absolute top-0 left-0 px-[12px] font-serif block sm:hidden"
79+
className={`w-full h-screen bg-secondary absolute top-0 left-0 px-[12px] font-serif block sm:hidden ${visible ? 'block' : 'hidden'} `}
6680
role="navigation"
6781
aria-label="메인 네비게이션 메뉴"
6882
tabIndex={-1}

src/shared/components/header/HamburgerMenu.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import DropdownMenu from './DropdownMenu';
44

55
function HamburgerMenu() {
66
const [isClicked, setIsClicked] = useState(false);
7+
const [visible, setVisible] = useState(false);
78

89
const handleClick = (e: React.MouseEvent) => {
910
e.stopPropagation();
11+
if (!visible) {
12+
setVisible(true);
13+
}
1014
setIsClicked(true);
1115
};
1216

@@ -22,7 +26,12 @@ function HamburgerMenu() {
2226
>
2327
<Menu />
2428
</button>
25-
{isClicked && <DropdownMenu isClicked={isClicked} setIsClicked={setIsClicked} />}
29+
<DropdownMenu
30+
isClicked={isClicked}
31+
setIsClicked={setIsClicked}
32+
visible={visible}
33+
setVisible={setVisible}
34+
/>
2635
</>
2736
);
2837
}

0 commit comments

Comments
 (0)