Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions client/components/ui/nav-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useState } from "react";
import Button from "./button";
import useAuth from "@/lib/hooks/use-auth";
import { auth } from "@/firebase/firebase";
import ThemeToggle from "./theme-toggle";

export default function NavBar() {
const { user } = useAuth();
Expand Down Expand Up @@ -52,19 +53,25 @@ export default function NavBar() {
</Link>
</li>
</ul>
{user.photoURL ? (
<Image
className="rounded-full ml-4 cursor-pointer"
src={user.photoURL}
alt={"user avatar"}
width={40}
height={40}
onClick={() => auth.signOut()}
/>
) : (
// TODO: Set default profile icon
<span>No Image</span>
)}

<div className="flex items-center ml-4 gap-3">
{/* Theme toggle */}
<ThemeToggle />

{/* user avatar / sign out */}
{user.photoURL ? (
<Image
className="rounded-full cursor-pointer"
src={user.photoURL}
alt={"user avatar"}
width={40}
height={40}
onClick={() => auth.signOut()}
/>
) : (
<span className="text-white">No Image</span>
)}
</div>
</div>
</nav>
);
Expand Down
62 changes: 62 additions & 0 deletions client/components/ui/theme-toggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use client";

import React, { useEffect, useState } from "react";
import { Sun, Moon } from "lucide-react";

/**
* ThemeToggle
* - toggles "dark" class on document.documentElement
* - persists choice in localStorage under "theme"
* - only supports "light" and "dark" (no "system" option)
*/
export default function ThemeToggle() {
const [theme, setTheme] = useState<"light" | "dark">("light");
const [mounted, setMounted] = useState(false);

useEffect(() => {
setMounted(true);
const saved = localStorage.getItem("theme");
if (saved === "light" || saved === "dark") {
applyTheme(saved);
setTheme(saved);
} else {
// default to system preference but store as concrete theme
const prefersDark = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
const initial = prefersDark ? "dark" : "light";
applyTheme(initial);
setTheme(initial);
}
}, []);

const applyTheme = (t: "light" | "dark") => {
const root = document.documentElement;
if (t === "dark") root.classList.add("dark");
else root.classList.remove("dark");
};

const toggle = () => {
const next = theme === "dark" ? "light" : "dark";
localStorage.setItem("theme", next);
applyTheme(next);
setTheme(next);
};

if (!mounted) return null;

const title = theme === "dark" ? "Theme: dark" : "Theme: light";

return (
<button
aria-label="Toggle theme"
title={title}
onClick={toggle}
className="inline-flex items-center justify-center p-1 rounded hover:bg-gray-200 dark:hover:bg-gray-700"
>
{theme === "dark" ? (
<Sun className="w-4 h-4 text-yellow-400" />
) : (
<Moon className="w-4 h-4 text-gray-700" />
)}
</button>
);
}