|
1 | 1 | import { cn } from '@/lib/utils'; |
2 | 2 | import { Monitor, Moon, Sun } from 'lucide-react'; |
| 3 | +import { useMemo } from 'react'; |
3 | 4 | import { useTheme } from '@/components/themeProvider'; |
4 | 5 | import { Button } from '@/components/ui/button'; |
5 | 6 |
|
| 7 | +type Theme = 'dark' | 'light' | 'system'; |
| 8 | + |
6 | 9 | export function ModeToggle() { |
7 | 10 | const { theme, setTheme } = useTheme(); |
8 | 11 |
|
| 12 | + const options: Array<{ |
| 13 | + value: Theme; |
| 14 | + label: string; |
| 15 | + Icon: React.ComponentType; |
| 16 | + }> = useMemo( |
| 17 | + () => [ |
| 18 | + { value: 'system', label: 'System', Icon: Monitor }, |
| 19 | + { value: 'light', label: 'Light', Icon: Sun }, |
| 20 | + { value: 'dark', label: 'Dark', Icon: Moon }, |
| 21 | + ], |
| 22 | + [] |
| 23 | + ); |
| 24 | + |
9 | 25 | return ( |
10 | | - <div className="flex rounded-full border p-1"> |
11 | | - <Button |
12 | | - variant={'link'} |
13 | | - size={'none'} |
14 | | - className={cn( |
15 | | - 'text-foreground hover:bg-muted border p-1', |
16 | | - theme === 'dark' ? 'border' : 'border-transparent' |
17 | | - )} |
18 | | - onClick={() => setTheme('dark')} |
19 | | - > |
20 | | - <Moon /> |
21 | | - </Button> |
22 | | - <Button |
23 | | - variant={'link'} |
24 | | - size={'none'} |
25 | | - className={cn( |
26 | | - 'text-foreground hover:bg-muted border p-1', |
27 | | - theme === 'light' ? 'border' : 'border-transparent' |
28 | | - )} |
29 | | - onClick={() => setTheme('light')} |
30 | | - > |
31 | | - <Sun /> |
32 | | - </Button> |
33 | | - <Button |
34 | | - variant={'link'} |
35 | | - size={'none'} |
36 | | - className={cn( |
37 | | - 'text-foreground hover:bg-muted border p-1', |
38 | | - theme === 'system' ? 'border' : 'border-transparent' |
39 | | - )} |
40 | | - onClick={() => setTheme('system')} |
41 | | - > |
42 | | - <Monitor /> |
43 | | - </Button> |
| 26 | + <div className="flex items-center rounded-full border p-1"> |
| 27 | + {options.map(({ value, label, Icon }) => { |
| 28 | + const selected = theme === value; |
| 29 | + return ( |
| 30 | + <Button |
| 31 | + key={value} |
| 32 | + type="button" |
| 33 | + variant="link" |
| 34 | + size="none" |
| 35 | + role="radio" |
| 36 | + aria-checked={selected} |
| 37 | + aria-label={label} |
| 38 | + title={label} |
| 39 | + className={cn( |
| 40 | + 'text-foreground hover:bg-muted p-1', |
| 41 | + selected && 'bg-transparent' |
| 42 | + )} |
| 43 | + onClick={() => { |
| 44 | + if (theme !== value) setTheme(value as Theme); |
| 45 | + }} |
| 46 | + > |
| 47 | + <Icon /> |
| 48 | + </Button> |
| 49 | + ); |
| 50 | + })} |
44 | 51 | </div> |
45 | 52 | ); |
46 | 53 | } |
0 commit comments