2026-10-17 10:27:36 +07:00
|
|
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
2026-10-06 23:42:49 +07:00
|
|
|
import { Link, useLocation } from 'react-router'
|
2026-10-17 10:27:36 +07:00
|
|
|
import { AnimatePresence, motion } from 'motion/react'
|
2026-10-06 11:42:11 +07:00
|
|
|
import { useTheme } from '@/hooks/useTheme'
|
2026-10-14 11:15:48 +07:00
|
|
|
import { useLanguage } from '@/hooks/useLanguage'
|
2026-10-22 07:29:06 +07:00
|
|
|
import { useI18n } from '@/i18n/provider'
|
2026-10-17 10:27:36 +07:00
|
|
|
import { navPaths } from '@/data/navigation'
|
2026-10-06 23:42:49 +07:00
|
|
|
import { RollingTextLink } from '@/components/ui/RollingTextLink'
|
2026-10-17 00:32:49 +07:00
|
|
|
import { ThemePaletteMenu } from '@/components/layout/ThemePaletteMenu'
|
2026-10-17 23:59:58 +07:00
|
|
|
import { LanguageIcon } from '@/components/ui/LanguageIcon'
|
2026-10-20 19:56:08 +07:00
|
|
|
import { MoonIcon, SunIcon } from '@/components/ui/icons'
|
2026-10-17 10:27:36 +07:00
|
|
|
import { MobileMenu } from '@/components/layout/MobileMenu'
|
2026-10-20 00:33:28 +07:00
|
|
|
import { ResumeNavButton } from '@/components/layout/ResumeNavButton'
|
2026-10-17 10:27:36 +07:00
|
|
|
|
2026-10-06 11:42:11 +07:00
|
|
|
export function Navigation() {
|
|
|
|
|
const { cycleTheme, resolvedTheme } = useTheme()
|
2026-10-14 11:15:48 +07:00
|
|
|
const { lang, toggleLang } = useLanguage()
|
2026-10-22 07:29:06 +07:00
|
|
|
const { ui: s } = useI18n()
|
2026-10-17 23:59:58 +07:00
|
|
|
const targetLang = lang === 'en' ? 'vi' : 'en'
|
2026-10-22 07:29:06 +07:00
|
|
|
const targetLabel = targetLang === 'en' ? s.nav.langEn : s.nav.langVi
|
2026-10-06 11:42:11 +07:00
|
|
|
const [mobileOpen, setMobileOpen] = useState(false)
|
|
|
|
|
const location = useLocation()
|
2026-10-17 10:27:36 +07:00
|
|
|
const triggerRef = useRef<HTMLButtonElement>(null)
|
|
|
|
|
|
|
|
|
|
const closeMenu = useCallback(() => {
|
|
|
|
|
setMobileOpen(false)
|
|
|
|
|
// The hamburger is display:none above 1024px — focusing it there would drop focus to <body>.
|
|
|
|
|
if (triggerRef.current?.offsetParent) triggerRef.current.focus()
|
|
|
|
|
}, [])
|
2026-10-06 11:42:11 +07:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-10-17 10:27:36 +07:00
|
|
|
if (!mobileOpen) return
|
|
|
|
|
document.body.style.overflow = 'hidden'
|
|
|
|
|
const covered = [
|
|
|
|
|
document.getElementById('main-content'),
|
|
|
|
|
document.querySelector('.footer'),
|
|
|
|
|
document.querySelector('.back-to-top'),
|
|
|
|
|
]
|
|
|
|
|
covered.forEach((el) => el?.setAttribute('inert', ''))
|
|
|
|
|
const handleKey = (event: KeyboardEvent) => {
|
|
|
|
|
if (event.key === 'Escape') closeMenu()
|
2026-10-06 11:42:11 +07:00
|
|
|
}
|
2026-10-17 10:27:36 +07:00
|
|
|
document.addEventListener('keydown', handleKey)
|
2026-10-06 11:42:11 +07:00
|
|
|
return () => {
|
|
|
|
|
document.body.style.overflow = ''
|
2026-10-17 10:27:36 +07:00
|
|
|
covered.forEach((el) => el?.removeAttribute('inert'))
|
|
|
|
|
document.removeEventListener('keydown', handleKey)
|
2026-10-06 11:42:11 +07:00
|
|
|
}
|
2026-10-17 10:27:36 +07:00
|
|
|
}, [mobileOpen, closeMenu])
|
2026-10-06 11:42:11 +07:00
|
|
|
|
2026-10-17 10:27:36 +07:00
|
|
|
// The sheet only exists at <=1024px; crossing upward must close it so aria-expanded stays honest.
|
2026-10-06 11:42:11 +07:00
|
|
|
useEffect(() => {
|
2026-10-17 10:27:36 +07:00
|
|
|
const mobile = window.matchMedia('(max-width: 1024px)')
|
|
|
|
|
const handleChange = (event: MediaQueryListEvent) => {
|
|
|
|
|
if (!event.matches) setMobileOpen(false)
|
|
|
|
|
}
|
|
|
|
|
mobile.addEventListener('change', handleChange)
|
|
|
|
|
return () => mobile.removeEventListener('change', handleChange)
|
|
|
|
|
}, [])
|
2026-10-06 11:42:11 +07:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2026-10-22 07:29:06 +07:00
|
|
|
<nav className="nav" role="navigation" aria-label={s.nav.mainNav}>
|
2026-10-06 11:42:11 +07:00
|
|
|
<div className="nav-container">
|
2026-10-17 22:33:42 +07:00
|
|
|
<div className="nav-brand">
|
|
|
|
|
{/* Trigger first, brand next: the hamburger is the sheet's own control and sits at
|
|
|
|
|
the bar's left edge, where a left-handed thumb reaches it. The three appearance
|
|
|
|
|
controls stay in .nav-actions at the right end. */}
|
|
|
|
|
<button
|
|
|
|
|
ref={triggerRef}
|
|
|
|
|
className="mobile-menu-btn"
|
|
|
|
|
onClick={() => setMobileOpen((prev) => !prev)}
|
2026-10-22 07:29:06 +07:00
|
|
|
aria-label={mobileOpen ? s.nav.closeMenu : s.nav.openMenu}
|
2026-10-17 22:33:42 +07:00
|
|
|
aria-expanded={mobileOpen}
|
|
|
|
|
aria-controls="mobile-menu"
|
|
|
|
|
>
|
|
|
|
|
<AnimatePresence mode="wait" initial={false}>
|
|
|
|
|
{mobileOpen ? (
|
|
|
|
|
<motion.svg
|
|
|
|
|
key="close"
|
|
|
|
|
width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"
|
|
|
|
|
initial={{ opacity: 0, rotate: -90 }}
|
|
|
|
|
animate={{ opacity: 1, rotate: 0 }}
|
|
|
|
|
exit={{ opacity: 0, rotate: 90 }}
|
|
|
|
|
transition={{ duration: 0.18 }}
|
|
|
|
|
>
|
|
|
|
|
<path d="M18 6L6 18M6 6l12 12" />
|
|
|
|
|
</motion.svg>
|
|
|
|
|
) : (
|
|
|
|
|
<motion.svg
|
|
|
|
|
key="menu"
|
|
|
|
|
width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"
|
|
|
|
|
initial={{ opacity: 0, rotate: 90 }}
|
|
|
|
|
animate={{ opacity: 1, rotate: 0 }}
|
|
|
|
|
exit={{ opacity: 0, rotate: -90 }}
|
|
|
|
|
transition={{ duration: 0.18 }}
|
|
|
|
|
>
|
|
|
|
|
<path d="M3 12h18M3 6h18M3 18h18" />
|
|
|
|
|
</motion.svg>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
|
|
|
|
</button>
|
|
|
|
|
{/* Brand is a home icon: the same 24px stroke language as the hamburger, and the same
|
|
|
|
|
<Link to="/"> target the {CV} wordmark always had. The accessible name is the
|
|
|
|
|
translated Home label; the icon itself is decorative. The current-page marker stays
|
|
|
|
|
on the nav link and the sheet's Home row. */}
|
2026-10-22 07:29:06 +07:00
|
|
|
<Link to="/" className="nav-logo" aria-label={s.nav.home}>
|
2026-10-17 22:33:42 +07:00
|
|
|
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
|
|
|
|
|
<path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" />
|
|
|
|
|
<polyline points="9 22 9 12 15 12 15 22" />
|
|
|
|
|
</svg>
|
|
|
|
|
</Link>
|
2026-10-20 00:33:28 +07:00
|
|
|
{/* Resume lives next to home: it is a site-wide action, not an appearance control, so
|
|
|
|
|
it belongs in the brand group and stays put when the sheet takes over the bar. */}
|
|
|
|
|
<ResumeNavButton />
|
2026-10-17 22:33:42 +07:00
|
|
|
</div>
|
2026-10-06 11:42:11 +07:00
|
|
|
|
|
|
|
|
<ul className="nav-menu">
|
2026-10-14 11:15:48 +07:00
|
|
|
{navPaths.map((path) => {
|
2026-10-22 07:29:06 +07:00
|
|
|
const navKey = path === '/' ? 'home' : path.replace('/', '') as keyof typeof s.nav
|
|
|
|
|
const label = s.nav[navKey]
|
2026-10-14 11:15:48 +07:00
|
|
|
return (
|
|
|
|
|
<li key={path}>
|
|
|
|
|
<RollingTextLink
|
|
|
|
|
to={path}
|
2026-10-17 10:27:36 +07:00
|
|
|
ariaCurrent={path === location.pathname ? 'page' : undefined}
|
2026-10-14 11:15:48 +07:00
|
|
|
className={path === location.pathname ? 'nav-link active' : 'nav-link'}
|
|
|
|
|
>
|
|
|
|
|
{label}
|
|
|
|
|
</RollingTextLink>
|
|
|
|
|
</li>
|
|
|
|
|
)
|
|
|
|
|
})}
|
2026-10-06 11:42:11 +07:00
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
<div className="nav-actions">
|
2026-10-17 23:59:58 +07:00
|
|
|
{/* The control names the language it switches TO — the same contract the old VI/EN
|
|
|
|
|
glyph had, now drawn: globe for English, red flag for Tiếng Việt. The icon is
|
|
|
|
|
decorative; the accessible name is the translated action, the target is the title. */}
|
2026-10-17 14:51:02 +07:00
|
|
|
<button
|
|
|
|
|
className="nav-control lang-toggle"
|
|
|
|
|
onClick={toggleLang}
|
2026-10-22 07:29:06 +07:00
|
|
|
aria-label={s.nav.langSwitch}
|
|
|
|
|
title={`${s.nav.switchTo} ${targetLabel}`}
|
2026-10-17 14:51:02 +07:00
|
|
|
>
|
2026-10-17 23:59:58 +07:00
|
|
|
<LanguageIcon lang={targetLang} />
|
2026-10-17 14:51:02 +07:00
|
|
|
</button>
|
|
|
|
|
|
2026-10-06 11:42:11 +07:00
|
|
|
<button
|
2026-10-17 10:27:36 +07:00
|
|
|
className="nav-control theme-toggle"
|
2026-10-06 11:42:11 +07:00
|
|
|
onClick={cycleTheme}
|
2026-10-22 07:29:06 +07:00
|
|
|
aria-label={`${s.nav.themeSwitch} (${resolvedTheme === 'dark' ? s.nav.themeDark : s.nav.themeLight})`}
|
|
|
|
|
title={`${s.nav.themeMode}: ${resolvedTheme === 'dark' ? s.nav.themeDark : s.nav.themeLight}`}
|
2026-10-06 11:42:11 +07:00
|
|
|
>
|
2026-10-20 19:56:08 +07:00
|
|
|
{resolvedTheme === 'dark' ? <SunIcon /> : <MoonIcon />}
|
2026-10-06 11:42:11 +07:00
|
|
|
</button>
|
|
|
|
|
|
2026-10-17 10:27:36 +07:00
|
|
|
<ThemePaletteMenu />
|
2026-10-06 11:42:11 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</nav>
|
|
|
|
|
|
2026-10-17 10:27:36 +07:00
|
|
|
<MobileMenu open={mobileOpen} onClose={closeMenu} />
|
2026-10-06 11:42:11 +07:00
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|