Skip to content

Commit 645a4cc

Browse files
Complete comprehensive repository overhaul with theme toggle
Co-authored-by: rezwana-karim <126201034+rezwana-karim@users.noreply.github.com>
1 parent ab85b50 commit 645a4cc

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/components/ui/theme-toggle.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,21 @@ import { Button } from '@/components/ui/button'
66
import { useTheme } from '@/hooks/use-theme'
77

88
export function ThemeToggle() {
9-
const { theme, toggleTheme } = useTheme()
9+
const { theme, toggleTheme, mounted } = useTheme()
10+
11+
if (!mounted) {
12+
return (
13+
<Button
14+
variant="ghost"
15+
size="sm"
16+
aria-label="Toggle theme"
17+
className="h-9 w-9 px-0"
18+
disabled
19+
>
20+
<SunIcon className="h-4 w-4" />
21+
</Button>
22+
)
23+
}
1024

1125
return (
1226
<Button

src/hooks/use-theme.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ export type Theme = 'light' | 'dark'
66

77
export function useTheme() {
88
const [theme, setTheme] = useState<Theme>('light')
9+
const [mounted, setMounted] = useState(false)
910

1011
useEffect(() => {
12+
setMounted(true)
13+
1114
// Check for stored theme preference or default to system preference
1215
const stored = localStorage.getItem('theme') as Theme | null
1316
if (stored) {
@@ -23,13 +26,17 @@ export function useTheme() {
2326
}, [])
2427

2528
const toggleTheme = () => {
29+
if (!mounted) return
30+
2631
const newTheme: Theme = theme === 'light' ? 'dark' : 'light'
2732
setTheme(newTheme)
2833
localStorage.setItem('theme', newTheme)
2934
document.documentElement.classList.toggle('dark', newTheme === 'dark')
3035
}
3136

3237
const setThemeValue = (newTheme: Theme) => {
38+
if (!mounted) return
39+
3340
setTheme(newTheme)
3441
localStorage.setItem('theme', newTheme)
3542
document.documentElement.classList.toggle('dark', newTheme === 'dark')
@@ -39,5 +46,6 @@ export function useTheme() {
3946
theme,
4047
toggleTheme,
4148
setTheme: setThemeValue,
49+
mounted,
4250
}
4351
}

0 commit comments

Comments
 (0)