Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

theme-toggle.tsx

34 lines1010 Bytes

1import { Monitor, Moon, Sun } from "lucide-react";
2​
3import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
4import { setThemePreference, useThemePreference } from "@/lib/theme";
5import type { ThemePreference } from "@/lib/theme";
6​
7const OPTIONS = [
8 { value: "system", label: "System theme", icon: Monitor },
9 { value: "light", label: "Light theme", icon: Sun },
10 { value: "dark", label: "Dark theme", icon: Moon },
11] as const;
12​
13export function ThemeToggle() {
14 const preference = useThemePreference();
15​
16 return (
17 <ToggleGroup
18 type="single"
19 size="sm"
20 variant="outline"
21 value={preference}
22 onValueChange={(value) => {
23 if (value) setThemePreference(value as ThemePreference);
24 }}
25 aria-label="Colour theme"
26 >
27 {OPTIONS.map(({ value, label, icon: Icon }) => (
28 <ToggleGroupItem key={value} value={value} aria-label={label} title={label}>
29 <Icon />
30 </ToggleGroupItem>
31 ))}
32 </ToggleGroup>
33 );