HubAPI / components /LanguageToggle.tsx
samirhosninet's picture
feat: complete HUBAPI overhaul (Security, i18n RTL, Dark/Light Mode, A11y & Performance fixes)
587459d
Raw History Blame Contribute Delete
2.99 kB
"use client";
import { useLanguage } from "@/components/LanguageContext";
import { useEffect, useState } from "react";
import { motion } from "framer-motion";
import { useSplitFlapAudio } from "@/components/ui/split-flap-text";
import { cn } from "@/lib/utils";
export function LanguageToggle() {
const { language, toggleLanguage } = useLanguage();
const [mounted, setMounted] = useState(false);
const audio = useSplitFlapAudio();
// Prevent hydration mismatch
useEffect(() => {
const frame = requestAnimationFrame(() => {
setMounted(true);
});
return () => cancelAnimationFrame(frame);
}, []);
if (!mounted) {
return (
<div className="w-[68px] h-7 rounded-full border border-black/5 dark:border-white/10 bg-white/10 dark:bg-black/20 backdrop-blur-md opacity-40" />
);
}
const isAr = language === "ar";
const handleToggle = (targetLang: "en" | "ar") => {
if (language === targetLang) return;
// Play satisfying mechanical click sound
if (audio && !audio.isMuted) {
audio.playClick();
}
toggleLanguage();
};
return (
<div
className="relative flex items-center p-0.5 rounded-full border border-[var(--border)] bg-[var(--muted)] backdrop-blur-md shadow-[0_4px_20px_rgba(0,0,0,0.05)] dark:shadow-[0_4px_20px_rgba(0,0,0,0.3)] w-[68px] h-7 select-none overflow-hidden"
>
{/* Sliding background container */}
<motion.div
className="absolute top-0.5 left-0.5 w-[31px] h-[22px] rounded-full bg-primary shadow-[0_2px_6px_rgba(0,0,0,0.12)] dark:shadow-[0_2px_6px_rgba(0,0,0,0.6)] border border-black/5 dark:border-white/5"
initial={false}
animate={{
x: isAr ? 31 : 0,
}}
transition={{ type: "spring", stiffness: 350, damping: 26 }}
/>
{/* English Button */}
<button
onClick={() => handleToggle("en")}
className={cn(
"relative z-10 flex items-center justify-center w-1/2 h-full rounded-full transition-colors duration-300 font-mono text-[10px] font-bold focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-primary/50 focus-visible:ring-offset-0 focus-visible:z-20",
!isAr ? "text-primary-foreground" : "text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
)}
aria-label="English language"
>
EN
</button>
{/* Arabic Button */}
<button
onClick={() => handleToggle("ar")}
className={cn(
"relative z-10 flex items-center justify-center w-1/2 h-full rounded-full transition-colors duration-300 font-sans text-[10px] font-bold focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-primary/50 focus-visible:ring-offset-0 focus-visible:z-20",
isAr ? "text-primary-foreground" : "text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
)}
aria-label="اللغة العربية"
>
عربي
</button>
</div>
);
}