-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavbar.tsx
146 lines (137 loc) · 4.85 KB
/
Navbar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"use client";
import { useCallback, useEffect, useRef, useState } from "react";
import Image from "next/image";
import Link from "next/link";
import MainNav from "./MainNav";
import { SwitchLanguage } from "../components/SwitchLanguage";
import { SwitchTheme } from "./SwitchTheme";
import close from "@/public/close.svg";
import logo from "@/public/logo.svg";
import menu from "@/public/menu.svg";
import { motion } from "framer-motion";
import { usePathname } from "next/navigation";
import { useTheme } from "../utils/themeProvider";
export default function Navbar(): JSX.Element {
const { theme } = useTheme();
const pathname = usePathname();
const [open, setOpen] = useState(false);
const menuRef = useRef<HTMLDivElement>(null);
const buttonRef = useRef<HTMLDivElement>(null);
const closeMenu = useCallback(() => {
setOpen(false);
}, []);
useEffect(() => {
const handleClickOutside = (event: MouseEvent | TouchEvent) => {
if (
menuRef.current &&
!menuRef.current.contains(event.target as Node) &&
buttonRef.current &&
!buttonRef.current.contains(event.target as Node)
) {
closeMenu();
}
};
if (open) {
document.addEventListener("mousedown", handleClickOutside);
document.addEventListener("touchstart", handleClickOutside);
} else {
document.removeEventListener("mousedown", handleClickOutside);
document.removeEventListener("touchstart", handleClickOutside);
}
return () => {
document.removeEventListener("mousedown", handleClickOutside);
document.removeEventListener("touchstart", handleClickOutside);
};
}, [closeMenu, open]);
useEffect(() => {
closeMenu();
}, [closeMenu, pathname]);
return (
<div className="max-w-[1440px]">
<div className="max-w-[1440px] hidden fixed top-0 left-0 right-0 mx-auto md:block z-50 bg-[#f2efea] dark:bg-[#222927]">
<div className="px-4 sm:px-9 lg:px-16 flex py-3 justify-between items-center border-b border-[#222927] dark:border-white">
<div className="w-12 h-12 flex justify-center items-center">
<SwitchTheme />
</div>
<MainNav>
<Link href="/" className="ml-4 flex lg:ml-0 lg:mr-6 px-5">
<Image
src={logo}
alt="Logo"
priority
className="w-10 h-auto"
style={{
filter:
theme === "dark" ? "invert(1) brightness(2)" : undefined,
}}
/>
</Link>
</MainNav>
<div className="w-12 h-12 flex justify-center items-center">
<SwitchLanguage />
</div>
</div>
</div>
<div
className="
max-w-[1440px] block fixed top-0 left-0 right-0 mx-auto md:hidden z-50 bg-[#f2efea] dark:bg-[#222927]"
>
<div className="w-full flex h-16 justify-between items-end">
<div className="w-full border-b border-[#222927] dark:border-white mr-4">
<div
ref={buttonRef}
className="w-12 h-12 pl-4 flex justify-center items-center"
>
<button onClick={() => setOpen(!open)}>
<Image
src={open ? close : menu}
alt="Menu image"
style={{
width: "32px",
height: "auto",
filter:
theme === "dark" ? "invert(1) brightness(2)" : undefined,
}}
/>
</button>
</div>
</div>
<div className="w-20 mt-4 flex justify-center items-center">
<Link href="/">
<Image
src={logo}
alt="Logo"
priority
className="w-full h-auto"
style={{
filter:
theme === "dark" ? "invert(1) brightness(2)" : undefined,
}}
/>
</Link>
</div>
<div className="w-full border-b border-[#222927] dark:border-white ml-4 flex justify-end item">
<div className="w-12 h-12 pr-4 flex justify-center items-center">
<SwitchTheme />
<SwitchLanguage />
</div>
</div>
</div>
</div>
{open && (
<motion.div
ref={menuRef}
className="fixed top-16 left-0 z-50 w-[41.5%] rounded-br-[3rem] flex flex-col justify-center bg-[#f2efea] dark:bg-[#222927] border-b border-r border-[#222927] dark:border-white py-8 pl-2"
initial={{ x: "-100%" }}
animate={{ x: 0 }}
exit={{ x: "-100%" }}
transition={{ duration: 0.5, ease: "easeOut" }}
>
<div>
<MainNav closeMenu={closeMenu} />
</div>
</motion.div>
)}
</div>
);
}