Become a Pro / website · becomeapro.ai / src / components / theme / theme-toggle.tsx
| 1 | "use client"; |
| 2 | |
| 3 | import { useEffect, useState } from "react"; |
| 4 | import { Moon, Sun } from "lucide-react"; |
| 5 | import { useTheme } from "next-themes"; |
| 6 | import { cn } from "@/lib/utils"; |
| 7 | |
| 8 | // Switches between the light and dark themes |
| 9 | export function ThemeToggle({ className }: { className?: string }) { |
| 10 | const { resolvedTheme, setTheme } = useTheme(); |
| 11 | const [mounted, setMounted] = useState(false); |
| 12 | useEffect(() => setMounted(true), []); |
| 13 | |
| 14 | const isDark = !mounted || resolvedTheme !== "light"; |
| 15 | return ( |
| 16 | <button |
| 17 | type="button" |
| 18 | onClick={() => setTheme(isDark ? "light" : "dark")} |
| 19 | aria-label={isDark ? "Switch to light theme" : "Switch to dark theme"} |
| 20 | title={isDark ? "Light theme" : "Dark theme"} |
| 21 | className={cn( |
| 22 | "flex h-9 w-9 items-center justify-center rounded-full border border-foreground/15 text-[var(--ash)] transition-colors hover:border-foreground/40 hover:text-foreground", |
| 23 | className |
| 24 | )} |
| 25 | > |
| 26 | {isDark ? <Sun className="h-4 w-4" /> : <Moon className="h-4 w-4" />} |
| 27 | </button> |
| 28 | ); |
| 29 | } |