-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy paththeme-selector.tsx
More file actions
140 lines (124 loc) · 4.06 KB
/
theme-selector.tsx
File metadata and controls
140 lines (124 loc) · 4.06 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
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
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@databricks/appkit-ui/react";
import { MonitorIcon, MoonIcon, SunIcon } from "lucide-react";
import { useEffect, useState } from "react";
type Theme = "light" | "dark" | "system";
const THEME_STORAGE_KEY = "appkit-playground-theme";
function getSystemTheme(): "light" | "dark" {
if (typeof window === "undefined") return "light";
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
}
function getStoredTheme(): Theme {
if (typeof window === "undefined") return "system";
const stored = localStorage.getItem(THEME_STORAGE_KEY);
return (stored as Theme) || "system";
}
function applyTheme(theme: Theme) {
if (typeof window === "undefined") return;
const root = document.documentElement;
root.classList.remove("light", "dark");
if (theme === "system") {
const systemTheme = getSystemTheme();
root.classList.add(systemTheme);
} else {
root.classList.add(theme);
}
}
export function ThemeSelector() {
const [theme, setTheme] = useState<Theme>(() => getStoredTheme());
const [mounted, setMounted] = useState(false);
const [systemTheme, setSystemTheme] = useState<"light" | "dark">(() =>
getSystemTheme(),
);
useEffect(() => {
setMounted(true);
applyTheme(theme);
}, [theme]);
useEffect(() => {
// Listen for system theme changes
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
const handleChange = (e: MediaQueryListEvent | MediaQueryList) => {
const isDark = e.matches;
setSystemTheme(isDark ? "dark" : "light");
// Apply theme if current theme is "system"
if (theme === "system") {
applyTheme("system");
}
};
// Set initial system theme
handleChange(mediaQuery);
if (mediaQuery.addEventListener) {
mediaQuery.addEventListener("change", handleChange);
return () => mediaQuery.removeEventListener("change", handleChange);
} else {
mediaQuery.addListener(handleChange);
return () => mediaQuery.removeListener(handleChange);
}
}, [theme]);
const handleThemeChange = (newTheme: Theme) => {
setTheme(newTheme);
localStorage.setItem(THEME_STORAGE_KEY, newTheme);
applyTheme(newTheme);
};
// Get current effective theme for icon display
const effectiveTheme = theme === "system" ? systemTheme : theme;
if (!mounted) {
// Return a placeholder to avoid hydration mismatch
return (
<Button variant="ghost" size="icon" className="h-9 w-9">
<SunIcon className="h-4 w-4" />
</Button>
);
}
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
size="icon"
className="h-9 w-9 text-foreground hover:text-secondary-foreground"
aria-label="Toggle theme"
>
{effectiveTheme === "dark" ? (
<MoonIcon className="h-4 w-4" />
) : (
<SunIcon className="h-4 w-4" />
)}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem
onClick={() => handleThemeChange("light")}
className="cursor-pointer"
>
<SunIcon className="mr-2 h-4 w-4" />
<span>Light</span>
{theme === "light" && <span className="ml-auto text-xs">✓</span>}
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => handleThemeChange("dark")}
className="cursor-pointer"
>
<MoonIcon className="mr-2 h-4 w-4" />
<span>Dark</span>
{theme === "dark" && <span className="ml-auto text-xs">✓</span>}
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => handleThemeChange("system")}
className="cursor-pointer"
>
<MonitorIcon className="mr-2 h-4 w-4" />
<span>System</span>
{theme === "system" && <span className="ml-auto text-xs">✓</span>}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}