Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e661111

Browse files
authoredJan 10, 2025··
Merge pull request #3 from parthratra11/dev
Theme toggle button fixed
2 parents d4bdfdd + 3a77e82 commit e661111

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed
 

‎components/theme-toggle.tsx

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
"use client"
1+
"use client";
22

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";
66

7-
import { Button } from "@/components/ui/button"
7+
import { Button } from "@/components/ui/button";
88

99
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]);
1117

1218
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;
1425
}
1526

1627
return (
@@ -21,16 +32,21 @@ export function ThemeToggle() {
2132
aria-label="Toggle theme"
2233
className="relative p-2 rounded-full transition-colors duration-300 hover:bg-gray-200 dark:hover:bg-gray-700"
2334
>
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"
2740
}`}
2841
/>
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"
3248
}`}
3349
/>
3450
</Button>
35-
)
51+
);
3652
}

0 commit comments

Comments
 (0)
Please sign in to comment.