-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeader.tsx
191 lines (156 loc) · 5.43 KB
/
Header.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"use client";
import React, { useEffect, useRef } from "react";
import Image from "next/image";
const Header = () => {
let lastScrollPos =0
const navbarRef = useRef<HTMLDivElement>(null);
const headerRef = useRef<HTMLDivElement>(null);
const overlayRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const navbar = navbarRef.current;
const navTogglers =
document.querySelectorAll<HTMLElement>("[data-nav-toggler]");
const overlay = overlayRef.current;
if (!navbar || !overlay) return;
const toggleNavbar = () => {
navbar.classList.toggle("active");
overlay.classList.toggle("active");
document.body.classList.toggle("nav-active");
};
const hideHeader = () => {
const currentScrollPos = window.scrollY;
const isScrollingDown = currentScrollPos > lastScrollPos;
if (isScrollingDown || currentScrollPos < 50) {
headerRef.current?.classList.add("hide");
} else {
headerRef.current?.classList.remove("hide");
}
lastScrollPos = currentScrollPos;
};
const addEventOnElements = (
elements: NodeListOf<HTMLElement>,
eventType: string,
callback: EventListenerOrEventListenerObject
) => {
elements.forEach((element) =>
element.addEventListener(eventType, callback)
);
};
addEventOnElements(navTogglers, "click", toggleNavbar);
window.addEventListener("scroll", () => {
if (window.scrollY >= 50) {
headerRef.current?.classList.add("active");
hideHeader();
} else {
headerRef.current?.classList.remove("active");
}
});
// Cleanup function to remove event listeners on unmount
return () => {
navTogglers.forEach((toggler) =>
toggler.removeEventListener("click", toggleNavbar)
);
window.removeEventListener("scroll", hideHeader);
};
}, []);
return (
<header className="header" data-header ref={headerRef}>
<div className="container">
<a href="#" className="logo">
<Image
src="https://paradisosportsbar.com/wp-content/uploads/2023/02/cropped-paradiso-logo-150x150-1.png"
width="100"
height="50"
alt="Paradiso - Home"
/>
</a>
<nav ref={navbarRef} className="navbar" data-navbar>
<button
className="close-btn"
aria-label="close menu"
data-nav-toggler
></button>
<a href="#" className="logo">
<Image
src="https://paradisosportsbar.com/wp-content/uploads/2023/02/cropped-paradiso-logo-150x150-1.png"
width="160"
height="50"
alt="Paradiso - Home"
/>
</a>
<ul className="navbar-list">
<li className="navbar-item">
<a href="#home" className="navbar-link hover-underline active">
<div className="separator"></div>
<span className="span">Home</span>
</a>
</li>
<li className="navbar-item">
<a href="#menu" className="navbar-link hover-underline">
<div className="separator"></div>
<span className="span">Menus</span>
</a>
</li>
<li className="navbar-item">
<a href="#about" className="navbar-link hover-underline">
<div className="separator"></div>
<span className="span">About Us</span>
</a>
</li>
<li className="navbar-item">
<a href="#" className="navbar-link hover-underline">
<div className="separator"></div>
<span className="span">Our Chefs</span>
</a>
</li>
<li className="navbar-item">
<a href="#" className="navbar-link hover-underline">
<div className="separator"></div>
<span className="span">Contact</span>
</a>
</li>
</ul>
<div className="text-center">
<p className="headline-1 navbar-title">Visit Us</p>
<address className="body-4">
Lakeside Ward No.6, <br />
Baidam, Pokhara
</address>
<p className="body-4 navbar-text">Open: 9.30 am - 2.30pm</p>
<a
href="mailto:[email protected]"
className="body-4 sidebar-link"
>
</a>
<div className="separator"></div>
<p className="contact-label">Booking Request</p>
<a
href="tel:+977061-451675"
className="body-1 contact-number hover-underline"
>
+977061-451675
</a>
</div>
</nav>
<a href="#table" className="btn btn-secondary">
<span className="text text-1">Find A Table</span>
<span className="text text-2" aria-hidden="true">
Find A Table
</span>
</a>
<button
className="nav-open-btn"
aria-label="open menu"
data-nav-toggler
>
<span className="line line-1"></span>
<span className="line line-2"></span>
<span className="line line-3"></span>
</button>
<div className="overlay" data-nav-toggler ref={overlayRef}></div>
</div>
</header>
);
};
export default Header;