-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathLocaleSwitcherSelect.tsx
56 lines (51 loc) · 1.52 KB
/
LocaleSwitcherSelect.tsx
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
'use client';
import clsx from 'clsx';
import {useParams} from 'next/navigation';
import {ChangeEvent, ReactNode, useTransition} from 'react';
import {Locale, usePathname, useRouter} from '@/i18n/routing';
type Props = {
children: ReactNode;
defaultValue: string;
label: string;
};
export default function LocaleSwitcherSelect({
children,
defaultValue,
label
}: Props) {
const router = useRouter();
const [isPending, startTransition] = useTransition();
const pathname = usePathname();
const params = useParams();
function onSelectChange(event: ChangeEvent<HTMLSelectElement>) {
const nextLocale = event.target.value as Locale;
startTransition(() => {
router.replace(
// @ts-expect-error -- TypeScript will validate that only known `params`
// are used in combination with a given `pathname`. Since the two will
// always match for the current route, we can skip runtime checks.
{pathname, params},
{locale: nextLocale}
);
});
}
return (
<label
className={clsx(
'relative text-gray-400',
isPending && 'transition-opacity [&:disabled]:opacity-30'
)}
>
<p className="sr-only">{label}</p>
<select
className="inline-flex appearance-none bg-transparent py-3 pl-2 pr-6"
defaultValue={defaultValue}
disabled={isPending}
onChange={onSelectChange}
>
{children}
</select>
<span className="pointer-events-none absolute right-2 top-[8px]">⌄</span>
</label>
);
}