save theme preference to localStorage

This commit is contained in:
Nathaniel Tampus
2023-04-09 14:03:04 +08:00
parent 8940a1bf3b
commit 6d2263432a
2 changed files with 30 additions and 16 deletions

View File

@@ -48,6 +48,19 @@ export default function RootLayout({ children }: { children: React.ReactNode })
</ContextProvider>
<Footer />
{/* not using <Script> from next/script since it executes too late */}
<script
defer
id="load-theme"
dangerouslySetInnerHTML={{
__html: `if (localStorage.theme === "dark" || (!("theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches)) {
document.documentElement.setAttribute("data-theme", "chessuDark");
} else {
document.documentElement.setAttribute("data-theme", "chessuLight");
}`
}}
></script>
</body>
</html>
);