forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemeToggle.tsx
More file actions
33 lines (28 loc) · 1.05 KB
/
Copy pathThemeToggle.tsx
File metadata and controls
33 lines (28 loc) · 1.05 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
'use client'
import { useTheme } from 'next-themes'
import { useEffect, useState } from 'react'
import { Sun, Moon, Monitor } from 'lucide-react'
const ICONS = { light: Sun, dark: Moon, system: Monitor }
const MODES = ['light', 'dark', 'system'] as const
export function ThemeToggle() {
const { theme, setTheme } = useTheme()
const [mounted, setMounted] = useState(false)
// Avoid hydration mismatch — only render after mount
useEffect(() => setMounted(true), [])
if (!mounted) return <div className="w-9 h-9" />
const cycle = () => {
const idx = MODES.indexOf((theme as (typeof MODES)[number]) ?? 'system')
const next = MODES[(idx + 1) % MODES.length]
setTheme(next)
}
const ThemeIcon = ICONS[(theme as keyof typeof ICONS) ?? 'system']
return (
<button
onClick={cycle}
aria-label={`Current theme: ${theme}. Click to switch.`}
className="p-2 rounded-lg hover:bg-muted min-h-[44px] min-w-[44px] flex items-center justify-center transition-colors"
>
<ThemeIcon className="size-[1.2rem]" />
</button>
)
}