diff --git a/src/layouts/DarkLightToggle.tsx b/src/layouts/DarkLightToggle.tsx
index 7272c0518..f0bf6ce90 100644
--- a/src/layouts/DarkLightToggle.tsx
+++ b/src/layouts/DarkLightToggle.tsx
@@ -1,9 +1,17 @@
+import { joinClasses } from "@/utils/common";
import React from "react";
export default function DarkLightToggle() {
- const [isdark, setIsdark] = React.useState(
- localStorage.getItem("isdark") === "true" ? true : false,
- );
+ function isWindowDarkMode(): boolean {
+ return window.matchMedia("(prefers-color-scheme: dark)").matches;
+ }
+
+ const [isdark, setIsdark] = React.useState(() => {
+ if (localStorage.getItem("isdark") === "true") return true;
+ if (localStorage.getItem("isdark") === "false") return false;
+ return isWindowDarkMode();
+ });
+
React.useEffect(() => {
localStorage.setItem("isdark", JSON.stringify(isdark));
}, [isdark]);
@@ -13,15 +21,16 @@ export default function DarkLightToggle() {
setIsdark(!isdark)}
/>
{/* sun icon */}
{/* moon icon */}