Skip to content

Commit 0e6c950

Browse files
authored
Merge pull request #9 from aidmax/claude/add-dark-mode-support-z9Csc
Add dark mode support with system preference detection
2 parents 1b1dcf1 + ac216b8 commit 0e6c950

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

client/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
<link rel="shortcut icon" href="/favicon.ico?v=2" />
1515
<!-- Apple touch icon -->
1616
<link rel="apple-touch-icon" sizes="180x180" href="/favicon-32x32.png?v=2" />
17+
<script>
18+
(function () {
19+
try {
20+
var stored = localStorage.getItem("theme");
21+
if (stored === "dark") {
22+
document.documentElement.classList.add("dark");
23+
} else if (!stored && window.matchMedia("(prefers-color-scheme: dark)").matches) {
24+
document.documentElement.classList.add("dark");
25+
}
26+
} catch (_) {}
27+
})();
28+
</script>
1729
</head>
1830
<body>
1931
<div id="root"></div>

client/src/hooks/use-theme.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useState, useEffect } from "react";
2+
3+
type Theme = "light" | "dark";
4+
5+
export function useTheme() {
6+
const [theme, setTheme] = useState<Theme>(() => {
7+
try {
8+
const stored = localStorage.getItem("theme") as Theme | null;
9+
if (stored === "light" || stored === "dark") return stored;
10+
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
11+
} catch {
12+
return "light";
13+
}
14+
});
15+
16+
useEffect(() => {
17+
const root = document.documentElement;
18+
if (theme === "dark") {
19+
root.classList.add("dark");
20+
} else {
21+
root.classList.remove("dark");
22+
}
23+
try {
24+
localStorage.setItem("theme", theme);
25+
} catch (_) {}
26+
}, [theme]);
27+
28+
const toggleTheme = () => setTheme((prev) => (prev === "dark" ? "light" : "dark"));
29+
30+
return { theme, toggleTheme };
31+
}

client/src/pages/home.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "
1111
import { Card, CardContent } from "@/components/ui/card";
1212
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
1313
import { useToast } from "@/hooks/use-toast";
14+
import { useTheme } from "@/hooks/use-theme";
1415
import {
1516
Zap,
1617
Copy,
@@ -26,7 +27,9 @@ import {
2627
Smile,
2728
Calendar,
2829
Info,
29-
Utensils
30+
Utensils,
31+
Sun,
32+
Moon
3033
} from "lucide-react";
3134

3235
const rpeOptions = [
@@ -170,6 +173,7 @@ function generateMarkdown(data: InsertWorkout): string {
170173

171174
export default function Home() {
172175
const { toast } = useToast();
176+
const { theme, toggleTheme } = useTheme();
173177
const [markdownOutput, setMarkdownOutput] = useState("");
174178

175179
const form = useForm<InsertWorkout>({
@@ -276,6 +280,13 @@ export default function Home() {
276280
<div className="text-sm text-gray-500 dark:text-gray-400 text-center sm:text-right">
277281
Privacy-first training journal for cyclists
278282
</div>
283+
<button
284+
onClick={toggleTheme}
285+
aria-label={theme === "dark" ? "Switch to light mode" : "Switch to dark mode"}
286+
className="p-2 rounded-md text-gray-500 hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-700 transition-colors"
287+
>
288+
{theme === "dark" ? <Sun className="w-5 h-5" /> : <Moon className="w-5 h-5" />}
289+
</button>
279290
</div>
280291
</div>
281292
</header>
@@ -296,14 +307,14 @@ export default function Home() {
296307
<Form {...form}>
297308
<form className="space-y-6">
298309
{/* Date Section */}
299-
<div className="bg-gray-50 rounded-lg p-4">
310+
<div className="bg-gray-50 dark:bg-gray-700 rounded-lg p-4">
300311
<FormField
301312
control={form.control}
302313
name="workoutDate"
303314
render={({ field }) => (
304315
<FormItem>
305316
<FormLabel className="flex items-center gap-2">
306-
<Calendar className="w-4 h-4 text-blue-600" />
317+
<Calendar className="w-4 h-4 text-blue-600 dark:text-blue-400" />
307318
Workout Date
308319
</FormLabel>
309320
<FormControl>

0 commit comments

Comments
 (0)