forked from stack-auth/stack-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage-toggle.tsx
More file actions
68 lines (63 loc) · 1.91 KB
/
language-toggle.tsx
File metadata and controls
68 lines (63 loc) · 1.91 KB
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
'use client';
import { type ButtonHTMLAttributes, type HTMLAttributes } from 'react';
import { useI18n } from 'fumadocs-ui/contexts/i18n';
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '../ui/popover';
import { cn } from '../../lib/cn';
import { buttonVariants } from '../ui/button';
export type LanguageSelectProps = ButtonHTMLAttributes<HTMLButtonElement>;
export function LanguageToggle(props: LanguageSelectProps): React.ReactElement {
const context = useI18n();
if (!context.locales) throw new Error('Missing `<I18nProvider />`');
return (
<Popover>
<PopoverTrigger
aria-label={context.text.chooseLanguage}
{...props}
className={cn(
buttonVariants({
color: 'ghost',
className: 'gap-1.5 p-1.5',
}),
props.className,
)}
>
{props.children}
</PopoverTrigger>
<PopoverContent className="flex flex-col overflow-hidden p-0">
<p className="mb-1 p-2 text-xs font-medium text-fd-muted-foreground">
{context.text.chooseLanguage}
</p>
{context.locales.map((item) => (
<button
key={item.locale}
type="button"
className={cn(
'p-2 text-start text-sm',
item.locale === context.locale
? 'bg-fd-primary/10 font-medium text-fd-primary'
: 'hover:bg-fd-accent hover:text-fd-accent-foreground',
)}
onClick={() => {
context.onChange?.(item.locale);
}}
>
{item.name}
</button>
))}
</PopoverContent>
</Popover>
);
}
export function LanguageToggleText(
props: HTMLAttributes<HTMLSpanElement>,
): React.ReactElement {
const context = useI18n();
const text = context.locales?.find(
(item) => item.locale === context.locale,
)?.name;
return <span {...props}>{text}</span>;
}