1
- "use client"
1
+ "use client" ;
2
2
3
- import * as React from "react"
4
- import { Moon , Sun } from ' lucide-react'
5
- import { useTheme } from "next-themes"
3
+ import * as React from "react" ;
4
+ import { Moon , Sun } from " lucide-react" ;
5
+ import { useTheme } from "next-themes" ;
6
6
7
- import { Button } from "@/components/ui/button"
7
+ import { Button } from "@/components/ui/button" ;
8
8
9
9
export function ThemeToggle ( ) {
10
- const { theme, setTheme } = useTheme ( )
10
+ const { theme, setTheme, resolvedTheme } = useTheme ( ) ;
11
+
12
+ React . useEffect ( ( ) => {
13
+ if ( ! resolvedTheme ) {
14
+ setTheme ( "dark" ) ; // DARK THEME BY DEFAULT
15
+ }
16
+ } , [ resolvedTheme , setTheme ] ) ;
11
17
12
18
const toggleTheme = ( ) => {
13
- setTheme ( theme === "dark" ? "light" : "dark" )
19
+ setTheme ( theme === "dark" ? "light" : "dark" ) ;
20
+ } ;
21
+
22
+ // NOT RENDERING ICONS UNTIL THE THEME IS RESOLVED
23
+ if ( ! resolvedTheme ) {
24
+ return null ;
14
25
}
15
26
16
27
return (
@@ -21,16 +32,21 @@ export function ThemeToggle() {
21
32
aria-label = "Toggle theme"
22
33
className = "relative p-2 rounded-full transition-colors duration-300 hover:bg-gray-200 dark:hover:bg-gray-700"
23
34
>
24
-
25
- < Sun className = { `h-5 w-5 text-black-500 transition-all duration-500 ease-in-out transform ${
26
- theme === "dark" ? "rotate-180 opacity-0 scale-0" : "rotate-0 opacity-100 scale-100"
35
+ < Sun
36
+ className = { `h-5 w-5 text-black-500 transition-all duration-500 ease-in-out transform ${
37
+ resolvedTheme === "dark"
38
+ ? "rotate-180 opacity-0 scale-0"
39
+ : "rotate-0 opacity-100 scale-100"
27
40
} `}
28
41
/>
29
-
30
- < Moon className = { `absolute h-5 w-5 text-white-700 transition-all duration-500 ease-in-out transform ${
31
- theme === "light" ? "rotate-0 opacity-0 scale-0" : "rotate-120 opacity-100 scale-100"
42
+
43
+ < Moon
44
+ className = { `absolute h-5 w-5 text-white-700 transition-all duration-500 ease-in-out transform ${
45
+ resolvedTheme === "light"
46
+ ? "rotate-0 opacity-0 scale-0"
47
+ : "rotate-120 opacity-100 scale-100"
32
48
} `}
33
49
/>
34
50
</ Button >
35
- )
51
+ ) ;
36
52
}
0 commit comments