Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed theme issue #146

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions client/src/app/Context/ThemeContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use client"

import * as React from "react"
import { ThemeProvider as NextThemesProvider } from "next-themes"
import { type ThemeProviderProps } from "next-themes/dist/types"

export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
return <NextThemesProvider {...props}>{children}</NextThemesProvider>
}
2 changes: 1 addition & 1 deletion client/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function RootLayout({
<html lang="en">
<link rel="icon" href="/chain.png" />
<body className={poppins.className}>
<ThemeProvider attribute="class">
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
<Navbar />
{children}
</ThemeProvider>
Expand Down
72 changes: 61 additions & 11 deletions client/src/components/ThemeToggle.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,96 @@
import React, { useEffect, useState, useRef } from "react";
import { MoonIcon } from "@/icons/MoonIcon";
import { SunIcon } from "@/icons/SunIcon";
import { useTheme } from "next-themes";
import { useEffect } from "react";

const ThemeToggle = () => {
const { theme, setTheme } = useTheme();
const themeContainerRef = useRef<HTMLDivElement>(null);

const [sunIconFill, setSunIconFill] = useState<string>("#FFD700");
const [moonIconFill, setMoonIconFill] = useState<string>("gray");

useEffect(() => {
// Check system theme
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';

// Check local storage
const storedTheme = localStorage.getItem("theme");
if (storedTheme === "light" || storedTheme === "dark") {

if (storedTheme === "dark" || storedTheme === "light") {
setTheme(storedTheme);
} else {
setTheme(systemTheme);
}

// Apply translation and icon fill colors based on the stored theme
const themeContainer = themeContainerRef.current;
if (themeContainer) {
if (storedTheme === "dark" || systemTheme === "dark") {
themeContainer.classList.add("translate-x-5");
setSunIconFill("gray");
setMoonIconFill("white");
} else {
themeContainer.classList.remove("translate-x-5");
setSunIconFill("#FFD700");
setMoonIconFill("gray");
}
}
}, [setTheme]);

useEffect(() => {
// Update translation when theme changes
const themeContainer = themeContainerRef.current;
if (themeContainer) {
if (theme === "dark") {
themeContainer.classList.add("translate-x-5");
setSunIconFill("gray");
setMoonIconFill("white");
} else {
themeContainer.classList.remove("translate-x-5");
setSunIconFill("#FFD700");
setMoonIconFill("gray");
}
}
}, [theme]);

return (
<div className="flex items-center space-x-3">
<SunIcon fillColor={theme === "dark" ? "gray" : "#FFD700"} />

<SunIcon fillColor={sunIconFill} />
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
checked={theme === "dark"}
onChange={() => setTheme(theme === "light" ? "dark" : "light")}
onChange={() => {
const newTheme = theme === "light" ? "dark" : "light";
setTheme(newTheme);
localStorage.setItem("theme", newTheme);

// Update translation and icon fill colors when theme changes
const themeContainer = themeContainerRef.current;
if (themeContainer) {
if (newTheme === "dark") {
themeContainer.classList.add("translate-x-5");
setSunIconFill("gray");
setMoonIconFill("white");
} else {
themeContainer.classList.remove("translate-x-5");
setSunIconFill("#FFD700");
setMoonIconFill("gray");
}
}
}}
className="sr-only"
/>
<div className="w-10 h-5 bg-gray-200 dark:bg-gray-700 rounded-full transition-colors duration-300">
<div
className={`w-5 h-5 bg-gray-400 dark:bg-blue-700 rounded-full shadow-md transform transition-transform duration-300 ease-in-out ${
theme === "dark" ? "translate-x-5" : ""
}`}
className="w-5 h-5 bg-gray-400 dark:bg-blue-700 rounded-full shadow-md transform transition-transform duration-300 ease-in-out"
id="theme_container"
ref={themeContainerRef}
></div>
</div>
</label>

<MoonIcon fillColor={theme === "dark" ? "white" : "gray"} />
<MoonIcon fillColor={moonIconFill} />
</div>
);
};
Expand Down