File size: 2,993 Bytes
587459d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"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>
  );
}