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

updated system theme: #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions hooks/useTheme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useEffect, useState } from "react";

// Define the types for themes
type Theme = "light" | "dark" | "system";

export default function useTheme() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file should be removed as it's not used. Similar logic is already in settings-provider.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to resolve it

// Set the initial theme based on localStorage or default to "system"
const [theme, setTheme] = useState<Theme>(() => {
if (typeof window !== "undefined") {
return (localStorage.getItem("theme") as Theme) || "system";
}
return "system";
});

useEffect(() => {
const root = window.document.documentElement;

const applyTheme = (theme: Theme) => {
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
const selectedTheme = theme === "system" ? systemTheme : theme;

root.classList.remove("light", "dark");
root.classList.add(selectedTheme);

localStorage.setItem("theme", theme);
};

applyTheme(theme);

// Listen for system theme changes if "system" theme is selected
if (theme === "system") {
const handleSystemThemeChange = (e: MediaQueryListEvent) => {
applyTheme("system");
};

const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
mediaQuery.addEventListener("change", handleSystemThemeChange);

return () => mediaQuery.removeEventListener("change", handleSystemThemeChange);
}
}, [theme]);

return { theme, setTheme };
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"date-fns": "^3.6.0",
"lodash": "^4.17.21",
"lucide-react": "^0.436.0",
"next-themes": "^0.3.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We dont need next-themes (I think this is for Nextjs app)

"react": "^18.3.1",
"react-day-picker": "^8.10.1",
"react-dom": "^18.3.1",
Expand Down
Loading