Jotley / website · jotley.ai / src / app / components / ThemeSwitcher.tsx
| 1 | 'use client' |
| 2 | |
| 3 | import { useTheme } from '@/contexts/ThemeContext' |
| 4 | import { Monitor, Sun, Moon } from 'lucide-react' |
| 5 | import { useEffect, useState } from 'react' |
| 6 | |
| 7 | export default function ThemeSwitcher() { |
| 8 | const { theme, setTheme } = useTheme() |
| 9 | const [mounted, setMounted] = useState(false) |
| 10 | |
| 11 | useEffect(() => { |
| 12 | setMounted(true) |
| 13 | }, []) |
| 14 | |
| 15 | const handleThemeChange = (newTheme: 'light' | 'dark' | 'system') => { |
| 16 | setTheme(newTheme) |
| 17 | } |
| 18 | |
| 19 | // compact segmented control sized to sit inside the 40px nav controls |
| 20 | const track = |
| 21 | 'theme-switch inline-flex items-center gap-0.5 rounded-full p-[3px] h-10 bg-[#14101f]/[0.05] dark:bg-white/[0.06] border border-[#14101f]/[0.08] dark:border-white/10' |
| 22 | const seg = (active: boolean) => |
| 23 | `grid place-items-center w-8 h-8 rounded-full transition-colors ${ |
| 24 | active |
| 25 | ? 'bg-white dark:bg-white/[0.14] text-[#7c3aed] dark:text-[#c4b5fd] shadow-sm' |
| 26 | : 'text-[#5b5766] dark:text-[#8b8794] hover:text-[#14101f] dark:hover:text-white' |
| 27 | }` |
| 28 | |
| 29 | // Prevent hydration mismatch |
| 30 | if (!mounted) { |
| 31 | return ( |
| 32 | <div className={track}> |
| 33 | <div className="w-8 h-8" /> |
| 34 | <div className="w-8 h-8" /> |
| 35 | <div className="w-8 h-8" /> |
| 36 | </div> |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | return ( |
| 41 | <div className={track}> |
| 42 | <button onClick={() => handleThemeChange('light')} className={seg(theme === 'light')} aria-label="Light theme"> |
| 43 | <Sun className="w-[15px] h-[15px]" /> |
| 44 | </button> |
| 45 | <button onClick={() => handleThemeChange('system')} className={seg(theme === 'system')} aria-label="System theme"> |
| 46 | <Monitor className="w-[15px] h-[15px]" /> |
| 47 | </button> |
| 48 | <button onClick={() => handleThemeChange('dark')} className={seg(theme === 'dark')} aria-label="Dark theme"> |
| 49 | <Moon className="w-[15px] h-[15px]" /> |
| 50 | </button> |
| 51 | </div> |
| 52 | ) |
| 53 | } |