-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeaderInteractive.tsx
More file actions
171 lines (159 loc) · 5.83 KB
/
Copy pathHeaderInteractive.tsx
File metadata and controls
171 lines (159 loc) · 5.83 KB
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
// src/components/HeaderInteractive.tsx
// React Island — handles mobile menu, scroll detection, and language switching
import { useState, useEffect } from 'react';
import { Menu, X } from 'lucide-react';
interface NavItem {
key: string;
label: string;
href: string;
isSection: boolean;
}
interface Props {
navItems: NavItem[];
logoSrc: string;
lang: string;
altLangHref: string;
}
const HeaderInteractive = ({ navItems, logoSrc, lang, altLangHref }: Props) => {
const [isMenuOpen, setIsMenuOpen] = useState(false);
const [isScrolled, setIsScrolled] = useState(false);
useEffect(() => {
const handleScroll = () => {
setIsScrolled(window.scrollY > 50);
};
window.addEventListener('scroll', handleScroll);
handleScroll();
return () => window.removeEventListener('scroll', handleScroll);
}, []);
const handleNavClick = (item: NavItem) => {
if (item.isSection) {
const element = document.getElementById(item.key);
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
setIsMenuOpen(false);
} else {
window.location.href = item.href;
}
} else {
window.location.href = item.href;
}
};
const altLang = lang === 'es' ? 'EN' : 'ES';
const handleLangSwitch = () => {
const targetLang = lang === 'es' ? 'en' : 'es';
localStorage.setItem('preferred_lang', targetLang);
sessionStorage.removeItem('lang_redirected');
};
return (
<header
className={`fixed top-0 left-0 right-0 z-50 transition-all duration-300 ${
isMenuOpen
? 'bg-background shadow-lg'
: isScrolled
? 'bg-background/80 backdrop-blur-md shadow-elegant py-1'
: 'bg-transparent py-2'
}`}
>
<div className="container mx-auto px-4">
<div className="flex items-center justify-between h-12 lg:h-14">
{/* Logo */}
<a href={lang === 'es' ? '/es/' : '/'} className="flex items-center space-x-3 transition-transform hover:scale-105">
<img
src={logoSrc}
alt="Jesus Ordosgoitty"
className={`h-8 lg:h-9 w-auto transition-all duration-300 ${isScrolled || isMenuOpen ? 'brightness-0' : ''}`}
/>
</a>
{/* Desktop Navigation */}
<nav className="hidden md:flex items-center space-x-8">
{navItems.map((item) => (
item.isSection ? (
<button
key={item.key}
onClick={() => handleNavClick(item)}
className={`text-base lg:text-lg transition-colors font-semibold ${
isScrolled || isMenuOpen ? 'text-foreground hover:text-primary' : 'text-blue-50 hover:text-primary'
}`}
>
{item.label}
</button>
) : (
<a
key={item.key}
href={item.href}
className={`text-base lg:text-lg transition-colors font-semibold ${
isScrolled || isMenuOpen ? 'text-foreground hover:text-primary' : 'text-blue-50 hover:text-primary'
}`}
>
{item.label}
</a>
)
))}
{/* Language switcher */}
<a
href={altLangHref}
onClick={handleLangSwitch}
className={`text-sm px-3 py-1 rounded-full border transition-all duration-300 font-bold ${
isScrolled || isMenuOpen
? 'text-foreground border-foreground/20 hover:border-primary hover:text-primary'
: 'text-blue-50 border-blue-50/30 hover:border-white hover:text-white'
}`}
aria-label={`Switch to ${altLang}`}
>
{altLang}
</a>
</nav>
{/* Mobile Menu Button */}
<button
className={`md:hidden p-2 transition-colors ${isScrolled || isMenuOpen ? 'text-foreground' : 'text-blue-50'}`}
onClick={() => setIsMenuOpen(!isMenuOpen)}
aria-label="Toggle menu"
>
{isMenuOpen ? <X size={28} /> : <Menu size={28} />}
</button>
</div>
{/* Mobile Navigation */}
<div
className={`md:hidden overflow-hidden transition-all duration-500 ease-in-out ${
isMenuOpen ? 'max-h-[80vh] opacity-100 pb-8' : 'max-h-0 opacity-0'
}`}
>
<nav className="flex flex-col space-y-6 pt-6">
{navItems.map((item) => (
item.isSection ? (
<button
key={item.key}
onClick={() => handleNavClick(item)}
className="text-2xl text-foreground hover:text-primary transition-colors font-bold text-left"
>
{item.label}
</button>
) : (
<a
key={item.key}
href={item.href}
className="text-2xl text-foreground hover:text-primary transition-colors font-bold"
onClick={() => setIsMenuOpen(false)}
>
{item.label}
</a>
)
))}
<div className="pt-6 border-t border-foreground/10">
<a
href={altLangHref}
onClick={handleLangSwitch}
className="inline-flex items-center text-lg font-bold text-foreground hover:text-primary transition-colors"
aria-label={`Switch to ${altLang}`}
>
<span className="mr-2">Language:</span>
<span className="px-3 py-1 bg-muted rounded-full text-primary">{altLang}</span>
</a>
</div>
</nav>
</div>
</div>
</header>
);
};
export default HeaderInteractive;