summaryrefslogtreecommitdiff
path: root/apps/web/src/components/language-select.tsx
blob: 43ed632d6e45938d860369b7312ba61028e2e1c3 (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { useState, useRef, useEffect } from "react";
import { ChevronDown, Globe } from "lucide-react";
import { cn } from "@/lib/utils";
import { motion } from "framer-motion";
import ReactCountryFlag from "react-country-flag";

export interface Language {
  code: string;
  name: string;
  flag?: string;
}

interface LanguageSelectProps {
  selectedCountry: string;
  onSelect: (country: string) => void;
  containerRef: React.RefObject<HTMLDivElement>;
  languages: Language[];
}

function FlagPreloader({ languages }: { languages: Language[] }) {
  return (
    <div className="absolute -top-[9999px] left-0 pointer-events-none">
      {languages.map((language) => (
        <ReactCountryFlag
          key={language.code}
          countryCode={language.code}
          svg
          style={{ width: "1.05rem", height: "1.05rem" }}
        />
      ))}
    </div>
  );
}

export function LanguageSelect({
  selectedCountry,
  onSelect,
  containerRef,
  languages,
}: LanguageSelectProps) {
  const [expanded, setExpanded] = useState(false);
  const [isTapping, setIsTapping] = useState(false);
  const [isClosing, setIsClosing] = useState(false);
  const collapsedHeight = "2.5rem";
  const expandHeight = "12rem";
  const buttonRef = useRef<HTMLButtonElement>(null);

  const expand = () => {
    setIsTapping(true);
    setTimeout(() => setIsTapping(false), 600);
    setExpanded(true);
    buttonRef.current?.focus();
  };

  useEffect(() => {
    if (!expanded) return;

    const handleClickOutside = (event: MouseEvent) => {
      if (
        buttonRef.current &&
        !buttonRef.current.contains(event.target as Node)
      ) {
        setIsClosing(true);
        setTimeout(() => setIsClosing(false), 600);
        setExpanded(false);
        buttonRef.current?.blur();
      }
    };

    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [expanded]);

  const selectedLanguage = languages.find(
    (lang) => lang.code === selectedCountry
  );

  const handleSelect = ({
    code,
    e,
  }: {
    code: string;
    e: React.MouseEvent<HTMLButtonElement>;
  }) => {
    e.stopPropagation();
    e.preventDefault();
    onSelect(code);
    setExpanded(false);
  };

  return (
    <div className="relative w-full h-9">
      <FlagPreloader languages={languages} />
      <motion.button
        type="button"
        className={cn(
          "absolute w-full h-full flex flex-col overflow-hidden items-start justify-between z-10 rounded-lg px-3 cursor-pointer",
          "!bg-foreground/10 backdrop-blur-md text-foreground py-0",
          "transition-[color,box-shadow] focus:border-ring focus:ring-ring/50 focus:ring-[1px]"
        )}
        initial={{
          height: collapsedHeight,
          scale: 1,
        }}
        animate={{
          height: expanded ? expandHeight : collapsedHeight,
          scale: isTapping ? [1, 0.985, 1] : 1,
        }}
        transition={{
          height: { duration: 0.25, ease: [0.4, 0, 0.2, 1] },
          scale: { duration: 0.6, ease: "easeOut" },
        }}
        onClick={expand}
        ref={buttonRef}
      >
        {!expanded ? (
          <div
            className="flex items-center justify-between w-full"
            style={{
              height: collapsedHeight,
            }}
          >
            <div className="flex items-center gap-2">
              {selectedCountry === "auto" ? (
                <Globe className="!size-[1.05rem]" />
              ) : (
                <ReactCountryFlag
                  countryCode={selectedCountry}
                  svg
                  style={{ width: "1.05rem", height: "1.05rem" }}
                />
              )}
              <span className="pt-[0.05rem]">
                {selectedCountry === "auto" ? "Auto" : selectedLanguage?.name}
              </span>
            </div>
          </div>
        ) : (
          <div className="flex flex-col gap-2 my-2.5 w-full overflow-y-auto scrollbar-hidden">
            <LanguageButton
              language={{ code: "auto", name: "Auto" }}
              onSelect={handleSelect}
              selectedCountry={selectedCountry}
            />
            {languages.map((language) => (
              <LanguageButton
                key={language.code}
                language={language}
                onSelect={handleSelect}
                selectedCountry={selectedCountry}
              />
            ))}
          </div>
        )}
      </motion.button>

      <motion.div
        className="absolute top-1/2 right-3 -translate-y-1/2 pointer-events-none z-20 mt-0.5"
        initial={{ opacity: 1 }}
        animate={{ opacity: expanded ? 0 : 1 }}
        transition={{ duration: 0.2, ease: "easeInOut" }}
      >
        <ChevronDown className="text-muted-foreground size-4" />
      </motion.div>
    </div>
  );
}

function LanguageButton({
  language,
  onSelect,
  selectedCountry,
}: {
  language: Language;
  onSelect: ({
    code,
    e,
  }: {
    code: string;
    e: React.MouseEvent<HTMLButtonElement>;
  }) => void;
  selectedCountry: string;
}) {
  return (
    <button
      type="button"
      className="flex items-center gap-2 cursor-pointer text-foreground hover:text-foreground/75"
      onClick={(e) => onSelect({ code: language.code, e })}
    >
      {language.code === "auto" ? (
        <Globe className="!size-[1.0rem]" />
      ) : (
        <ReactCountryFlag
          countryCode={language.code}
          svg
          style={{ width: "1.05rem", height: "1.05rem" }}
        />
      )}
      <span className="pt-[0.05rem]">{language.name}</span>
    </button>
  );
}