/opt/canhelp/apps/web/src/components
Edit: /opt/canhelp/apps/web/src/components/LanguageSwitcher.tsx (2438B)
'use client'
import { useState, useRef, useEffect } from 'react'
import { useLocale } from '@/context/locale'
import { LOCALES } from '@/lib/translations'
export function LanguageSwitcher() {
const { locale, setLocale } = useLocale()
const [open, setOpen] = useState(false)
const ref = useRef
(null)
const current = LOCALES.find((l) => l.code === locale) ?? LOCALES[0]
// Close on outside click
useEffect(() => {
function handleClick(e: MouseEvent) {
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false)
}
document.addEventListener('mousedown', handleClick)
return () => document.removeEventListener('mousedown', handleClick)
}, [])
return (
{open && (
{LOCALES.map((loc) => (
))}
)}
)
}