frontend: close navigation dropdown when clicked

close the navigation dropdown if any click is registered anywhere
in the DOM. Previously the dropdown was closed only when clicked
This commit is contained in:
2025-04-11 11:51:40 +03:00
parent 6944751b82
commit c1abd36482
2 changed files with 16 additions and 3 deletions

View File

@ -26,6 +26,19 @@ const Navbar: React.FC<NavProps> = ({ toggleDarkMode, darkMode }) => {
localStorage.setItem('DARK_MODE', String(darkMode))
}, [darkMode])
useEffect(() => {
const handleClickoutside = (event: MouseEvent) => {
if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
setIsMenuOpen(false)
}
}
document.addEventListener('mousedown', handleClickoutside)
return () => {
document.removeEventListener('mousedown', handleClickoutside)
}
})
const menuItem = [
{ title: 'Home', href: '/' },
{ title: 'Experience', href: '/experience' },